#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int a[n];
  int b[100001] = {0};
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    b[a[i]]++;
  }
  int m;
  cin >> m;
  while (m--) {
    int p, q;
    cin >> p >> q;
    b[q] += b[p];
    b[p] = 0;
    int sum = 0;
    for (int i = 0; i < 100001; i++) {
      if (b[i] > 0) {
        sum += b[i] * i;
      }
    }
    cout << sum << "\n";
  }
  return 0;
}
