#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#define FASTIO ios_base::sync_with_stdio(false); cin.tie(NULL);
#define rep(i, a, b) for(int (i) = (a); (i) < (b); (i)++)
#define rrep(i, b, a) for(int (i) = (b); (i) > (a); (i)--)
using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ii, pair<int, int>> iii;


int arr[100'005];
map<ll, int> mp;
ll ans = 0;
int main() {
	FASTIO;

	int n; cin >> n;
	rep(i, 0, n) {
		cin >> arr[i];
		mp[arr[i]]++;
		ans += arr[i];
	}
	int q; cin >> q;
	rep(i, 0, q) {
		int b, c; cin >> b >> c;
		ans -= mp[b] * b;
		ans += mp[b] * c;
		mp[c] += mp[b];
		mp[b] = 0;
		cout << ans << endl;
	}

	return 0;
}


