from collections import Counter
n = int(input())
a_li = list(map(int,input().split()))
a_counter = Counter(a_li)
q = int(input())
for i in range(q):
    ans = 0
    a,b = map(int,input().split())
    dum = a_counter[a]
    a_counter[a] = 0
    a_counter[b] += dum
    for j in a_counter:
        ans += j * a_counter[j]
    print(ans)
