Read problems statements in Mandarin Chinese  and Russian. 
Once upon a time chef decided to learn encodings. And, obviously, he started with the easiest one (well, actually the easiest after Caesar cypher) – substitution cypher.

But very soon Chef got bored with encoding/decoding, so he started thinking how to hack this cypher. 
He already knows some algorithm, which is not always correct, 
but it’s sufficient for now. Here is its description.

Imagine we know frequency sequence of English letters (this means, that letters are sorted by their frequency of appearing in English texts, in ascending order). 
And let’s find frequency sequence of cyphered letters (if some of them appear equal number of times, then first in frequency sequence will be lower letter between them).
Now, using this two frequency sequences we can recover plain text. Just substitute cyphered letter with origin one, if they are at same positions in sequences. 

Now, Chef has frequency sequence of English letters and cypher text. And he asks you to recover plain text. Please, help him.

------ Input ------ 

In first line number T is given - number of test cases. Then T test cases follow. Each test case consists of two lines - frequency sequence and encrypted text.

------ Output ------ 

For each test case you should output decrypted with the given frequency sequence text. Please note, that the case of letters should be preserved. 

------ Constraints ------ 

$ 1 ≤ T ≤ 1000; $
$ Length of frequency sequence is always 26; $
$ 1 ≤ length of the text ≤ 150000; $
$ 1 ≤ sum lengths of all texts ≤ 150000. $
$ Frequency sequence consists of all lowercase English letters. Text consists of any characters. $

----- Sample Input 1 ------ 
3
qwrtyuipasdfgjkzxcvbnmheol
dummy!
bfgjklmopqrstuwxzhvnicdyea
abcd b efgd hbi!
qwrtyuipasdfgjkzxcvbnmheol
Dummy!
----- Sample Output 1 ------ 
hello!
have a nice day!
Hello!

Difficulty: MEDIUM_HARD

Solutions:
ri = lambda : int(input())
rl = lambda : list(map(int, input().split()))
rs = lambda : input()
from collections import defaultdict
import sys
sys.setrecursionlimit(100000)
t = ri()
for test in range(t):
	s = rs()
	p = list(rs())
	arr = [[0, chr(i + 97)] for i in range(26)]
	for i in p:
		i = i.lower()
		if ord(i) < 97 or ord(i) > 122:
			continue
		arr[ord(i) - ord('a')][0] += 1
	arr.sort()
	d = {}
	for i in range(26):
		d[arr[i][1]] = s[i]
	ans = ''
	for i in p:
		if i.lower() in d:
			if i.islower():
				ans += d[i]
			else:
				ans += d[i.lower()].upper()
		else:
			ans += i
	print(ans)


import os.path
from math import gcd, floor, ceil
from collections import *
import sys
mod = 1000000007
INF = float('inf')

def st():
	return list(sys.stdin.readline().strip())

def li():
	return list(map(int, sys.stdin.readline().split()))

def mp():
	return map(int, sys.stdin.readline().split())

def inp():
	return int(sys.stdin.readline())

def pr(n):
	return sys.stdout.write(str(n) + '\n')

def prl(n):
	return sys.stdout.write(str(n) + ' ')
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
from collections import OrderedDict
t = inp()
for i in range(t):
	s1 = st()
	s2 = st()
	d = OrderedDict()
	s3 = []
	for i in s2:
		s3.append(i.lower())
	for i in range(26):
		d[chr(i + 97)] = s3.count(chr(i + 97))
	d1 = {}
	d = sorted(d.items(), key=lambda x: x[1])
	d.reverse()
	s1.reverse()
	for i in range(len(d)):
		d1[d[i][0]] = s1[i]
	s = ''
	for i in s2:
		if i in d1:
			s += d1[i]
		elif i.lower() in d1:
			s += d1[i.lower()].upper()
		else:
			s += i
	print(s)


from collections import defaultdict

