#pragma GCC optimize("-Ofast","-funroll-all-loops")
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+10;
int n,cnt[N],q,a[N],sum;
signed main(){
	cin>>n;
	for(int i=1;i<=n;i++)	cin>>a[i],sum+=a[i],cnt[a[i]]++;
	cin>>q;
	for(int i=1,x,y;i<=q;i++){
		cin>>x>>y;	sum+=(y-x)*cnt[x]; cnt[y]+=cnt[x]; cnt[x]=0;
		cout<<sum<<'\n';
	}
	return 0;
}
