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)

/// In the name of ALLAH
/// I'm THEOVE46
#include<bits/stdc++.h>
using namespace std;

#define gap             " "
#define nn              "\n"
#define pi              2*acos(0.0)
#define db              double
#define ft              float
#define ll              long long
#define ull             unsigned long long
#define pf              printf
#define sf              scanf
#define ff              first
#define ss              second
#define sc(n)           sf("%d",&n)
#define sc2(n, m)       sf("%d%d",&n,&m);
#define scl(n)          sf("%lld",&n)
#define scl2(n, m)      sf("%lld%lld",&n,&m);
#define pc(n)           pf("%d",n)
#define scf(f)          sf("%f",&f)
#define pcf(f)          pf("%f",f)
#define bfs             cin.get()
#define bfc             getchar()
#define gl(s)           getline(cin, s);
#define loop(a,n)       for(ll i=a; i<n; i++)
#define rloop(a,n)      for(ll i=a; i>n; i--)
#define tloop(test)     for(ll tl=1; tl<=test; tl++)
#define case1(z)        cout<<"Case "<<z<<": "
#define case2(z)        printf("Case %d: ",z)
#define vi              vector<int>
#define vl              vector<ll>
#define vld(vc,rw)      vector<vector<ll> >vc(rw)
#define pii             pair<int,int>
#define pll             pair<ll,ll>
#define pis             pair<int,string>
#define psi             pair<string,int>
#define mls             map <ll,string>
#define msl             map <string,ll>
#define mll             map <ll,ll>
#define mp              make_pair
#define pb              push_back
#define itr             vector<int> :: iterator
#define sz(s)           s.size()
#define sl(s)           s.length()
#define Sort(s)         sort(s.begin(), s.end());
#define SortR(s)        sort(s.rbegin(), s.rend());
#define all(x)          x.begin(),x.end()
#define rall(x)         x.end(), x.begin()
#define MEM(ar,val)     memset(ar, (val), sizeof(ar))
#define IN(A,B,C)       assert(B<=A && A<=C)
#define EPS             1e-9
#define bug1            "**1**"
#define bug2            "****2****"
#define artri(b,h)      0.5*b*h
#define artrap(b1,b2,h) (b1+b2)/2*h
#define arcrl(r)        pi*r*r
#define vlsyl(r,h)      pi*r*r*h
#define vlcone(r,h)     pi*r*r*(h/3)
#define vlsph(r)        (4/3)*pi*r*r*r
#define flush           fflush(stdout);
#define pcn(dg,val)     fixed << setprecision(dg) << val
#define fap(x)          cout << __LINE__ << " says: " << #x << " = " << x << "\n"
#define FastIO          ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define showsort(n, a)  for(int i=0; i<n; i++) cout << a[i] << gap; cout << endl;

///THEOVE46

int main()
{
    FastIO;
    ll n, s=0, q, i, k, j, x, b, c;
    ll a[100004] = {0};
    cin >> n;
    for(i=0; i<n; i++)
    {
        cin >> x;
        a[x]++;
        s+=x;
    }
    cin >> q;
    while(q--)
    {
        cin >> b >> c;
        s += a[b]*c;
        s -= a[b]*b;
        a[c] += a[b];
        a[b] = 0;

        cout << s << endl;
    }
    return 0;
}

///   check for integer overflow, array bounds
///   check for n=1
///   - - - The Ove 46