#include <bits/stdc++.h>
#difine long long int
using namespace std;
const int N = 1e5 + 10;
int ton[N], n, q, sum;
signed main(){
	cin >> n;
	for (int i = 1, x; i <= n; i++) {
		cin >> x;
		++ton[x];
		sum = x; 
	}
	cin >> q;
	while(q--) {
		int b, c;
		cin >> b >> c;
		sum -= ton[b] * b;
		sum += ton[b] * c;
		ton[c] += ton[b];
		ton[b] = 0;
		cout << sum << endl;
	}
	return 0;
}