N = int(input())
A =  input().split()
A = sorted(A)
Q = int(input())
X2=[]
for i in range(Q):
  B,C = input().split()
  X2 = [s.replace(B,C) for s in A]
  A = []
  A = sorted(X2)
  X2=[]
  Z = list(map(int,A))
  print(sum(Z))