#include <bits/stdc++.h>
using namespace std;
using ll=long long;
int main(){
  ll N,ans=0; cin>>N;
  vector<ll> A(100010,0);
  for(ll i=0; i<N; i++){
    ll a; cin>>a;
    A[a]++;
    ans+=a;
  }
  ll Q; cin>>Q;
  for(ll i=0; i<Q; i++){
    ll b,c; cin>>b>>c;
    ans-=A[b]*b;
    ans+=A[b]*c;
    A[c]+=A[b];
    A[b]=0;
    cout<<ans<<endl;
  }
}