// AOJ 2206: Compile
// 2017.11.10 bal4u@uu

#include <stdio.h>
#include <string.h>

char m[12][7], t[12][7];

int same(char a, int r, int c)
{
	int dr, dc;
	int ans = 1;

	for (dc = -1; dc <= 1; dc++) for (dr = -1; dr <= 1; dr++) {
		if (dr == dc || dr && dc) continue;
		if (c + dc < 0 || c + dc >= 6 || r + dr < 0 || r + dr >= 12) continue;
		if (t[r+dr][c+dc] == a) {
			t[r+dr][c+dc] = 'x';
			ans += same(a, r+dr, c+dc);
		} else if (t[r+dr][c+dc] == 'O') t[r+dr][c+dc] = 'x';
	}
	return ans;
}

int main()
{
	int cno, r, c, f, ans;
	char a;

	scanf("%d", &cno);
	while (cno--) {
		for (r = 0; r < 12; r++)  scanf("%s", m[r]);

		ans = 0, f = 1;
		while (f) {
			f = 0;
			for (c = 0; c < 6; c++) for (r = 11; r >= 0; r--) {
				memcpy(t, m, sizeof(m));
				a = m[r][c];
				if (a == 'O' || a == '.' || a == 'x') continue;
				t[r][c] = 'x';
				if (same(a, r, c) >= 4) {
					memcpy(m, t, sizeof(m));
					f = 1;
				}
			}

			if (f) {
				ans++;
				for (c = 0; c < 6; c++) {
					for (r = 11; r >= 0; r--) {
						int rr;
						if (m[r][c] != 'x') continue;
						rr = r-1;
						while (rr >= 0 && m[rr][c] == 'x') rr--;
						if (rr < 0) m[r][c] = '.';
						else m[r][c] = m[rr][c], m[rr][c] = 'x';
					}
				}
			}
		}
		printf("%d\n", ans);
	}
	return 0;
}