type Typ = record
    token : integer;
    back : longint;
  end;
 
var textik : array [1..330000] of Typ;
    pos, a, l : integer;
    t : integer;
    poc : integer;
    i, ii : integer;
    max : longint;

begin
  readln(poc);

  for ii:=1 to poc do
   begin
     readln(l,a);
     t:=0; pos:=0;
     while (a>=0) and (l>=1) do
      begin
        inc(t);
	for i:=1 to l do textik[pos+i].token:=t;
	pos:=pos+l;
	for i:=1 to a do inc(textik[pos+i].back);
	readln(l,a);
      end;

     t:=textik[1].token;
     max:=0;  i:=1;
     writeln('Zadani ',ii,':');

     while (i<=pos) do
      begin
        if textik[i].token = t then
	  begin
	   if textik[i].back > max then max:=textik[i].back;
	   textik[i].back:=0;
	   textik[i].token:=0;
	   inc(i);
	  end
        else
	 begin
	   t:=textik[i].token;
	   writeln(max);
	   max:=0;
	 end;
      end;
     for i:=1 to 33000 do  
      begin
       textik[i].token:=0;
       textik[i].back:=0;
       
      end;
     writeln(max);
     writeln;
   
   end;
end.
	
