#include <bits/stdc++.h>
using namespace std;
const long long INF = 1LL << 60;
signed main() {
  long long n;
  cin >> n;
  vector<long long> a(n), v(1e6, 0);
  long long sum = 0;
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    v[a[i]]++;
    sum += a[i];
  }
  long long q;
  cin >> q;
  for (long long i = 0; i < q; i++) {
    long long b, c;
    cin >> b >> c;
    if (v[b] == 0) {
      cout << sum << endl;
    } else {
      sum += v[b] * (c - b);
      v[c] += v[b];
      cout << sum << endl;
    }
  }
  return 0;
}
