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