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