#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=1e5+5;
signed main(){
	int n;cin>>n;int sb[N];int sum=0;map<int,int>kk;
	for(int i=1;i<=n;i++){cin>>sb[i];sum+=sb[i];kk[sb[i]]++;
	} int t;cin>>t;
	while(t--){
		int a,b;cin>>a>>b;kk[b]+=kk[a];
		sum+=(b-a)*kk[a];kk[a]=0;
		cout<<sum<<"\n";
	}
}