#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <utility>
#include <iomanip>
#include <algorithm>
#include <functional>
using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef pair<int,int> pint;

#define DE 1
#define FI first
#define SE second
#define PB push_back
#define MP make_pair
#define ALL(s) (s).begin(),(s).end()
#define REP(i,n) for (int i = 0; i < (int)(n); ++i)
#define EACH(i,s) for (typeof((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define COUT(x) cout<<#x<<" = "<<(x)<<" (L"<<__LINE__<<")"<<endl

template<class T1, class T2> ostream& operator<<(ostream &s, pair<T1,T2> P){return s<<'<'<<P.first<<", "<<P.second<<'>';}
template<class T> ostream& operator<<(ostream &s, vector<T> P) {s<<"{ ";for(int i=0;i<P.size();++i){if(i>0)s<<", ";s<<P[i];}return s<<" }"<<endl;}
template<class T1, class T2> ostream& operator<<(ostream &s, map<T1,T2> P) {s<<"{ ";for(typeof(P.begin()) it=P.begin();it!=P.end();++it){if(it!=P.begin())s<<", ";s<<'<'<<it->first<<"->"<<it->second<<'>';}return s<<" }"<<endl;}




vector<string> field(6);

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

bool seen[6][12];

vector<vector<pint> > ram;
vector<pint> temp;
vector<pint> oja;

void dfs(int x, int y) {
    seen[x][y] = true;
    char c = field[x][y];
    temp.PB(MP(x,y));
    
    for (int i = 0; i < 4; ++i) {
        int nx = x + dx[i], ny = y + dy[i];
        if (nx >= 0 && nx < 6 && ny >= 0 && ny < 12) {
            if (seen[nx][ny]) continue;
            if (field[nx][ny] == c) {
                dfs(nx, ny);
            }
            else if (field[nx][ny] == 'O') {
                oja.PB(MP(nx,ny));
            }
        }
    }
}

int main() {
    //freopen( "/Users/macuser/Documents/Programming/Contest/input.in", "r", stdin );
    
    int N;
    cin >> N;
    
    for (int id = 0; id < N; ++id) {
        field.clear();
        field.resize(6);
        
        char c;
        for (int i = 0; i < 12; ++i) {
            for (int j = 0; j < 6; ++j) {
                cin >> c;
                field[j].PB(c);
            }
        }
        for (int i = 0; i < 6; ++i) reverse(ALL(field[i]));
        
        int con = 0;
        while (true) {
            //COUT(field);
            
            ram.clear();
            memset(seen, 0, sizeof(seen));
            for (int i = 0; i < 6; ++i) {
                for (int j = 0; j < 12; ++j) {
                    if (!seen[i][j] && field[i][j] != '.' && field[i][j] != 'O') {
                        temp.clear(); oja.clear();
                        dfs(i,j);
                        if (temp.size() >= 4) {
                            for (int k = 0; k < oja.size(); ++k) temp.PB(oja[k]);
                            ram.PB(temp);
                        }
                    }
                }
            }
            
            if (ram.size() == 0) break;
            
            //COUT(ram);
            
            for (int i = 0; i < ram.size(); ++i) {
                for (int j = 0; j < ram[i].size(); ++j) {
                    field[ram[i][j].FI][ram[i][j].SE] = '-';
                }
            }
            for (int i = 0; i < 6; ++i) {
                for (int j = 0; j < field[i].size(); ++j) {
                    if (field[i][j] == '-') {
                        field[i].erase(field[i].begin() + j--);
                    }
                }
            }
            for (int i = 0; i < 6; ++i) {
                while (field[i].size() < 12) field[i].PB('.');
            }
            
            ++con; 
        }
        
        cout << con << endl;
    }
    
    return 0;
}


Measured memory_bytes: 1232