import collections

N = int(input())
a = list(map(int,input().split()))
Q = int(input())

total = sum(a)
a = collections.Counter(a)

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