#include <algorithm>
#include <iostream>

using namespace std;

const int A = 100000;

int kk[A + 1];

int main() {
	int n; cin >> n;
	long long x = 0;
	while (n--) {
		int a; cin >> a;
		x += a;
		kk[a]++;
	}
	int q; cin >> q;
	while (q--) {
		int b, c; cin >> b >> c;
		cout << (x += (long long) (c - b) * kk[b]) << '\n';
		kk[c] += kk[b];
		kk[b] = 0;
	}
	return 0;
}
