#include <bits/stdc++.h>
using namespace std;
string s[10];
int t, x, y;
struct JK{ int p, q; } a[3];
int main()
{
    freopen(".1.in","r",stdin);
    freopen(".1.out","w",stdout);
    cin>>t;
    while( t-- )
    {
        int cnt = 0, ans = 987654321, b[3] = {0, 1, 2};
        for(int i = 0 ; i < 8 ; i++ )
        {
            cin>>s[i];
            for(int j = 0 ; j < 8 ; j++ )
                if( s[i][j] == 'B' ) x = i, y = j;
                else if( s[i][j] == 'T' ) a[cnt].p = i, a[cnt].q = j, cnt++;
        }
        int u = (x+y)%2 + (a[0].p+a[0].q)%2 + (a[1].p+a[1].q)%2 + (a[2].p+a[2].q)%2;
        if( u == 0 or u == 4 )
        {
            do{
                int now = 0, xx = x, yy = y;
                for(int i = 0 ; i < 3 ; i++ )
                {
                    if( (xx+yy) == (a[b[i]].q+a[b[i]].p) ) now++;
                    else if( xx-yy == a[b[i]].p-a[b[i]].q ) now++;
                    else now+=2;
                    xx = a[b[i]].p, yy = a[b[i]].q;
                }
                ans = min( ans, now );
            }while( next_permutation(b,b+3) );
            cout<<ans<<endl;
        }
        else cout<<"-1\n";
    }
}

