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

namespace _171_d
{
    internal class Replacing
    {
        private static void Main(string[] args)
        {
            var n = int.Parse(Console.ReadLine() ?? throw new InvalidOperationException());
            var a = GetInputDictionary();
            var q = int.Parse(Console.ReadLine() ?? throw new InvalidOperationException());

            for (var i = 0; i < q; i++)
            {
                var bcLine = Console.ReadLine()?.Split(" ").Select(int.Parse).ToArray();
                if (bcLine == null) return;
                var b = bcLine[0];
                var c = bcLine[1];

                if (b == c)
                {
                    Console.WriteLine(a.Values.Sum());
                    continue;
                }

                if (a.ContainsKey(b))
                {
                    var value = a[b] / b * c;

                    if (a.ContainsKey(c))
                    {
                        a[c] += value;
                    }
                    else
                    {
                        a.Add(c, value);
                    }
                    a[b] = 0;
                }
                Console.WriteLine(a.Values.Sum());
            }
        }

        private static Dictionary<int, long> GetInputDictionary()
        {
            var sb = new StringBuilder();
            while (true)
            {
                var ch = Convert.ToChar(Console.Read());
                sb.Append(ch);
                if (ch == '\n') break;
            }
            var array = sb.ToString().Split(" ").Select(int.Parse).ToArray();

            var dictionary = new Dictionary<int, long>();
            foreach (var data in array)
            {
                if (data == '\n') break;

                if (dictionary.ContainsKey(data))
                {
                    dictionary[data] += data;
                }
                else
                {
                    dictionary.Add(data, data);
                }
            }

            return dictionary;
        }
    }
}