N = int(input())
A = list(map(str, input().split()))
Q = int(input())
for i in range(1,Q+1):
  x, y = map(str, input().split())
  A = [s.replace(x, y) for s in A]
  A_int = map((lambda x: int(x)), A)
  print(sum(A_int))