// Arup Guha
// Edited on 7/20/06 for 2006 BHCSI Mock Contest #2
// Originally written as a solution in C++ for a UCF Programming Team
// practice problem.
import java.util.*;
import java.io.*;

public class perm {

	public static void main(String[] args) throws IOException {

  		Scanner fin = new Scanner(new File("perm.in"));

  		
		// Read in the number of input cases.
  		int numlines = fin.nextInt();
  		
  		// Process each case.
  		for (int i=0; i<numlines;i++) {
    		int n,k;
    		n = fin.nextInt(); 
    		k = fin.nextInt();
    		int[] perm = new int[10];
   
   			// Set up permutation #1.
    		for (int j=0; j<n;j++)
      			perm[j] = j+1;
 
 			// Subtract out of n until you reach 1.
    		while (n > 1) {
      
      			// This calculates the value of the leading digit left
      			// to print.
      			int curplace = (k-1)/fact(n-1);
      			System.out.print(perm[curplace]+",");
      			
      			// Copy over the number just placed in the permutation.
      			for (int j=curplace; j<n-1; j++)
        			perm[j] = perm[j+1];
        			
        		// This "advances" our counter the appropriate number of spaces.
      			k -= curplace*fact(n-1);
      			
      			// One fewer numbers to permute the next time around.
      			n--;     
    		}
    		System.out.println(perm[0]);
  		}  
  		fin.close();
	}

	// Regular factorial method...
	public static int fact(int n) {
  		int prod = 1;
  		for (int i=2; i<=n; i++)
    		prod *= i;
  		return prod;
	}

}
