N = int(input())
A = list(input().split())
Q = int(input())
B = []
C = []
for q in range(Q):
    newB, newC = map(int,input().split())
    B.append(newB)
    C.append(newC)
for i in range(Q):
    A = [int(str(s).replace(str(B[i]), str(C[i]))) for s in A]
    sums = sum(A)
    print(sums)