import sys
input = sys.stdin.readline


N=int(input())
A=[str(i) for i in input().split()]
Q=int(input())

BC= [list(map(str, input().split())) for i in range(Q)]


for i in range(Q):
    A = [s.replace(BC[i][0],BC[i][1]) for s in A]

    print(sum(list(map(int,A))))
