#include <bits/stdc++.h>

using namespace std;

namespace {

    typedef double real;
    typedef long long ll;

    template<class T> ostream& operator<<(ostream& os, const vector<T>& vs) {
        for (int i = 0; i < vs.size(); i++) os << vs[i] << endl;
        return os;
    }
    template<class T> istream& operator>>(istream& is, vector<T>& vs) {
        for (auto it = vs.begin(); it != vs.end(); it++) is >> *it;
        return is;
    }

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

    const int H = 12;
    const int W = 6;

    vector<string> F;
    void input() {
        F.clear(); F.resize(12);
        cin >> F;
    }

    bool step() {
        bool del[H][W];
        memset(del, 0, sizeof(del));

        bool flag = false;

        for (int i = 0; i < H; i++) {
            for (int j = 0; j < W; j++) {
                if (F[i][j] == '.') continue;
                if (F[i][j] == 'O') continue;
                static bool used[H][W];
                memset(used, 0, sizeof(used));
                int count = 1;
                queue< pair<int, int> > Q;
                Q.push(make_pair(i, j));
                used[i][j] = true;
                while (not Q.empty()) { 
                    pair<int, int> c = Q.front(); Q.pop();
                    int y = c.first;
                    int x = c.second;
                    for (int k = 0; k < 4; k++) {
                        int ny = y + dy[k];
                        int nx = x + dx[k];
                        if (ny < 0 || ny >= H) continue;
                        if (nx < 0 || nx >= W) continue;
                        if (used[ny][nx]) continue;
                        if (F[ny][nx] == F[i][j]) {
                            used[ny][nx] = true;
                            count++;
                            Q.push(make_pair(ny, nx));
                        } else if (F[ny][nx] == 'O') {
                            used[ny][nx] = true;
                        }
                    }
                }
                if (count >= 4) {
                    flag = true;
                    for (int y = 0; y < H; y++) {
                        for (int x = 0; x < W; x++) {
                            if (used[y][x]) {
                                del[y][x] = true;
                                //cout << y << "," << x << endl;
                            }
                        }
                    }
                }
            }
        }

        auto G = vector<string>(12, "......");
        for (int j = 0; j < W; j++) {
            string t;
            for (int i = 0; i < H; i++) {
                if (F[i][j] != '.' && (not del[i][j])) {
                    t.push_back(F[i][j]);
                }
            }
            reverse(t.begin(), t.end());
            for (int i = 0; i < t.size(); i++) {
                G[H - 1 - i][j] = t[i];
            }
        }
        F = G;

        return flag;
    }

    void solve() {
        int c = 0;
        while (step()) {
            c++;
        }
        cout << c << endl;
    }
}

int main() {
    int T; cin >> T;
    for (int t = 0; t < T; t++) {
        input(); solve();
    }
    return 0;
}

Measured memory_bytes: 3092