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

for i in B:
    A = [str(s) for s in A]
    ans  =[d.replace(i[0],i[1]) for d in A]
    A = [int(s) for s in ans]
    print(sum(A))