#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, s = 0, q, a;
  cin >> n;
  map<long long, long long> ml;
  for (long long i = 0; i < n; i++) {
    cin >> a;
    ml[a]++;
    s += a;
  }
  cin >> q;
  while (q--) {
    long long b, c;
    cin >> b >> c;
    long long diff = c - b;
    s += (c - b) * ml[b];
    ml[c] += ml[b];
    cout << s << '\n';
  }
  return 0;
}
