from collections import Counter

N = int(input())
data = list(map(int,input().split()))
Q = int(input())
pala = list()
for i in range(Q):
    b = list(map(int,input().split()))
    pala.append(b)

count = Counter(data)
flag = 0


sum = 0
for k , v in count.items():
    sum += (k * v)

for B,C in pala:
    if B in count:
      diff = (C - B) * count[B]
      count[C] += count.pop(B)

    res = sum + diff
    sum = res
    print(res)
    diff = 0
    

