from sys import stdin
N = int(stdin.readline().rstrip())
list = [int(x) for x in stdin.readline().rstrip().split()]
Q = int(stdin.readline().rstrip())
#print(1 in list)
for i in range(Q):
  s, t = [int(x) for x in stdin.readline().rstrip().split()]
  sum = 0
  if s in list:
    for j in range(len(list)):
      if list[j] == s:
        list[j] = t
      sum += list[j]
  print(sum)