#!/usr/bin/env python3 frequencies = [int(x) for x in input().split(" ")] n = int(input()) def is_letter(c): return c >= 'a' and c <= 'z' def count_occurrences(line): occurrences = [0] * 26 # First, count the occurrences for character in line: if is_letter(character): occurrences[ord(character) - ord("a")] += 1 return occurrences def create_mapping(occurrences, frequencies): # Then, create a mapping between the occurrences and the frequencies # Example # occurrences[0]=3 ... the letter 'a' (0) is used 3 times in the ciphertext # occurrences[1]=6 ... the letter 'b' (1) is used 6 times in the ciphertext # occurrences[2]=1 ... the letter 'c' (2) is used 1 time in the ciphertext # frequencies[0]=6 ... the letter 'a' (0) is used 6 times in the source text # frequencies[1]=3 ... the letter 'b' (1) is used 3 times in the source text # frequencies[2]=1 ... the letter 'c' (2) is used 1 time in the source text # We need to remember the index of the letter, so we can sort the array # according to the frequencies occurrences = sorted(enumerate(occurrences), key=lambda x: (x[1], x[0]), reverse=True) frequencies = sorted(enumerate(frequencies), key=lambda x: (x[1], x[0]), reverse=True) # Example now (after sorting) # occurrences[0]=(1,6) ... the letter 'b' (1) is used 6 times in the ciphertext # occurrences[1]=(0,3) ... the letter 'a' (0) is used 3 times in the ciphertext # occurrences[2]=(2,1) ... the letter 'c' (2) is used 1 time in the ciphertext # frequencies[0]=(0,6) ... the letter 'a' (0) is used 6 times in the source text # frequencies[1]=(1,3) ... the letter 'b' (1) is used 3 times in the source text # frequencies[2]=(2,1) ... the letter 'c' (2) is used 1 time in the source text # The mapping is now given by the same index (=the same occurrence count): # b ciphertext = a source text # a sourcetext = b ciphertext # c sourcetext = c ciphertext mapping = [None] * 26 for i in range(len(occurrences)): # occurrences[i][0] is the index of the letter in the ciphertext # frequencies[i][0] is the index of the letter in the source text mapping[occurrences[i][0]] = frequencies[i][0] return mapping # Then, print the result based on the occurrences def print_results(message, mapping): for character in message: if is_letter(character): cipher_index = ord(character) - ord("a") source_index = mapping[cipher_index] source_letter = chr(source_index + ord("a")) print(source_letter, end="") else: print(character, end="") message = "" for _ in range(n): message += input() + "\n" occurrences = count_occurrences(message) mapping = create_mapping(occurrences, frequencies) print_results(message, mapping)