var
t,n,time,i:longint;
ans,s,now:ansistring;
function min(a,b:ansistring):ansistring;
var
i:longint;
begin
for i:=1 to n do
begin
if a[i]<b[i] then exit(a);
if a[i]>b[i] then exit(b);
end;
end;
begin
assign(input,'bob.in'); reset(input);
assign(output,'bob.out'); rewrite(output);
readln(t);
for time:=1 to t do
begin
readln(s);
n:=length(s);
ans:=s;
for i:=2 to n do
begin
now:=copy(s,1,n-i+1);
now:=copy(s,n-i+2,i-1)+now;
ans:=min(ans,now);
end;
writeln(ans);
end;
close(input);
close(output);
end.