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