#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, q, t, s = 0;
  cin >> n;
  map<int, int> m;
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    cin >> t;
    v.push_back(t);
    m[t] += 1;
    s += t;
  }
  cin >> q;
  int b, c;
  for (int i = 0; i < q; i++) {
    cin >> b >> c;
    s -= m[b] * b;
    s += m[b] * c;
    m[c] += m[b];
    m[b] = 0;
    cout << s << "\n";
  }
  return 0;
}
