from collections import defaultdict
n = int(input())
a = list(map(str,input().split()))
q = int(input())
d = defaultdict(lambda: 0)
for nums in a:
    d[nums] += 1
for i in range (q):
    Sum = 0
    b,c = map(str,input().split())
    d[c] += d[b]
    del d[b]
    for keys in d:
        cnt = d[keys]
        keys = int(keys)
        Sum += keys*cnt
    print(Sum)