from collections import Counter

N = int(input())
As = list(map(int, input().split()))

d = Counter(As)

Q = int(input())
for _ in range(Q):
    B, C = map(int, input().split())
    if B in d:
        d[C] += d[B]
        d[B] = 0
    
    print(sum(k*v for k, v in d.items()))