#include <iostream>
#include <cstring>
#include <vector>
#include <map>
#include <algorithm>
#include <sstream>
using namespace std;
const int maxn = 10000 + 10;
vector<int> depend[maxn], depend2[maxn];
vector<int> installed;
map<string, int> ID;
string name[maxn];
int cnt;
int status[maxn];
int find_ID(string &item)
{
if (!ID.count(item))
{
ID[item] = cnt;
name[cnt++] = item;
}
return ID[item];
}
bool needed(int id)
{
for (int i = 0; i < depend2[id].size(); i++)
{
if (status[depend2[id][i]])
return true;
}
return false;
}
void List()
{
for (int i = 0; i < installed.size(); i++)
{
cout << " " << name[installed[i]] << endl;
}
}
void install(int id, bool toplevel)
{
if (!status[id])
{
for (int i = 0; i < depend[id].size(); i++)
{
install(depend[id][i], false);
}
status[id] = toplevel ? 1 : 2;
cout << " Installing " << name[id] << endl;
installed.push_back(id);
}
}
void Remove(int id, bool toplevel)
{
if ((toplevel || status[id] == 2) && !needed(id))
{
installed.erase(remove(installed.begin(), installed.end(), id), installed.end());
cout << " Removing " << name[id] << endl;
status[id] = 0;
for (int i = 0; i < depend[id].size(); i++)
{
Remove(depend[id][i], false);
}
}
}
int main()
{
//freopen("input.txt","r",stdin);
cnt = 0;
string ope;
memset(status, 0, sizeof(status));
while (getline(cin, ope))
{
cout << ope << endl;
stringstream ss(ope);
ss >> ope;
if (ope[0] == 'E')
break;
string item1, item2;
if (ope[0] == 'L')
List();
else
{
ss >> item1;
int id = find_ID(item1);
if (ope[0] == 'D')
{
while (ss >> item2)
{
int id2 = find_ID(item2);
depend[id].push_back(id2);
depend2[id2].push_back(id);
}
}
else if (ope[0] == 'I')
{
if (status[id])
cout << " " << item1 << " is already installed." << endl;
else
install(id, true);
}
else
{
if (!status[id])
cout << " " << item1 << " is not installed." << endl;
else if (needed(id))
cout << " " << item1 << " is still needed." << endl;
else
Remove(id, true);
}
}
}
system("pause");
return 0;
}