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