// Arup Guha
// 9/14/2016
// ADFGVX encrypter
/*** File format:

1. 6 x 6 square, space separated on six lines
2. 7th line will be keyword all caps
3. 8th line will be n, # of messages to encrypt.
4. Next n lines will have each message, one by one. (A-Z, 0-9 only)

***/

/**** Edited on 9/14/2026 to add decryption. ***/

import java.util.*;

public class adfgvx {

	final private static String LETTERS = "ADFGVX";
	final private static int[] LETTER_LOOKUP = {0,-1,-1,1,-1,2,3,-1,-1,-1,-1,-1,-1,
												-1,-1,-1,-1,-1,-1,-1,-1,4,-1,5,-1,-1};
												
	final public static boolean DEBUG = false;
	
	// Part of my object.
	private char[][] key1;
	private String[] letterLookUp;
	private String[] digitLookUp;
	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 myCode = new adfgvx(key1, key2);

		// Encrypt each plaintext.
		int numPlain = stdin.nextInt();
		for (int i=0; i<numPlain; i++) {
			
			// Encrypt.
			String msg = stdin.next();
			String cipher = myCode.encrypt(msg);
			
			// Print final cipher.
			System.out.println(cipher);
			
			// Decrypt, print original message.
			String getback = myCode.decrypt(cipher);
			System.out.println(getback);
		}
	}

	// Creates an adfgvx object
	public adfgvx(char[][] myKey1, char[] myKey2) {
		
		// Now part of my object.
		key1 = new char[6][6];

		letterLookUp = new String[26];
		digitLookUp = new String[10];

		// Goes through each item in the 6 x 6 square
		for (int i=0; i<6; i++) {
			for (int j=0; j<6; j++) {
				
				// Copy it in.
				key1[i][j] = myKey1[i][j];

				// Update appropriate look up table,
				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;
			System.out.print(perm[i]+" ");
		}
		System.out.println();
	}

	// Encryption is a 2 part process.
	public String encrypt(String message) {
		String middle = sub(message);
		if (DEBUG) System.out.println(middle);
		return trans(middle);
	}
	
	// So is decryption, just do in reverse order.
	public String decrypt(String cipher) {
		String middle = undotrans(cipher);
		if (DEBUG) System.out.println(middle);
		return revsub(middle);
	}

	// Performs the one for one substitution between letters in the message
	// and ADFGVX pairs.
	private 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;
	}
	
	// Performs a basic column transposition on message using the object's second key.
	private 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));
		}

		// Ta da!
		return new String(sb);
	}
	
	// Takes ciphertext and undoes transposition step.
	private String undotrans(String cipher) {
		
		// Copy the answer here.
		char[] res = new char[cipher.length()];
		
		// Index into cipher.
		int idx = 0;
		
		// Go through permutation array.
		for (int i=0; i<perm.length; i++) {
			
			// Copy in each letter from the ciphertext into the appropriate grid positions.
			for (int j=perm[i]; j<cipher.length(); j+= perm.length)
				res[j] = cipher.charAt(idx++);
		}
		
		// Ta da!
		return new String(res);
	}
	
	// Takes the middle ciphertext and decrypts to the final plain text.
	private String revsub(String cipherMid) {
		
		char[] res = new char[cipherMid.length()/2];
		for (int i=0; i<cipherMid.length(); i+=2) {
			
			// Extract row and column corresponding to ciphertext.
			int idx1 = LETTER_LOOKUP[cipherMid.charAt(i)-'A'];
			int idx2 = LETTER_LOOKUP[cipherMid.charAt(i+1)-'A'];
			
			// Add character
			res[i/2] = key1[idx1][idx2];
		}
		
		// Here is the plaintext!
		return new String(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;
	}
}