n = int(input())
A = list(map(int, input().split()))
Q = int(input())
k = [list(map(int, input().split())) for i in range(Q)]

for i in range(Q):
  A = [int( str(s).replace( str(k[i][0]),str(k[i][1]) ) ) for s in A]
  print(sum(A))