#include <bits/stdc++.h>
using namespace std;
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout.tie(nullptr);
  ;
  long long n;
  cin >> n;
  vector<int> v(n);
  long long sum = 0;
  map<long long, long long> m;
  for (long long i = 0; i < n; i += 1) {
    cin >> v[i];
    sum += v[i];
    m[v[i]]++;
  }
  long long q;
  cin >> q;
  for (long long i = 0; i < q; i += 1) {
    long long old, ne;
    cin >> old >> ne;
    replace(v.begin(), v.end(), old, ne);
    sum -= m[old] * old;
    sum += ne * m[old];
    cout << sum << endl;
    m[ne] += m[old];
    m.erase(old);
  }
  return 0;
}
