from collections import Counter
N = int(input())
A = list(map(int,input().split()))
s = sum(A)
Q = int(input())
c = Counter(A)
for _ in range(Q):
    B,C = map(int,input().split())
    s -= B*c[B]
    s += C*c[B]
    c[C] += c[B]
    c[B] = 0
    print(s)