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