n = int(input())
a = list(map(str, input().split()))

d = {}
for i in a:
    if not i in d.keys():
        d[i] = 1
    else:
        d[i] += 1

q = int(input())

for _ in range(q):
    b, c = map(str, input().split())
    if b in d.keys() and c in d.keys():
        d[c] += d[b]
        d.pop(b)
    elif b in d.keys():
        d[c] = d[b]
        d.pop(b)
    ans = 0
    for k, v in d.items():
        ans += int(k) * v
    print(ans)