using System;
using System.Linq;

class D{
  public static void Main(){
  int N = int.Parse(Console.ReadLine());
  string FAr = Console.ReadLine();
  string Outp = FAr;
  int Q = int.Parse(Console.ReadLine());
  for (int i = 0; i < Q; i++){
    string[] temp = Console.ReadLine().Split(' ');
    Outp = Outp.Replace(temp[0], temp[1]);
    int[] sun = Array.ConvertAll(Outp.Split(' '), int.Parse);
    int s = sun.Sum();
    Console.WriteLine(s);
  }
  }
}