N=int(input())
A=list(map(str,input().split()))
Q=int(input())
BC=[list(map(str,input().split())) for _ in range(Q)]

for i,j in BC:
  for l in range(N):
    A[l]=A[l].replace(i,j)
  ans=0
  for k in A:
    ans+=int(k)
  print(ans)
  