from collections import Counter

n = int(input())
a = list(map(int, input().split()))
q = int(input())
l = [list(map(int, input().split())) for _ in range(q)]
dic = Counter(a)

for i in range(q):
    (b, c) = l[i] 
    num = dic[b]
    dic[b] = 0
    if dic[c] == 0:
        dic[c] = num
    else:
        dic[c] += num
    ans = 0
    for i, j in dic.items():
        ans += i*j
    print(ans)