import sys

def retList(target, A):
  split = target.split()  
  return [int(str(s).replace(split[0], split[1])) for s in A]

N = int(input())
#print(N)

a = input().split()
A = [int(b.strip()) for b in a]
#print(A)

Q =  int(input())
#print(Q)

for i in range(Q):
  target = input()
  if i == 0:
    ret = retList(target, A)
  else:
    ret = retList(target, ret)
  print(sum(ret))
