#include <iostream>
#include <string>
#include <queue>
#include <stack>
using namespace std;
int n, m;
string op;
int main()
{
cin >> n;
while (n -- )
{
cin >> m >> op;
if (op[2] == 'F')
{
queue<int> q;
for (int i = 0; i < m; i ++ )
{
string str;
cin >> str;
if (str == "IN")
{
int x;
cin >> x;
q.push(x);
}
else
{
if (!q.empty())
{
cout << q.front() << endl;
q.pop();
}
else cout << "None" << endl;
}
}
}
else
{
stack<int> sk;
for (int i = 0; i < m; i ++ )
{
string str;
cin >> str;
if (str == "IN")
{
int x;
cin >> x;
sk.push(x);
}
else
{
if (!sk.empty())
{
cout << sk.top() << endl;
sk.pop();
}
else cout << "None" << endl;
}
}
}
}
return 0;
}