There are three doors in front of you, numbered from $$$1$$$ to $$$3$$$ from left to right. Each door has a lock on it, which can only be opened with a key with the same number on it as the number on the door.There are three keys — one for each door. Two of them are hidden behind the doors, so that there is no more than one key behind each door. So two doors have one key behind them, one door doesn't have a key behind it. To obtain a key hidden behind a door, you should first unlock that door. The remaining key is in your hands.Can you open all the doors?

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 18$$$) — the number of testcases. The first line of each testcase contains a single integer $$$x$$$ ($$$1 \le x \le 3$$$) — the number on the key in your hands. The second line contains three integers $$$a, b$$$ and $$$c$$$ ($$$0 \le a, b, c \le 3$$$) — the number on the key behind each of the doors. If there is no key behind the door, the number is equal to $$$0$$$. Values $$$1, 2$$$ and $$$3$$$ appear exactly once among $$$x, a, b$$$ and $$$c$$$.

For each testcase, print "YES" if you can open all the doors. Otherwise, print "NO".

program aous;
uses wincrt;
var
tt ,x , i: integer;
w : boolean;
A : array[1..3] of integer;
check : array[1..3] of boolean;
begin
    read(tt);
    while(tt > 0) do
    begin
        readln(x);
        w := false;
        for i := 1 to 3 do
        read(A[i]);
        for i := 1 to 3 do
        check[i] := false;
        check[x] := true;
        check[A[x]] := true;
        check[A[A[x]]] := true;
        for i := 1 to 3 do
        if check[i] = false then
        w := true;
         if w = false then
         writeln('YES')
         else
         writeln('NO');
      tt := tt - 1;
    end;
end.

program aous;
uses wincrt;
var
tt ,x , i: integer;
A : array[1..3] of integer;
begin
    read(tt);
    while(tt > 0) do
    begin
        readln(x);
        for i := 1 to 3 do
        read(A[i]);
        if A[x] = 0 then
        writeln('NO')
        else
        begin
        if A[A[x]] = 0 then
        writeln('NO')
        else
        writeln('YES');
        end;
      tt := tt - 1;
    end;
end.

label k;
var l,i,j,t,x:byte;
    a:Array[1..3]of byte;
begin
	readln(t);
	for l:=1 to t do
	 begin
	   readln(x);
	   for i:=1 to 3 do read(a[i]);
	   	for i:=1 to 3 do
	        if (i=x)and(a[i]<>0) then
 			   for j:=1 to 3 do
 			    if (j=a[i])and(a[j]<>0)then begin writeln('YES');goto k end;
 		writeln('NO');
      k:
	 end;
end.

var n,t,i,kt,j:longint;
    a:ARRAY[-1..100] of longint;
begin
        readln(t);
        for i:=1 to t do
                begin
                        readln(n);
                        for j:=1 to 3 do
                                read(a[j]);
                        readln;
                        kt:=1;
                        if a[n]=0 then
                                begin
                                        writeln('NO');
                                        kt:=0;
                                end;
                        if (kt=1) then
                                if (a[a[n]]=0) then
                                begin
                                        writeln('NO');
                                        kt:=0;
                                end;
                        if kt=1 then writeln('YES');
                end;
end.

var t,x:integer;
    a:array[1..3] of byte;
begin

    readln(t);
    if (t>=1) and (t<=18) then
    while (t<>0) do
      begin
        readln(x);
          if (x>=1) and (x<=3) then
            begin
              read(a[1],a[2],a[3]);
              if (a[1]>=0) and (a[1]<=3) and (a[2]>=0) and (a[2]<=3) and (a[3]>=0) and (a[3]<=3) then
                if (x=0) or (a[x]=0) or (a[a[x]]=0) then writeln('NO') else writeln('YES');
             end;
         t:=t-1;
      end;

end.

{***
  Educational Codeforces Round 132
  Problem A
*}
program Three_Doors;
var t:integer;
    a:array[1..3]of byte;

procedure solve;
var x,i:integer;
begin
  read(x);
  for i:=1 to 3 do read(a[i]);
  for i:=1 to 2 do
  begin
    if a[x]=0 then
    begin
      writeln('NO');
      exit;
    end;
    x:=a[x];
  end;
  writeln('YES');
end;

begin
  read(t);
  while t>0 do
  begin
    solve; dec(t);
  end;
end.

program p1709a;
var  a : array[1..3] of integer;
     door : array[1..3] of boolean;
     t : integer;
     x : integer;

