#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

ll N;
map<ll, ll> S;
ll Q;
ll R;

int main() {
  cin >> N;
  for (ll i = 0; i < N; ++i) {
    ll a;
    cin >> a;
    ++S[a];
    R += a;
  }
  cin >> Q;
  for (ll q = 0; q < Q; ++q) {
    ll b, c;
    cin >> b >> c;
    R += (c - b) * S[b];
    S[c] += S[b];
    S[b] = 0;
    cout << R << endl;
  }
}
