#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  map<long, int> cnt;
  long res = 0;
  for (int i = 0; i < n; ++i) {
    int t;
    cin >> t;
    ++cnt[t];
    res += t;
  }
  int q;
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    res -= cnt[b] * b;
    res += cnt[b] * c;
    cnt[c] += cnt[b];
    cnt[b] = 0;
    cout << res << '\n';
  }
}
