#include <iostream>
#include <time.h>

using namespace std;

int main(){
	int T, n, q;
	int depth[100000];
	int max_depth;
	bool used[100000];

	srand(time(NULL));	

	// Read number of test cases
	cin >> T;
	cout << T << endl;
	for (int cnt=1; cnt<=T; ++cnt){
		// Read n and q
		cin >> n >> q;
		if (n>100000)
			cout << "N is too large";
		else {
			cout << n << " " << q << endl;
			
			// Generate the tree, maintain max_depth for next part
			depth[0] = 0;
			max_depth = 0;
			for (int i=1; i<n-1; ++i){
				int p = rand() % i;
				cout << p << " ";  
				depth[i] = depth[p]+1;
				if (max_depth<depth[i])
					max_depth = depth[i];
			}
			cout << rand () % (n-1) << endl;

			// Generate queries
			memset(used, 0, sizeof(used));
			for (int i=0; i<q; ++i){
				int d;
				// Avoid asking for the same d as far as possible
				do {
					d = rand() % (max_depth+2);
				} while (used[d] && i<=max_depth+1);
				if (i!=q-1)
					cout << d << " " ;
				else 
					cout << d << endl;
				used[d] = true;
			}
		}
	}
}
