#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll mxn=2e5+5;

int main() {
	ll n;
	cin>>n;
	ll a[n],i;
	unordered_map<ll,ll> mp;
	ll s=0;
	for(i=0;i<n;i++) {
		cin>>a[i];
		mp[a[i]]++;
		s+=a[i];
	}
	ll q;
	cin>>q;
	while(q--) {
		ll b,c;
		cin>>b>>c;
		s+= (c*mp[b]);
		s-= (b*mp[b]);
		mp[c]+=mp[b];
		mp[b]=0;
		cout<<s<<"\n";
	}
	return 0;
}