#include <iostream>
#include <map>

using namespace std;

int main() {
	int n; cin >> n;
	long long s = 0;
	map<int, int> mp;
	for (int i = 0; i < n; ++i) {
		int a; cin >> a;
		s += a;
		++mp[a];
	}
	int q; cin >> q;
	while (q--) {
		int b, c; cin >> b >> c;
		long long t = mp[b];
		mp[b] = 0;
		mp[c] += t;
		s += t * (c - b);
		cout << s << endl;
	}

	return 0;
}