//#include <fsociety>
#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <iomanip> 
#include <stdio.h>
//end of libraries ;
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
// ll lmax(ll o , ll w) {if(o>w)return o; return w;}
// ll lmin(ll o , ll w) {if(o<w)return o; return w;}
#define LNF 3999999999999999999
#define INF 999999999
#define N 9000003
#define PI 3.14159265359
#define F first
#define S second
#define pb push_back
#define ll long long
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
ll n , x , sum = 0;
int main(){
fcin;
	cin >> n;
	for(ll i = 1 ; i <= n ; i++){
		cin >> x;
		d[x]++;
		sum += x;
	}
	ll q;
	cin >> q;
	while(q--){
		ll x , y;
		cin >> x >> y;
		sum += (d[x]*(y-x));
		d[y] += d[x];
		d[x] = 0;
		cout << sum << '\n';
		}


}