#include<bits/stdc++.h>
using namespace std;

int main(){
	int n,q;
	unordered_map<int,int> count;
	cin>>n;
	long long int sum = 0;
	for(int i=0;i<n;i++){
		int d;
		cin>>d;
		count[d]++;
		sum+=d;
	} 
	cin>>q;
	for(int i=0;i<q;i++){
		int b,c;
		cin>>b>>c;
		sum+= (count[b]*(c-b));
		count[c]+=count[b];
		count[b]=0;
		cout<<sum<<"\n";
	}
}