\(\text{Code}\)
#include <bits/stdc++.h>
using namespace std;
template<typename Tp>
void read(Tp &x) {
x = 0; char ch = getchar(); int f = 0;
for(; !isdigit(ch); f |= (ch == '-'), ch = getchar());
for(; isdigit(ch); x = (x<<3)+(x<<1)+(ch^48), ch = getchar());
}
const int N = 205;
int n, a[N][N], p[N][N], vis[N][N];
struct Network {
const int inf = 300;
int tot, h[N << 2], cur[N << 2], T, dep[N << 2];
struct edge{int to, nxt, w;}e[N * N << 2];
void init() {
tot = 1, T = n + n + 1;
for(int i = 0; i <= T; i++) h[i] = 0;
}
void add(int x, int y) {
e[++tot] = edge{y, h[x], 1}, h[x] = tot;
e[++tot] = edge{x, h[y], 0}, h[y] = tot;
}
queue<int> Q;
int bfs() {
for(int i = 0; i <= T; i++) dep[i] = 0, cur[i] = h[i];
Q.push(0), dep[0] = 1;
while (!Q.empty()) {
int now = Q.front(); Q.pop();
for(int i = h[now]; i; i = e[i].nxt) {
int v = e[i].to;
if (dep[v] || !e[i].w) continue;
dep[v] = dep[now] + 1, Q.push(v);
}
}
return dep[T];
}
int dfs(int x, int lim) {
if (lim <= 0 || x == T) return lim;
int flow = 0;
for(int i = cur[x], v, f; i; i = e[i].nxt) {
cur[x] = i, v = e[i].to;
if (dep[v] != dep[x] + 1 || !e[i].w) continue;
f = dfs(v, min(lim, e[i].w));
if (f <= 0) continue;
e[i].w -= f, e[i ^ 1].w += f, flow += f, lim -= f;
if (lim <= 0) break;
}
return flow;
}
int dinic(){int flow = 0; while (bfs()) flow += dfs(0, inf); return flow;}
void work(int t) {
for(int i = 2; i <= tot; i++) if (!e[i].w && e[i ^ 1].to <= n) {
if (!e[i].to || !e[i ^ 1].to) continue;
int u = e[i ^ 1].to;
for(int j = 1; j <= n; j++)
if (!vis[u][j] && a[u][j] == e[i].to - n){p[u][t] = j, vis[u][j] = 1; break;}
}
}
}G;
int main() {
int t; read(t);
for(; t; --t) {
read(n);
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++) read(a[i][j]), vis[i][j] = 0;
for(int k = 1; k <= n; k++) {
G.init();
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
if (!vis[i][j]) G.add(i, a[i][j] + n);
for(int i = 1; i <= n; i++) G.add(0, i);
for(int j = 1; j <= n; j++) G.add(j + n, G.T);
G.dinic(), G.work(k);
}
printf("%d\n", n * (n - 1) / 2);
for(int i = 1; i <= n; i++)
for(int j = i + 1; j <= n; j++)
printf("%d %d %d %d\n", i, p[i][j], j, p[j][i]);
}
}