#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  map<int, int> m;
  int ans;
  ans = 0;
  int p;
  for (int i = 0; i < n; i++) {
    cin >> p;
    ans += p;
    m[p]++;
  }
  int q;
  int b, c;
  cin >> q;
  while (q--) {
    cin >> b >> c;
    ans -= m[b] * b;
    ans += m[b] * c;
    m[c] += m[b];
    m[b] = 0;
    cout << ans << endl;
  }
}
