#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cassert>
#include<queue>
#include<string.h>
#define F first
#define S second
using namespace std;
typedef pair<int,int> P;
char cmap[13][13];
bool used[13][13];
int imap[13][13];
static const int dx[4] = {0,1,0,-1};
static const int dy[4] = {1,0,-1,0};
int Cnt = 0;

void dfs(int x,int y,char c,int cnt)
{
  used[y][x] = true; 
  int fcnt = 0;
  for(int i=0;i<4;i++){
    int nx = x+dx[i],ny = y+dy[i];
    if(0<=nx&&nx<6&&0<=ny&&ny<12&&cmap[ny][nx] == c && !used[ny][nx]){
      fcnt++; Cnt++;
      dfs(nx,ny,c,cnt+1);
    }
  }
  if(fcnt == 0)imap[y][x] = Cnt;
}

void erase(int x,int y,char c)
{
  used[y][x] = true;
  cmap[y][x] = '.';

  queue<P> que;
  que.push(P(x,y));

  while(!que.empty())
    {
      P p = que.front(); que.pop();
      for(int i=0;i<4;i++)
	{
	  int nx = p.F+dx[i],ny = p.S+dy[i];

	  if(0<=nx&&nx<6&&0<=ny&&ny<12&& ((!used[ny][nx] && cmap[ny][nx] == c) || cmap[ny][nx] == 'O'))
	    {
	      if(cmap[ny][nx] != 'O')que.push(P(nx,ny));
	      cmap[ny][nx] = '.';
	      used[ny][nx] = true;
	      
	    }
	}
    }
}

void display()
{

  for(int i=0;i<12;i++)
    {
      for(int j=0;j<6;j++)
	{
	  cout << cmap[i][j] << " ";
	}
      cout << endl;
    }
  /*
for(int i=0;i<12;i++)
    {
      for(int j=0;j<6;j++)
	{
	  cout << imap[i][j] << " ";
	}
      cout << endl;
    }
  */
  cout << endl;
}


void drop(int x,int y)
{
  //cout << "x = " << x << ", y = " << y << endl;
  int st,ed;
  st = y;
  ed = -1;
  for(int i=y;i>=0;i--)
    {
      if(ed == -1 && cmap[i][x] != '.')
	{
	  ed = i+1;
	  //cout << "cmap["<<st-ed+(i+1)<<"][x] =" << cmap[st+ed-(i+1)][x] << ", cmpa["<<i<<"][x] = "<<  cmap[i][x] << endl;
	  cmap[st-ed+(i+1)][x] = cmap[i][x];
	  cmap[i][x] = '.';
	}
      else if(ed != -1 && cmap[i][x] != '.')
	{
	  //cout << "cmap["<<st-ed+(i+1)<<"][x] = " << cmap[st+ed-(i+1)][x] << ", cmap["<<i<<"][x] = " <<  cmap[i][x] << endl;
	  cmap[st-ed+(i+1)][x] = cmap[i][x];
	  cmap[i][x] = '.';
	}
      else if(ed != -1 && cmap[i][x] == '.')
	{
	  for(int k=11;k>=0;k--)
	    {
	      if(cmap[k][x] == '.')
		{
		  st = k;
		  break;
		}
	    }
       
	  ed = -1;
	}      

    }
}


int main()
{
  int N;
  cin >> N;
  while(N-- > 0)
    {
      for(int i=0;i<12;i++)
	{
	  string s;
	  cin >> s;
	  for(int j=0;j<s.length();j++)
	    {
	      cmap[i][j] = s[j];
	      used[i][j] = false;
	      imap[i][j] = -1;
	    }
	}

      int cnt = 0;
      while(true)
	{
	  //display();
	  int increase = 0;
	  for(int i=0;i<12;i++)for(int j=0;j<6;j++)
				 {
				   imap[i][j] = -1;
				   used[i][j] = false;
				 }
	  for(int i=0;i<12;i++)
	    {
	      for(int j=0;j<6;j++)
		{
		  if(cmap[i][j] != '.' && cmap[i][j] != 'O' && !used[i][j])
		    {
		      Cnt=1;
		      dfs(j,i,cmap[i][j],1); //最大何マスつながっているか
		    }
		}
	    }
	
	  
	  for(int i=0;i<12;i++)for(int j=0;j<6;j++)used[i][j] = false;
	  for(int i=0;i<12;i++)
	    {
	      for(int j=0;j<6;j++)
		{
		  if(!used[i][j] && imap[i][j] >= 4){
		    increase++;
		    erase(j,i,cmap[i][j]);
		  }
		}
	    }
	  //display();
	  if(increase == 0)
	    {
	      break;
	    }
	  cnt++;


	  for(int i=0;i<6;i++)
	    {
	      for(int j=11;j>=0;j--)
		{
		  if(cmap[j][i] == '.')
		    {
		      drop(i,j);
		      break;
		    }
		}

	    }





	}

      cout<< cnt << endl;

    }
  return 0;
}

Measured memory_bytes: 1212