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