#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll freq[100001];
int main(){

	ll sum=0,temp;
	int n; cin>>n;
	for(int i=0;i<n;i++){
		cin>>temp;
		freq[temp]++;
		sum+=temp;
	}


	int q; cin>>q;
	while(q--){

		ll a,b;
		cin>>a>>b;
		sum=sum+(b-a)*freq[a];
		freq[b]+=freq[a];
		freq[a]=0;
		cout<<sum<<endl;
	}
}
