const
GOOD_SIZE = 2;
GOOD_SIZE2 = 2;
MEGA_SIZE = 7;
WINNINGS = 1300;
type
int = word;
integer = word;
Arr = array[0..100] of int;
AArr = array[0..100] of Arr;
Var
LAST: integer;
amount: array[0..51] of integer;
ttt, t: longint;
bx, by: Array[0..10000] of int;
tm: AArr;
n, m, c: int;
tot: int = 0;
{}
dx : array[1..4] of shortint = (0, 0, 1, -1);
dy : array[1..4] of shortint = (1, -1, 0, 0);
{}
procedure gen();
var
i, j: int;
begin
writeln('4 90 10');
for i := 1 to 100 do begin
for j := 1 to 90 do write(random(10), ' ');
writeln;
end;
writeln;
end;
{}
procedure big(var a: aarr);
var
used: array[0..100,0..100] of int;
size, left, right: array[0..10000] of int;
yn, k, i, j, temp, max: int;
procedure dfs(u, v: int);
var dir: int;
nx, ny: shortint;
begin
inc(size[k]);
used[u][v] := k;
if v < left[k] then left[k] := v;
if v > right[k] then right[k] := v;
for dir := 1 to 4 do
begin
nx := dx[dir] + u;
ny := dy[dir] + v;
if (ny >= m) or (ny < 0) or (nx >= n) or (nx < a[n][ny]) then Continue;
if used[nx][ny]>0 then Continue;
if a[nx][ny] <> a[u][v] then Continue;
dfs(nx, ny);
end;
end;
begin
while true do begin
k := 0;
yn := 0;
max := 1;
fillchar(used ,sizeof(used), 0);
for j := 0 to m - 1 do
for i := n - 1 downto a[n][j] do
begin
if a[i][j] = LAST then Continue;
if max >= GOOD_SIZE then break;
if used[i][j] > 0 then Continue;
inc(k);
size[k] := 0;
left[k] := j;
right[k] := j;
dfs(i, j);
if (size[k] > max) then begin
yn := k;
max := size[k];
end;
end;
{}
if max < 2 then begin
for j := 0 to m - 1 do
for i := a[n][j] to n - 1 do
begin
if max >= GOOD_SIZE2 then break;
if used[i][j] > 0 then Continue;
inc(k);
size[k] := 0;
left[k] := j;
right[k] := j;
dfs(i, j);
if (size[k] > max) then begin
yn := k;
max := size[k];
end;
end;
end;
if max < 2 then break;
{}
inc(bx[0]);
inc(by[0]);
for j := left[yn] to right[yn] do begin
temp := a[n][j];
a[n][j] := n;
for i := n - 1 downto temp do begin
if used[i][j] <> yn then begin
dec(a[n][j]);
a[ a[n][j] ][ j ] := a[i][j];
end else begin
bx[bx[0]] := i;
by[by[0]] := j;
end;
end;
end;
end;
end;
{}
procedure work();
var i, j, mx: int;
begin
read(n, m, c);
FillChar(amount, sizeof(amount), 0);
last := 0;
mx := 0;
for i := 0 to n - 1 do
for j := 0 to m - 1 do begin
read(tm[i][j]);
tm[n][j] := 0;
inc(amount[tm[i][j]]);
if amount[tm[i][j]] > mx then begin
mx := amount[tm[i][j]];
last := tm[i][j];
end;
end;
bx[0] := 0;
by[0] := 0;
if not((mx >= WINNINGS) or (c <= 7)) then begin
writeln('N');
end else
begin
big(tm);
writeln('Y');
for i := 1 to bx[0] do
writeln(bx[i], ' ', by[i]);
writeln('-1 -1');
end;
end;
{}
begin
read( t );
for ttt := 1 to t do work();
end.
|