BEGIN
    readln(t);
    while t >= 1 do
     begin
         dec(t);
         fillchar(door, sizeof(door), false);
         readln(x);
         readln(a[1], a[2], a[3]);
         while (door[1] = false) or (door[2] = false) or (door[3] = false) do
           begin
               if x = 0 then break;
               door[x] := true;
               x := a[x];
           end;
         if (door[1] = false) or (door[2] = false) or (door[3] = false)
         then writeln('NO')
         else writeln('YES')
     end;
END.

var t, i, x, j: longint;
    a:array[1..3] of longint ;
check : string;
begin
readln(t);
for i := 1 to t do
    begin
        check := 'YES';
        readln(x);
        for j := 1 to 3 do read(a[j]);
        if a[x] = 0 then check := 'NO';
        if a[a[x]] = 0 then  check := 'NO';
        writeln(check);
    end;
end.

 program firstprog;
    var j,i,t,k:integer;
     A: array[1..3] of integer;
      begin
       Readln(t);
        for k:= 1 to t do begin
            Readln(j);
             for i:=1 to 3 do begin
                 Read(A[i]);

              end; // reading array

             if A[j] = 0 then writeln('NO')
              else
              if A[A[j]] = 0 then writeln('NO')
               else  writeln('YES');

         end; // for k:= 1 to t do end

      end.

program Three_Doors;
var
    ntc: int8;
    x, n: int8;
    a: array [1 .. 3] of int8;

begin
    readln(ntc);
    while ntc > 0 do begin
        dec(ntc);

        readln(x);
        readln(a[1], a[2], a[3]);

        n := 0;
        while x > 0 do begin
            x := a[x];
            inc(n);
        end;

        if n = 3 then
            writeln('YES')
        else
            writeln('NO');

    end;
end.

procedure solve;
var n:longint;
    a:array[0..3]of longint;
begin
  a[0]:=0;
  readln(n,a[1],a[2],a[3]);
  if a[a[n]]<>0 then writeln('YES')
  else writeln('NO');
end;
var _t:longint;
begin
  readln(_t);
  for _t:=1 to _t do
    solve;
end.

var a:array [1..3] of int64;
t,i,j,x:int64;
begin
  readln(t);
  for i:=1 to t do
  begin
    readln(x);
    for j:=1 to 3 do
      read(a[j]);
    if (a[x]<>0) and (a[a[x]]<>0) then writeln('YES')
    else writeln('NO');
  end;
