N = int(input())
num_list = list(map(int,input().split()))
q = int(input())

for i in range(q):
  key,rep = map(int,input().split())
  while key in num_list:
    num_list.remove(key)
    num_list.append(rep)
  print(sum(num_list))
    
    