#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  int64_t s = 0;
  vector<vector<int>> v(100000, vector<int>(0));
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
    s += a.at(i);
    v.at(a.at(i)).push_back(i);
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    if (b != c) {
      int z = v.at(b).size();
      for (int j = 0; j < z; j++) {
        v.at(c).push_back(v.at(b).at(j));
      }
      for (int j = 0; j < z; j++) {
        v.at(b).pop_back();
      }
      s += (c - b) * z;
      cout << s << endl;
    } else
      cout << s << endl;
  }
}
