uses crt;
var x : array[1..2, 1..1006] of integer;
    goredole : array[0..1005] of record a, b : integer; end;
    bio : array[0..1005] of boolean;
    i, j, q, n, tmp : integer;
    t : longint;
    f, fout : text;

function solve : boolean;
var i, j, prvi, drugi, prvicur, drugicur : integer;
begin
  for i:=n+1 to n+5 do
      begin goredole[i].a:=1; goredole[i].b:=2; end;

  for i:=0 to n+5 do bio[i]:=True;
  t:=1; prvicur:=1; prvi:=1; drugi:=2; drugicur:=0;

  while t<1010 do
    begin
    for i:=1 to prvicur do
        begin
        tmp:=x[prvi, i];
        for j:=-5 to 5 do
            if (tmp+j>=0) and
               (bio[tmp+j]) and
               ((t-1) mod goredole[tmp+j].a < goredole[tmp+j].b) then
               begin
               inc (drugicur);
               x[drugi, drugicur]:=tmp+j;
               bio[x[drugi, drugicur]]:=False;
               if x[drugi, drugicur]>n then begin solve:=True; exit; end;
               end;
        end;

    prvicur:=drugicur; prvi:=drugi; drugicur:=0; inc (t);
    if prvi=1 then drugi:=2 else drugi:=1;
    for i:=1 to prvicur do bio[x[prvi, i]]:=True;
    end;

  solve:=False;
end;

begin
  assign (f, 'RIV.IN'); reset (f);
  assign (fout, 'RIV.OUT'); rewrite (fout);

  read (f, q);
  goredole[0].a:=1; goredole[0].b:=2;

  for i:=1 to q do
      begin
      read (f, n);
      for j:=1 to n do
          begin
          read (f, goredole[j].b, goredole[j].a);
          inc (goredole[j].a, goredole[j].b);
          end;
      if solve then writeln (fout, t)
               else writeln (fout, 'NO');
      end;

  close (fout);
end.
