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

for _ in range(Q):
    query = input().split()
    query = [str(i) for i in query]

    B = query[0]
    C = query[1]

    given_list_replased = [a.replace(B, C) for a in given_list]
    given_list_replased = [int(i) for i in given_list_replased]
    print(sum(given_list_replased))
    
    given_list = [str(i) for i in given_list_replased]