s=str(input())
s=str(input())
q = int(input())
x = [list(map(int,input().split())) for i in range(q)]
ans=s
for i in range(q):
    ans = ans.replace(str(x[i][0]),str(x[i][1]))
    print(sum(map(int,ans.split())))