a = input()
sequence_integer = [str(idx) for idx in input().split()]
num = int(input())
data = []
for idx in range(num):
  data.append([str(idx) for idx in input().split()])
for idx, jdx in data:
  sequence_integer = [s.replace(idx, jdx) for s in sequence_integer] 
  result = sum([int(idx) for idx in sequence_integer])
  print(result)
