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

namespace AtCoder.Abc171
{
    class D
    {
        static readonly int MaxN = 10000;

        public static void Main()
        {
            int n = int.Parse(Console.ReadLine());
            var a = Console.ReadLine().Split(' ').Select(i => int.Parse(i)).ToArray();

            var count = new long[MaxN + 1];

            foreach (var item in a)
                count[item]++;

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

            long sum = 0;
            for (int i = 1; i <= MaxN; i++)
                sum += i * count[i];

            for (int i = 0; i < q; i++)
            {
                var bc = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray();

                long minus = bc[0] * count[bc[0]];
                long plus = bc[1] * count[bc[0]];
                count[bc[1]] += count[bc[0]];
                count[bc[0]] = 0;
                sum += plus - minus;
                Console.WriteLine(sum);
            }
        }
    }
}
