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