N = int(input())
l = [0]*(10**5+1)
A = list(map(int,input().split()))
Q = int(input())
for a in A:
  l[a] += a

for _ in range(Q):
  b,c = map(int,input().split())
  l[c] += (l[b]//b)*c
  l[b] = 0
  print(sum(l))