#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <queue>
#include <cassert>

using namespace std;

//------------------------------------------------------------------------------------------------------------------------
// CONSTANTS
//------------------------------------------------------------------------------------------------------------------------

#define PI          3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803   /* all from memory! */
#define PHI         1.6180339887498948482045868343656   /* = (sqrt(5)+1)/2  */
#define MAXINT      0x7fffffff                          /* for a signed integer */
#define EPSILON     0.00000001                          /* for floating-point value comparison */
#define MAXDOUBLE   1.79769313486231570e+308

#define NORTH       0
#define EAST        1
#define SOUTH       2
#define WEST        3

#define MS(a,b)     memset(a, b, sizeof(a))
#define FOR(a,b,c)  for (a = (b); a < (c); ++a)

const int fact[] = { 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 39916800, 479001600 }; // up to 12!
const int month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};  // lengths of each month


//------------------------------------------------------------------------------------------------------------------------
// GLOBAL VARIABLES
//------------------------------------------------------------------------------------------------------------------------

int dc[] = {  0,  1,  0, -1 };
int dr[] = { -1,  0,  1,  0 };
int cmax = -MAXINT;
int cmin = MAXINT;


//------------------------------------------------------------------------------------------------------------------------
// FUNCTIONS/CLASS DEFINITIONS
//------------------------------------------------------------------------------------------------------------------------

int rows = 10, columns;


string grid[11];
struct Point
{
    int x, y;
    Point () {}
    Point (int X, int Y)
    {
        x = X;
        y = Y;
    }
    void out()
    {
        cout << "(" << x << ", " << y << ")";
    }
};

bool inBounds(int row, int column)
{
	return (row >= 0 && row < rows && column >= 0 && column < columns);
}




int bfs(Point a, Point b)
{

    int dist[11][256];
    memset(dist,-1,sizeof(dist));


    queue<Point> Q;
    Q.push(a);
    //cout << a.y << " " << a.x; cin.get();
    dist[a.y][a.x] = 0;

    if (a.x == b.x && a.y == b.y) return 0;
    while (!Q.empty())
    {

        Point cur = Q.front();
        Q.pop();

        //
        //cout << cur.y << " " << cur.x; cin.get();

        for (int i = 0; i < 4; ++i)
        {
            Point next = Point(cur.x+dc[i],cur.y+dr[i]);
            //next.out(); cout << " " << grid[next.y][next.x]; cin.get();

            if (inBounds(next.y,next.x) && dist[next.y][next.x] == -1 && grid[next.y][next.x] != '#')
            {
                //cout << "inserted\n";
                Q.push(next);
                dist[next.y][next.x] = dist[cur.y][cur.x]+1;
                if (next.x == b.x && next.y == b.y)
                {
                    return dist[next.y][next.x];
                }
            }
        }
    }


}




//------------------------------------------------------------------------------------------------------------------------
// MAIN PROGRAM EXECUTION
//------------------------------------------------------------------------------------------------------------------------
int main()
{
    ifstream fin("data3.txt");
    ofstream fout("out3.txt");
    int i = 0, j = 0, k = 0;
    int num, n1, n2, n3;
    string str;

    Point coord[200];



    for (i = 0; i < 10; ++i)
    {
        fin >> grid[i];
    }
    columns = grid[0].length();
    //cout << columns;
    // go thru grid
    for (i = 0; i < 10; ++i)
    {
        cout << grid[i] << endl;
        for (j = 0; j < columns; ++j)
        {
            if (isalpha(grid[i][j]))
            {

                coord[grid[i][j]] = Point(j,i);
                coord[grid[i][j]].out(); cout << endl;
            }
        }
    }



    for (i = 0; i < 5; ++i)
    {
        fin >> str;
        int sum = 0;
        if (str.length()==1)
        {
            fout << "0\n";
            continue;
        }

        for (j = 0; j < str.length()-1; ++j)
        {
            //cout << str[i] << " -> " << str[i+1] << " = " << bfs(coord[str[i]],coord[str[i+1]]); cin.get();
            sum += bfs(coord[str[j]],coord[str[j+1]]);
        }
        fout << sum << endl;
    }










    return 0;
}




