#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define pi pair<ll,ll>
#define pii pair<ll,pi>
#define pb push_back
#define mk make_pair
int main() {
// #ifndef ONLINE_JUDGE
// 	freopen("input.txt", "r", stdin);
// 	freopen("output.txt", "w", stdout);
// #endif
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	ll n;
	cin >> n;
	ll sum = 0;
	unordered_map<ll,ll>m;
	for (int i = 0; i < n; i++) {
		ll x;
		cin >> x;
		m[x]++;
		sum += x;
	}
	ll q;
	cin >> q;
	while (q--) {
		ll b, c;
		cic >> b >> c;
		ll no = m[b];
		sum = sum + (no * c) - no * b;
		m[c] += no;
		m[b] = 0;
		cout << sum << endl;
	}
}
