#include <bits/stdc++.h>

using namespace std;

int arr[100005];

int main() {
	int N, Q;
	cin >> N;

	long long sum = 0;

	for (int i = 0; i < N; i++) {
		int p;
		cin >> p;
		arr[p]++;

		sum += p;
	}

	cin >> Q;

	for (int i = 0; i < Q; i++) {
		int u, v;
		cin >> u >> v;

		sum += (v - u) * arr[u];
		arr[v] += arr[u];
		arr[u] = 0;

		cout << sum << endl;
	}
}
