fork download
import math
for _ in range(input()):
    n=input()
    c={}
    for i in range(n):
        a,b=raw_input().split()
        #print ord(a),ord(b)
        c[a]=b
    s=raw_input() 
    for i in c.keys():
        s=s.replace(i,c[i])
    s=s.strip('0')    
    a,b=math.floor(float(s)),float(s)
    #print a,b
    if a==b:
        print int(b)
    else:
        if b<1:
            d=str(b)
            print d[1:]
        else:
            print b
            
        
Success #stdin #stdout 0s 23352KB
stdin
4
2
5 3
3 1
5
0
01800.00
0
0.00100
3
x 0
d 3
# .
0xd21#dd098x
stdout
3
1800
.001
321.33098