1 var
2 i,j,k,l,m,n,aug,jl,mi,tmp,ans,vi,vx:longint;
3 flag:boolean;
4 a:array[0..500,0..500] of longint;
5 di,dis,his,pre,vh:array[0..1000] of longint;
6 function max(x,y:longint):longint;
7 begin
8 if x>y then max:=x else max:=y;
9 end;
10 function min(x,y:longint):longint;
11 begin
12 if x<y then min:=x else min:=y;
13 end;
14 begin
15 readln(vx);
16 for vi:=1 to vx do
17 begin
18
19 readln(n);
20 fillchar(a,sizeof(a),0);
21 for i:=1 to n do
22 begin
23 a[1,i+1]:=1;
24 a[n+1+i,n*2+2]:=1;
25 end;
26 for i:=1 to n do
27 begin
28 for j:=1 to n do
29 begin
30 read(k);
31 a[1+i,n+1+j]:=k;
32 end;
33 readln;
34 end;
35 fillchar(dis,sizeof(dis),0);
36 fillchar(his,sizeof(his),0);
37 fillchar(vh,sizeof(vh),0);
38 for i:=1 to n*2+2 do
39 di[i]:=1;
40 vh[0]:=n*2+2;
41 aug:=maxlongint;
42 i:=1;ans:=0;
43 while dis[1]<(n*2+2) do
44 begin
45 flag:=false;
46 his[i]:=aug;
47 for j:=di[i] to n*2+2 do
48 begin
49 if (a[i,j]>0) and ((dis[i]-1)=dis[j]) then
50 begin
51 aug:=min(aug,a[i,j]);
52 pre[j]:=i;
53 di[i]:=j;
54 i:=j;
55 if i=(2*n+2) then
56 begin
57 ans:=ans+aug;
58 while i<>1 do
59 begin
60 tmp:=i;
61 i:=pre[i];
62 a[i,tmp]:=a[i,tmp]-aug;
63 a[tmp,i]:=a[i,tmp]+aug;
64 end;
65 aug:=maxlongint;
66 end;
67 flag:=true;
68 break;
69
70 end;
71 end;
72 if flag then continue;
73 jl:=-1;mi:=2*n+1;
74 for j:=1 to 2*n+2 do
75 begin
76 if (dis[j]<mi) and (a[i,j]>0) then
77 begin
78 mi:=dis[j];
79 jl:=j;
80 end;
81 end;
82 di[i]:=jl;
83 dec(vh[dis[i]]);
84 if vh[dis[i]]<=0 then break;
85 dis[i]:=mi+1;
86 inc(vh[dis[i]]);
87 if i<>1 then
88 begin
89 i:=pre[i];
90 aug:=his[i];
91 end;
92 end;
93 if ans=n then writeln(‘Yes‘) else writeln(‘No‘);
94
95 end;
96 end.
97