#include <bits/stdc++.h>
using namespace std;
#define int long long
#define mod 1000000007

signed main() 
{
	int n , q , temp , x , y , sum = 0;
	map<int,int> m;
	cin >> n;
	for(int i = 0 ; i < n ; i++)
		cin >> temp , m[temp]++ , sum += temp;
	cin >> q;
	while(q--)
	{
		cin >> x >> y;
		sum += (y-x)*m[x];
		m[y]+=m[x];
		m[x]=0;
		cout << sum << endl;
	}
}