n = int(input())
a = input()
q = int(input())
for i in range(q):
    b, c = map(str, input().split())
    A = a.replace(b, c)    
    ans = 0
    s = "0"
    for j in list(A):
        if j != " ":
            s += j
        else :
            ans += int(s[1:])
            s = "0"
    ans += int(s[1:])
    print(ans)
    a = A
    
