n = int(input())
numlist = list(map(int,input().split()))
numcount = [0]*10**5
for num in numlist:
    numcount[num] += 1
n = int(input())
anslist = []
for i in range(n):
    b,c = map(int,input().split())
    numcount[c] += numcount[b]
    numcount[b] = 0
    result = 0
    for i,num in enumerate(numcount):
        result += i*num
    anslist.append(result)

for ans in anslist:
    print(ans)
