//* Problem  : 1166 - Team Queue
//* Contest  : acm.tju.edu.cn
//* Origin   : University of Ulm Local Contest 1999
//* Type     : Solution
//* Date     : 2008.06.06
//* Author   : alt
//* Language : C++
//* Compiler : Microsoft Visual C++ 8.0

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>

#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <string>
#include <iostream>
using namespace std;
#define int64 long long
#define INF 1000000000
#define EPS 1E-8

#define ALL(a) a.begin(), a.end()
#define PB(a, v) a.push_back(v)
#define MP(a,b) make_pair(a, b)
#define FOR(i, n) for (int i = 0; i < (n); i++)

int team_num[1000000];

queue <int> qteams, qqteams[1024];

char s[100];

void dequeue()
{
	int team = qteams.front();
	int val = qqteams[team].front(); qqteams[team].pop();
	if (qqteams[team].empty())
		qteams.pop();
	printf("%d\n", val);
}

void enqueue(int val)
{
	int team = team_num[val];
	if (qqteams[team].empty())
		qteams.push(team);
	qqteams[team].push(val);
}

void make_empty(queue <int> &q)
{
	while (!q.empty())
		q.pop();
}

int main()
{
#ifdef _DEBUG
	freopen("1166.in", "r", stdin);
#endif
	int it = 0, n;
	while (scanf("%d", &n) == 1 && n)
	{
		make_empty(qteams);
		for (int i = 0; i < 1024; i++)
			make_empty(qqteams[i]);
		printf("Scenario #%d\n", ++it);
		for (int i = 0; i < n; i++)
		{
			int m, v; scanf("%d", &m); 
			for (int j = 0; j < m; j++)
			{
				scanf("%d", &v); 
				team_num[v] = i;
			}
		}
		while (1)
		{
			scanf("%s", s);
			if (s[0] == 'S') break;
			if (s[0] == 'D') dequeue();
			else {int v; scanf("%d", &v); enqueue(v);}
		}
		printf("\n");
	}
	return 0;
}
