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

S = 0
d = {}
for i in A:
    S += i
    if i in d:
        d[i] += 1
    else:
        d[i] = 1


Q = int(input())

for i in range(Q):
    B,C = map(int,input().split())
    if B in d:
        S -= B * d[B]
        S += C * d[B]
        if C in d:
            d[C] += d[B]
        else:
            d[C] = d[B]
    print(S)
