N = int(input())
A = list(map(str, input().split()))
Q = int(input())
L = [list(map(str, input().split())) for i in range(Q)]

for j in range(Q):
    A = [k.replace(L[j][0], L[j][1]) for k in A]
    B = sum(map(int, A))
    print(B)
