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

