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