#include <bits/stdc++.h>
using namespace std;
const int mx = 1e5 + 5;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, q, t;
  cin >> n;
  int a[n];
  vector<int> val[mx];
  long long int sum = 0;
  for (int i = (0); i < (n); i++) {
    cin >> a[i];
    t = a[i];
    sum += t;
    val[t].push_back(i);
  }
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    for (auto x : val[b]) {
      val[c].push_back(x);
    }
    int s = val[b].size();
    sum = sum - (b * s) + (c * s);
    val[b].clear();
    cout << sum << "\n";
  }
  return 0;
}
