#include <bits/stdc++.h>
using namespace std;
using ll=long long;
int main() {
  ll n; cin >> n;
  ll a;
  map<ll, ll> m;
  ll sum=0;
  for(ll i=0; i<n; i++){
    cin >> a;
    m[a]++;
    sum+=a;
  }
  ll q; cin >> q;
  for(ll i=0; i<q; i++){
    ll b, c; cin >> b >> c;
    sum-=m[b]*b;
    sum+=m[b]*c;
    m[c]+=m[b];
    m[b]=0;
    cout << sum << endl;
  }
  return 0;
}