#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  map<int, int> a;
  int in1;
  long long res = 0;
  for (int i = 0; i < n; i++) {
    cin >> in1;
    a[in1]++;
    res += in1;
  }
  int q;
  cin >> q;
  int in2, in3;
  for (int i = 0; i < q; i++) {
    cin >> in2 >> in3;
    res = res - a[in2] * in2 + a[in2] * in3;
    a[in3] += a[in2];
    a[in2] = 0;
    cout << res << endl;
  }
  return 0;
}
