n = int(input())

freq = {}
A = list(map(int,input().split()))
for i in range(len(A)):
    freq[A[i]] = freq.get(A[i],0) + 1

ans = 0

q = int(input())

for i in range(q):
    ans = 0
    b,c = map(int,input().split())
    freq[c] += freq[b]
    freq[b] = 0
    for keys,values in freq.items():
        ans += keys*values
    print(ans)