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