def get_original_text(cipher, freq_seq):
	cipher_freq = defaultdict(lambda : 0)
	for c in cipher:
		if c.isalpha():
			cipher_freq[c.lower()] += 1
	cipher_freq = dict(sorted(cipher_freq.items(), key=lambda x: (x[1], x[0])))
	start = 26 - len(cipher_freq)
	for c_f_s in cipher_freq:
		cipher_freq[c_f_s] = freq_seq[start]
		start += 1
	origin = ''
	for c in cipher:
		if c.isupper() and c.isalpha():
			origin += cipher_freq[c.lower()].upper()
		elif c.isalpha():
			origin += cipher_freq[c]
		else:
			origin += c
	return origin
for i in range(int(input())):
	freq_seq = input()
	cipher = input()
	original_text = get_original_text(cipher, freq_seq)
	print(original_text)


t = int(input())
o = 0
while o < t:
	frequency = input()
	text = input()
	text = list(text)
	posOfText = {}
	length = 0
	for (count, i) in enumerate(text):
		i = i.lower()
		if i in posOfText:
			posOfText[i].append(count)
		else:
			posOfText[i] = [count]
			if i != ' ' and i.isalpha():
				length += 1
	frequency = list(frequency)
	frequency = frequency[-length:]
	occurrenceOfText = {}
	for (keys, values) in posOfText.items():
		if len(values) in occurrenceOfText:
			occurrenceOfText[len(values)].append(keys)
		else:
			occurrenceOfText[len(values)] = [keys]
	count = 0
	for keys in sorted(occurrenceOfText.keys()):
		for txt in sorted(occurrenceOfText[keys]):
			if txt != ' ' and txt.isalpha():
				for p in posOfText[txt]:
					text[p] = frequency[count].upper() if text[p].isupper() else frequency[count]
				count += 1
	print(''.join(text))
	o += 1


t = int(input())
alphabets = 'abcdefghijklmnopqrstuvwxyz'

def find_index(i, lst):
	for (index, item) in enumerate(lst):
		if item[0] == i:
			return index
while t > 0:
	t -= 1
	alphalist = [[i, 0] for i in alphabets]
	freq = input()
	enc = input()
	encl = enc.lower()
	for (index, letter) in enumerate(alphabets):
		alphalist[index][1] = encl.count(letter)
	alphalist.sort(key=lambda x: x[1])
	for letter in enc:
		ll = letter.lower()
		if ll not in alphabets:
			print(ll, end='')
		elif letter == ll:
			print(freq[find_index(ll, alphalist)], end='')
		else:
			print(freq[find_index(ll, alphalist)].upper(), end='')
	print()


from sys import stdin, stdout
import math
from collections import Counter
import functools
import time

def tr():
	return range(int(line()))

def sm():
	return map(int, line().split())

def ln():
	return list(sm())

def nl():
	return int(line())

def ssm():
	return map(str, line().split())

def line():
	return stdin.readline().rstrip()

def b(x):
	return bin(x).replace('0b', '')

def o(x):
	if type(x) != type(''):
		x = str(x)
	stdout.write(x + '\n')

def osp(x):
	if type(x) != type(''):
		x = str(x)
	stdout.write(x + ' ')

def ol(x):
	stdout.write(' '.join(map(str, x)) + '\n')

def main():
	for _ in tr():
		freq_seq = line()
		encrypt = line()
		f_table = [[0, chr(ord('a') + i)] for i in range(26)]
		for i in encrypt:
			temp = ord(i.lower()) - ord('a')
			if 0 <= temp < 26:
				f_table[temp][0] += 1
		f_table.sort(key=lambda x: x[0])
		mapping = {}
		for i in range(26):
			mapping[f_table[i][1]] = freq_seq[i]
		new_string = ''
		for i in encrypt:
			if i.isalpha():
				if i.isupper():
					new_string += mapping[i.lower()].upper()
				else:
					new_string += mapping[i.lower()]
			else:
				new_string += i
		print(new_string)
main()


