#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int N, B, C, Q;
  long long s = 0;
  cin >> N;
  vector<int> v(N), count_vec(1e6);
  for (int i = 0; i < 1e6; ++i) count_vec[i] = 0;
  for (int i = 0; i < N; ++i) {
    cin >> v[i];
    s += v[i];
    count_vec[v[i] - 1]++;
  }
  cin >> Q;
  while (Q--) {
    cin >> B >> C;
    if (count_vec[B - 1] != 0) {
      s = s - (count_vec[B - 1] * B) + (count_vec[B - 1] * C);
      count_vec[C - 1] += count_vec[B - 1];
      count_vec[B - 1] = 0;
    }
    cout << s << endl;
  }
  return 0;
}
