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: Time Limit Exceeded)

#include <bits/stdc++.h>
#include <boost/range/irange.hpp>
#define RANGE_1(e) (boost::irange(0,(e)))
#define RANGE_2(s,e) (boost::irange((s),(e)))
#define RANGE_3(s,e,step) (boost::irange((s),(e),(step)))
#define RANGE_SELECTER(_1,_2,_3,SELECT,...) SELECT
#define range(...) RANGE_SELECTER(__VA_ARGS__, RANGE_3, RANGE_2, RANGE_1) (__VA_ARGS__)
#define guard(x) if(x);
#define CINS(x) for(auto&& __E__:(x))cin>>__E__
#define ALL(x) (x).begin(),(x).end()
#define RALL(x) (x).rbegin(),(x).rend()
#define CALL(x) (x).cbegin(),(x).cend()
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
#define BIT(n) (1LL<<(n))
#define MOD 1000000007

using namespace std;
using lint = long long;

// template<class T>class irange{private:struct I{T x;T operator*(){return x;}bool operator!=(I&lhs){return x<lhs.x;}void operator++(){++x;}};I i,n;public:irange(T i,T n):i({i}),n({n}){}I&begin(){return i;}I&end(){return n;}};
// template<class T>class srange{private:struct I{T x,s;T operator*(){return x;}bool operator!=(I&lhs){if(s<(T)0)return x>lhs.x;return x<lhs.x;}void operator++(){x=x+s;}};I i,n;public:srange(T i,T n,T s):i({i,s}),n({n,s}){}I&begin(){return i;}I&end(){return n;}};
// template<class T>irange<T>range(T n){return irange(0,n);}template<class T>irange<T>range(T i,T n){return irange(i,n);}template<class T>srange<T>range(T i,T n,T s){return srange(i,n,s);}
template<class T>bool amax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T>bool amin(T &a,const T &b){if(a>b){a=b;return true;}return false;}

struct initialize{initialize(){cin.tie(nullptr);ios::sync_with_stdio(false);};}__ini__;

int main(){
    int n;
    cin >> n;
    vector<int> a(n);
    CINS(a);

    int q;
    cin >> q;

    int b, c;

    int e;

    for (int i : range(q)) {
        cin >> b >> c;
        sort(ALL(a));
        e = lower_bound(ALL(a), b) - a.begin();
        for (auto&& j : range(e, n)) {
            if (a[j] == b) {
                a[j] = c;
            } else if (a[j] > b) {
                break;
            }
        }
        cout << accumulate(ALL(a), 0) << '\n';
    }
}