#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, a, b, c, i, q, ans = 0;
  cin >> n;
  map<int, int> mp;
  for (i = 0; i < n; i++) {
    cin >> a;
    mp[a]++;
    ans += a;
  }
  cin >> q;
  while (q--) {
    cin >> b >> c;
    if (mp[b]) {
      ans += mp[b] * (c - b);
      mp[c] += mp[b];
      mp[b] = 0;
    }
    cout << ans << endl;
  }
}
