#include<cstdio>
#define ll long long
#define N 100005

int n,q;

int b[N];

ll ans;

int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		int a;
		scanf("%d",&a);
		b[a]++;
		ans+=a;
	}
	scanf("%d",&q);
	while(q--){
		int x,y;
		scanf("%d%d",&x,&y);
		ans+=1ll*b[x]*(y-x);
		b[y]+=b[x];
		b[x]=0;
		printf("%lld\n",ans);
	}
}