import numpy as np
n = input()
a = input()

q = int(input())
while q>0:
    b,c = input().split()
    a=a.replace(b,c)
    d = a.split()
    d = np.array(d)
    d = d.astype(int)
    print(d.sum())
    q-=1