import collections
n = int(input())
a = list(map(int, input().split()))
q = int(input())
data = collections.Counter(a)
count, key = list(data.values()), list(data.keys())

for i in range(q):
    b, c = map(int, input().split())
    if b in key and c in key:
        count[key.index(c)] += count[key.index(b)]
        count[key.index(b)] = 0
    elif b in key:
        key.append(c)
        count.append(count[key.index(b)])
        count[key.index(b)] = 0
    res = 0
    for j in range(len(key)):
        res += count[j] * key[j]
    print(res)