end.

            const eps=1e-10;
   besk=1800000000000000000; label 33,55,5,4,1,3,2,44,22;
   type
   mas=array[-250000..1570460+1] of int64;
   integer=longint;   zap=record  p1,p2,p3:int64; end;
   zap1=^zap;
   var dlina,newnap,nnap,nap,m,pos,kolshagov,kk,hh,koled,px,py,posx,posy,cur,init,res,oldpos,i1,j1,mask,stepp,n2,i,j,k,l,step,k1,mesto:longint;
   sss:array[-1..1000000] of string;
   ch1,ch2:char;
   ss,ssans,ss1,ss4,oldss,smin,ss3,ss2:string;
   newaans,aans:zap;
   fx,fy,koef,dfy,dfx,coef,mc,xxc,yyc,zzc,otr1,otr0,vx,vy,ddist,tttt:extended;
   pqqq:zap;
   kolzapr,sumtek,time,dx,dy,mexx,diff,jmin,summax,summin,znach,sum,start,start1,kol1,raz,raz1:int64;
   xp,yp,almax,almin,gam,al0,otal,al1,al2,acc, disk,prop,bet,al:extended; fl1,fl2,fl:boolean;
   dr3,dr1,dr2,pt,ott1,ott2:zap;
   dans,sumyy,sumxx,sumxy:array[-10..500000] of extended;
   p1,p2,ans,tt,ii,pp,ymax,ymin,xmax,xmin,sumx,sumy,w,pov,sumost,gr,oldsum,amin,amax,ansmax,kolgor,kolver,oldcc,finish,p3,p4,stoimost,kolodin,maxsum,i3,p,pp1,pp2,root,par,dlkruga,topt,t3,ot,cc,znak,rr,kolmax,kkol,pq,razm,mm,tip,ppos1,oldtt,ps,pos1,pos2,pred,color,buk:int64;
   nomer,napx,napy,kol,z,stepen1,stepen,xnewoch,ynewoch,xoch,yoch,strx,stry,ppole,y,dold,d,poryadok,num,zz:array[-200000..1110000] of int64;
   stroka,sss1,sss2:array[-10..1610000] of string;
   xc,yc,zc,v3,v1,v2,pointx,pointy,vel,tt0,radius,ote,dl1,dl2:extended;
   dist,dd1,dnewr,dnewl,dr,ddpos:array[-1..50002,-2..500] of int64;
   firstvh,lastvh,sootv,nomermax,left,right,poss,zzz,x,d0,d1,str,zx,zy,xxx,yyy,invfact,ansx,ansy,ugol:array[-300000..1111000] of int64;
   t1,t2,tx,ty,delit,dl,ll,ost,shag,ppos,jj,t,ptek,dloch1,oldaa,oldbb,zapas,ott,mnog,ttt,delta,tt1,tt2,koll,newt,osn,oldot,oldt,znmax,opld,t4,iii,jjj,aaa,bbb,ccc,p0,newpos,newdl,len,pp3,ans1,ans2,modul,t0,dloch,koldel,mmax,period, dolg,ot1,ot2,oldgr,predsum,pmin,tmin,kool,number,imin,qq,n,numpos,down,imax,qq1,qq2,q1,q2,pmax,pmax2,pos0,maxot,tmax,nn,n1,q:int64; flag1:boolean;
   xx,yy,olda,b,c,a,fact,stan,a2,invx,invy,place,b2,sum1,sum0:mas; flag2,flag:boolean;
   aa,bb,pleft,pright,kol2,kolminus,pos3,gor,ver,i2,j2,sdvig,izb, nextp,razn,ppp,minx,miny,maxx,maxy,jmax,grr,ener,dob:int64;
   zan1,zan2,prost,zan:array[-1000..20001001] of boolean;
   zzan,newzan,zann:array[-1000000..4000000] of boolean;
   zanx,marked:array[-1000000..4100000] of boolean;
   s1,s2:array[-1..1000000] of char;
   dmax,dd,est,nado,z1,z2,x1,y1,x2,y2,ddnew,d3,dmin,dnomer:array[-211100..1100000] of int64;
   ochh,newochh:array[-1000..1000000] of longint;
   ba1,ba2,next0,next1,newx,newy,dplus,dminus,och1,b1,a1,first,last,newoch,och,lx,a3,b3,predok:array[-20000..1610000] of int64;
   ddd:array[-1..500001,0..3] of longint;
   dinleft,din,nextt,polenext,pole,newpole:array[-1..1300,-1..1305] of int64;
   ddmax,ddmin,dd2:array[-100..1300,-100..1300] of int64;
   dd3:array[-10..130,-10..130,-10..130] of int64;
   ssan,bufer:array[-1000..2000000] of char;
   cosa,sina,cosb,sinb,rx,ry,h1,h2,h,l1,l2,otmax,v:int64;
   obmen,ch:char;
   flagg,fflag,ok,flagg1:boolean;
   dmaxchet,dmaxnechet,oldx,oldy,numx,numy,xleft,xright,s,newplace,newstr,next,prev,aold:array[-1000..220002] of int64;
   function srav(i,j:longint):boolean;
     var k,tt:longint;
       begin
          if (a[i]<a[j])or(a[i]=a[j])and(a1[i]<a1[j]) then begin srav:=true;   end else srav:=false;
       end;
   procedure sl (k,l,m:integer);
  begin
        i:=l;j:=m; flag:=true; step:=0;while flag do begin
        if (srav(i,j)) then begin begin b[l+step]:=a[i];b1[l+step]:=a1[i];b2[l+step]:=a2[i];b3[l+step]:=a3[i];end;
        inc(step);inc(i);
      end;
        if (not srav(i,j)) then begin begin b[l+step]:=a[j];b1[l+step]:=a1[j];b2[l+step]:=a2[j];b3[l+step]:=a3[j];end;
        inc(step);inc(j);end;
        if  ((i>=l+k) or (j>=m+k)) then flag:=false;
       end;
      if(i>=l+k) then repeat b[l+step]:=a[j];b1[l+step]:=a1[j];b2[l+step]:=a2[j]; b3[l+step]:=a3[j];
      inc(step);inc(j);until (j>=m+k);
      if(j>=m+k) then repeat b[l+step]:=a[i];b1[l+step]:=a1[i];b2[l+step]:=a2[i];  b3[l+step]:=a3[i];inc(step);inc(i);until (i>=l+k);
  end;

  procedure sort(var c:mas);
   begin
    k:=1; repeat
    l:=1;m:=k+l; repeat sl(k,l,m); l:=l+2*k;m:=m+2*k; until (l>=n); k:=k*2;
    for i:=1 to n do begin a[i]:=b[i];a1[i]:=b1[i];a2[i]:=b2[i];a3[i]:=b3[i]; end;
    until (k>=n);
   end;

 function min (a,b:int64):int64;begin min:=b;if a<b then min:=a;  end;
 function max (a,b:int64):int64; begin max:=b;if a>b then max:=a;  end;
 function evkl(a,b:int64):zap1; var d,x,y,r,q,i,j,k,x1,x2,y1,y2:int64;
   tt:zap; tt1:zap1;  fl:boolean;
   begin
      fl:=false;
      if a<b then begin r:=a;a:=b;b:=r; fl:=true; end;
      x2:=1;x1:=0;y2:=0;y1:=1;
      while b>0 do
      begin q:=a div b; r:=a mod b; x:=x2-q*x1;y:=y2-q*y1;
      a:=b;b:=r;x2:=x1;x1:=x;y2:=y1;y1:=y;
      end;
      if not fl then begin d:=a; x:=x2;y:=y2; tt.p1:=d;tt.p2:=x;tt.p3:=y; tt1:=@tt; end else
       begin d:=a; x:=x2;y:=y2; tt.p1:=d;tt.p2:=y;tt.p3:=x; tt1:=@tt; end;
       evkl:=tt1;
   end;
   function phi(a,b:double):double; label 222;var p:double;
     begin
        if (a>0)and (b>=0) then begin p:=arctan(b/a);goto 222; end;
        if (a<0) then begin p:=pi+arctan(b/a);goto 222; end;
        if (a>0)and (b<0) then begin p:=2*pi+arctan(b/a);goto 222; end;
        if (a=0)and (b>0) then begin p:=pi/2;goto 222; end;
        if (a=0)and (b<0) then begin p:=3*pi/2;goto 222; end;
       222: phi:=p;
     end;

   function nod(a,b:int64):int64; var p,t:int64;
    begin
     a:=abs(a); b:=abs(b);
     if (a>0)and(b>0) then
     begin while (b>0) do begin t:=a mod b;a:=b;b:=t;    end;
     p:=a;
     end else if a=0 then p:=b else p:=a;
     nod:=p;
    end;

