#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
#define ll long long 
int main()
{
	ll n;
	cin>>n;
	map<ll,ll> m;
	ll x;
	ll sum=0;
	for(ll i=0;i<n;i++)
	{
		cin>>x;
		sum+=x;
		m[x]++;
	}
	ll q;
	cin>>q;
	ll b,c;
	while(q--)
	{
		cin>>b>>c;
		ll temp=m[b];
		sum-=(b*temp);
		sum+=(c*temp);
		m[c]+=temp;
		m[b]=0;
		cout<<sum<<endl;
	}
	return 0;
}