#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1e5+5;
ll n,q,a[N],mp[N];
ll ans;
int main()
{
	cin>>n;
	for(int i=0;i<n;i++)cin>>a[i],mp[a[i]]+=a[i],ans+=a[i];
	cin>>q;
	while(q--)
	{
		int x,y;
		cin>>x>>y;
		mp[y]+=mp[x]/x*y;
		ans-=mp[x];
		ans+=mp[x]/x*y;
		mp[x]=0;
		cout<<ans<<"\n";
	}
	return 0;
}