using System;
using System.Linq;
using System.Collections.Generic;

namespace AtCoderMake.ABC171
{
    class ABC171D
    {
        static void Main(string[] args)
        {
            var n = int.Parse(Console.ReadLine());
            var a = Console.ReadLine().Split().Select((x) => int.Parse(x)).ToArray();
            var q = int.Parse(Console.ReadLine());
            long sumAll = a.Sum();
            int[] dic = new int[1000000] ;

            foreach(int ai in a)
            {
                dic[ai]++;
            }

            for (int i = 0; i < q; i++)
            {
                var bcT = Console.ReadLine().Split();
                if (bcT.Length != 0)
                {
                    var bc = bcT.Select((x) => int.Parse(x)).ToArray();
                    sumAll = sumAll + (bc[1] - bc[0]) * dic[bc[0]];
                    dic[bc[1]] += dic[bc[0]];
                    dic[bc[0]] = 0;
                    Console.WriteLine(sumAll);
                }
            }
        }
    }
}
