//code of chef and codes
//http://www.codechef.com/problems/CHODE
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
using namespace std;
void sort( long int arr[26][4])
{ int j,temp1,temp2;
for(int i=0;i<26;i++)
{
j=i;
temp1=arr[j+1][0];
temp2=arr[j+1][1];
while(j>=0 && arr[j][1]>temp2)
{
arr[j+1][0]=arr[j][0];
arr[j+1][1]=arr[j][1];
j--;
}
arr[j+1][0]=temp1;
arr[j+1][1]=temp2;
}
}
int main()
{
int t;char ch;
scanf("%d",&t);
while(t>0)
{
long int map[26][4]={0,0,0,0};
char arr[28]={'\0'},arr1[100000]={'\0'},arr2[100000]={'\0'};
for(int i=0;i<26;i++)
{
map[i][0]=i;
map[i][2]=i;
map[i][1]=0;
map[i][3]=0;
}
scanf("%s",arr);
cin.get(ch);
cin.getline(arr1,100000);
int i=0;
for(i=0;arr1[i]!='\0';i++)
{
if(arr1[i]>='a' && arr1[i]<='z')
map[int(arr1[i]-'a')][1]++;
else if(arr1[i]>='A' && arr1[i]<='Z')
map[int(arr1[i]-'A')][1]++;
}
sort(map);
for(int i=0;i<26;i++)
{
for(int j=0;j<26;j++)
{
if(map[j][0]==i)
{
map[i][3]=map[j][2];
break;
}
}
}
for(i=0;arr1[i]!='\0';i++)
{
if(arr1[i]>='a' && arr1[i]<='z')
arr2[i]=arr[map[map[int(arr1[i])-97][3]][2]];
else if(arr1[i]>='A' && arr1[i]<='Z')
arr2[i]=(arr[map[map[int(arr1[i])-65][3]][2]]-32);
else
arr2[i]=arr1[i];
}
arr2[i]='\0';
printf("%s",arr2);
t--;
}
}