import collections

n = int(input())
a = list(map(int, input().split()))
q = int(input())
b_c = [list(map(int,input().split())) for i in range(q)]

cc = collections.Counter(a)
c_buf = 0
cc_j_buf = 0
for i in range(q):
  flag = 0
  b = b_c[i][0]
  c = b_c[i][1]
  count = 0
  for j in cc:
    if j == b:
      if c in cc:
        cc[c] = cc[c] + cc[j]
      else:
        cc_j_buf = cc[j]
        flag = 1
      cc[j] = 0
  if flag == 1:
    cc[c] = cc_j_buf
  for j in cc:
    count = count + j*cc[j]
  print(count)