public static void main(String[] args) throws Throwable{
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		Comparator<int[]> comp = new Comparator<int[]>() {
			public int compare(int[] o1, int[] o2) {
				if(o1[0]<o2[0])return -1;
				if(o1[0]>o2[0])return 1;
				return -1;
			}
		};
		StringBuffer sb = new StringBuffer();
		for (int i = 0, C = parseInt(in.readLine().trim()); i < C; i++) {
			int N = parseInt(in.readLine().trim());
			arr = new int[N][];
			int []sol = new int[N];
			for (int j = 0; j < N; j++) {
				StringTokenizer st = new StringTokenizer(in.readLine().trim());
				arr[j] = new int[]{parseInt(st.nextToken()), parseInt(st.nextToken()), j};
			}
			mem = new Point[N][6][6];
			Arrays.sort(arr, comp);
			sb.append(function(0,10,10) + "\n");
			int valIzq = 10, valDer = 10;
			for (int j = 0; j < sol.length; j++) {
				sol[arr[j][2]] = mem[j][valIzq/10][valDer/10].y;
				if(mem[j][valIzq/10][valDer/10].y == 1)valIzq = arr[j][1];
				else if(mem[j][valIzq/10][valDer/10].y == 2)valDer = arr[j][1];
			}
			for (int j = 0; j < sol.length; j++)
				sb.append(sol[j] + "\n");
		}
		System.out.print(new String(sb));
	}