var s:ansistring; cas:longint; function minp(st,len:longint):longint; var p1,p2,k,tmp:longint; begin p1:=0; p2:=1; k:=0; while ((p1<len)and(p2<len)and(k<=len)) do begin tmp:=ord(s[st+(p1+k) mod len])-ord(s[st+(p2+k) mod len]); if (tmp=0) then inc(k) else begin if (tmp<0) then p2:=p2+k+1 else p1:=p1+k+1; if (p1=p2) then inc(p2); k:=0; end; end; if (p1>p2) then exit(p2+st) else exit(p1+st); end; begin readln(cas); while cas>0 do begin dec(cas); readln(s); writeln(minp(1,length(s))); end; end.