#include <iostream>
#include <vector>
#include <cstring>
#include <climits>
using namespace std;
const int N = 16; // Sudoku board size
int sudoku[N][N]; // Sudoku board
int rowCheck[N][N + 1]; // Check for row-wise numbers
int colCheck[N][N + 1]; // Check for column-wise numbers
struct Rotation {
int x, y, t; // Rotation position and count
};
int minRotations; // Minimum rotation count
vector<Rotation> currentRotations; // Current rotation sequence
vector<Rotation> minRotationsSequence; // Minimum rotation sequence
// Rotate a small area counterclockwise by 90 degrees
void rotateCounterClockwise(int x, int y) {
x *= 4; // Convert coordinates to the small area
y *= 4;
for (int i = 0; i < 2; i++) {
for (int j = i; j < 3 - i; j++) {
int temp = sudoku[x + i][y + j];
sudoku[x + i][y + j] = sudoku[x + j][y + 3 - i];
sudoku[x + j][y + 3 - i] = sudoku[x + 3 - i][y + 3 - j];
sudoku[x + 3 - i][y + 3 - j] = sudoku[x + 3 - j][y + i];
sudoku[x + 3 - j][y + i] = temp;
}
}
}
// Check if the rotated small area is valid
bool isValidRotation(int x, int y) {
x *= 4; // Convert coordinates to the small area
y *= 4;
for (int i = x; i <= x + 3; i++) {
for (int j = y; j <= y + 3; j++) {
int num = sudoku[i][j];
if (rowCheck[i][num] || colCheck[j][num]) {
return false;
}
}
}
// Update the occurrence of numbers in each row and column
for (int i = x; i <= x + 3; i++) {
for (int j = y; j <= y + 3; j++) {
int num = sudoku[i][j];
rowCheck[i][num] = true;
colCheck[j][num] = true;
}
}
return true;
}
// Undo the rotation operation
void undoRotation(int x, int y) {
x *= 4; // Convert coordinates to the small area
y *= 4;
for (int i = x; i < x + 4; i++) {
for (int j = y; j < y + 4; j++) {
int num = sudoku[i][j];
rowCheck[i][num] = false;
colCheck[j][num] = false;
}
}
}
// Depth-first search, try different rotation sequences
void dfs(int x, int y, int count) {
if (x == 4) { // If all small areas have been traversed
if (count < minRotations) { // Update the minimum rotation count and sequence
minRotations = count;
minRotationsSequence = currentRotations;
}
return;
}
for (int op = 0; op < 4; op++) { // Try different rotation counts
if (!isValidRotation(x, y)) { // If the rotated small area is not valid, continue to rotate counterclockwise
rotateCounterClockwise(x, y);
continue;
}
currentRotations.push_back({x + 1, y + 1, op}); // Add the current rotation sequence to the list
if (y == 3) { // If all small areas in the current row have been traversed, move to the next row
dfs(x + 1, 0, count + op);
}
else dfs(x, y + 1, count + op); // Continue to traverse the next small area in the current row
currentRotations.pop_back(); // Undo the current rotation sequence
undoRotation(x, y); // Undo the rotation operation
rotateCounterClockwise(x, y); // Restore the small area
}
}
int main() {
int T;
cin >> T;
while (T--) {
minRotations = INT_MAX;
currentRotations.clear();
minRotationsSequence.clear();
memset(colCheck, 0, sizeof colCheck);
memset(rowCheck, 0, sizeof rowCheck);
// Read the Sudoku board
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
char hex;
cin >> hex;
int dec = stoi(string(1, hex), nullptr, 16);
sudoku[i][j] = dec;
}
}
dfs(0, 0, 0); // Start depth-first search
cout << minRotations << endl; // Output the minimum rotation count
for (const auto& rotation : minRotationsSequence) { // Output the minimum rotation sequence
for (int i = 0; i < rotation.t; i++) {
cout << rotation.x << " " << rotation.y << endl;
}
}
}
return 0;
}