/*   @JUDGE_ID:   1705PZ   468   C */
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#define SIZE 32767

struct a { char ch; int times; };
typedef struct a A;
int sort_func(const void *x,const void *y)
	{ return (*((A*)y)).times - (*((A*)x)).times; }

int main()
{
	int i,n;
	char s1[SIZE],s2[SIZE],map[128];
	A t1[128],t2[128];

	fgets(s1,SIZE,stdin); n = atoi(s1);
	fgets(s1,SIZE,stdin);

	while(n--)
	{
		for(i = 0;i < 128;i++)
		{
			t1[i].ch = t2[i].ch = i;
			t1[i].times = t2[i].times = isalpha(i)?16384:0;
		}
		fgets(s1,SIZE,stdin); fgets(s2,SIZE,stdin);
		for(i = 0;s1[i];i++) t1[s1[i]].times++;
		for(i = 0;s2[i];i++) t2[s2[i]].times++;
		qsort((void *)t1, 128, sizeof(A), sort_func);
		qsort((void *)t2, 128, sizeof(A), sort_func);
		for(i = 0;i < 128;i++)
			map[t2[i].ch] = t1[i].ch;
		for(i=0;s2[i];i++) printf("%c",isalpha(s2[i])?map[s2[i]]:s2[i]);
		fgets(s1,SIZE,stdin);
	}

	return 0;
}
@END_OF_SOURCE_CODE