function nok(aa,bb:int64):int64;
var tt:int64;
begin tt:=nod(aa,bb); ot:=(aa*bb) div tt; nok:=ot;  end;

 function arccos(tt:extended):extended;
    begin
      if abs(tt)>1-1e-13 then
      if tt>0 then  arccos:=0 else arccos:=pi;
      if tt=0 then arccos:=pi/2 else
      if abs(tt)<=1-1e-13 then if tt>=0 then arccos:=arctan(sqrt(1-tt*tt)/tt)
      else arccos:=pi+arctan(sqrt(1-tt*tt)/tt);
    end;

    function degg(a,k,modul:int64):int64; var p:int64;    label 4;
      begin
         if k<=0 then degg:=1
         else if odd(k) then degg:=(degg(a,k-1,modul)*a) mod modul  else
         begin  p:=degg(a,k div 2,modul);  degg:=(p*p) mod modul;    end;
      end;

{  function zaprmax(left,right:longint):int64;
   begin
    if left>=right then zaprmax:=y[left] else
    begin
     if not odd(left) and odd(right) then zaprmax:=zaprmax(left shr 1,right shr 1) else
       begin
        if odd(left) then zaprmax:=max(zaprmax(left+1,right),y[left]);
        if not odd(right) then zaprmax:=max(zaprmax(left,right-1),y[right]);
       end;
    end;
   end;     }

            function zaprmin(left,right:longint):int64;
              begin
               if left>=right then zaprmin:=z[left] else
                begin
                 if not odd(left) and odd(right) then zaprmin:=zaprmin(left shr 1,right shr 1) else
                  begin
                   if odd(left) then zaprmin:=min(zaprmin(left+1,right),z[left]);
                   if not odd(right) then zaprmin:=min(zaprmin(left,right-1),z[right]);
                  end;
                end;
              end;

   function zaprsum(left,right:longint):int64;
      begin
         if left>=right then if left=right then zaprsum:=y[left] else zaprsum:=0 else
          begin
                 if not odd(left) and odd(right) then zaprsum:=zaprsum(left shr 1,right shr 1) else
               begin
                 if odd(left) then zaprsum:=zaprsum(left+1,right)+y[left];
                 if not odd(right) then zaprsum:=zaprsum(left,right-1)+y[right];
               end;
          end;
      end;

  function zaprsum1(left,right:longint):int64;
      begin
         if left>=right then if left=right then zaprsum1:=yy[left] else zaprsum1:=0 else
          begin
                 if not odd(left) and odd(right) then zaprsum1:=zaprsum1(left shr 1,right shr 1) else
               begin
                 if odd(left) then zaprsum1:=zaprsum1(left+1,right)+yy[left];
                 if not odd(right) then zaprsum1:=zaprsum1(left,right-1)+yy[right];
               end;
          end;
      end;

   procedure udal(ii:longint);
  begin  next[prev[ii]]:=next[ii];prev[next[ii]]:=prev[ii];  end;

   function pprost(pp:int64):boolean;
     var i,j,t:longint; tt:extended; fl:boolean;
       begin
          tt:=pp;
          tt:=sqrt(tt);
          fl:=true;
          for i:=2 to round(tt) do if pp mod i=0 then  fl:=false;
          pprost:=fl;
       end;

                function gl(c:char):boolean;
                   begin
                   if (c='a')or (c='o')or(c='e')or(c='i')or(c='u') then gl:=true else gl:=false;
                   end;
            {        function slog(d1,d2:zap):zap1;
                    var t,tt,p,q:int64; ans:zap;
                 begin
                    p:=d1.x*d2.y+d1.y*d2.x;
                    q:=d1.y*d2.y;
                    t:=nod(p,q);
                    p:=p div t; q:=q div t;
                    ans.x:=p mod modul;ans.y:=q mod modul;
                    slog:=@ans;
                 end;

                function umn(d1,d2:zap):zap1;
                 var t,tt,p,q:int64; ans:zap;
                 begin
                  p:=d1.x*d2.x;
                  q:=d1.y*d2.y;
                  t:=nod(p,q);
                  p:=p div t; q:=q div t;
                  ans.x:=p mod modul;ans.y:=q mod modul;
                  umn:=@ans;
                 end;

                  function sravfr(d1,d2:zap):boolean;
                    begin
                     if  d1.x*d2.y<d2.x*d1.y then sravfr:=true else sravfr:=false;
                    end;
                      }
  function bincoef(ii,jj:longint):int64;
      var t1,t2,t3,p1,p2,p3,ot:int64;
      begin
           if ii>=jj then
             begin
              p1:=fact[ii];
              p2:=fact[jj];
              p3:=fact[ii-jj];
              t2:=degg(p2,modul-2,modul);
              t3:=degg(p3,modul-2,modul);
              ot:=((p1*t2) mod modul*t3) mod modul;
              bincoef:=ot;
            end  else bincoef:=0;
      end;

