#include <bits/stdc++.h>
using namespace std;
vector<long long> v(1000000);
int main() {
  int n, p, q, t, a, i, b, ans = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a;
    ans += a;
    v[a] += 1;
  }
  cin >> t;
  while (t--) {
    cin >> p >> q;
    if (v[p] != 0) {
      v[q] += v[p];
      ans -= p * v[p];
      ans += q * v[p];
      v[p] = 0;
    }
    cout << ans << endl;
  }
  return 0;
}
