#include<stdio.h>
#include<string.h>
#include<queue>
#include<stack>
using namespace std;
int main() {
int n,m,i,j,k,t;
char s[30];
scanf("%d",&t);
while(t--) {
scanf("%d%s",&n,s);
if(strcmp(s,"FIFO")==0) {
queue<int>q;
while(n--) {
scanf("%s",s);
if(strcmp(s,"IN")==0) {
scanf("%d",&j);
q.push(j);
}
else {
if(!q.empty()) {
printf("%d\n",q.front());
q.pop();
}
else
printf("None\n");
}
}
}
else {
stack<int>q;
while(n--) {
scanf("%s",s);
if(strcmp(s,"IN")==0) {
scanf("%d",&j);q.push(j);
}
else {
if(!q.empty()) {
printf("%d\n",q.top());
q.pop();
}
else
printf("None\n");
}
}
}
}
return 0;
}
#include<string.h>
#include<queue>
#include<stack>
using namespace std;
int main() {
int n,m,i,j,k,t;
char s[30];
scanf("%d",&t);
while(t--) {
scanf("%d%s",&n,s);
if(strcmp(s,"FIFO")==0) {
queue<int>q;
while(n--) {
scanf("%s",s);
if(strcmp(s,"IN")==0) {
scanf("%d",&j);
q.push(j);
}
else {
if(!q.empty()) {
printf("%d\n",q.front());
q.pop();
}
else
printf("None\n");
}
}
}
else {
stack<int>q;
while(n--) {
scanf("%s",s);
if(strcmp(s,"IN")==0) {
scanf("%d",&j);q.push(j);
}
else {
if(!q.empty()) {
printf("%d\n",q.top());
q.pop();
}
else
printf("None\n");
}
}
}
}
return 0;
}