#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, q;
  cin >> n;
  long long cur_sum = 0;
  unordered_map<int, int> store;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    store[x]++;
    cur_sum += x;
  }
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    int occurences = store[b];
    if (occurences > 0) {
      cur_sum -= (occurences * b);
      cur_sum += (occurences * c);
      store[b] = 0;
      store[c] += occurences;
    }
    cout << cur_sum << endl;
  }
  return 0;
}
