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

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

  Q = int(input())

  for i in range(Q):
    li = list(map(int, input().split()))
    one = li[0]
    two = li[1]
    indexes = [j for j, x in enumerate(arr) if x == one]
    for k in range(len(indexes)):
      arr[indexes[k]] = two
    print(sum(arr))

if __name__ == '__main__':
    main()