#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ll n;
  cin >> n;
  map<ll, ll> m;
  ll su = 0;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    m[a]++;
    su += a;
  }
  ll q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    su = su - b * m[b] + c * m[b];
    cout << su << endl;
    m[c] += m[b];
  }
}
