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# (.NET Core 3.1.201) (judge verdict: Accepted)

using System;
using System.Numerics;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace debug
{
    class main
    {
        static void Main(string[] args)
        {
            //問題クラスを展開
            ProgramD a = new ProgramD();
            a.main();//実行する

        }

    }
    //ABC170
    class ProgramA
    {
        public void main()
        {
            //入力
            string s = Console.ReadLine();

            //入力が0ならその位置を答で出す
            if (s[0] > 96)
                Console.WriteLine("a");
            else
                Console.WriteLine("A");

        }
    }

    class ProgramB
    {
        public void main()
        {
            //入力
            string[] s = Console.ReadLine().Split(' ');
            int n = int.Parse(s[0]);
            int k = int.Parse(s[1]);

            string[] t = Console.ReadLine().Split(' ');
            int[] a = new int[n];
            for (int i = 0; i < n; i++)
                a[i] = int.Parse(t[i]);


            Array.Sort(a);
            int ans = 0;
            for (int i = 0; i < k; i++)
                ans += a[i];

            Console.WriteLine(ans);


        }
    }

    class ProgramC
    {
        public void main()
        {

            //入力
            long n = long.Parse(Console.ReadLine());
            //n -= 1;

            List<long> list = new List<long>();

            while (n != 0)
            {
                list.Add(n % 26);
                if(n % 26 == 0)
                    n /= 26;
            }


            for (int i = 0; i < list.Count; i++)
            {
                Console.Write((char)(list[i] + 'a' - 1));
            }

        }
    }

    class ProgramD
    {
        public void main()
        {

            //入力
            long n = long.Parse(Console.ReadLine());
            string[] s = Console.ReadLine().Split(' ');
            long[] a = new long[100000];


            for (int i = 0; i < n; i++)
                a[long.Parse(s[i]) -1]++;

            long sum = 0;

            for (int i = 0; i < n; i++)
                sum += long.Parse(s[i]);

            long q = long.Parse(Console.ReadLine());


            //それぞれの数で残るか検討
            for (int i = 0; i < q; i++)
            {
                string[] t = Console.ReadLine().Split(' ');
                sum -= a[long.Parse(t[0]) - 1] * long.Parse(t[0]);
                sum += a[long.Parse(t[0]) - 1] * long.Parse(t[1]);
                a[long.Parse(t[1]) - 1] += a[long.Parse(t[0]) -1];
                a[long.Parse(t[0]) - 1] = 0;
                //答え出力
                Console.WriteLine(sum);


            }

           
        }
    }

    class ProgramE
    {

        //static List<long>[] list = new List<long>[200001];
        static SortedSet<long>[] list = new SortedSet<long>[200001];

        public void main()
        {
            //入力
            string[] s = Console.ReadLine().Split(' ');
            long n = long.Parse(s[0]);
            long q = long.Parse(s[1]);

            long[] a = new long[n];//レート
            long[] b = new long[n];//所属幼稚園
            

            for (int i = 0; i < 200001; i++)
                list[i] = new SortedSet<long>();

            //とりあえず園児に対して所属幼稚園とレートを読む
            for (int i = 0; i < n; i++)
            {
                string[] t = Console.ReadLine().Split(' ');
                a[i] = long.Parse(t[0]);
                b[i] = long.Parse(t[1]);
                list[b[i]].Add((a[i] << 32) + i);//幼稚園児ごとにIDで分けておかないと全部消える
            }


            //答候補入れる
            for (int i = 1; i < 200001; i++)
            {
                if (list[i].Count != 0)
                    list[0].Add(list[i].Max);
            }


            //転園していく
            for(int i = 0;i < q;i++)
            {
                string[] x = Console.ReadLine().Split();
                long c = long.Parse(x[0]);
                long d = long.Parse(x[1]);

                //移動前の最大値を削除
                list[0].Remove(list[b[c - 1]].Max);
                //移動後の幼稚園も削除
                if(list[d].Count != 0)
                    list[0].Remove(list[d].Max);
                //移動する前のその位置を削除する
                list[b[c - 1]].Remove((a[c -1] << 32) + c -1);
                //移動後の幼稚園に追加する
                list[d].Add((a[c -1] << 32) + c -1);
                //移動後の幼稚園の最大値も追加
                 list[0].Add(list[d].Max);
                //移動する前の最大値を追加
                if(list[b[c - 1]].Count != 0)
                    list[0].Add(list[b[c - 1]].Max);

                //園児の所属を更新
                b[c - 1] = d;

                //最小値を出力
                Console.WriteLine((list[0].Min >> 32));
            }

        }

        
    }
 
}