#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n, v, q, b, c, s = 0;
  map<long long, long long> m;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> v;
    m[v]++;
    s += v;
  }
  cin >> q;
  while (q--) {
    cin >> b >> c;
    s += m[b] * (c - b);
    m[c] += m[b];
    m[b] = 0;
    cout << s << "\n";
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  solve();
  return 0;
}
