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