#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  int i;
  long long sum = 0;
  cin >> n;
  vector<long long> cnt(n + 1);
  for (i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    cnt[tmp]++;
    sum += tmp;
  }
  int q;
  cin >> q;
  for (i = 0; i < q; i++) {
    long long t, f;
    cin >> t >> f;
    sum -= t * cnt[t];
    sum += f * cnt[t];
    cnt[f] += cnt[t];
    cnt[t] = 0;
    cout << sum << "\n";
  }
}
