D = [[-1, 0], [1, 0], [0, -1], [0, 1]]

def puyo_rensa(old, cnt)
  check = 14.times.map{ [false] * 8 }
  res = 0
  (1..12).each do |i|
    (1..6).each do |j|
      unless check[i][j] || old[i][j] == 'O' || old[i][j] == '.'
        puyo_connector(i, j, old){ |arr|
          if arr.size >= 4
            arr.each do |x, y|
              check[x][y] = true
              res = 1
            end
          end
        }
      end
    end
  end

  buf = []
  (1..12).each do |i|
    (1..6).each do |j|
      if check[i][j]
        D.each do |x, y|
          p, q = i + x, j + y
          buf << [p, q] if old[p][q] == 'O'
        end
      end
    end
  end
  buf.each{ |x, y| check[x][y] = true }
  fld = puyo_erase(old, check)
  if res == 1 then puyo_rensa(fld, cnt + 1)
  else cnt
  end
end

def puyo_connector(i, j, fld)
  check = 14.times.map{ [false] * 8 }
  arr = [[i, j]]
  ret = [[i, j]]
  check[i][j] = true
  until arr.empty?
    i, j = arr.shift
    D.each do |x, y|
      p, q = i + x,  j + y
      if !check[p][q] && fld[i][j] == fld[p][q]
        check[p][q] = true
        arr << [p, q]
        ret << [p, q]
      end
    end
  end
  yield(ret)
end

def puyo_erase(fld, check)
  fld = fld.transpose
  nfld = 8.times.map{ [] }
  8.times do |i|
    13.downto(0) do |j|
      unless check.transpose[i][j]
        nfld[i] << fld[i][j]
      end
    end
    (14 - nfld[i].size).times{ nfld[i] << '.' }
    nfld[i].reverse!
  end
  nfld.transpose
end

gets.to_i.times do
  fld = 14.times.map{ ['.'] * 8 }
  12.times do |i|
    gets.chomp.split('').each_with_index do |c, j|
      fld[i+1][j+1] = c
    end
  end
  p puyo_rensa(fld, 0)
end