import re
n=int(input())
lst=list(map(str,input().split()))
q=int(input())
for i in range(q):
  s,t=map(str,input().split())
  lst=[re.sub(s, t, r) for r in lst]
  ans=[int(x) for x in lst]
  print(sum(ans))