#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int hist[10001] = {0};
  int summ = 0;
  for (int i = 0; i < n; i++) {
    int t;
    cin >> t;
    summ += t;
    hist[t]++;
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    if (b != c) {
      summ -= hist[b] * b;
      summ += hist[b] * c;
      hist[c] += hist[b];
      hist[b] = 0;
    }
    cout << summ << endl;
  }
  return 0;
}
