Problem:
Score : 400 points

Problem Statement

You have a sequence A composed of N positive integers: A_{1}, A_{2}, \cdots, A_{N}.

You will now successively do the following Q operations:

In the i-th operation, you replace every element whose value is B_{i} with C_{i}.

For each i (1 \leq i \leq Q), find S_{i}: the sum of all elements in A just after the i-th operation.

Constraints

All values in input are integers.

1 \leq N, Q, A_{i}, B_{i}, C_{i} \leq 10^{5}

B_{i} \neq C_{i}

Input

Input is given from Standard Input in the following format:

N
A_{1} A_{2} \cdots A_{N}
Q
B_{1} C_{1}
B_{2} C_{2}
\vdots
B_{Q} C_{Q}

Output

Print Q integers S_{i} to Standard Output in the following format:

S_{1}
S_{2}
\vdots
S_{Q}

Note that S_{i} may not fit into a 32-bit integer.

Sample Input 1

4
1 2 3 4
3
1 2
3 4
2 4

Sample Output 1

11
12
16

Initially, the sequence A is 1,2,3,4.

After each operation, it becomes the following:

2, 2, 3, 4

2, 2, 4, 4

4, 4, 4, 4

Sample Input 2

4
1 1 1 1
3
1 2
2 1
3 5

Sample Output 2

8
4
4

Note that the sequence A may not contain an element whose value is B_{i}.

Sample Input 3

2
1 2
3
1 100
2 100
100 1000

Sample Output 3

102
200
2000

Solution in C++ (GCC 9.2.1) (judge verdict: Accepted)


// Problem : D - Replacing
// Contest : AtCoder - AtCoder Beginner Contest 171
// URL : https://atcoder.jp/contests/abc171/tasks/abc171_d
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

/*when the going gets tough the tough gets going  */
/*we are what we repeatedly do , Excellence then is not an act but a habit  - aristotle */
/*The best way to predict the future is to create it - abraham lincoln*/
/*First think , second believe , third dream and finally dare - walt disney*/
/*It is during our darkest moments that we must focus to see the light */
/*Nothing is impossible the word itself says "i'm fucking possible man just try me again and again till i seem impossible "*/
/*Dont judge each day by the harvest you reap but by the seeds you plant */
/*Do not dwell in the past, do not dream of the future, concentrate the mind on the present moment*/
/*Success is not final failure is not fatal : it is the courage to continue that counts */ 
/*the future belongs to those who believe in the beauty of their dreams */
/*The pessimist complains about the wind , the optimist waits for it to change; the realist adjusts the sails  */
/*Believe you can and you are half way there */
/*Everything has beauty but not everyone sees it */
/*Life is not about finding yourself , life is about creating yourself*/
/*You are the average of the 5 people you spend the most time with .........*/
/*Everything is hard before it is easy see for example now the list of examples is very long hahahahahaha...... ......*/
/*I believe in miracles .......*/
#include<iostream>
#include<string.h>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<bitset>
#include<set>
// #include<multiset>
#include<climits>
#include<math.h>
#include<stdlib.h>
#define ll long long 
#define pb push_back
#define mp make_pair
#define pii pair<int , int > 
#define pll pair<ll , ll >
#define ld long double
#define vll vector<ll>
#define vpii vector<pii>
#define vi vector<int>
#define vpll vector<pll>
#define vld vector<ld>
#define vvld vector<vld>
#define vvi vector<vi>
#define vvll vector<vll>
#define vvpll vector<vpll>
#define vvii vector<vpii>
#define in insert
#define wht while (t--)
#define fu( i , a  , n ) for ( int i = (int)a ; i <=(int)n ; i++  )
#define fd( i , n , a ) for (int i = (int)n ; i >=(int)a ;i--)
#define rep(i,n) for ( int i = 0 ; i < (int)n;  i++)
#define fst  ios_base::sync_with_stdio(false);cin.tie(NULL);
#define sz(a) int((a).size())
#define ln length()
#define ps printf(" ") 
#define pn printf("\n")
#define pi(n) printf("%d" , n ) 
#define pl(n) printf("%lld" , n )
#define ff first
#define ss second
#define ii(n) scanf("%d" , &n)
#define il(n) scanf("%lld" , &n )
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define all(V) V.begin(), V.end()
#define ic(n) cin>>n
#define pr printf
#define inf 99999999
#define en endl
using namespace std ;
string str ;
const int tt = 0;
 void solve()
 {
    ll int   m , n ,a , b ,c ;
 	cin>> n ;
 	map<ll, ll > mp;
 	ll sum = 0;
 	rep(i, n) 
 	{
 		cin>> a;
 		mp[a]++;
 		sum += a;
 	}
 	cin>> c;
 	while (c--)
 	{
 	     cin>> a >> b ;
 	     ll rem = mp[a] ;
 	     rem *= a;
 	     ll add = mp[a] * b;
 	     mp[b] += mp[a];
 	     mp[a] = 0;
 	     sum += add;
 	     sum -= rem;	
 	     cout << sum << en;;
 	}
 	
 }
int main()
{
	fst;
	
    if (tt)
    {
    	int t;
    	cin>> t ;
    	while (t--)
    	{
    		solve();
    	}
    }
    else
    {
    	solve();
    }
	return 0 ;
	
 
}