#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout.precision(30);
  ;
  int n, x, q, b, c, s = 0;
  cin >> n;
  map<int, int> m;
  while (n--) {
    cin >> x;
    s += x;
    m[x]++;
  }
  cin >> q;
  while (q--) {
    cin >> b >> c;
    s = s + (c - b) * m[b];
    m[c] += m[b];
    m[b] = 0;
    cout << s << '\n';
  }
  return 0;
}
