#include <cstring>
#include <list>
#include <iostream>
#include <cstdio>
#include <map>
#include <vector>
using namespace std;
list<string> queue;
map<string, bool> dict;
vector<string> cyz;
int n;
void solve()
{
string op;
cin >> op;
if (op[0] == 'a')
{
string name;
cin >> name;
if (dict[name])
{
printf("Error\n");
return;
}
dict[name] = 1;
queue.push_back(name);
printf("OK\n");
return;
}
if (op[0] == 's')
{
if (queue.size() == 0)
{
printf("Error\n");
return;
}
if (cyz.size() && cyz.at(0) == queue.front())
{
queue.push_back(queue.front());
queue.pop_front();
}
if (cyz.size() > 1)
{
if (cyz.at(1) == queue.front() || cyz.at(0) == queue.front())
{
queue.push_back(queue.front());
queue.pop_front();
}
}
if (queue.size() > 2 && cyz.size())
{
auto it = queue.begin();
it++;
string tmp = *it;
while (*it == cyz[0])
{
it = queue.begin();
it++;
tmp = *it;
queue.remove(tmp);
queue.push_back(tmp);
}
}
#ifndef ONLINE_JUDGE
cout << "--- ";
for (auto it = queue.begin(); it != queue.end(); it++)
{
cout << *it << " ";
}
cout << endl;
cout << "###";
for (string v : cyz)
{
cout << v << " ";
}
cout << endl;
#endif
vector<string> cyztmp;
if (queue.size() >= 2)
{
cout << queue.front() << " ";
cyztmp.push_back(queue.front());
queue.push_back(queue.front());
queue.pop_front();
queue.push_back(queue.front());
cout << queue.front() << endl;
cyztmp.push_back(queue.front());
queue.pop_front();
}
else
{
queue.push_back(queue.front());
cout << queue.front() << endl;
cyztmp.push_back(queue.front());
queue.pop_front();
}
cyz = cyztmp;
return;
}
if (op[0] == 'l')
{
if (queue.size() <= 2)
{
cout << "Error\n";
return;
}
string name;
cin >> name;
if (cyz.size())
{
if (cyz[0] == name)
{
cout << "Error\n";
return;
}
if (cyz.size() > 1 && cyz[1] == name)
{
cout << "Error\n";
return;
}
}
if (dict[name])
{
queue.remove(name);
dict[name] = 0;
printf("OK\n");
return;
}
printf("Error\n");
return;
}
}
int main()
{
int T;
cin >> T;
while (T--)
{
solve();
}
}