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