input()
a = list(map(int, input()))
b = int(input())
for i in range(b):
  c = list(map(int, input()))
  for d, n in enumerate(a):
    if n == c[0]:
      a[d] == c[1]
  f = 0
  for e in a:
    f += e
  print(f)