for _ in range(int(input())):
	s = input()
	t = input()
	x = []
	xd = {}
	for i in range(len(t)):
		if t[i].isupper():
			x += [1]
			xd[t[i].lower()] = xd.setdefault(t[i].lower(), 0) + 1
		elif t[i].isalpha():
			x += [0]
			xd[t[i]] = xd.setdefault(t[i], 0) + 1
		else:
			x += [-1]
	xd = sorted(xd.items(), key=lambda x: (x[1], x[0]))
	ans = {}
	for i in range(len(xd)):
		ans[xd[i][0]] = s[26 - len(xd) + i]
	for i in range(len(t)):
		if t[i].isalpha():
			if x[i] == 0:
				print(ans[t[i]], end='')
			else:
				print(ans[t[i].lower()].upper(), end='')
		else:
			print(t[i], end='')
	print()


t = int(input())
while t > 0:
	string = input()
	string_input = input()
	input_str = ''
	for i in range(len(string_input)):
		if 65 <= ord(string_input[i]) <= 90:
			input_str += chr(ord(string_input[i]) + 32)
		else:
			input_str += string_input[i]
	dct_str = {}
	for i in range(len(input_str)):
		if input_str[i] not in dct_str:
			dct_str[input_str[i]] = 1
		else:
			dct_str[input_str[i]] += 1
	dct_rev = {}
	for word in dct_str:
		if dct_str[word] not in dct_rev:
			dct_rev[dct_str[word]] = [word]
		else:
			dct_rev[dct_str[word]].append(word)
	dct_lst_rev = []
	for word in dct_rev:
		dct_lst_rev.append(word)
	dct_lst_rev.sort()
	final_freq_seq = []
	for i in range(len(dct_lst_rev)):
		final_freq_seq += sorted(dct_rev[dct_lst_rev[i]])
	final_freq_seq_str = ''
	for i in range(len(final_freq_seq)):
		if 65 <= ord(final_freq_seq[i]) <= 90 or 97 <= ord(final_freq_seq[i]) <= 122:
			final_freq_seq_str += final_freq_seq[i]
	final_seq_dct = {}
	length = len(final_freq_seq_str)
	initial = 26 - length
	for i in range(initial, 26):
		final_seq_dct[final_freq_seq_str[i - initial]] = string[i]
	ans_str = ''
	for i in range(len(string_input)):
		if 65 <= ord(string_input[i]) <= 90:
			ans_str += chr(ord(final_seq_dct[chr(ord(string_input[i]) + 32)]) - 32)
		elif 97 <= ord(string_input[i]) <= 122:
			ans_str += final_seq_dct[string_input[i]]
		else:
			ans_str += string_input[i]
	print(ans_str)
	t -= 1


for _ in range(int(input())):
	abc = list(input())
	frq = {}
	ans = {}
	inp = input()
	for u in map(ord, inp):
		if 64 < u < 91:
			if u not in frq:
				frq[u] = 0
			frq[u] += 1
		elif 64 < (u := (u - 32)) < 91:
			if u not in frq:
				frq[u] = 0
			frq[u] += 1
	for x in sorted(frq.items(), key=lambda a: (a[1], a[0]), reverse=True):
		ans[chr(x[0] + 32)] = (n := abc.pop())
		ans[chr(x[0])] = chr(ord(n) - 32)
	print(''.join([ans[c] if c in ans else c for c in inp]))


import operator
import string

def freqEnco():
	effs = dict()
	for c in string.ascii_lowercase:
		effs[c] = 0
	for c in encrypted:
		if c.isalpha():
			effs[c.lower()] += 1
	freq = []
	for c in effs:
		freq += [(c, effs[c])]
	return sorted(freq, key=operator.itemgetter(1, 0))

def solveCase():
	global encrypted
	freq = input()
	encrypted = input()
	freqEnc = freqEnco()
	corres = dict()
	for i in range(len(freqEnc)):
		corres[freqEnc[i][0]] = freq[i]
	decrypted = ''
	for c in encrypted:
		if c.isupper():
			decrypted += corres[c.lower()].upper()
		elif c.islower():
			decrypted += corres[c]
		else:
			decrypted += c
	print(decrypted)

def main():
	n = int(input())
	for _ in range(n):
		solveCase()
main()


from collections import defaultdict

