using System;
using System.Linq;

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

            var a = Console.ReadLine();
            if (a == null) return;
            var aLengthWithoutSpace = a.Length / 2 + 1;
            if (aLengthWithoutSpace != n) return;

            var q = int.Parse(Console.ReadLine() ?? throw new InvalidOperationException());

            var replacedA = a.Split(' ');
            for (var i = 0; i < q; i++)
            {
                var bcLine = Console.ReadLine()?.Split(' ');
                if (bcLine == null) return;
                var b = bcLine[0];
                var c = bcLine[1];

                replacedA = replacedA.Select(x => x.Replace(b, c)).ToArray();
                long sum = replacedA.Select(int.Parse).ToArray().Sum();
                Console.WriteLine(sum);
            }
        }
    }
}