#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll gcd(ll a, ll b) {
  if (a % b == 0)
    return (b);
  else
    return (gcd(b, a % b));
}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
int main() {
  ll n;
  ll b;
  map<ll, ll> a;
  cin >> n;
  for (ll i = 0; i < n; i++) {
    cin >> b;
    a[b]++;
  }
  ll q;
  cin >> q;
  pair<ll, ll> c;
  map<ll, ll> ans;
  for (ll i = 0; i < q; i++) {
    cin >> c.first >> c.second;
    decltype(a)::iterator it = a.find(c.first);
    if (it != a.end()) {
      ll f = a[c.first];
      a[c.first] = 0;
      a[c.second] += f;
    }
    ans[i] = 0;
    for (auto j = a.begin(); j != a.end(); j++) {
      ans[i] += j->first * j->second;
    }
  }
  for (ll i = 0; i < q; i++) {
    cout << ans[i] << endl;
  }
}
