import sys

def main():
  # Xはもっとも近い整数，Nは与えられるP
  #X,N = map(int, input().split())
  N = input()

  # listを取得
  arr = list(map(int, input().split()))

  Q = int(input())

  li =  []
  for i in range(Q):
    lis = list(map(int, input().split()))
    li.append(lis)
  
  for i in range(Q):
    ans = 0
    one = li[i][0]
    two = li[i][1]
    for j in range(len(arr)):
      if one == arr[j]:
        arr[j] = two
      ans = ans + arr[j]
    print(ans)
  
  # 正常終了
  sys.exit()

if __name__ == '__main__':
    main()