#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9;
const ll mod = 1e9 + 7;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

int main() {
  ll n;
  cin >> n;
  map<ll, ll> mp;
  rep(i,n) {
    ll a;
    cin >> a;
    mp[a]++;
  }

  ll q;
  cin >> q;

  vector<vector<ll>> array(q);


  rep(i,q) {
    ll a, b;
    cin >> a >> b;
    mp[b] += mp[a];
    mp[a] = 0;
    rep(j,10001) {
      array[i][j] = mp[j];
    }
  }

  rep(i,q) {
    ll sum = 0
    rep(j,10001) {
      sum += array[i][j] * j;
    }
    cout << sum << endl;
  }

  return 0;
}

