N = int(input())
A = [int(i) for i in input().split()]
Q = int(input())
B = [];C=[]
for i in range(Q):
    b,c = map(int,input().split())
    B.append(b);C.append(c)
from collections import defaultdict
R = defaultdict(int)
for a in A:
    R[a] += 1
for q in range(Q):
    cnt = R[B[q]]
    R[C[q]] += cnt
    del R[B[q]]
    ans = 0
    for key in R.keys():
        ans += (R[key] * key)
    print(ans)