#include <bits/stdc++.h>

#define REP(i,a,b) for(int i=a;i<(int)b;i++)
#define rep(i,n) REP(i,0,n)

using namespace std;

vector<string> G;

const int H = 12, W = 6;

int dx[4] = {-1,0,1,0};
int dy[4] = {0,-1,0,1};

bool in_range(int y, int x) {
  return 0<=x&&x<W && 0<=y&&y<H;
}

void drop() {
  vector<vector<char>> vs(W);
  rep(j, W) rep(i, H) {
    if(G[H-1-i][j] != '.')
      vs[j].push_back(G[H-1-i][j]);
  }
  rep(i, W) {
    while((int)vs[i].size() < H) vs[i].push_back('.');
  }
  rep(j, W) rep(i, H) {
    G[H-1-i][j] = vs[j][i];
  }
}

bool vis[H][W];

int counte(int y, int x, char c) {
  int r = 1;
  rep(k, 4) {
    int ny = y + dy[k], nx = x + dx[k];
    if(!in_range(ny, nx)) continue;
    if(G[ny][nx] != c) continue;
    if(vis[ny][nx]) continue;
    vis[ny][nx] = 1;
    r += counte(ny, nx, c);
  }
  return r;
}

void erase(int y, int x, char c) {
  G[y][x] = '.';
  rep(k, 4) {
    int ny = y + dy[k], nx = x + dx[k];
    if(!in_range(ny, nx)) continue;
    if(G[ny][nx] == 'O') {
      G[ny][nx] = '.';
    }
  }
  rep(k, 4) {
    int ny = y + dy[k], nx = x + dx[k];
    if(!in_range(ny, nx)) continue;
    if(G[ny][nx] == c) {
      erase(ny, nx, c);
    }
  }
}

int simulate() {
  int ret = 0;
  while(1) {
    bool ok = 0;
    rep(i, H) rep(j, W) {
      if(G[i][j] != '.' && G[i][j] != 'O') {
	memset(vis, 0, sizeof vis);
	vis[i][j] = 1;
	if(counte(i, j, G[i][j]) < 4) continue;
	ok = 1;
	erase(i, j, G[i][j]);
      }
    }
    if(ok) {
      ret ++, drop();
      ///      rep(i, H) cout << G[i] << endl; cout << endl;
    }
    else break;
  }
  return ret;
}

int main() {
  
  int T; cin >> T;
  rep(_, T) {
    G.clear();
    G.resize(H);
    rep(i, H) cin >> G[i];
    cout << simulate() << endl;
  }
  
  return 0;
}