#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll ans=0;
map<int,int>mp;
int n,q;
int main(){
	cin>>n;
	while(n--){
		int x;cin>>x;mp[x]++,ans+=x;
	}
	cin>>q;
	while(q--){
		int a,b;cin>>a>>b;
		ans+=1ll*(b-a)*mp[a];
		mp[b]+=mp[a],mp[a]=0;
		cout<<ans<<"\n";
	}
}