// Arup Guha
// 3/4/2022
// Solution to 2022 February Bronze USACO Problem: Blocks

import java.util.*;

public class blocks {

	final public static int N = 4;
	public static boolean[][] letters;
	
	public static void main(String[] args) {
	
		Scanner stdin = new Scanner(System.in);
		int numWords = stdin.nextInt();
		
		// Store blocks by looking at which letters they have.
		letters = new boolean[N][26];
		for (int i=0; i<4; i++) {
		
			// Mark all letters in word i as true.
			char[] word = stdin.next().toCharArray();
			for (int j=0; j<word.length; j++)
				letters[i][word[j]-'A'] = true;
		}
		
		// Determine if we can form each word.
		for (int i=0; i<numWords; i++) {
			char[] word = stdin.next().toCharArray();
			if (solve(word))
				System.out.println("YES");
			else
				System.out.println("NO");
		}
	}
	
	// Returns true iff we can form word some way.
	public static boolean solve(char[] word) {
		return go(new int[N], new boolean[N], word, 0);
	}
	
	// Returns true iff we can form word with the first k blocks fixed.
	public static boolean go(int[] perm, boolean[] used, char[] word, int k) {
	
		// Filled in all the blocks we need.
		if (k == word.length) return canDo(perm, word);
	
		// Try each item in slot k.
		for (int i=0; i<N; i++) {
		
			// Used this block already.
			if (used[i]) continue;
			
			// Place item i in slot k.
			used[i] = true;
			perm[k] = i;
			
			// See if it works, if so return true.
			boolean tmp = go(perm, used, word, k+1);
			if (tmp) return true;
			used[i] = false;
		}
		
		// If we get here it doesn't work.
		return false;
	}
	
	// Returns true iff the arrangement of blocks stored in perm can form word.
	public static boolean canDo(int[] perm, char[] word) {
	
		// Try each letter.
		for (int i=0; i<word.length; i++)
		
			// Return false if this letter can't be formed by this block.
			if (!letters[perm[i]][word[i]-'A'])
				return false;
				
		// If we make it here, we're good.
		return true;
	}
}