#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n;
  cin >> n;
  long long cnt[500001];
  for (int i = 0; i < 500000; i++) cnt[i] = 0;
  long long s = 0LL;
  for (long long i = 0, x; i < n; i++) {
    cin >> x;
    s += x;
    cnt[x]++;
  }
  int q;
  cin >> q;
  while (q--) {
    long long a, b;
    cin >> a >> b;
    s -= a * cnt[a];
    s += b * cnt[b];
    cnt[b] += cnt[a];
    cnt[a] = 0;
    cout << s << endl;
  }
  return 0;
}
