#include <bits/stdc++.h>
using namespace std;
long long fun_sum(int &i, vector<long long> &num) {}
int main() {
  long long N, Q;
  cin >> N;
  vector<long long> A(N);
  for (int i; i < N; i++) cin >> A.at(i);
  cin >> Q;
  vector<long long> B(Q), C(Q);
  for (int i; i < Q; i++) cin >> B.at(i) >> C.at(i);
  vector<long long> num(100001, 0);
  for (int x : A) num.at(x)++;
  for (int i = 0; i < Q; i++) {
    num.at(C.at(i)) += num.at(B.at(i));
    num.at(B.at(i)) = 0;
    long long sum = 0;
    for (long long i; i < 100001; i++) {
      sum += num.at(i) * (i + 1);
    }
    cout << num.at(1) << num.at(2) << num.at(3) << num.at(4) << endl
         << sum << endl;
  }
}
