// Arup Guha
// 9/17/2018
// Solution to CIS 3362 Homework #3

import java.util.*;

public class adfgvx_hmk3 {

	// Our two modes.
	final public static int ENCRYPT = 1;
	final public static int DECRYPT = 2;
	
	// Helpful for look ups.
	final private static String LETTERS = "ADFGVX";
	private static HashMap<Character,Integer> map;
	private String[] letterLookUp;
	private String[] digitLookUp;
	private char[] reverse;
	private int[] perm;

	public static void main(String[] args) {

		// Read in the keys.
		Scanner stdin = new Scanner(System.in);
		char[][] key1 = new char[6][6];
		for (int i=0; i<6; i++)
			for (int j=0; j<6; j++)
				key1[i][j] = stdin.next().charAt(0);
		char[] key2 = stdin.next().toCharArray();

		// Set up the adfgvx object and get the message.
		adfgvx_hmk3 myCode = new adfgvx_hmk3(key1, key2);
		
		// Encrypt/Decrypt each message.
		int numMsg = stdin.nextInt();
		for (int i=0; i<numMsg; i++) {
			int mode = stdin.nextInt();
			
			// Perform encryption here.
			if (mode == ENCRYPT) {
				String msg = stdin.next();
				String cipher = myCode.encrypt(msg);
				System.out.println(cipher);
			}
			
			// And decryption here.
			else {
				String cipher = stdin.next();
				String msg = myCode.decrypt(cipher);
				System.out.println(msg);
			}
		}
	}

	// Creates an adfgvx object
	public adfgvx_hmk3(char[][] myKey1, char[] myKey2) {
		
		// To make it easier to reverse.
		map = new HashMap<Character,Integer>();
		for (int i=0; i<6; i++)
			map.put(LETTERS.charAt(i), i);

		letterLookUp = new String[26];
		digitLookUp = new String[10];
		reverse = new char[36];
		
		// Goes through each item in the 6 x 6 square
		for (int i=0; i<6; i++) {
			for (int j=0; j<6; j++) {
				
				// Also store this backwards.
				reverse[6*i+j] = myKey1[i][j];
				if (Character.isLetter(myKey1[i][j])) 
					letterLookUp[myKey1[i][j]-'A'] = "" + LETTERS.charAt(i) + LETTERS.charAt(j);
				else
					digitLookUp[myKey1[i][j]-'0'] = "" + LETTERS.charAt(i) + LETTERS.charAt(j);
			}
		}

		// Set up permutation for reading columns
		perm = new int[myKey2.length];
		tile[] myTiles = new tile[myKey2.length];
		for (int i=0; i<myKey2.length; i++)
			myTiles[i] = new tile(myKey2[i], i);
		Arrays.sort(myTiles);

		// Copy in rankings for tiles.
		for (int i=0; i<myKey2.length; i++) 
			perm[i] = myTiles[i].index;
	}

	// Encryption is a 2 part process.
	public String encrypt(String message) {
		String middle = sub(message);
		return trans(middle);
	}
	
	// Decryption is also a 2 part process, in reverse order.
	public String decrypt(String cipher) {
		String middle = undoTrans(cipher);
		return undoSub(middle);
	}
	
	public String undoTrans(String cipher) {

		// These dimensional items are important.
		int shortCol = cipher.length()/perm.length;
		int numLongCol = cipher.length()%perm.length;
		
		// Will store result here.
		char[] res = new char[cipher.length()];
		
		
		int idx = 0;
		
		// Loop through the perm array, copying things back to where they came from.
		// So, j is going in order of the cipher text, and k is hopping around in
		// columns.
		for (int i=0; i<perm.length; i++) {
			for (int j=idx, k=perm[i]; k<cipher.length(); j++,k+=perm.length,idx++)
				res[k] = cipher.charAt(j);
		}
		
		// This is what we get after undoing part B.
		return new String(res);
	}

	// Performs a basic column transposition on message using the object's second key.
	public String trans(String message) {

		// These dimensional items are important.
		int shortCol = message.length()/perm.length;
		int numLongCol = message.length()%perm.length;

		// Use this for a better run time...
		StringBuilder sb = new StringBuilder();

		// Go through each column.
		for (int i=0; i<perm.length; i++) {

			// Nice way to avoid calculating whether it's a short or long column.
			for (int j=perm[i]; j<message.length(); j+= perm.length)
				sb.append(message.charAt(j));
		}

		return new String(sb);
	}
	
	// Reverses the substitution phase of ADFGVX.
	public String undoSub(String middle) {
		
		// Take a pair of characters at a time.
		StringBuilder msg = new StringBuilder("");
		for (int i=0; i<middle.length(); i+=2) {
			int idx = 6*map.get(middle.charAt(i)) + map.get(middle.charAt(i+1));
			msg = msg.append(reverse[idx]);
		}
		
		// This is our result.
		return new String(msg);
	}

	public String sub(String message) {

		String res = "";

		// Add each character code, one by one.
		for (int i=0; i<message.length(); i++) {
			char c = message.charAt(i);
			if (Character.isLetter(c))
				res = res + letterLookUp[c-'A'];
			else
				res = res + digitLookUp[c-'0'];
		}

		return res;
	}
}

// Just so we can let Java resort the second key word.
class tile implements Comparable<tile> {

	public char letter;
	public int index;

	public tile(char c, int i) {
		letter = c;
		index = i;
	}

	public int compareTo(tile other) {
		if (this.letter != other.letter)
			return this.letter - other.letter;
		return this.index - other.index;
	}
}
