from collections import Counter
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
num = Counter(A)
for i in range(Q):
    b,c = map(int,input().split())
    num[c] += num[b]
    num[b] = 0
    sums = 0
    for j in num:
        sums += num[j]*j
    print(sums)