def get_original_text(cipher, freq_seq):
	cipher_freq = defaultdict(lambda : 0)
	for c in cipher:
		if c.isalpha():
			cipher_freq[c.lower()] += 1
	cipher_freq_seq = sorted(cipher_freq, key=lambda x: (cipher_freq[x], x))
	f_s_counter = 25
	c_f_s_counter = len(cipher_freq_seq) - 1
	while c_f_s_counter != -1:
		cipher_freq[cipher_freq_seq[c_f_s_counter]] = freq_seq[f_s_counter]
		c_f_s_counter -= 1
		f_s_counter -= 1
	origin = ''
	for c in cipher:
		if c.isupper() and c.isalpha():
			origin += cipher_freq[c.lower()].upper()
		elif c.isalpha():
			origin += cipher_freq[c]
		else:
			origin += c
	return origin
for i in range(int(input())):
	freq_seq = input()
	cipher = input()
	original_text = get_original_text(cipher, freq_seq)
	print(original_text)


from sys import maxsize
import functools

def customSort(a, b):
	if a[0] > b[0]:
		return 1
	elif a[0] < b[0]:
		return -1
	elif a[1].lower() > b[1].lower():
		return 1
	elif a[1].lower() < b[1].lower():
		return -1
	return 0

def mgc(a, b):
	mp = {}
	for i in b:
		if not i.isalpha():
			continue
		i = i.lower()
		if i in mp:
			mp[i] += 1
		else:
			mp[i] = 1
	temp = []
	for i in mp:
		temp.append([mp[i], i])
	temp.sort(key=functools.cmp_to_key(customSort))
	mapped = {}
	i = 25
	j = len(temp) - 1
	while j >= 0:
		mapped[temp[j][1]] = a[i]
		j -= 1
		i -= 1
	ans = ''
	for i in b:
		if i.isupper():
			if i.lower() in mapped:
				ans += mapped[i.lower()].upper()
			continue
		if i in mapped:
			ans += mapped[i]
		else:
			ans += i
	return ans
for i in range(int(input())):
	a = input()
	b = input()
	print(mgc(a, b))


from collections import Counter

def decrypt(freq: str, ciphertext: str) -> str:
	counts = Counter((c.lower() for c in ciphertext if c.isalpha()))
	ordered = sorted(counts.items(), key=lambda x: x[::-1])
	translation = {ord(cipherc): plainc for ((cipherc, _), plainc) in zip(ordered, freq[-len(ordered):])}
	translation.update({ord(chr(cipherc).upper()): plainc.upper() for (cipherc, plainc) in translation.items()})
	plaintext = ciphertext.translate(translation)
	return plaintext
t = int(input())
for _ in range(t):
	freq = input()
	ciphertext = input()
	plaintext = decrypt(freq, ciphertext)
	print(plaintext)


n = int(input())
for _ in range(n):
	str1 = input()
	str = input()
	dic = {}
	for i in str:
		if i.isalpha():
			i = i.lower()
			if i in dic:
				dic[i] += 1
			else:
				dic[i] = 1
	dic = sorted(dic.items(), reverse=True)
	marklist = sorted(dic, key=lambda x: x[1], reverse=True)
	sortdict = dict(marklist)
	new_dic = {}
	j = 25
	for key in sortdict:
		new_dic[key] = str1[j]
		j -= 1
	for i in str:
		if i.isalpha():
			s1 = i.lower()
			s = new_dic[s1]
			if i.islower():
				print(s, end='')
			else:
				print(s.upper(), end='')
		else:
			print(i, end='')
	print()


t = int(input())
for i in range(t):
	fs = input()
	c = input()
	c_dict = {}
	c_list = []
	c_count = 0
	c_str = ''
	r_str = ''
	for e in range(len(c)):
		c_char = c[e].lower()
		if c_char.isalpha():
			if c_char in c_dict:
				c_dict[c_char] = c_dict[c_char] + 1
			else:
				c_dict[c_char] = 1
	for (k, v) in sorted(c_dict.items(), key=lambda item: item[1]):
		if v > c_count:
			c_list.sort()
			c_str = c_str + ''.join(c_list)
			c_list = [k]
			c_count = v
		else:
			c_list.append(k)
	c_list.sort()
	c_str = c_str + ''.join(c_list)
	len_a = len(fs)
	len_b = len(c_str)
	result_dict = dict(list(((c_str[len_b - 1 - i], fs[len_a - 1 - i]) for i in range(len_b))))
	for i in range(len(c)):
		if not c[i].isalpha():
			r_str = r_str + c[i]
		else:
			c_char = c[i].lower()
			f_char = result_dict[c_char]
			if c[i].isupper():
				f_char = f_char.upper()
			r_str = r_str + f_char
	print(r_str)


