#include <iostream>
#include <vector>
#include <algorithm>
#include <map>

typedef long long ll;

int hoge4(){
  ll N;
  std::cin >> N;
  std::map<ll, ll> A;
  for(ll i = 0 ; i < N ; i++){
    ll temp;
    std::cin >> temp;
    A[temp]++;
  }
  ll Q;
  std::cin >> Q;
  std::vector<ll> B(Q);
  std::vector<ll> C(Q);
  for(ll i = 0 ; i < Q ; i++){
    std::cin >> B[i] >> C[i];
  }
  ll sum = 0;
  for(auto it = A.begin() ; it != A.end(); it++){
    sum += (it -> second) * (it -> first);
  }
  for(int i = 0 ; i < Q ; i++){
    if(A[B[i]] != 0){
      A[C[i]] += A[B[i]];
      sum += (C[i] - B[i]) * A[B[i]];
      A[B[i]] = 0;
      // for(auto it = A.begin(); it != A.end() ; it++){
      // 	std::cout << "f : " << it -> first << " s : " << it -> second << std::endl;
      // }
    }
    std::cout << sum << std::endl;
  }
  return 0;
}

