#include<stdio.h>
#include<string.h>
int
t,n,m[1000];
char
a[1000][10],s[10],
in
[1000][12];
int
main()
{
scanf(
"%d"
,&t);
while
(t--)
{
int
i,j,m1[10000];
memset(m1,0,
sizeof
(m1));
memset(m,0,
sizeof
(m));
scanf(
"%d"
,&n);
getchar();
scanf(
"%s"
,s);getchar();
for
(i=0;i<n;i++)
{
scanf(
"%s"
,a[i]);
if
(strcmp(a[i],
"IN"
)==0)
{
scanf(
"%d"
,&m[i]);
}
}
int
top1=0,t=0;
//top1为队列的尾部 或 栈的顶部
if
(strcmp(s,
"FIFO"
)==0)
//队列模似
{
int
head=0;
//队头
for
(i=0;i<n;i++)
{
if
(strcmp(a[i],
"IN"
)==0)
{
strcpy(
in
[top1],a[i]);
m1[top1]=m[i];top1++;t=i;
}
else
if
(head!=top1)
{
printf(
"%d\n"
,m1[head]);head++;
}
else
printf(
"None\n"
);
for
(j=0;a[i][j]!=
'\0'
;j++)
//运行完后进行初始化
{
a[i][j]=
'\0'
;
in
[t][j]=
'\0'
;
}
}
}
else
//栈的模似
for
(i=0;i<n;i++)
{
if
(strcmp(a[i],
"IN"
)==0)
{
strcpy(
in
[++top1],a[i]);m1[top1]=m[i];t=top1;
}
else
if
(top1)
{
printf(
"%d\n"
,m1[top1--]);
}
else
{
printf(
"None\n"
);
}
for
(j=0;a[i][j]!=
'\0'
;j++)
//运行完后进行初始化
{
a[i][j]=
'\0'
;
in
[t][j]=
'\0'
;
}
}
}
return
0;
}