#include<iostream>
#include<string>
#include<cstring>
#include<sstream>
#include<cassert>
using namespace std;
int main( ) {
int T,N,myi;
cin >> T;
assert ( T>= 0 && T< 1001 ) ;
char c[ 94 ] ,p[ 94 ] ;
string s,fhalf,shalf,sans;
s.resize ( 1000000 ) ,fhalf.resize ( 1000000 ) ,shalf.resize ( 1000000 ) ,sans.resize ( 1000000 ) ;
while ( T-- ) {
cin >> N;
assert ( N< 95 ) ;
for ( int i= 0 ; i< N; i++ )
cin >> c[ i] >> p[ i] ;
cin >> s;
for ( int i= 0 ; i< N; i++ ) {
size_t found = s.find_first_of ( c[ i] ) ;
while ( found! = std:: string :: npos ) {
if ( i> 0 && c[ i] == p[ i- 1 ] )
break ;
else {
s[ found] = p[ i] ;
found = s.find_first_of ( c[ i] ,found+ 1 ) ;
}
}
}
size_t fdot = s.find ( '.' ) ;
if ( fdot! = std:: string :: npos ) {
fhalf = s.substr ( 0 ,fdot) ;
shalf = s.substr ( fdot) ;
size_t flast = shalf.find_last_of ( "123456789" ) ;
size_t ffirst = fhalf.find_first_of ( "123456789" ) ;
if ( flast! = std:: string :: npos && ffirst! = std:: string :: npos )
sans = fhalf.substr ( ffirst) + shalf.substr ( 0 ,flast+ 1 ) ;
else if ( ffirst! = std:: string :: npos )
sans = fhalf.substr ( ffirst) ;
else if ( flast! = std:: string :: npos )
sans = shalf.substr ( 0 ,flast+ 1 ) ;
else
sans = '0' ;
cout << sans<< endl;
}
else {
stringstream( s) >> myi;
cout << myi<< endl;
}
}
return 0 ;
}
I2luY2x1ZGU8aW9zdHJlYW0+CiNpbmNsdWRlPHN0cmluZz4KI2luY2x1ZGU8Y3N0cmluZz4KI2luY2x1ZGU8c3N0cmVhbT4KI2luY2x1ZGU8Y2Fzc2VydD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKaW50IG1haW4oKXsKCWludCBULE4sbXlpOwoJY2luPj5UOwoJYXNzZXJ0KFQ+PTAmJlQ8MTAwMSk7CgljaGFyIGNbOTRdLHBbOT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