   import java.io.*;
   import java.util.*;

    public class Q3
   {
      static char [][] map = new char [10][19];
      static int [][] imap = new int [10][19];
      static int [][] dist = new int [12][12];
   
       public static void rec (int x, int y, int c)
      {
         if (x >= 0 && x < 10 && y >= 0 && y < 19 && map [x][y] != '#' && c < imap [x][y])
         {
            imap [x][y] = c;
            rec (x + 1, y, c + 1);
            rec (x - 1, y, c + 1);
            rec (x, y + 1, c + 1);
            rec (x, y - 1, c + 1);
         }
      }
   
       public static void main (String [] args) throws IOException
      {
         BufferedReader in = new BufferedReader (new FileReader ("DATA3.txt"));
         BufferedWriter out = new BufferedWriter (new FileWriter ("OUT3.txt"));  
      	
         for (int x = 0; x < 10; x++)
         {
            map [x] = in.readLine ().toCharArray ();
            Arrays.fill (imap [x], 9999);
         }
      	
         int []R = {0, 3, 7, 7, 3, 3, 3, 3, 3, 3, 9, 9};
         int []C = {9, 3, 3, 15, 15, 7, 8, 9, 10, 11, 5, 13};
      	
         for (int gg = 0; gg < 12; gg++)
         {
            for (int x = 0; x < 10; x++)
            {
               Arrays.fill (imap [x], 9999);
            }
         
            rec (R [gg], C[gg], 0);
            for (int x = 0; x < 12; x++)
            {
               dist [gg][x] = imap [R [x]][C [x]];
            }
         }
         
      	int cnt;
         for (int A = 0; A < 5; A++)
         {
            String t = in.readLine ();
            cnt = 0;
            for (int B = 1; B < t.length (); B++)
            {
            	cnt += dist [t.charAt (B - 1) - 'A'][t.charAt (B) - 'A'];
            }
            out.write ("" + cnt);
         	out.newLine ();
         	
         }
      	out.close ();
      	
      }
   }
