# abc171_d.py
import collections

N = int(input())
A = list(map(int,input().split()))
C = collections.Counter(A)

Q = int(input())
for i in range(Q):
    b ,c = map(int,input().split())
    C[c] += C[b]
    C[b] = 0
    ans = 0
    for i,v in C.items():
        ans+=(i*v)
    print(ans)
