import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class Main {
public static void main(String[] args) throws NumberFormatException,IOException
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader sc = new BufferedReader(isr);
Main x=new Main();
//System.out.println(x.getDecrypted("123du_)ÅÉÉÐÿ ~`(0#$^&*(mMyz!", "qwrtyuipasdfgjkzxcvbnmheol"));
int testCase = Integer.parseInt(sc.readLine());
for (int i = 0; i < testCase; i++)
{
String seq=sc.readLine();
String text=sc.readLine();
System.out.println(x.getDecrypted(text, seq));
}
}
public boolean isLetter(Character ch)
{
if(ch.toString().matches("[a-zA-Z]{1}"))
{
return true;
}
return false;
}
public String getDecrypted(String cypher,String engEreqSec)
{
Main x = new Main();
HashMap<Character, Integer> freqCount = new HashMap<Character, Integer>();
char letter = 'a'; //65 - 90 97 - 122
for (int i = 0; i < 26; i++)
{
int counter = 0;
for (int j = 0; j < cypher.length(); j++)
{
if (cypher.charAt(j) == letter || cypher.charAt(j) == letter - 32)
{
counter++;
}
}
freqCount.put(letter, counter);
letter++;
}
ValueComparator bvc = x.new ValueComparator(freqCount);
TreeMap<Character, Integer> sorted_map = new TreeMap<Character, Integer>(bvc);
sorted_map.putAll(freqCount);
Set<Character> sortedSet=sorted_map.keySet();
//System.out.println(sorted_map);
HashMap<Character, Character> charMapping = new HashMap<Character, Character>();
int index=25;
for (Character character : sortedSet)
{
charMapping.put(character,engEreqSec.charAt(index));
index--;
}
//System.out.println(charMapping);
StringBuilder decypherdText=new StringBuilder();
for(int i=0;i<cypher.length();i++)
{
char c=cypher.charAt(i);
//65 - 90 97 - 122
//System.out.println((int)c);
//Character.isJavaLetter(arg0)
//if(Character.isLetter(c))
if(x.isLetter(c))
{
if(Character.isUpperCase(c))
{
Character ch=charMapping.get(Character.toLowerCase(c));
decypherdText.append(Character.toUpperCase(ch));
}
else
{
decypherdText.append(charMapping.get(c).charValue());
}
}
else
{
decypherdText.append(c);
}
}
return decypherdText.toString();
}
class ValueComparator implements Comparator<Character> {
Map<Character, Integer> base;
public ValueComparator(Map<Character, Integer> base) {
this.base = base;
}
// Note: this comparator imposes orderings that are inconsistent with
// equals.
public int compare(Character a, Character b)
{
if (base.get(a) > base.get(b))
{
return -1;
}
else if(base.get(a)==base.get(b))
{
if(a < b)
return 1;
else
return -1;
}
else
{
return 1;
} // returning 0 would merge keys
}
}
}