#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
int main() {
  int N;
  cin >> N;
  map<int, int> m;
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    m[a]++;
  }
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int b, c;
    cin >> b >> c;
    m[c] += m[b];
    m[b] = 0;
    ll ans = 0;
    for (auto i = m.begin(); i != m.end(); ++i) {
      ans += i->first * i->second;
    }
    cout << ans << endl;
  }
  return 0;
}
