/***> @author : a_e_kasem <***/
// ﷽
// { وَأَنْ لَيْسَ لِلْإِنْسَانِ إِلَّا مَا سَعَى }
//
// فَالجُهدُ يُثمِرُ إنْ تَضافَرَ صَفوُهُ، والعَزمُ يَرفعُ صَرحَ كُلِّ بُنيانِ
//
// وَما نَيلُ المَطالِبِ بِالتَمَنّي
// وَلَكِن تُؤخَذُ الدُنيا غِلابا
// ***
// وَما اِستَعصى عَلى قَومٍ مَنالٌ
// إِذا الإِقدامُ كانَ لَهُم رِكابا
//
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define cinAll(a) for (auto &it : a) cin >> it
#define all(x) (x).begin(), (x).end()
#define NO void(cout << "NO\n")
#define YES void(cout << "YES\n")
const int N = 8;
vector<vector<int>> board(N, vector<int>(N));
bool col[N], diag1[2*N], diag2[2*N];
vector<vector<int>> allSolutions;
int testCase = 1;
void backtrack(int r = 0) {
if (r == N) {
vector<int> temp(N);
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (board[i][j] == 1) temp[j] = i+1;
}
}
allSolutions.push_back(temp);
return;
}
for (int c = 0; c < N; c++) {
if (!col[c] && !diag1[r - c + N] && !diag2[r + c]) {
col[c] = diag1[r - c + N] = diag2[r + c] = true;
board[r][c] = 1;
backtrack(r + 1);
board[r][c] = 0;
col[c] = diag1[r - c + N] = diag2[r + c] = false;
}
}
}
void solve() {
int arr[N];
while (cin >> arr[0]) {
for (int i = 1; i < N; i++) cin >> arr[i];
int minMove = 8;
for (auto &sol : allSolutions) {
int moves = 0;
for (int col = 0; col < N; col++)
if (arr[col] != sol[col]) moves++;
minMove = min(minMove, moves);
}
cout << "Case " << testCase++ << ": " << minMove << "\n";
}
}
void FastIO();
int32_t main() {
FastIO();
backtrack();
int t = 1;
cin >> t;
while(t--)
{
solve();
}
}
void FastIO()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
}