#include<stdio.h>
typedef struct node{
long data;
struct node *next;
}node;
long n,belong[1000000];
node *head,*tail,*team[1001];
void enqueue(long x)
{
node *p,*q;
q=(node *)malloc(sizeof(node));
q->data=x;
if (team[belong[x]]){
p=team[belong[x]];
q->next=p->next;
p->next=q;
if (p==tail) tail=q;
}else{
tail->next=q;
tail=q;
q->next=NULL;
}
if (belong[x]) team[belong[x]]=q;
}
void dequeue()
{
node *p; int f;
p=head->next;
if (p){
f=0;
if (p->next==NULL) f=1;
else if (belong[p->data]!=belong[p->next->data]) f=1;
if (f) team[belong[p->data]]=NULL;
printf("%ld\n",p->data);
head->next=p->next;
if (p==tail) tail=head;
free(p);
}
}
int main()
{
long i,j,k,tn,x;
char s[10];
k=0;
head=(node *)malloc(sizeof(node));
while (scanf("%ld",&n)==1)
{
head->next=NULL; tail=head;
memset(team,0,sizeof(team));
memset(belong,0,sizeof(belong));
for (i=1;i<=n;i++)
{
scanf("%ld",&tn);
for (j=0;j<tn;j++)
{ scanf("%ld",&x); belong[x]=i; }
}
printf("Line #%ld\n",++k);
scanf("%s",s);
while (s[0]!='S')
{
if (s[0]=='E')
{ scanf("%ld",&x); enqueue(x); }
else dequeue();
scanf("%s",s);
}
}
return 0;
}