// Arup Guha
// 5/6/2019
// Solution to 2019 FHSPS Playoff Problem: Peaceful Bishops

import java.util.*;

public class peaceful {
	
	public static int n;
	public static ArrayList<sol> all;
	
	public static void main(String[] args) {
	
		Scanner stdin = new Scanner(System.in);
		int numCases = stdin.nextInt();
		
		// Process each case.
		for (int loop=0; loop<numCases; loop++) {
			
			// Get the input and run the backtracker.
			n = stdin.nextInt();
			int rank = stdin.nextInt()-1;
			all = new ArrayList<sol>();
			ArrayList<Integer> cur = new ArrayList<Integer>();
			go(0, cur);
			
			// Sort the results.
			Collections.sort(all);
			
			// Print out the solution in the desired format.
			int size = all.get(rank).list.size();
			System.out.println(size);
			System.out.print(all.get(rank));
		}
	}
	
	public static void go(int k, ArrayList<Integer> cur) {
		
		// Finished a permutation, add it if it is maximal size.
		if (k == 2*n-1) {
			if (cur.size() == 2*n-2 || (n==1 && cur.size()==1)) {
				all.add(new sol(cur, n));
			}
			return;
		}
		
		// Branch doomed to fail since we only want answers of size 2n-2.
		if (k - cur.size() > 1) return;
		
		// Don't add anything in this diagonal.
		go(k+1, cur);
		
		// Now try adding a bishop at each separate spot on diagonal k.
		for (int r=0; r<=k && r<n; r++) {
			
			// Off the board.
			if (k-r >= n) continue;
			
			// Conflicts with a previous bishop.
			if (!canDo(cur, r, k-r)) continue;
			
			// Try it.
			cur.add(r*n+k-r);
			go(k+1, cur);
			
			// Undo it.
			cur.remove(cur.size()-1);
		}
	}
	
	public static boolean canDo(ArrayList<Integer> cur, int r, int c) {
		
		// See if (r, c) is on any previous bishop diagonal.
		for (Integer me: cur) {
			int x = me/n;
			int y = me%n;
			if (Math.abs(x-r) == Math.abs(y-c)) return false;
		}
		
		// Okay if we get here.
		return true;
	}
}

class sol implements Comparable<sol> {
	
	public int n;
	public ArrayList<Integer> list;
	
	// Make this one in sorted order.
	public sol(ArrayList<Integer> tmp, int myn) {
		list = new ArrayList<Integer>();
		for (Integer x: tmp) list.add(x);
		Collections.sort(list);
		n = myn;
	}
	
	public int compareTo(sol other) {
		
		// This isn't need for this problem, but I am putting longer ones first.
		if (this.list.size() < other.list.size()) return 1;
		if (other.list.size() < this.list.size()) return -1;
		
		// Go through both lists in order.
		for (int i=0; i<list.size(); i++) {
			if (this.list.get(i) != other.list.get(i))
				return this.list.get(i) - other.list.get(i);
		}
		
		// They are the same lists if we get here.
		return 0;
	}
	
	// This is how to do the output for the question.
	public String toString() {
		String res = "";
		for (int i=0; i<list.size(); i++)
			res = res + (list.get(i)/n+1) + " " + (list.get(i)%n+1) + "\n";
		return res;
	}
	
}