# Arup Guha
# 2/9/2026
# Solution to Name Tag Problem

# Get # of cases.
n = int(input())

# Go through cases.
for loop in range(n):

    # Read string.
    s = input().strip()

    # Initial answer.
    res = s

    # Rotate to each position.
    for i in range(len(res)):
        s = s[1:] + s[0]

        # Update if better.
        if s < res:
            res = s

    # Ta da!
    print(res)
