N = int(input())
A = list(input().split()) 
Q = int(input())
aLine = ""
for a in A:
  aLine += a
Rep = [list(input().split()) for i in range(Q)]
for i in range(Q):
  result = 0
  aLine = aLine.replace(Rep[i][0],Rep[i][1])
  for a in aLine:
    result += int(a)
  print(result)