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