   import java.io.*;
   import java.util.*;
    public class Q3
   {
       
      //  public static void travel (int x,int y, int d)
//       {
//          arr [x][y]=d;
//          if (d+map[x-1][y]<array[x-1][y]&&x!=0)
//          {
//             travel (x-1,y,d+map[x-1][y]);
//          }
//          else if (d+map[x+1][y]<array[x+1][y]&&x!=0)
//          {
//             travel (x+1,y,d+map[x+1][y]);
//          }
//          else if (d+map[x][y-1]<array[x][y-1]&&x!=0)
//          {
//             travel (x,y-1,d+map[1][y-1]);
//          }
//          else if (d+map[x][y+1]<array[x][y+1]&&x!=0)
//          {
//             travel (x,y+1,d+map[x][y+1]);
//          }
//        
//       }
       
       public static void main (String[] args)throws Exception
      {
         BufferedReader in = new BufferedReader (new FileReader ("DATA3.TXT"));
         BufferedWriter out = new BufferedWriter (new FileWriter ("OUT3.TXT"));
			int [][] point={ {0, 11, 13, 13, 11, 11, 12, 13, 12, 11, 13, 13}, 
			 {11, 0, 8, 16, 14, 6, 7, 8, 9, 10, 8, 16},
			 {13, 8, 0, 18, 16, 8, 9, 10, 11, 12, 6, 18},
		{13, 16, 18, 0, 8, 12, 11, 10, 9, 8, 18, 6},
		{11, 14, 16, 8, 0, 10, 9, 8, 7, 6, 16, 8},
		{11, 6, 8, 12, 10, 0, 1, 2 ,3, 4, 8, 12},
	 {12, 7, 9, 11, 9, 1, 0, 1, 2, 3, 9, 11},
		 {13, 8, 10, 10, 8, 2, 1, 0, 1, 2, 10, 10},
		 {12, 9, 11, 9, 7, 3, 2, 1, 0, 1, 11, 9},
		 {11, 10, 12, 8, 6, 4, 3, 2, 1, 0, 12, 8},
		 {13, 8, 6, 18, 16, 8, 9, 10, 11, 12, 0, 18},
			 {13, 16, 18, 6, 8, 12, 11, 10, 9, 8, 8, 0}};
	/*		point [0] = {0, 11, 13, 13, 11, 11, 12, 13, 12, 11, 13, 13};
			point [1] = {11, 0, 8, 16, 14, 6, 7, 8, 9, 10, 8, 16};
			point [2] = {13, 8, 0, 18, 16, 8, 9, 10, 11, 12, 6, 18};
			point [3] = {13, 16, 18, 0, 8, 12, 11, 10, 9, 8, 18, 6};
			point [4] = {11, 14, 16, 8, 0, 10, 9, 8, 7, 6, 16, 8};
			point [5] = {11, 6, 8, 12, 10, 0, 1, 2 ,3, 4, 8, 12};
			point [6] = {12, 7, 9, 11, 9, 1, 0, 1, 2, 3, 9, 11};
			point [7] = {13, 8, 10, 10, 8, 2, 1, 0, 1, 2, 10, 10};
			point [8] = {12, 9, 11, 9, 7, 3, 2, 1, 0, 1, 11, 9};
			point [9] = {11, 10, 12, 8, 6, 4, 3, 2, 1, 0, 12, 8};
			point [10] = {13, 8, 6, 18, 16, 8, 9, 10, 11, 12, 0, 18};
			point [11] = {13, 16, 18, 6, 8, 12, 11, 10, 9, 8, 8, 0};*/
			
			int hold = 0;
			String temp = "";
			int total=0;
			int last =0 ;
			in.readLine();
			in.readLine();
			in.readLine();
			in.readLine();
			in.readLine();
			in.readLine();
			in.readLine();
			in.readLine();
			in.readLine();
			in.readLine();
			for (int i=0; i<5;i++)
			{
				temp=in.readLine();
				last = (int)(temp.charAt(0)-'A');
				for (int j=1; j<temp.length();j++)
				{
					hold = (int)(temp.charAt(j)-'A');
					total+=point[hold][last];
					last=hold;
				}
				out.write(""+total);
				out.newLine();
				total=0;
			}
			out.close();
			
								
    /*     for (int i = 0; i< 13;i++)
         {
            dis[i][i]=0;
         }
         String s;
         for (int i = 0; i < 10;i++)
         {
            s = in.readLine();
            for (int j = 0; j < s.length();j++)
            {
               if (s.charAt(j)=='#')
               {
                  map[i][j]=999999;
               }
               else{map[i][j]=1;}
            }
         }
         map[0][9]=999999;
         map[9][5]=999999;
         map[9][13]=999999;
         */
      /*for (int i = 0; i < 10;i++)
      {
      map[i][0]=99999;
      map[i][18]=99999;
      }
      for (int i = 0; i < 19;i++)
      {
      map[0][i]=9
      }*/
      }
   }
