from collections import defaultdict
n = int(input())
a = list(map(int, input().split()))
dic = defaultdict(int)
for i in range(n):
    dic[a[i]]+=1
Q= int(input())
for q in range(Q):
    b, c = map(int, input().split())
    dic[c]+=dic[b]
    dic[b]=0
    ans = 0
    for j in dic:
        ans += dic[j]*j
    print(ans)