#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  long long t, n, i, j, k, l, s = 0, q, b, c;
  cin >> n;
  map<long long, long long> m;
  for (i = 1; i <= n; i++) {
    cin >> l;
    s += l;
    m[l]++;
  }
  cin >> q;
  while (q--) {
    cin >> b >> c;
    if (m.count(b)) {
      j = m[b];
      s = s - b * j;
      s = s + c * j;
      m.erase(b);
      m[c] += j;
      cout << s << "\n";
    }
  }
}
