#include <cstring>
#include <iostream>
#include <algorithm>
#include <map>
#include <string>
#include <set>
#include <vector>

using namespace std;

int F[20], used[20];

int base, totval;
bool godeep;

void endRound()
{
	base = totval = 0;
	godeep = false;
}

bool chance(int x)
{
	if (F[x] + F[7] >= 2 || (4 - used[x] + 4 - used[7]) <= F[x] + F[7]) return true;
	return false;
}

int main()
{
	vector<int> V;
	for (int match = 1; match <= 1000; ++match)
	{
		V.clear();
		
		memset(F, 0, sizeof(F));
		memset(used, 0, sizeof(used));
		
		for (int i = 0, val; i < 4; ++i)
		{
			cin >> val;
			++F[val];
		}
		
		int type;
		while (true)
		{
			cin >> type;
			
			if (type == 2)
			{
				endRound();
				
				int N;
				cin >> N;
				for (int i = 0, val; i < N; ++i)
				{
					cin >> val;
					++F[val];
				}
			}
			else if (type == 3) // start
			{
				endRound();
				
				int N;
				cin >> N;
				for (int i = 0, val; i < N; ++i)
				{
					cin >> val;
					++F[val];
				}
				
				bool any = false;
				for (int i = 7; i < 15; ++i)
					if (i != 7 && i != 10 && i != 14 && F[i] != 0)
					{
						any = true;
						
						cout << i << '\n';
						cout.flush();
						
						base = i;
						--F[i];
						++used[i];
						
						break;
					}
				
				if (!any && chance(10))
				{
					cout << 10 << '\n';
					cout.flush();
					
					base = 10;
					--F[10];
					++used[10];
					++totval;
					godeep = true;
				}
				else if (!any && chance(14))
				{
					cout << 14 << '\n';
					cout.flush();
					
					base = 14;
					--F[14];
					++used[14];
					++totval;
					godeep = true;
				}
				else if (!any && F[7])
				{
					cout << 7 << '\n';
					cout.flush();
					
					base = 7;
					--F[7];
					++used[7];
				}
				else if (!any)
				{
					for (int i = 7; i < 15; ++i)
						if (F[i] != 0)
						{
							godeep = true;
							any = true;
							
							cout << i << '\n';
							cout.flush();
							
							base = i;
							--F[i];
							++used[i];
							
							break;
						}
				}
			}
			else if (type == 4)
			{
				int val1, val2;
				cin >> val1 >> val2;
				break;
			}
			else // start
			{
				int val = type;
				
				++used[val];
				
				if (base == 0)
				{
					base = val;
					if (base == 10 || base == 14)
						if (chance(base))
							godeep = true;
				}
				if (val == 10 || val == 14)
					++totval;
				
				if (base != 10 && base != 14 && base != 7)
				{
					if (F[base] != 0)
					{
						cout << base << '\n';
						cout.flush();
						
						--F[base];
						++used[base];
					}
					else
					{
						cout << 0 << '\n';
						cout.flush();
					}
				}
				else if (base == 7)
				{
					cout << 0 << '\n';
					cout.flush();
				}
				else // base == 10 || base == 14
				{
					if (godeep)
					{
						if (F[base])
						{
							cout << base << '\n';
							cout.flush();
							
							--F[base];
							++used[base];
						}
						else if (F[7])
						{
							cout << 7 << '\n';
							cout.flush();
							
							--F[7];
							++used[7];
						}
						else
						{
							cout << 0 << '\n';
							cout.flush();
						}
					}
					else
					{
						cout << 0 << '\n';
						cout.flush();
					}
				}
			}
		}
	}
}