import sys
input = sys.stdin.readline

nums = [0]*(10**5)
n = int(input())
li = list(map(int,input().split()))
q = int(input())
BC = []
for _ in range(q):
    b, c = map(int,input().split())
    BC.append([b, c])

for i in range(n):
    nums[li[i]] += 1

for i in range(q):
    nums[BC[i][1]] += nums[BC[i][0]]
    nums[BC[i][0]] = 0
    s = 0
    for i in range(1,10**5):
        s += i*nums[i]
    print(s)