public static void main(String[] args) throws IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		String line = "";
		StringBuilder out = new StringBuilder();
		while ((line = in.readLine()) != null && line.length() != 0) {
			int n = Integer.parseInt(line.trim());
			for (int i = 0; i < n; i++) {
				line = in.readLine().trim();
				int size = line.length();
				s = (line + line).toCharArray();
				int[] sa1 = suffixArray();
				for (int j = 0; j < sa1.length; j++) {
					if (sa1[j] < size) {
						for (int k = 0; k < size; k++)
							out.append(s[sa1[j] + k]);
						break;
					}
				}
				out.append("\n");
			}
		}
		System.out.print(out);
	}