def sol():
    n=int(input())
    A=input()
    q=int(input())
    for i in range(q):
        b,c=map(str,input().split())
        A=A.replace(b,c)
        print(sum([int(i) for i in A.split()]))
if(__name__=='__main__'):
    sol()
