n=int(input())
a=list(map(int,input().split()))


q=int(input())
for i in range(q):
  s=0
  b,c=map(int,input().split())
  for j in a:
    if a[j]==b:
      a[j]=c
      s+=a[j]
    else:s+=a[j]
  print(s)
  