#include<bits/stdc++.h>

using namespace std;
typedef long long ll;

int main(){
	int n, tmp;
	ll sum = 0;
	cin >> n;
	map<int, int> a;
	for(int i = 0; i<n ;i++){
		cin >> tmp;
		sum+=tmp;
		a[tmp]++;
	} 
	int k, b, c;
	cin >> k;
	while(k){
		cin >> b >> c;
		sum += 1ll * (c - b)* a[b];
		cout << sum << endl;
		
		a[c] += a[b];
		a[b] = 0;
		k--;
	}
	return 0;
}
