#include <bits/stdc++.h>
using namespace std;
int main() {
  int sum = 0, q, n, i, l, r;
  cin >> n;
  int p;
  vector<int> b(1000000, 0);
  for (i = 1; i <= n; i++) {
    cin >> p;
    sum = sum + p;
    b[p]++;
  }
  cin >> q;
  while (q--) {
    cin >> l >> r;
    sum = sum + (b[l] * (r - l));
    b[r] = b[l] + b[r];
    b[l] = 0;
    cout << sum << endl;
  }
}
