#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
int main(){
  ll n;cin>>n;
  ll a,su[100001],ans=0;fill(su,su+100001,0);
  for(ll i=0;i<n;i++){
    cin>>a;
    ans+=a;
    su[a]++;
  }
  ll q;cin>>q;
  ll b,c;
  for(ll i=0;i<q;i++){
    cin>>b>>c;
    su[c]+=su[b];
    ans+=su[b]*(c-b);
    su[b]=0;
    cout << ans<<endl;
    
  }
}
  