import java.util.*;

class Solution
{
  public static void arraySwap(int[] arr, int i, int j) {
    int tmp = arr[i];
    arr[i] = arr[j];
    arr[j] = tmp;
  }

  public static void reverse(int[] arr, int l, int r) {
    for (int i = l, j = r-1; i < (l+r)/2; ++i, --j)
      arraySwap(arr, i, j);
  }

  public static boolean nextPermutation(int[] arr) {
    if (arr.length <= 1) return false;
    int i = arr.length - 1;

    for (;;) {
      int ii = i--;
      if (arr[i] < arr[ii]) {
        int j = arr.length;
        while (!(arr[i] < arr[--j]))
          ;
        arraySwap(arr, i, j);
        reverse(arr, ii, arr.length);
        return true;
      }
      if (i == 0) {
        reverse(arr, 0, arr.length);
        return false;
      }
    }
  }

  public static int compareTo(int[] a, int[] b) {
    for (int i = 0; i < a.length; ++i) {
      if (a[i] > b[i])
        return 1;
      if (a[i] < b[i])
        return -1;
    }
    return 0;
  }

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    int N = sc.nextInt();
    for (int I = 1; I <= N; ++I) {

      int a = sc.nextInt(), b = sc.nextInt();

      int[][] tab = new int[b][a];
      for (int j = 0; j < a; ++j)
        for (int i = 0; i < b; ++i)
          tab[i][j] = sc.nextInt();

      System.out.println("Scenario #" + I + ":");

      int[] rowIndex = new int[b];
      for (int i = 0; i < b; ++i) rowIndex[i] = i;

      int[] colIndex = new int[a];
      for (int i = 0; i < a; ++i) colIndex[i] = i;

      int[] best = null;
      do {
        do {
          int[] currentTableString = new int[a*b];
          for (int j = 0; j < a; ++j)
            for (int i = 0; i < b; ++i) {
              //System.out.println(i + " " + j + " : " + tab[i][j]);
              currentTableString[j*b + i] = tab[rowIndex[i]][colIndex[j]];
            }

          if (best == null || compareTo(best, currentTableString) > 0) {
            best = currentTableString;
            //System.out.println(Arrays.toString(currentTableString));
            //System.out.println(Arrays.toString(colIndex));
            //System.out.println(Arrays.toString(rowIndex));
          }
        } while (nextPermutation(colIndex));
      } while (nextPermutation(rowIndex));

      System.out.print(best[0]);
      for (int i = 1; i < a*b; ++i)
        System.out.print(" " + best[i]);
      System.out.println();
      System.out.println();
    }
  }
}

