#include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<(n);i++)
using namespace std;
typedef pair<char, int>P;

char s[7];
P f[12][6];
int dx[]{ 1,-1,0,0 }, dy[]{ 0,0,1,-1 };
bool used[12][6];

int dfs(int x, int y) {
	used[x][y] = true;
	int res = 1;
	rep(i, 4) {
		int nx = x + dx[i], ny = y + dy[i];
		if (0 <= nx&&nx < 12 && 0 <= ny&&ny < 6 && !used[nx][ny] && f[nx][ny].first == f[x][y].first)
			res += dfs(nx, ny);
	}
	return res;
}
int main() {
	int n; scanf("%d", &n);
	rep(i, n) {
		rep(i, 12) {
			scanf("%s", s);
			rep(j, 6)f[i][j] = P(s[j], 0);
		}
		int cnt = 0; bool update = true;
		while (update) {
			update = false;
			rep(i, 12)rep(j, 6)f[i][j].second = 0;
			rep(i, 12)rep(j, 6) {
				memset(used, 0, sizeof(used));
				if (f[i][j].first != '.'&&f[i][j].first != 'O'&&dfs(i, j) >= 4)
					f[i][j].second = 1;
			}
			rep(i, 12)rep(j, 6) {
				if (f[i][j].first != 'O')continue;
				bool ok = false;
				rep(k, 4) {
					int nx = i + dx[k], ny = j + dy[k];
					if (0 <= nx&&nx < 12 && 0 <= ny&&ny < 6 && f[nx][ny].first != 'O'&&f[nx][ny].second)ok = true;
				}
				if (ok) {
					f[i][j].second = 1;
				}
			}
			rep(i, 12)rep(j, 6) {
				if (f[i][j].second) {
					f[i][j].first = '.'; update = true;
				}
			}
			for (int i = 10; i >= 0; i--)rep(j, 6) {
				int k = i;
				while (k <= 10 && f[k + 1][j].first == '.')swap(f[k][j], f[k + 1][j]), k++;
			}
			if (update)cnt++;
		}
		printf("%d\n", cnt);
	}
}