#include <bits/stdc++.h>
using namespace std;
bool SortbySecDesc(const pair<long long, long long> &a,
                   const pair<long long, long long> &b) {
  return a.second > b.second;
}
bool sortinrev(const pair<int, int> &a, const pair<int, int> &b) {
  return (a.first > b.first);
}
long long cnt[1000006];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(nullptr);
  ;
  long long n;
  cin >> n;
  long long a[n + 1];
  long long sum = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    sum += a[i];
    cnt[a[i]]++;
  }
  sort(a + 1, a + n + 1);
  long long maxi = a[n];
  cout << maxi << endl;
  sort(cnt, cnt + maxi);
  int q;
  cin >> q;
  while (q--) {
    long long b, c;
    cin >> b >> c;
    maxi = max(maxi, c);
    cnt[c] = cnt[b] + cnt[c];
    cnt[b] = 0;
    long long sum1 = 0;
    for (int i = maxi; i >= 1; i--) {
      if (cnt[i] > 0) {
        sum1 += (cnt[i] * i);
      }
    }
    cout << sum1 << endl;
  }
  return 0;
}
