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