#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, q;
  cin >> n;
  map<int, int> A;
  for (int i = 0; i < n; ++i) {
    int a;
    cin >> a;
    A[a] += 1;
  }
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int b, c;
    cin >> b >> c;
    A[c] += A[b];
    A.erase(b);
    long sum = 0;
    for (auto x : A) {
      sum += x.first * x.second;
    }
    cout << sum << endl;
  }
  return 0;
}
