# Arup Guha
# 7/22/2014
# Solution to 2014 SI@UCF Final Contest Problem: Meat

def main():

    # Open file.
    myFile = open("meat.in", "r")
    numCases = int(myFile.readline())

    # Go through each case.
    for loop in range(numCases):

        # Easier as a string!
        num = myFile.readline().strip()

        # Not efficient, but this will be very easy.
        allRot = []
        for i in range(len(num)):
            num = num[1:]+num[0]
            allRot.append(num)

        # Sort all rotations and print the first one!
        allRot.sort()
        print(allRot[0])

    myFile.close()

main()