import collections, sys
T = int(sys.stdin.readline())
for _ in range(T):
	key = sys.stdin.readline().strip()
	text = sys.stdin.readline().strip()
	count = collections.Counter(filter(str.isalpha, text.lower()))
	freq = sorted(((count[k], k) for k in count))
	key = key[-len(freq):]
	key += key.upper()
	trans = ''.join((x[1] for x in freq))
	trans += trans.upper()
	print(text.translate(str.maketrans(trans, key)))


import sys
import operator
from operator import itemgetter
t = int(input())
for i in range(0, t):
	pstr = input()
	cstr = input()
	n = len(cstr)
	table = 26 * [0]
	for j in range(0, n):
		if cstr[j] >= 'a' and cstr[j] <= 'z':
			table[ord(cstr[j]) - 97] += 1
		elif cstr[j] >= 'A' and cstr[j] <= 'Z':
			table[ord(cstr[j]) - 65] += 1
	list = []
	for j in range(0, 26):
		list.append((j, table[j]))
	list.sort(key=itemgetter(1))
	mapchars = dict()
	for k in range(0, 26):
		mapchars[chr(list[k][0] + 97)] = pstr[k]
	s = ''
	for j in range(0, n):
		if cstr[j] >= 'a' and cstr[j] <= 'z':
			s += mapchars[cstr[j]]
		elif cstr[j] >= 'A' and cstr[j] <= 'Z':
			index = chr(ord(cstr[j]) - 65 + 97)
			s += chr(ord(mapchars[index]) - 97 + 65)
		else:
			s += cstr[j]
	print(s)


t = int(input())
for _ in range(t):
	a = input()
	b = input()
	alpha = []
	c = []
	for i in range(26):
		c.append(0)
		alpha.append(chr(i + 97))
	s = ''
	for i in b:
		if 25 >= ord(i) - 97 >= 0:
			c[ord(i) - 97] += 1
		elif 25 >= ord(i) - 65 >= 0:
			c[ord(i) - 65] += 1
	alpha.sort(key=lambda x: c[ord(x) - 97])
	for i in range(26):
		c[ord(alpha[i]) - 97] = i
	for i in b:
		if 25 >= ord(i) - 97 >= 0:
			s += a[c[ord(i) - 97]]
		elif 25 >= ord(i) - 65 >= 0:
			s += a[c[ord(i) - 65]].upper()
		else:
			s += i
	print(s)


from collections import Counter
t = int(input())
for _ in range(t):
	freq = str(input())[::-1]
	cryp = str(input())
	fcount = Counter(cryp.lower())
	csort = []
	enc = {}
	for f in fcount:
		if f.isalpha():
			csort.append([fcount[f], f])
		else:
			enc[f] = f
	csort.sort(reverse=True)
	for c in range(len(csort)):
		enc[csort[c][1]] = freq[c]
	fin = ''
	for c in cryp:
		if c.islower():
			fin += enc[c]
		elif c.isupper():
			fin += enc[c.lower()].upper()
		else:
			fin += enc[c]
	print(fin)


def frequency(s):
	ch = 'a'
	frq_arr = [[chr(i + ord('a')), 0] for i in range(26)]
	for i in s:
		if ord(i) - ord('a') <= 25 and ord(i) - ord('a') >= 0:
			frq_arr[ord(i) - ord('a')][1] += 1
	frq_arr.sort(key=lambda x: x[1])
	return frq_arr
