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();
            Dictionary<int, int> dic = new Dictionary<int, int>();

            foreach(int ai in a)
            {
                if (dic.ContainsKey(ai))
                {
                    dic[ai]++;
                }
                else
                {
                    dic.Add(ai, 1);
                }
            }
                    
            for(int i = 0; i < q; i++)
            {
                var bc= Console.ReadLine().Split().Select((x) => int.Parse(x)).ToArray();
                if (dic.ContainsKey(bc[0]))
                {
                    sumAll = sumAll + (bc[1] - bc[0]) * dic[bc[0]];
                    if (dic.ContainsKey(bc[1]))
                    {
                        dic[bc[1]] += dic[bc[0]];
                    }
                    else
                    {
                        dic.Add(bc[1], dic[bc[0]]);
                    }
                    dic[bc[0]]=0;
                }
                Console.WriteLine(sumAll);
            }
        }
    }
}
