#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> a(n);
  long long sum = 0;
  map<int, int> m;
  for (int &i : a) {
    cin >> i;
    sum += i;
    ++m[i];
  }
  int q;
  cin >> q;
  int b, c;
  while (q--) {
    cin >> b >> c;
    sum += (m[b] * (c - b));
    m[c] += m[b];
    cout << sum << '\n';
  }
}
