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.Linq;
using System.Collections;
using System.Collections.Generic;

namespace ABC171
{
    using static Ex;
    public class Solve
    {
        public static void Main(string[] args)
        {
            new ABC_D();
        }
    }
    public class ABC_A
    {
        public ABC_A()
        {
            var a = Read[0];
            if ('a' <= a && a <= 'z') 'a'.Cw();
            else 'A'.Cw();
        }
    }
    public class ABC_B
    {
        int n, k;
        public ABC_B()
        {
            (n, k) = ReadIntArray().ToTpl2();
            var p = ReadIntArray().OrderBy(v => v).ToArray();
            long ans = 0;
            for (int i = 0; i < k; i++) ans += p[i];
            ans.Cw();
        }
    }
    public class ABC_C
    {
        long n, pre;
        char[] arr;
        public ABC_C()
        {
            n = ReadLong;

            if (n <= 26)
            {
                var c = (char)('a' + n - 1);
                c.Cw();
                return;
            }

            long a = 0;
            long tmp = 26;
            int i = 2;
            for (; ; i++)
            {
                //i.Cw();
                a += tmp;
                tmp *= 26;
                if (tmp + a >= n) break;
            }
            arr = new char[i];
            for (int j = 0; j < i; j++)
            {
                arr[j] = 'a';
            }
            pre = a;
            //rec(i - 1, false, false);
            A(n, tmp + a, i - 1);
            string.Join("", arr).Cw();
        }

        void rec(int i, bool b, bool bb)
        {
            //$"{pre} {i} {b} { string.Join("", arr)}".Cw();
            if (pre >= n) return;

            for (int j = b ? 1 : 0; j < 26; j++)
            {
                //if (arr[i] == (char)('a' + j)) continue;
                arr[i] = (char)('a' + j);
                pre++;
                if (pre >= n) return;

                if (i + 1 < arr.Length) rec(i + 1, true, true);
                $"{pre} {i} {b} { string.Join("", arr)}".Cw();
                if (pre >= n) return;
            }
            string.Join("", arr).Cw();
            arr[i] = 'a';
            string.Join("", arr).Cw();
            if (bb) return;
            rec(i - 1, true, false);
        }

        void A(long n, long max, int i)
        {
            bool bo = false;
            var tmp = n - pre;
            while (i >= 0)
            {
                var a = tmp / 26;
                var b = tmp % 26;
                //$"{n} {pre} {a} {b}".Cw();

                arr[i] = b == 0 ? 'z' : (char)('a' + b - 1);
                i--;
                tmp = a + (bo ? 0 : 1);
                //a.Cw();
                if (a % 26 == 0 && !bo) tmp--;
                bo = (a == 0);
            }
        }
    }
    public class ABC_D
    {
        int b, c;
        public ABC_D()
        {
            var n = ReadInt;
            var a = ReadIntArray();
            var q = ReadInt;

            var arr = new Dictionary<int, int>();
            for (int i = 0; i < (int)1e5 + 1; i++)
            {
                arr[i] = 0;
            }
            var num = new int[(int)1e5 + 1];
            foreach (var v in a)
            {
                arr[v]++;
            }
            long res = 0;
            for (int i = 0; i < (int)1e5 + 1; i++)
            {
                res += arr[i] * i;
            }

            for (int i = 0; i < q; i++)
            {
                (b, c) = ReadIntArray().ToTpl2();

                res -= arr[b] * (long)b;
                res += arr[b] * (long)c;

                arr[c] += arr[b];
                arr[b] = 0;
                res.Cw();
            }
        }
    }
    public class ABC_E
    {
        public ABC_E()
        {
        }
    }
    public class ABC_F
    {
        public ABC_F()
        {
        }
    }

    public static class Ex
    {
        public static void Cw(this object s) { Console.WriteLine(s); }



        public static bool Mins<T>(ref T a, T b) where T : IComparable<T> { if (a.CompareTo(b) > 0) { a = b; return true; } return false; }
        public static bool Maxs<T>(ref T a, T b) where T : IComparable<T> { if (a.CompareTo(b) < 0) { a = b; return true; } return false; }
        public static void Swap<T>(ref T a, ref T b) { var t = b; b = a; a = t; }
        public static void Swap<T>(List<T> list, int i, int j) { T tmp = list[j]; list[j] = list[i]; list[i] = tmp; }

        public static long Lcm(int x, int y) { return x / Gcd(x, y) * y; }
        public static long Gcd(long m, long n) { if (n == 0) return m; return Gcd(n, m % n); }
        public static List<string> FastSort(this List<string> s) { s.Sort(StringComparer.Ordinal); return s.ToList(); }
        public static void yesno(this bool b) { (b ? "yes" : "no").Cw(); }
        public static void YesNo(this bool b) { (b ? "Yes" : "No").Cw(); }
        public static void YESNO(this bool b) { (b ? "YES" : "NO").Cw(); }

        public static string Read { get { return Console.ReadLine().Trim(); } }
        public static int ReadInt { get { return int.Parse(Read); } }
        public static long ReadLong { get { return long.Parse(Read); } }
        public static double ReadDouble { get { return double.Parse(Read); } }
        public static string[] ReadArray() { return Read.Split(' '); }
        public static int[] ReadIntArray() { return Read.Split(' ').Select(int.Parse).ToArray(); }
        public static long[] ReadLongArray() { return Read.Split(' ').Select(long.Parse).ToArray(); }
        public static string[] ReadArray(long N) { var v = new string[N]; for (long i = 0; i < N; ++i) v[i] = Read; return v; }
        public static int[] ReadIntArray(long N) { var v = new int[N]; for (long i = 0; i < N; ++i) v[i] = ReadInt; return v; }
        public static long[] ReadLongArray(long N) { var v = new long[N]; for (long i = 0; i < N; ++i) v[i] = ReadLong; return v; }

        public static (T, T) ToTpl2<T>(this T[] v) { return (v[0], v[1]); }
        public static (T, T, T) ToTpl3<T>(this T[] v) { return (v[0], v[1], v[2]); }
        public static (T, T, T, T) ToTpl4<T>(this T[] v) { return (v[0], v[1], v[2], v[3]); }
        public static (T, T, T, T, T) ToTpl5<T>(this T[] v) { return (v[0], v[1], v[2], v[3], v[4]); }


        public static int LowerBound<T>(T[] a, T v)
        {
            return LowerBound(a, v, Comparer<T>.Default);
        }

        public static int LowerBound<T>(T[] a, T v, Comparer<T> cmp)
        {
            var l = 0;
            var r = a.Length - 1;
            while (l <= r)
            {
                var mid = l + (r - l) / 2;
                var res = cmp.Compare(a[mid], v);
                if (res == -1) l = mid + 1;
                else r = mid - 1;
            }
            return l;
        }

        /// <summary>
        /// lower_boundは、探索したいkey以上のイテレータを返す
        /// upper_boundは、探索したいkeyより大きいイテレータを返す
        public static int UpperBound<T>(List<T> a, T v)
        {
            return UpperBound(a, v, Comparer<T>.Default);
        }

        public static int UpperBound<T>(List<T> a, T v, Comparer<T> cmp)
        {
            var l = 0;
            var r = a.Count - 1;
            while (l <= r)
            {
                var mid = l + (r - l) / 2;
                var res = cmp.Compare(a[mid], v);
                if (res <= 0) l = mid + 1;
                else r = mid - 1;
            }
            return l;
        }
    }
}