import collections
n = int(input())
a_lst = list(map(int, input().split()))

cs = collections.Counter(a_lst)
res = sum(a_lst)
q = int(input())
for i in range(q):
  b, c = map(int, input().split())
  if b in cs:
    res = res - b * cs[b] + c * cs[b]
    if c in cs:
      cs[c] += cs[b]
      cs[b] = 0
    else:
      cs[c] = cs[b]
      cs[b] = 0      
    print(res)