for _ in range(int(input())):
	freq_seq = input()
	encrypted_txt = input()
	e_txt_lc = encrypted_txt.lower()
	frq_arr = frequency(e_txt_lc)
	for i in range(26):
		frq_arr[i][1] = freq_seq[i]
	frq_arr.sort(key=lambda x: x[0])
	for i in range(len(encrypted_txt)):
		if ord(encrypted_txt[i]) >= ord('a') and ord(encrypted_txt[i]) <= ord('z'):
			print(chr(ord(encrypted_txt[i]) + ord(frq_arr[ord(encrypted_txt[i]) - ord('a')][1]) - ord(frq_arr[ord(encrypted_txt[i]) - ord('a')][0])), end='')
		elif ord(encrypted_txt[i]) >= ord('A') and ord(encrypted_txt[i]) <= ord('Z'):
			print(chr(ord(encrypted_txt[i]) + ord(frq_arr[ord(encrypted_txt[i]) - ord('A')][1]) - ord(frq_arr[ord(encrypted_txt[i]) - ord('A')][0])), end='')
		else:
			print(encrypted_txt[i], end='')
	print('')


from collections import defaultdict
t = int(input())
for _ in range(t):
	s = input()
	s = s[::-1]
	c = input()
	d = defaultdict(int)
	for ele in c:
		ele = ele.lower()
		if ele not in s:
			continue
		d[ele] += 1
	res = [(d[key], key) for key in d]
	res.sort(reverse=True)
	d = defaultdict(int)
	ind = 0
	for ele in res:
		(_, k) = ele
		d[k] = s[ind]
		ind += 1
	res = ''
	for ele in c:
		if ele not in s and ele.lower() not in s:
			res += ele
		elif ord(ele) < 97:
			e = ele.lower()
			res += d[e].upper()
		else:
			res += d[ele]
	print(res)


import heapq

def rt(s, q):
	r = {}
	for i in range(0, 26):
		r[i] = chr(97 + i)
	d = [0 for i in range(0, 26)]
	h = []
	for i in range(0, len(q)):
		if 65 <= ord(q[i]) <= 90:
			r1 = ord(q[i]) - 65
			d[r1] += 1
		elif 97 <= ord(q[i]) <= 122:
			r1 = ord(q[i]) - 97
			d[r1] += 1
	for i in range(0, len(d)):
		if d[i] > 0:
			heapq.heappush(h, [-d[i], -i])
	j = 25
	while len(h) > 0:
		[a1, b1] = heapq.heappop(h)
		r[-b1] = s[j]
		j = j - 1
	k = ''
	for i in range(0, len(q)):
		if 65 <= ord(q[i]) <= 90:
			r1 = ord(q[i]) - 65
			y1 = ord(r[r1]) - 32
			k = k + chr(y1)
		elif 97 <= ord(q[i]) <= 122:
			r1 = ord(q[i]) - 97
			k = k + r[r1]
		else:
			k = k + q[i]
	return k
x = int(input())
for i in range(0, x):
	s = input()
	y = input()
	print(rt(s, y))


def isAlphabet(c):
	a = ord(c)
	return a >= 97 and a <= 122 or (a >= 65 and a <= 90)

def isCapital(c):
	a = ord(c)
	return a >= 65 and a <= 90

def solve(freq, cypher):
	alphabets = [chr(i) for i in range(97, 97 + 26)]
	counts = [0] * 26
	for c in cypher:
		if isAlphabet(c):
			c = c.lower()
			counts[ord(c) - 97] += 1

	def k(v):
		return counts[ord(v) - 97]
	alphabets.sort(key=k)
	ans = ''
	mp = {}
	for i in range(26):
		mp[alphabets[i]] = freq[i]
	for c in cypher:
		a = c.lower()
		if not isAlphabet(c):
			ans += c
		elif isCapital(c):
			ans += mp[a].upper()
		else:
			ans += mp[a]
	return ans
for _ in range(int(input())):
	freq = input()
	cypher = input()
	print(solve(freq, cypher))


from sys import stdin
from collections import Counter
from string import ascii_lowercase as letters
inpt = stdin.read().strip().split('\n')
i = 1
for _ in range(int(inpt[0])):
	freqs = inpt[i].strip()
	s = inpt[i + 1]
	i += 2
	s_letters = ''.join((c for c in s.lower() if c.isalpha()))
	counts = Counter(s_letters)
	h = {c: s_letters.find(c) for c in counts.keys()}
	k = ''.join(sorted(counts.keys(), key=lambda x: (counts[x], x)))
	v = freqs[26 - len(k):]
	print(s.translate(str.maketrans(k + k.upper(), v + v.upper())))


