From the childhood we are taught that a comes before b then b comes before c and so on.So whenever we try to sort any given string we sort it in that manner only placing a before b and so on.But what happens if we initially change the pattern of sorting .This question arrived in Arav's  young mind. He thought what would the final string be like if z comes before a and a comes after c and so on. He got really puzzled in finding out the final sorted string.So he asks you for help.

He gives you two strings.One the pattern string P which decides the order of alphabets and the second that is the final string F which needs to be sorted. Help him by telling him the final sorted string.

Input:

The first line contains T denoting the number of test cases.Each test case consists of two lines.The first line contains the pattern string P indicating the relative ordering of alphabets and the second line contains the final string F to be sorted.

Output:

Print T lines where each line contains the sorted string.

Constraints:

1 ≤  T ≤ 10    

|P| = 26

1 ≤ |F| ≤ 10^5

Both the string consists only of  characters 'a'-'z'.

SAMPLE INPUT
2
wcyuogmlrdfphitxjakqvzbnes
jcdokai
miruqopsxthzdcnvkfbglwayje
wgfsyvno

SAMPLE OUTPUT
codijak
osnvfgwy
'''
# Read input from stdin and provide input before running code

name = raw_input('What is your name?\n')
print 'Hi, %s.' % name
'''

t=input()
while t>0:
	x = raw_input()
	d = dict()
	j=0
	for i in x:
		d[i]=j
		j=j+1
	y = raw_input()
	d1 = dict()
	for i in y:
		if d[i] in d1.keys():
			k = d1[d[i]] + str(i)
		else:
			k = str(i)
		d1[d[i]]= k
	s=""
	for i in d1:
		s=s+str(d1[i])
	print s
	t=t-1