from collections import Counter

N = int(input())

A = list(map(int, input().split()))

Q = int(input())

BC = []
for _ in range(Q):
    b, c = map(int, input().split())
    BC.append([b, c])

C = Counter(A)
for bc in BC:
    b = bc[0]
    c = bc[1]
    if b in C:
        if c in C:
            C[c] += C[b]
            C[b] = 0
        else:
            C[c] = C[b]
            C[b] = 0
    print(sum([k*v for k, v in C.items()]))