function vhodit(znach:int64):int64;
var ans,aa,bb,cc,i:longint; flag:boolean;
begin
  aa:=1; bb:=tt; cc:=aa;  ans:=-100;
  while abs(aa-bb)>1 do
    begin
       cc:=(aa+bb) shr 1;
       if xxx[cc]<=znach then aa:=cc else bb:=cc;

    end;
 { writeln(cc);
}
  flag:=false;
 cc:=max(0,cc-3); repeat inc(cc);   until (xxx[cc]>=znach)or(cc>tt);
  if (xxx[cc]=znach)and(cc<=tt) then ans:=cc;
  vhodit:=ans;
end;

function maxnomer(znach:int64):int64;
var ans,aa,bb,cc,i:longint; flag:boolean;
begin
  aa:=1; bb:=p; cc:=aa;  ans:=-100;
  while abs(aa-bb)>1 do
    begin
       cc:=(aa+bb) shr 1;
       if x[cc]<=znach then aa:=cc else bb:=cc;

    end;
 { writeln(cc);
}
  flag:=false;
 cc:=min(p+1,cc+3); repeat dec(cc);   until (x[cc]<=znach)or(cc<0);
  if (x[cc]<=znach)and(cc>=0) then ans:=cc;
  maxnomer:=ans;
end;

 begin

   { assign (input,'D:\Projects\input.txt');reset(input);
    assign (output,'D:\Projects\output.txt'); rewrite(output);
    }
 readln(qq);

  for stepp:=1 to qq do

  begin

     readln(pos);
     for i:=1 to 3 do read(x[i]);
      tt:=0;
      while pos<>0 do begin inc(tt); pos:=x[pos]; end;
    {  writeln('tt= ',tt);
    }  if tt=3 then writeln('YES') else writeln('NO');

  2:
  end;

1: close (output);

 end.