//prewritten code: https://github.com/antma/algo
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.traits;
import std.random;

int ri () {
  return readln.stripRight.to!int;
}

string slow (string z, int k) {
  auto s = z.dup;
  for (int i = 0; i + k <= s.length; ++i) {
    reverse (s[i .. i + k]);
  }
  return s.idup;
}

void research (int k) {
  string p = "1234567890";
  foreach (i; 1 .. k + 1) {
    stderr.writeln (slow (p[0 .. k], i));
  }
}

void reseach0 () {
  research (5);
  stderr.writeln;
  research (6);
  stderr.writeln;
  research (7);
  stderr.writeln;
  research (9);
}

void main() {
  debug reseach0 ();
  const nt = ri ();
  foreach (tid; 0 .. nt) {
    const n = ri ();
    const s = readln.stripRight;
    auto z = new byte[2*n];
    foreach (i; 0 .. 2*n) z[i] = s[i%n].to!byte;
    int best = n - 1;
    byte[] ans = z[0 .. n].dup;
    reverse (ans);
    auto u = new byte[n];
    foreach_reverse (k; 0 .. n - 1) {
      u[] = z[k .. k + n][];
/*
123456 0
234561 1
345612 2
456321 3
561234 4
654321 5

123456789 0
234567891 1
345678921 2
456789123 3
567894321 4
678912345 5
789654321 6
891234567 7
987654321 8
*/
      if ((k & 1) != (n & 1)) {
        reverse (u[n - k .. n]);
      }
      if (cmp (u, ans) <= 0) {
        ans = u.dup;
        best = k;
      }
    }
    foreach (i; ans) {
      write(i.to!char);
    }
    writeln;
    writeln (best+1);
  }
}

