import collections

N = int(input())
A = input().split()
Q = int(input())
a = collections.Counter(A)
ans2 = []
for i in range(Q):
    ans = ""
    B, C = map(str, input().split())
    if B in a and C in a:
        a[C] += a[B]
        del a[B]
    elif B in a and C not in a:
        a[C] = a[B]
        del a[B]
    for key, value in a.items():
        ans += "{}*{}+".format(key, value)
    ans2.append(eval(ans[:-1]))
[print(a) for a in ans2] 