#include <bits/stdc++.h>
using ll = long long;
using namespace std;
int main() {
	int n; cin >> n;
	ll sum = 0;
	map<int, int> mp;
	for (int i = 0; i < n; i++) {
		int x; cin >> x;
		mp[x]++;
		sum += x;
	}
	int q; cin >> q;
	for (int i = 0; i < q; i++) {
		int b, c; cin >> b >> c;
		sum += 1LL * (c - b) * mp[b];
		mp[c] += mp[b];
		mp[b] = 0;
		cout << sum << "\n";
	}
}