#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<cstdio>
#include<cstring>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define REP(n) rep(i,n)
#define all(n) n.begin(),n.end()

const int MAXW = 6, MAXH = 12;
char tile[MAXW + 2][MAXH + 2];
int checked[MAXW + 2][MAXH + 2];
char c;

const int dx[] = {-1, 1, 0, 0};
const int dy[] = {0, 0, -1, 1};
int countt(int x, int y)
{
    if(tile[x][y] != c) return 0;
    checked[x][y] = 1;
    int ans = 1;
    REP(4)if(!checked[x + dx[i]][y + dy[i]]) ans += countt(x + dx[i], y + dy[i]);
    return ans;
}
void van(int x, int y)
{
    if(tile[x][y] == 'O') { tile[x][y] = 'v'; return ;}
    if(tile[x][y] != c) return ;
    tile[x][y] = 'v';
    REP(4) van(x + dx[i], y + dy[i]);
    return;
}
void test()
{
    rep(j, MAXH){rep(i, MAXW) cout << tile[i+1][j+1]; cout << endl;}

}

int main()
{
    int t; cin >> t;
    while(t--)
    {
        memset(tile, '.', sizeof(tile));
        rep(j,MAXH)rep(i,MAXW)cin >> tile[i + 1][j + 1];
        int vaned = 0;
        for(int ans = 0; 1; ans++)
        {
            vaned = 0;
            memset(checked, 0, sizeof(checked));
            for(int i = 1; i <= MAXW; i++)for(int j= 1; j<= MAXH; j++)if(tile[i][j] != 'O' && tile[i][j] != '.' && tile[i][j] != 'v')
            {
                int cnt = 0;
                c = tile[i][j];
                cnt = countt(i, j);// cout << "count " << i << " "<< j << " :" << cnt << endl;
                if(cnt >= 4) { van(i, j); vaned = 1;}
            }
            //test();

            if(!vaned) { cout << ans << endl; break;}
        
            rep(l,12)for(int i = 1; i <= MAXW; i++)for(int j = MAXH; j > 0; j--)if(tile[i][j] == 'v')
            {
                for(int k = j; k > 0; k--)tile[i][k] = tile[i][k - 1];
                /*
                for(int k = 1 ;k <= MAXH; k++)if(tile[i][j - k] != 'v')
                    for(int l = j; l >= j - k; l--)
                    {
                        if(l - k >= 0) tile[i][l - k] = tile[i][l - k];
                        else tile[i][l - k] = '.';
                    }*/
            }
            //cout << ans << endl;
        }
    }
    return 0;
}