#include <bits/stdc++.h>
using namespace std;
using ull = unsigned long long;
int main() {
  int n, q, a, b, c;
  ull s = 0;
  cin >> n;
  vector<int> m(n, 0);
  for (int i = 0; i < n; ++i) {
    cin >> a;
    m[a]++;
    s += a;
  }
  cin >> q;
  while (q--) {
    cin >> b >> c;
    s += (c - b) * m[b];
    m[c] += m[b];
    m[b] = 0;
    cout << s << endl;
  }
  return 0;
}
