// Arup Guha
// 2/9/2015
// Solution to 2009 MCPC Problem D: Black Vienna

import java.util.*;

public class d {

	public static void main(String[] args) {

		Scanner stdin = new Scanner(System.in);
		int turns = stdin.nextInt();

		// Go through all cases.
		while (turns != 0) {

			// Read input.
			int p1 = getMask(stdin.next());
			int p2 = getMask(stdin.next());
			int p3 = getMask(stdin.next());
			int ans = getMask(stdin.next());

			// Read in history.
			turn[] history = new turn[turns];
			for (int i=0; i<turns; i++) {
				int player = stdin.nextInt()-1;
				int mask = getMask(stdin.next());
				int res = stdin.nextInt();
				history[i] = new turn(player, mask, res);
			}

			// Solve, output and go to next case.
			int result = solveWrapper(p1, p2, p3, ans, history);
			if (result == turns) 	System.out.println("?");
			else					System.out.println(result+1);

			turns = stdin.nextInt();
		}
	}

	// Solves the problem by solving it from all three players' points of view.
	public static int solveWrapper(int p1,int p2,int p3, int ans, turn[] history) {
		int p1Solve = solve(0, p1, ans, history);
		int p2Solve = solve(1, p2, ans, history);
		int p3Solve = solve(2, p3, ans, history);
		return Math.min(p1Solve, Math.min(p2Solve, p3Solve));
	}

	// Solves game from the perspective of player.
	public static int solve(int player, int mask, int res, turn[] history) {

		// Try each mask except res.
		// Try each setting for the two players.
		// Of all settings, get the maximum for simulate.

		// Build each wrong mask for the result.
		ArrayList<Integer> posRes = new ArrayList<Integer>();
		int possible = (1 << 18) - 1 - mask;
		for (int i=0; i<(1 << 18); i++)
			if (Integer.bitCount(i) == 3 && ((possible & i) == i) && i != res)
				posRes.add(i);

		// Go through each of these wrong masks.
		int ans = 0;
		for (int wrongRes: posRes) {

			int left = (1 << 18) - 1 - mask - wrongRes;

			// Calculate each possible mask for one of the other players.
			ArrayList<Integer> posPlayer = getCombo(left, 5);

			// Go through all second player possibilities.
			for (int second: posPlayer) {

				// Third player is fixed.
				int third = left - second;

				// Simulate appropriately.
				int turn = 0;
				if (player == 0) 		turn = simulate(mask, second, third, history);
				else if (player == 1)	turn = simulate(second, mask, third, history);
				else					turn = simulate(second, third, mask, history);

				ans = Math.max(ans, turn);
			}
		}

		return ans;
	}

	// Returns the bitmask form of letters (unique and all capital)
	public static int getMask(String letters) {
		int mask = 0;
		for (int i=0; i<letters.length(); i++)
			mask += (1 << (letters.charAt(i)-'A'));
		return mask;
	}

	// Returns all masks with bits number of bits on that are a subset of mask.
	public static ArrayList<Integer> getCombo(int mask, int bits) {
		ArrayList<Integer> result = new ArrayList<Integer>();
		getComboRec(mask, bits, 0, 0, result);
		return result;
	}

	// Adds all subsets of mask with bits number of bits where cur is already set and we are free
	// to fill in bit numbers k and higher into the set list.
	public static void getComboRec(int mask, int bits, int cur, int k, ArrayList<Integer> list) {

		if (Integer.bitCount(cur) == bits) list.add(cur);
		if ((1 << k) > mask) return;

		// Don't add bit.
		getComboRec(mask, bits, cur, k+1, list);

		// Add bit.
		if (((1 << k) & mask) > 0)
			getComboRec(mask, bits, cur + (1 << k), k+1, list);
	}


	public static int simulate(int p1, int p2, int p3, turn[] history) {

		int[] players = {p1, p2, p3};

		// Find the first inconsistency.
		for (int i=0; i<history.length; i++)
			if (Integer.bitCount(players[history[i].player] & history[i].mask) != history[i].response)
				return i;

		return history.length;
	}
}

class turn {

	public int player;
	public int mask;
	public int response;

	public turn(int a, int b, int c) {
		player = a;
		mask = b;
		response = c;
	}
}