n = int(input())
a = map(int, input().split(" "))
nums = {}
sum = 0
for e in a:
    if e in nums:
         nums[e] += 1
    else:
        nums[e] = 1
    sum += e
q = int(input())
for i in range(q):
    [b, c] = map(int, input().split())
    cnt = nums[b]
    sum -= cnt * b
    sum += cnt * c
    nums[b] = 0
    if c in nums:
        nums[c] += cnt
    else:
        nums[c] = cnt
    print(sum)
