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