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

cnt = 0
l = [0]*(10**5+1)
for v in A:
    l[v] += 1
    cnt += v

for i in range(Q):
    b,c = map(int, input().split())
    cnt += (c-b)*l[b]
    print(cnt)
    l[c] += l[b]
    l[b] = 0