import java.io.*;
import java.util.*;
import java.math.*;
import static java.
lang.
Math.
*; import static java.
util.
Arrays.
*;
class B {
static final char PROB = B.class.getSimpleName().charAt(0);
static final boolean PRAC = false;
static final int INF = 1 << 20;
static final int[] di = { -1, 0, 0, 1 };
static final int[] dj = { 0, -1, 1, 0 };
static Scanner sc
= new Scanner
(System.
in);
final int C, D, N;
final char[] c1, c2, c3, d1, d2;
final char[] n;
public B() {
C = sc.nextInt();
c1 = new char[2 * C];
c2 = new char[2 * C];
c3 = new char[2 * C];
for (int i = 0; i < C; i++) {
char[] cs = sc.next().toCharArray();
c1[2 * i] = c2[2 * i + 1] = cs[0];
c1[2 * i + 1] = c2[2 * i] = cs[1];
c3[2 * i] = c3[2 * i + 1] = cs[2];
}
D = sc.nextInt();
d1 = new char[2 * D];
d2 = new char[2 * D];
for (int i = 0; i < D; i++) {
char[] cs = sc.next().toCharArray();
d1[2 * i] = d2[2 * i + 1] = cs[0];
d1[2 * i + 1] = d2[2 * i] = cs[1];
}
N = sc.nextInt();
n = sc.next().toCharArray();
}
List<Character> list = new ArrayList<Character>();
Set<Character> set = new HashSet<Character>();
for (int i = 0; i < N; i++) {
if (pre != null)
for (int j = 0; j < 2 * C; j++)
if (c1[j] == pre && c2[j] == n[i])
c = c3[j];
if (pre != null && c != null) {
list.set(list.size() - 1, c);
pre = c;
} else {
if (pre != null)
for (int j = 0; j < 2 * D; j++)
if (d1[j] == pre)
set.add(d2[j]);
if (set.contains(n[i])) {
set.clear();
list.clear();
pre = null;
} else {
list.add(n[i]);
pre = n[i];
}
}
}
return list.toString();
}
public static void main
(String...
args) { // large();
int T
= Integer.
parseInt(sc.
nextLine()); for (int t = 1; t <= T; t++) {
System.
err.
printf("Case #%s%n", t
); System.
out.
printf("Case #%s: %s%n", t,
new B
().
solve()); }
}
public static void small() {
String in
= PROB
+ "-small" + (PRAC
? "-practice" : "-attempt" + 0) + ".in"; String out
= PROB
+ "-small.out"; try {
if (!PRAC)
for (int i
= 1; new File(PROB
+ "-small" + "-attempt" + i
+ ".in").
exists(); i
++) in = PROB + "-small" + "-attempt" + i + ".in";
e.printStackTrace();
}
}
public static void large() {
String in
= PROB
+ "-large" + (PRAC
? "-practice" : "") + ".in"; String out
= PROB
+ "-large.out"; try {
e.printStackTrace();
}
}
private static void debug
(Object...
os) { System.
err.
println(deepToString
(os
)); }
}
aW1wb3J0IGphdmEuaW8uKjsKaW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5tYXRoLio7CgppbXBvcnQgc3RhdGljIGphdmEubGFuZy5NYXRoLio7CmltcG9ydCBzdGF0aWMgamF2YS5sYW5nLkNoYXJhY3Rlci4qOwppbXBvcnQgc3RhdGljIGphdmEudXRpbC5BcnJheXMuKjsKaW1wb3J0IHN0YXRpYyBqYXZhLnV0aWwuQ29sbGVjdGlvbnMuKjsKaW1wb3J0IHN0YXRpYyBqYXZhLm1hdGguQmlnSW50ZWdlci4qOwoKY2xhc3MgQiB7CglzdGF0aWMgZmluYWwgY2hhciBQUk9CID0gQi5jbGFzcy5nZXRTaW1wbGVOYW1lKCkuY2hhckF0KDApOwoJc3RhdGljIGZpbmFsIGJvb2xlYW4gUFJBQyA9IGZhbHNlOwoKCXN0YXRpYyBmaW5hbCBpbnQgSU5GID0gMSA8PCAyMDsKCXN0YXRpYyBmaW5hbCBpbnRbXSBkaSA9IHsgLTEsIDAsIDAsIDEgfTsKCXN0YXRpYyBmaW5hbCBpbnRbXSBkaiA9IHsgMCwgLTEsIDEsIDAgfTsKCXN0YXRpYyBTY2FubmVyIHNjID0gbmV3IFNjYW5uZXIoU3lzdGVtLmluKTsKCglmaW5hbCBpbnQgQywgRCwgTjsKCWZpbmFsIGNoYXJbXSBjMSwgYzIsIGMzLCBkMSwgZDI7CglmaW5hbCBjaGFyW10gbjsKCglwdWJsaWMgQigpIHsKCQlDID0gc2MubmV4dEludCgpOwoJCWMxID0gbmV3IGNoYXJbMiAqIENdOwoJCWMyID0gbmV3IGNoYXJbMiAqIENdOw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