// Danny Wasserman
// 7/23/2014
// Solution to 2014 SI@UCF Contest Problem: I Want My Meat

import java.util.Scanner;

public class meat {

  public static void main(String[] args) {

    Scanner in = new Scanner(System.in);
    int cases = in.nextInt();

    // Go through each case.
    while (cases-- != 0) {

      // Read the input.
      String v = in.next();
      int best = Integer.parseInt(v);

      // Try each rotation and save the smallest.
      for (int i = 0; i < v.length(); i++) {
        String pv = v.substring(i + 1) + v.substring(0, i + 1);
        int pval = Integer.parseInt(pv);
        if (pval < best) best = pval;
      }

      // Here is our answer.
      System.out.println(best);
    }
  }
}
