import collections

N = int(input())
a = list(map(int,input().split()))
Q = int(input())
ans = sum(a)
a = collections.Counter(a)

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