#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
  int n;
  cin >> n;
  int m;
  map<int, int> mp;
  for (int i = 0; i < (n); ++i) {
    cin >> m;
    mp[m]++;
  }
  int q;
  cin >> q;
  vector<ll> a(q);
  for (int i = 0; i < (q); ++i) {
    int b, c = 0;
    ll ans = 0;
    cin >> b >> c;
    mp[c] += mp[b];
    mp.erase(b);
    auto begin = mp.begin(), end = mp.end();
    for (auto iter = begin; iter != end; iter++) {
      ans += (iter->first) * (iter->second);
    }
    a[i] = ans;
  }
  for (int i = 0; i < (q); ++i) cout << a[i] << endl;
}
