#include <bits/stdc++.h>

/**
 * Author: Niyaz Nigmatullin
 */

using namespace std;

// B L K F R T

// R D
vector<vector<int>> const ROTATE_CUBE = {
	{4, 0, 2, 3, 5, 1},
	{3, 1, 0, 5, 4, 2},
};

vector<int> const DR = {0, 1};
vector<int> const DC = {1, 0};
string const DIRS = "LRUD";
int const ALL_COLORS = 1 << 24;

vector<int> rotate(vector<int> const &cube, int dir) {
	vector<int> res(6);
	for (int i = 0; i < 6; i++) {
		res[i] = cube[ROTATE_CUBE[dir][i]];
	}
	return res;
}

struct state {
	vector<int> cube;
	int row;
	int column;

	bool operator==(state const &other) const {
		return row == other.row && column == other.column && cube == other.cube;
	}
};

template<>
struct std::hash<state> {
	static const unsigned int P = 239017;

    std::size_t operator()(state const& s) const {
    	unsigned int hash = 0;
    	for (int i = 0; i < 6; i++) {
    		hash = hash * P + s.cube[i];
    	}
    	hash = hash * P + s.row;
    	hash = hash * P + s.column;
    	return hash;
    }
};

bool path_exists(state start, vector<vector<int>> const &field) {
	if (start.cube[0] != field[0][0]) {
		return false;
	}
	unordered_set<state> used;
	vector<state> queue;
	used.insert(start);
	queue.push_back(std::move(start));
	int n = (int) field.size();
	while (!queue.empty()) {
		state cur = std::move(queue.back());
		queue.pop_back();
		for (int dir = 0; dir < 2; dir++) {
			int nRow = cur.row + DR[dir];
			int nCol = cur.column + DC[dir];
			if (nRow >= n || nCol >= n) {
				continue;
			}
			state nState { rotate(cur.cube, dir), nRow, nCol };
			if (field[nRow][nCol] != nState.cube[0]) {
				continue;
			}
			if (nRow + 1 == n && nCol + 1 == n) {
				return true;
			}
			if (used.insert(nState).second) {
				queue.push_back(std::move(nState));
			}
		}
	}
	return false;
}

void solve() {
	int n;
	cin >> n;
	vector<vector<int>> field(n, vector<int>(n));
	unordered_set<int> all_colors;
	for (auto &row : field) {
		for (auto &x : row) {
			cin >> x;
			all_colors.insert(x);
		}
	}
	for (int c1: all_colors) {
		for (int c2: all_colors) {
			for (int c3: all_colors) {
				for (int c4: all_colors) {
					for (int c5: all_colors) {
						for (int c6: all_colors) {
							vector<int> cube { c1, c2, c3, c4, c5, c6 };
							state init { cube, 0, 0 };
							if (path_exists(std::move(init), field)) {
								cout << "Yes\n";
								for (int i = 0; i < 6; i++) {
									if (i > 0) cout << ' ';
									cout << cube[i];
								}
								cout << '\n';
								return;
							}
						}
					}
				}
			}
		}
	}
	cout << "No\n";
}

int main() {
	ios::sync_with_stdio(false); cin.tie(NULL);
	int t;
	cin >> t;
	while (t--) solve();
}
