import sys
input = sys.stdin.readline


N = int(input())
A_list = list(input().split())
Q = int(input())
num_list = [list(input().split()) for _ in range(Q)]

for i in num_list:
    B = i[0]
    C = i[1]
    A_list = [int(str(A).replace(B, C)) for A in A_list]
    A_result = sum(A_list)
    print(A_result)
