import sys

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

N = int(input())
a = input().split()
Q =  int(input())

for i in range(Q):
  target = input()
  if i == 0:
    ret = retList(target, a)
  else:
    ret = retList(target, ret)
  print(sum([int(b.strip()) for b in ret]))
