#include <bits/stdc++.h>
using namespace std;
template <typename T>
string to_string(T o) {
  stringstream s;
  s << o;
  return s.str();
}
template <typename T>
string to_string(vector<T> v) {
  stringstream s;
  s << "[";
  for (auto i = 0; i < v.size(); i++) {
    s << to_string(v.at(i));
    if (i != v.size() - 1) {
      s << ", ";
    }
  }
  s << "]";
  return s.str();
}
template <typename T>
ostream &operator<<(ostream &o, const vector<T> &obj) {
  o << to_string(obj);
  return o;
}
template <typename T>
string to_string(vector<vector<T>> m) {
  stringstream s;
  s << "[" << endl;
  for (auto i = 0; i < m.size(); i++) {
    auto v = m[i];
    s << "  "
      << "#" << i << ":"
      << " " << to_string(v);
    s << ", " << endl;
  }
  s << "]";
  return s.str();
}
template <typename T>
ostream &operator<<(ostream &o, const vector<vector<T>> &obj) {
  o << to_string(obj);
  return o;
}
template <typename K, typename V>
string to_string(pair<K, V> p) {
  stringstream s;
  s << "pair(" << to_string(p.first) << ", " << to_string(p.second) << ")";
  return s.str();
}
template <typename K, typename V>
ostream &operator<<(ostream &o, const pair<K, V> &obj) {
  o << to_string(obj);
  return o;
}
template <typename K, typename V>
string to_string(map<K, V> m) {
  stringstream s;
  s << "{" << endl;
  for (const auto &p : m) {
    s << "  " << to_string(p.first) << ": " << to_string(p.second) << ","
      << endl;
  }
  s << "}";
  return s.str();
}
template <typename K, typename V>
ostream &operator<<(ostream &o, const map<K, V> &obj) {
  o << to_string(obj);
  return o;
}
template <typename T>
string to_string(set<T> ss) {
  stringstream s;
  s << "set"
    << "{";
  for (auto itr = ss.begin(); itr != ss.end(); itr++) {
    s << to_string(*itr);
    if (next(itr, 1) != ss.end()) {
      s << ", ";
    }
  }
  s << "}";
  return s.str();
}
template <typename T>
ostream &operator<<(ostream &o, const set<T> &obj) {
  o << to_string(obj);
  return o;
}
template <typename T>
string to_string(queue<T> q) {
  auto temp = queue<T>{};
  stringstream s;
  s << "queue{";
  while (!q.empty()) {
    auto top = q.front();
    s << to_string(top) << ", ";
    q.pop();
    temp.push(top);
  }
  s << "}";
  while (!temp.empty()) {
    auto top = temp.front();
    temp.pop();
    q.push(top);
  }
  return s.str();
}
template <typename T>
ostream &operator<<(ostream &o, const queue<T> &obj) {
  o << to_string(obj);
  return o;
}
const int N = 100010;
int uf[N];
void init(void) {
  for (int i = 0; i < N; i++) uf[i] = -1;
}
int root(int x) {
  int r = x;
  while (uf[r] >= 0) r = uf[r];
  while (uf[x] >= 0) {
    int tmp = uf[x];
    uf[x] = r;
    x = tmp;
  }
  return r;
}
void join(int x, int y) {
  uf[x] = y;
  uf[y] = -1;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  std::cout << std::setprecision(20);
  long long n;
  cin >> n;
  vector<long long> as;
  for (auto i = 0; i < n; i++) {
    long long a;
    cin >> a;
    as.push_back(a);
  }
  init();
  long long q;
  cin >> q;
  for (auto i = 0; i < q; i++) {
    long long f, t;
    cin >> f >> t;
    join(f, t);
    long long total = 0;
    for (auto i = 0; i < n; i++) {
      total += root(as[i]);
    }
    cout << total << endl;
  }
  return 0;
}
