n = int(input())
numlist = list(map(int,input().split()))
numcount = [0]*10**6

numset = set()
for num in numlist:
    numcount[num] += 1
    numset.add(num)
n = int(input())
anslist = []

for i in range(n):
    b,c = map(int,input().split())
    numcount[c] += numcount[b]

    numcount[b] = 0
    numset.add(c)
    numset.discard(b)
    result = 0
    for num in list(numset):
        result += numcount[num]*num
    anslist.append(result)

for ans in anslist:
    print(ans)
