#include <stdio.h>

#define WIDTH 6
#define HEIGHT 12

enum { YET, ALREADY };

int del( void );
int search( int, int );
int search_body( int, int, char );
void fall( void );

typedef struct {
	char color;
	int state;
} FIELD;

FIELD field[ WIDTH ][ HEIGHT ];
const int dx[ 4 ] = { -1, 0, 1, 0 }, dy[ 4 ] = { 0, -1, 0, 1 };

int stack[ WIDTH * HEIGHT ];
int sp;

int main( void ) {
	int x, y, n, chain;

	scanf( "%d", &n );
	while ( n-- ) {
		for ( y = 0; y < HEIGHT; y++ )
			for ( x = 0; x < WIDTH; x++ ) {
				scanf( " %c", &field[ x ][ y ].color );
				field[ x ][ y ].state = YET;
			}

		for ( chain = 0; del(); chain++ )
			fall();

		printf( "%d\n", chain );
	}

	return 0;
}

int del( void ) {
	int x, y, del = 0;

	for ( x = 0; x < WIDTH; x++ )
		for ( y = 0; y < HEIGHT; y++ )
			if ( field[ x ][ y ].color != '.' && field[ x ][ y ].color != 'O' && field[ x ][ y ].state == YET ) {
				sp = 0;
				if ( search( x, y ) >= 4 ) {
					while ( sp-- )
						field[ stack[ sp ] / HEIGHT ][ stack[ sp ] % HEIGHT ].color = '.';

					del = 1;
				}
			}

	return del;
}

int search( int x, int y ) {
	return search_body( x, y, field[ x ][ y ].color );
}

int search_body( int x, int y, char color ) {
	int i, sum;

	stack[ sp++ ] = x * HEIGHT + y;
	field[ x ][ y ].state = ALREADY;

	sum = 0;
	for ( i = 0; i < 4; i++ ) {
		int nx = x + dx[ i ], ny = y + dy[ i ];

		if ( nx >= 0 && nx < WIDTH && ny >= 0 && ny < HEIGHT && field[ nx ][ ny ].state == YET && ( field[ nx ][ ny ].color == color || field[ nx ][ ny ].color == 'O' ) )
			if ( field[ nx ][ ny ].color == 'O' )
				stack[ sp++ ] = nx * HEIGHT + ny;
			else
				sum += search_body( nx, ny, color );
	}

	return sum + 1;
}

void fall( void ) {
	int x, y, cnt;

	for ( x = 0; x < WIDTH; x++ ) {
		cnt = 0;
		for ( y = HEIGHT - 1; y >= 0; y-- )
			if ( field[ x ][ y ].color == '.' )
				cnt++;
			else {
				field[ x ][ y + cnt ].color = field[ x ][ y ].color;
				field[ x ][ y + cnt ].state = YET;
			}

		for ( y = cnt - 1; y >= 0; y-- )
			field[ x ][ y ].color = '.';
	}
}