N = int(input())

A_in = input()
    
Q = int(input())

for i in range(Q):
    B, C = map(str, input().split())
    
    A_in = A_in.replace(B, C)
    
    #A_val = []
    #for i in A_in.split():
    #    A_val.append(int(i))
    #
    #print(sum(A_val))
     
    S = 0
    for i in A_in.split():
        S += int(i)
    print(S)
        