#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
ll gcd(ll a, ll b) {
  if (b > a) swap(a, b);
  if (b == 0) return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a / g * b;
}
ll rep_jijo(ll n, ll x) {
  if (x == 0) return 1;
  if (x % 2 == 0) {
    ll t = rep_jijo(n, x / 2);
    return t * t % 1000000007;
  }
  return n * rep_jijo(n, x - 1) % 1000000007;
}
ll c2(ll n) { return n * (n - 1) / 2; }
int main() {
  ll n, q;
  cin >> n;
  map<ll, ll> m;
  for (ll i = 0; i < (ll)(n); i++) {
    ll k;
    cin >> k;
    m[k]++;
  }
  cin >> q;
  for (ll i = 0; i < (ll)(q); i++) {
    ll b, c;
    cin >> b >> c;
    if (m[b] > 0) {
      m[c] += m[b];
      m[b] = 0;
    }
    ll ans = 0;
    for (auto p : m) {
      auto k = p.first;
      auto v = p.second;
      ans += k * v;
    }
    cout << ans << endl;
  }
}
