#include <bits/stdc++.h>
using namespace std;
long long binpow(long long a, long long b) {
  long long res = 1;
  while (b > 0) {
    if (b & 1) res = (res * a);
    a = (a * a);
    b >>= 1;
  }
  return res;
}
void solve() {
  int n, sum = 0;
  cin >> n;
  unordered_map<int, int> make_pair;
  int a;
  for (int i = 0; i < n; i++) {
    cin >> a;
    make_pair[a]++;
    sum += a;
  }
  int q;
  cin >> q;
  while (q--) {
    int l, r;
    cin >> l >> r;
    if (make_pair[l] > 0) {
      make_pair[r] += make_pair[l];
      sum += (r - l) * make_pair[l];
      make_pair[l] = 0;
    }
    cout << sum << "\n";
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
