N = int(input())
A_lst =  list(input().split())
Q = int(input())
BC_lst = []

for i in range(Q):
    BC_lst += [list(input().split())]
    
for BC in BC_lst:
    A_lst = [A.replace(BC[0],BC[1]) for A in A_lst]
    print(sum(list(map(int,A_lst))))

