#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
	ll n;
	cin>>n;
	ll a[n],sum=0;
	unordered_map<ll,ll> mp;
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
		mp[a[i]]++;
		sum+=a[i];
	}
	ll q;
	cin>>q;
	while(q--)
	{
		ll b,c;
		cin>>b>>c;
		sum+=mp[b]*(c-b);
		mp[c]+=mp[b];
		mp[b]=0;
		cout<<sum<<endl;
	}
	return 0;
	
}