#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vs = vector<string>;
using vi = vector<int>;
using vl = vector<ll>;
using vb = vector<bool>;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using vpi = vector<pair<int, int>>;
using vpl = vector<pair<ll, ll>>;
using ld = double;
const int MAX = 1e5;
int main() {
  int n;
  cin >> n;
  vi a(n);
  map<int, int> cnt;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    cnt[a[i]] += 1;
  }
  ll sum = accumulate(a.begin(), a.end(), 0);
  int Q;
  cin >> Q;
  while (Q--) {
    ll b, c;
    cin >> b >> c;
    if (b == c) {
      cout << sum << endl;
      continue;
    }
    sum -= cnt[b] * b;
    sum -= cnt[c] * c;
    cnt[c] += cnt[b];
    cnt[b] = 0;
    sum += cnt[c] * c;
    cout << sum << endl;
  }
  return 0;
}
