n=int(input())
A=input()
q=int(input())
su=[]
for i in range(q):
    s=0
    l=''
    b,c=map(str,input().split())
    A=A.replace(b,c)
    l=A.split(' ')
    for i in l:
        s+=int(i)
    su.append(s)
for i in su:
    print(i)