#include <iostream>
#include <string>
#include <queue>
#include <stack>
using namespace std;
int t, n, m;
int main() {
cin >> t;
while (t--) {
queue<int>q;
stack<int>s;
string op, str;
cin >> n;
cin >> op;
if (op == "FIFO") {
while (n--) {
cin >> str;
if (str == "IN") {
cin >> m;
q.push(m);
} else {
if (q.empty()) {
cout << "None" << endl;
} else {
cout << q.front() << endl;
q.pop();
}
}
}
}
if (op == "FILO") {
while (n--) {
cin >> str;
if (str == "IN") {
cin >> m;
s.push(m);
} else {
if (s.empty()) {
cout << "None" << endl;
} else {
cout << s.top() << endl;
s.pop();
}
}
}
}
}
return 0;
}