#include <bits/stdc++.h>
using ll = long long;
using namespace std;
int main() {
  int n = -1;
  cin >> n;
  map<int, ll> valcnt;
  for (int i = 0; i < n; ++i) {
    int inp = -1;
    cin >> inp;
    valcnt[inp]++;
  }
  int q = -1;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int from_val, to_val;
    cin >> from_val >> to_val;
    valcnt[to_val] += valcnt[from_val];
    valcnt[from_val] = 0;
    ll ans = 0;
    for (auto x : valcnt) {
      ans += x.first * x.second;
    }
    cout << ans << endl;
  }
  return 0;
}
