import java.io.*;
import java.util.*;

public class ProgramIII {

	static int width = 0;
	static String types = ".ABCDEFGHIJKL";
	static boolean maze[] = new boolean[10000];
	static int locs[] = new int[20];
	static int result[][] = new int[13][13];

	public static int getPos(int r, int c) {
		if (r < 0) return -1;
		if (c < 0) return -1;
		if (r >= 10) return -1;
		if (c >= width) return -1;
		return (r*width)+c;
	}
	public static int[] getCoords(int pos) {
		return new int[]{(pos/width),(pos%width)};
	}

	public static MazeState copy(MazeState other) {
		MazeState result = new MazeState();
		result.curPos = other.curPos;
		result.distance = other.distance;
		result.visited = new boolean[other.visited.length];
		for (int i=0; i<other.visited.length; i++) {
			result.visited[i] = other.visited[i];
		}
		return result;
	}

	public static void printMaze(boolean data[]) {
		for (int r=0; r<10; r++) {
			for (int c=0; c<width; c++) {
				System.out.print(data[getPos(r,c)] ? "X" : ".");
			}
			System.out.println();
		}
	}

	public static int getDistance(int typeA, int typeB) {
		int dest = locs[typeB];
		LinkedList queue = new LinkedList();
		// create queue
		MazeState start = new MazeState();
			start.curPos = locs[typeA];
			start.distance = -1;
			start.visited = new boolean[10*width];
		queue.add(start);
		// put in current pos
		while (queue.size() > 0) {
		// while queue has stuff
			MazeState now = (MazeState)queue.removeFirst();
			now.distance++;
			// add distance
			now.visited[now.curPos] = true;
			// add visited
			if (now.curPos == dest) {
				//printMaze(now.visited);
				return now.distance;
			}
			// check if matched dest -- return result
			// for each dir
				MazeState up = copy(now);
				MazeState down = copy(now);
				MazeState left = copy(now);
				MazeState right= copy(now);
				// update pos
				int[] coords = getCoords(now.curPos);
				up.curPos = getPos(coords[0]-1,coords[1]);
				down.curPos = getPos(coords[0]+1,coords[1]);
				left.curPos = getPos(coords[0],coords[1]-1);
				right.curPos = getPos(coords[0],coords[1]+1);
				// check if visited
				if ((up.curPos >= 0) && (up.visited[up.curPos])) {up.curPos = -1;}
				if ((down.curPos >= 0) && (down.visited[down.curPos])) {down.curPos = -1;}
				if ((left.curPos >= 0) && (left.visited[left.curPos])) {left.curPos = -1;}
				if ((right.curPos >= 0) && (right.visited[right.curPos])) {right.curPos = -1;}
				// check if wall
				if ((up.curPos >= 0) && (!maze[up.curPos])) {up.curPos = -1;}
				if ((down.curPos >= 0) && (!maze[down.curPos])) {down.curPos = -1;}
				if ((left.curPos >= 0) && (!maze[left.curPos])) {left.curPos = -1;}
				if ((right.curPos >= 0) && (!maze[right.curPos])) {right.curPos = -1;}
				// add to queue
				if (up.curPos >= 0) queue.add(up);
				if (down.curPos >= 0) queue.add(down);
				if (left.curPos >= 0) queue.add(left);
				if (right.curPos >= 0) queue.add(right);
		}
		// return -1
		return -1;
	}

	public static int getDistanceMemo(int typeA, int typeB) {
		if (typeA == typeB) { return 0; }
		if (result[typeA][typeB] < 0) {
			int tempResult = getDistance(typeA,typeB);
			result[typeA][typeB] = tempResult;
		}
		return result[typeA][typeB];
	}

	public static void main(String args[]) throws Exception {
		result = new int[13][13];
		for (int r=0; r<result.length; r++) {
			for (int c=0; c<result[r].length; c++) {
				result[r][c] = -1;
			}
		}
		BufferedReader in = new BufferedReader(new FileReader("DATA3.txt"));
		PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("OUT3.txt")));
		for (int r = 0; r<10; r++) {
			String line = in.readLine();
			if (r==0) {
				width = line.length();
				maze = new boolean[10*line.length()];
			}
			for (int c = 0; c<line.length(); c++ ){
				int type = types.indexOf(line.substring(c,c+1));
				if (type >= 0) {
					maze[getPos(r,c)] = true;
					if (type >= 1) {
						locs[type-1] = getPos(r,c);
					}
				}
			}
		}
		//printMaze(maze);
		//System.out.println(getDistance(0,1));
		for (int r=0; r<5; r++) {
			String line = in.readLine();
			int total = 0;
			int typeA = types.indexOf(line.substring(0,1))-1;
			for (int c=1; c<line.length(); c++) {
				int typeB = types.indexOf(line.substring(c,c+1))-1;
				total += getDistanceMemo(typeA,typeB);
				typeA = typeB;
			}
			out.println(total);
		}
		out.close();
		System.exit(0);
	}
}

class MazeState {
	public int curPos;
	public int distance;
	public boolean visited[];
}
