#include<stdio.h>#include <string.h>#include <algorithm>using namespace std;char s[20000005];char tmp[20000005];int minP(char s[]){ int l=strlen(s); int i = 0, j = 1, k = 0; while (1) { if (i + k >= l || j + k >= l) break; if (s[i + k] == s[j + k]) { k++; continue; } else { if (s[j + k] > s[i + k]) j += k + 1; else i += k + 1;k = 0; if (i == j) j++; } } return min(i, j);}int main (){ int t; int len; scanf("%d", &t); while (t--) { scanf("%s",s); strcpy(tmp,s); strcat(s,tmp); int ans=minP(s); printf("%dn", ans+1); } return 0;}