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