import string
for _ in range(int(input())):
	freq = input()
	cyp = input()
	d = {}
	a = set(string.ascii_lowercase)
	for i in cyp:
		j = i.lower()
		if j in a:
			if j in d:
				d[j] += 1
			else:
				d[j] = 1
	d = [[d[x], x] for x in d]
	d.sort()
	d = list((x[1] for x in d))
	d = {x: y for (x, y) in zip(d[::-1], freq[::-1])}
	s = ''
	for i in cyp:
		j = i.lower()
		if j in d:
			if i in a:
				s += d[j]
			else:
				s += d[j].upper()
		else:
			s += j
	print(s)


import sys
import os
import math
import collections
T = int(input())
for _ in range(T):
	A = input()
	B = input()
	C = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
	D = [0] * 26
	E = ''
	for i in B:
		if 26 > ord(i) - 97 >= 0:
			D[ord(i) - 97] += 1
		elif 26 > ord(i) - 65 >= 0:
			D[ord(i) - 65] += 1
	C.sort(key=lambda x: D[ord(x) - 97])
	for i in range(26):
		D[ord(C[i]) - 97] = i
	for i in B:
		if 26 > ord(i) - 97 >= 0:
			E += A[D[ord(i) - 97]]
		elif 26 > ord(i) - 65 >= 0:
			E += A[D[ord(i) - 65]].upper()
		else:
			E += i
	print(E)


import sys
for _ in range(int(input())):
	order = list(input())
	s = input()
	freq = [[0, chr(ord('a') + i)] for i in range(26)]
	for ch in s:
		val = ord(ch.lower()) - ord('a')
		if 0 <= val < 26:
			freq[val][0] += 1
	freq.sort(key=lambda x: x[0])
	mapp = dict()
	for i in range(26):
		mapp[freq[i][1]] = order[i]
	ans = []
	for ch in s:
		if ch.isalpha():
			if ch.isupper():
				ans.append(mapp[ch.lower()].upper())
			else:
				ans.append(mapp[ch.lower()])
		else:
			ans.append(ch)
	print(''.join(ans))


import sys

def Q(c):
	return (E[c], c)
T = int(input())
Anss = ''
for t in range(T):
	E = {}
	A = 'abcdefghijklmnopqrstuvwxyz'
	AA = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
	for item in A:
		E[item] = 0
	F = input()
	s = input()
	u = s.lower()
	s = list(s)
	for item in u:
		if item in A:
			E[item] += 1
	L = sorted(E, key=Q)
	Ans = {}
	for item in A:
		Ans[item] = -1
	for i in range(26):
		Ans[L[i]] = F[i]
	ind = 0
	for i in range(len(s)):
		if s[i] not in A and s[i] not in AA:
			continue
		if s[i] in A:
			s[i] = Ans[s[i]]
		else:
			s[i] = Ans[s[i].lower()].upper()
	for item in s:
		Anss += item
	Anss += '\n'
sys.stdout.write(Anss)


T = int(input())
for i in range(T):
	eng_freq = input()
	enc_text = input()
	enc_dict = {}
	for letter in enc_text:
		if letter.lower() in 'qwertyuiopasdfghjklzxcvbnm':
			if letter.lower() in enc_dict.keys():
				enc_dict[letter.lower()] += 1
			else:
				enc_dict[letter.lower()] = 1
	enc_freq = sorted(enc_dict.items(), key=lambda x: (x[1], x[0]))
	int_dict = {}
	for i in range(len(enc_freq) - 1, -1, -1):
		int_dict[enc_freq[i][0]] = i - len(enc_freq)
	res_strg = ''
	for letter in enc_text:
		if letter.lower() in 'qwertyuiopasdfghjklzxcvbnm':
			if letter.islower():
				res_strg += eng_freq[int_dict[letter]]
			else:
				res_strg += eng_freq[int_dict[letter.lower()]].upper()
		else:
			res_strg += letter
	print(res_strg)