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

int main(){
	ll sum=0;
	int n;cin>>n;
	map<ll,ll>nums;
	for(int i=0;i<n;i++){
		ll a;cin>>a;
		nums[a]++;
		sum+=a;
	}
	int q;cin>>q;
	while(q--){
		ll b,c;
		cin>>b>>c;
		ll qtd=nums[b];nums[b]=0;
		nums[c]+=qtd;
		sum-=qtd*b;
		sum+=qtd*c;
		cout<<sum<<'\n';
	}


	return 0;
}