from collections import defaultdict

n = int(input())
d = defaultdict(int)
a = list(map(int, input().split()))
for key in range(n):
  d[key] = a[key]
  
q = int(input())

for i in range(q):
  b,c = map(int, input().split())
  # print(b,c)
  keys = [k for k, v in d.items() if v == b]
  for j in keys:
    d[j] = c
  print(sum(d.values()))