n = int(input())
a = input()
q = int(input())
bcls = []
for _ in range(q):
  bcls.append(list(map(str,input().split())))
for i in range(q):
  a = a.replace(bcls[i][0],bcls[i][1])
  ls = list(map(int,a.split()))
  print(sum(ls))