#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1000000007
const int maxn=1e5+5;
ll n,q,tmp,a,b,sum=0;
map<int,int>m;
int main(){
	cin>>n;
	while(n--){
		cin>>tmp;
		sum+=tmp;
		m[tmp]++;
	}
	cin>>q;
	while(q--){
		cin>>a>>b;
		sum-=m[a]*(a-b);
		m[b]+=m[a];
		m[a]=0;
		cout<<sum<<endl;
	}
	return 0;
}