N = int(input())
A = input().replace(' ', '')
S = A
Q = int(input())
S_sum = 0

for _ in range(Q):
    B, C = map(str, input().split())
    S = S.replace(B, C)
    for s in list(S):
        S_sum += int(s)
    else:
        print(S_sum)
        S_sum = 0