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


}
