#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  set<int> v;
  int N;
  int A;
  int Q;
  int B, C;
  cin >> N;
  int k[1000000] = {0};
  while (N--) {
    cin >> A;
    k[A] += 1;
    v.insert(A);
  }
  cin >> Q;
  while (Q--) {
    int sum = 0;
    cin >> B >> C;
    if (v.count(B)) {
      v.insert(C);
      v.erase(B);
      k[C] += k[B];
      k[B] = 0;
    }
    for (auto it = v.begin(); it != v.end(); it++) {
      sum += *it * k[*it];
    }
    cout << sum << "\n";
  }
  return 0;
}
