#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  vector<int> m1(n + 1, -1);
  unordered_map<int, vector<int> > m2;
  for (int i = 1; i < n + 1; i++) {
    int t;
    cin >> t;
    m1[i] = t;
    m2[t].push_back(i);
  }
  int q;
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    if (!m2[b].empty()) {
      int len = m2[b].size();
      for (int i = 0; i < len; i++) {
        m1[m2[b][i]] = c;
      }
      m2[c].insert(m2[c].end(), m2[b].begin(), m2[b].end());
      m2.erase(b);
    }
    long long sum = accumulate(m1.begin(), m1.end(), 0);
    sum += 1;
    cout << sum << "\n";
  }
}
