// D - Replacing
#include <bits/stdc++.h>
using namespace std;

int main(){
	int N; cin>>N;
	map<int, int> C;
	long long s = 0;
	while(N--){
		int a; cin>>a;
		C[a]++;
		s += a;
	}
	int Q; cin>>Q;
	while(Q--){
		int b, c; cin>>b>>c;
		s += (c - b) * C[b];
		C[c] += C[b];
		C[b] = 0;
		cout<< s << endl;
	}
}
