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)

#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
using namespace __gnu_pbds; 
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> 
#define FAST std::ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pi       acos(-1)
#define mp make_pair
#define pb push_back
#define ALL(v) v.begin(), v.end()
#define SORT(v) sort(ALL(v))  
#define REVERSE(v) reverse(ALL(v))
#define F first
#define S second
#define ppb pop_back
#define GCD(m,n) __gcd(m,n)
#define LCM(m,n) (m*n)/GCD(m,n)
#define rep(i,a,n)  for(int i=a ; i<n ; i++)
#define repe(i,a,n) for(int i=a;i<=n;i++)
#define rev(i,a,b) for(int i=a;i>=b;i--)

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<vvvi> vvvvi;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<vvc> vvvc;
typedef vector<pii > vpii;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef map<int,int> mii;
typedef unordered_map<int,int> umii;
typedef map<char,int> mci;

#define itv for(auto it= v.begin(); it != v.end(); ++it )
#define present(container, element) (container.find(element) != container.end())
#define cpresent(container, element) (find(ALL(container),element) != container.end())

#define trav(a, x) for(auto& a : x)
#define display(x) trav(a,x) cout<<a<<" ";cout<<endl
#define displaycerr(x) trav(a,x) cerr<<a<<" ";cerr<<endl
 
#define bs binary_search
#define  lb(v,val)  lower_bound(ALL(v), val)
#define  ub(v,val)  upper_bound(ALL(v), val)
#define Max(x,y,z) max(x,max(y,z))
#define Min(x,y,z) min(x,min(y,z))
#define maxc(v) *max_element(ALL(v))
#define minc(v) *min_element(ALL(v))
 /*
#define dbg1(a) cout<<" *"<<a<<" ";
#define dbg2(a,b) cout<<" *"<<a<<" **"<<b<<" "     /////
#define dbg cout<<"move"
*/

#define vin(v,n);   vi v(n); rep(i,0,n)  cin>>v[i];
#define dbg cerr <<"At line "<<__LINE__<<" move "<<nl
#define dbg1(x) cerr <<"At line "<<__LINE__<<" "<<#x<<"="<<x<<nl
#define dbg2(x,y) cerr <<"At line "<<__LINE__<<" "<<#x<<"="<<x<<" "<<#y<<"="<<y<<nl
#define dbg3(x,y,z) cerr <<"At line "<<__LINE__<<" "<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<nl
#define prv(v);     rep(i,0,sz(v))  cerr<<v[i]<<" ";
#define nl "\n"
#define sz(s)         (int)((s).size())
#define coutsp(k)   cout<<setprecision(k)  //set precision (total length k icluding decimal and non decimal)
#define coutfsp(k)   cout<<fixed<<setprecision(k)  //fixed set precision(after decimal fixing)

#define isvowel(a) (a=='a'||a=='e'||a=='i'||a=='o'||a=='u')

#define  INF   (int)1e18 
#define int long long ////////////
#define MOD 1000000007    ////////
#define POD 998244353 ////////
/////define ll long long
#define ld long double 
//-------------------------------------------------------------------------

//------------------------ Global VARIABLE ------------------------------------

//------------------------ Global VARIABLE ------------------------------------


//-------------------------------------------------------------------------------
void dquit()
{
  int n;  cin>>n;  

  vin(v,n);
  mii M;

  int sum=0;

  rep(i,0,n){
    M[v[i]]++;
    sum+=v[i];
  }

  int q;  cin>>q;

  rep(i,0,q)
  {
  
   int b,c; cin>>b>>c;

   if(present(M,b))
   {
    sum-=M[b]*b;
    sum+=M[b]*c;
    M[c]+=M[b];
    M.erase(b);
   }

   cout<<sum<<nl;


  }

}
//-------------------------------------------------------------------------------

 
signed main()
{

//--------------------------------
     FAST    // MAKE IT FAST   ---
//--------------------------------

/////////////////////////////////////////////////////////////////////////////////////////// 
// cout<<fixed<<setprecision(20);
/////////////////////////////////////////////////////////////////////////////////////////// 
   
   int t;  t=1;    
   int co=1;
///////---------------------------------|||||
               // cin>>t;        ////////>>>>>>>
//////----------------------------------|||||

    while(t--)
    {
       // cout<<"Case #"<<co<<": ";

      dquit();
      cout<<nl;   //  IMPORTANT FOR NEXT LINE     
      co++;  
    }


 // #ifndef ONLINE_JUDGE
 //  cerr<<(1000*clock())/CLOCKS_PER_SEC<<"ms";
 // #endif      

   return 0;
}