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

namespace AtCoder
{
    class Program
    {
        static void Main(string[] args)
        {
            var N = ReadLineInt();

            var sum = 0L;
            var count = new int[1000000];
            foreach (var A in ReadLineStr().Split(' '))
            {
                var num = Convert.ToInt32(A);
                sum += num;

                count[num]++;
            }

            var ans = new List<long>();
            var Q = ReadLineInt();
            for (var i = 0; i < Q; i++)
            {
                var (B, C) = ReadLineInt2();
                sum -= B * count[B];
                sum += C * count[B];
                ans.Add(sum);

                count[C] += count[B];
                count[B] = 0;
            }

            foreach (var num in ans)
            {
                Console.WriteLine(num);
            }
        }


        #region 使いまわし
        static string ReadLineStr() { return Console.ReadLine(); }
        static int ReadLineInt() { return Convert.ToInt32(Console.ReadLine()); }
        static (int, int) ReadLineInt2()
        {
            var temp = Console.ReadLine().Split(' ');
            return (Convert.ToInt32(temp[0]), Convert.ToInt32(temp[1]));
        }
        static List<int> ReadLineIntList()
        {
            var data = new List<int>();
            foreach (var temp in Console.ReadLine().Split(' '))
            {
                data.Add(Convert.ToInt32(temp));
            }

            return data;
        }

        static long ReadLineLong() { return Convert.ToInt64(Console.ReadLine()); }
        #endregion
    }
}
