#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> vec(n);
  for (int i = 0; i < n; ++i) {
    cin >> vec.at(i);
  }
  int q;
  cin >> q;
  vector<vector<int>> che(q, vector<int>(2));
  for (int i = 0; i < q; ++i) {
    cin >> che.at(i).at(0) >> che.at(i).at(1);
  }
  map<int, int> num;
  for (int i = 0; i < n; ++i) {
    if (num.count(i)) {
      num[i] = 1;
    } else {
      num[i]++;
    }
  }
  for (int i = 0; i < q; ++i) {
    int ans = 0;
    if (num.count(che.at(i).at(0))) {
      if (num.count(che.at(i).at(1))) {
        num[che.at(i).at(1)] += num[che.at(i).at(0)];
        num.erase(che.at(i).at(0));
      } else {
        num[che.at(i).at(1)] = num[che.at(i).at(0)];
      }
    }
    for (int j = 0; j < num.size(); ++j) {
      ans += (num.at(j) + 1) * num[num.at(j)];
    }
    cout << ans << endl;
  }
}
