#include <stdio.h>
#include <string.h>
int main()
{
   int N,i,len;
   char  nec[20000],*tok,*best,*konec;

   scanf("%d\n",&N);
   for(;N>0;N--)
   {
      scanf("%s\n",nec);
      len=strlen(nec);
      konec=nec+len;
      strcpy(konec+1,nec+1);*konec=*nec;
      tok=best=nec;
      while((*tok==*best)&&(tok<konec))tok++;
      /* " !!! tok<konec */
      for(;tok<konec;)
      {
        	if(*tok>*best)
                {       	tok++;continue;
                }
                if(*tok<*best)
                {       	best=tok;
                                while(*tok==*best&&tok<konec)tok++;
                                /* " !!! tok<konec */
                }
                if(*tok==*best)
                {
                        	i=0;
                                while((*(tok+i)==*(best+i))&&(i<len))i++;
                                if(i==len){tok++;continue;}
                                if(*(tok+i)>*(best+i)){tok++;continue;}
                                if(*(tok+i)<*(best+i))
                                {     best=tok;
                                      while((*tok==*best)&&(tok<konec))tok++;
                                      /* " !!! tok<konec */
                                }
                }
                                
      }
   printf("%d\n",(best-nec)+1);
   
   }
   
   return 0;
}
