Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.

The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).

If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.

NoteIn the first sample 4 = 2 + 2, a = 2 is the only possibility.In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.It's easy to check that there is no suitable a in the third sample.In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer.

var str = readline();
var len = str.length;

function work(type, beg, end) {
    var arr = new Array(len);
    for (var i = 0; i < len; ++i) {
        arr[i] = Number(str[i]);
    }
    var ans = new Array(len);
    var down = false;

    while (true) {
        if (down) {
            if (arr[end] !== 0) {
                --arr[end];
                down = false;
            } else {
                arr[end] = 9;
                down = true;
            }
        }
        if (beg === end) {
            if (down) {
                return false;
            }
            if (type === 1) {
                if (arr[end] % 2 === 0) {
                    ans[end] = String(arr[end] / 2);
                    return ans;
                } else {
                    return false;
                }
            } else {
                if (arr[end] % 2 === 0) {
                    ans[end] = String((arr[end] + 10) / 2);
                    return ans;
                } else {
                    return false;
                }
            }
        }
        if (beg +1 === end) {
            if (down) {
                if (arr[beg] !== 0) {
                    --arr[beg];
                    down = false;
                } else {
                   arr[beg] = 9;
                    down = true;
                }
            }
            if (down) {
                if (type === 1) {
                    return false;
                } else {
                    ans[beg] = "9";
                    ans[end] = "0";
                    return ans;
                }
            }
            if (type === 1) {
                if (arr[beg] === arr[end]) {
                    ans[beg] = String(arr[end]);
                    ans[end] = "0";
                    return ans;
                } else {
                    return false;
                }
            } else {
                if (arr[beg] === arr[end] + 1) {
                    ans[beg] = "9";
                    ans[end] = String(arr[end] + 1);
                    return ans;
                } else {
                    return false;
                }
            }
        }
        if (type === 1) {
            if (arr[beg] === arr[end]) {
                type = 1;
                ans[beg] = String(arr[end]);
                ans[end] = "0";
                ++beg, --end;
            } else if (arr[beg] === arr[end] + 1) {
                type = 2;
                ans[beg] = String(arr[end]);
                if (beg === 0 && ans[beg] === "0") {
                    return false;
                }
                ans[end] = "0";
                ++beg, --end;
            } else {
                return false;
            }
        } else {
            if (arr[beg] === arr[end]) {
                if (arr[beg] !== 9) {
                    type = 1;
                    ans[beg] = "9";
                    ans[end] = String(arr[end] + 1);
                    down = true;
                    ++beg, --end;
                } else {
                    return false;
                }
            } else if (arr[beg] === arr[end] + 1) {
                type = 2;
                ans[beg] = "9";
                ans[end] = String(arr[end] + 1);
                down = true;
                ++beg, --end;
            } else if (arr[beg] === 0 && arr[end] === 9) {
                type = 2;
                ans[beg] = "9";
                ans[end] = "0";
                ++beg, --end;
            } else {
                return false;
            }
        }
    }
}

var ans = work(1, 0, len - 1);
if (ans) {
    print(ans.join(""));
} else {
    if (str[0] === "1") {
        ans = work(2, 1, len - 1);
        if (ans) {
            print(ans.join(""));
        } else {
            print(0);
        }
    } else {
        print(0);
    }
}

var str = readline();
var len = str.length;

function work(type, beg, end) {
    var arr = new Array(len);
    for (var i = 0; i < len; ++i) {
        arr[i] = Number(str[i]);
    }
    var ans = new Array(len);
    var down = false;

    while (true) {
        if (beg > end) {
            if (type === 1 && !down || type === 2 && down) {
                return ans;
            } else {
                return false;
            }
        }
        if (down) {
            if (arr[end] !== 0) {
                --arr[end];
                down = false;
            } else {
                arr[end] = 9;
                down = true;
            }
        }
        if (beg === end) {
            if (down) {
                return false;
            }
            if (type === 1) {
                if (arr[end] % 2 === 0) {
                    ans[end] = String(arr[end] / 2);
                    return ans;
                } else {
                    return false;
                }
            } else {
                if (arr[end] % 2 === 0) {
                    ans[end] = String((arr[end] + 10) / 2);
                    return ans;
                } else {
                    return false;
                }
            }
        }
        if (type === 1) {
            if (arr[beg] === arr[end]) {
                type = 1;
                ans[beg] = String(arr[end]);
                ans[end] = "0";
                ++beg, --end;
            } else if (arr[beg] === arr[end] + 1) {
                type = 2;
                ans[beg] = String(arr[end]);
                if (beg === 0 && ans[beg] === "0") {
                    return false;
                }
                ans[end] = "0";
                ++beg, --end;
            } else {
                return false;
            }
        } else {
            if (arr[beg] === arr[end]) {
                if (arr[beg] !== 9) {
                    type = 1;
                    ans[beg] = "9";
                    ans[end] = String(arr[end] + 1);
                    down = true;
                    ++beg, --end;
                } else {
                    return false;
                }
            } else if (arr[beg] === arr[end] + 1) {
                type = 2;
                ans[beg] = "9";
                ans[end] = String(arr[end] + 1);
                down = true;
                ++beg, --end;
            } else if (arr[beg] === 0 && arr[end] === 9) {
                type = 2;
                ans[beg] = "9";
                ans[end] = "0";
                ++beg, --end;
            } else {
                return false;
            }
        }
    }
}

var ans = work(1, 0, len - 1);
if (ans) {
    print(ans.join(""));
} else {
    if (str[0] === "1") {
        ans = work(2, 1, len - 1);
        if (ans) {
            print(ans.join(""));
        } else {
            print(0);
        }
    } else {
        print(0);
    }
}

var str = readline();
var len = str.length;

function work(type, beg, end) {
    var arr = new Array(len);
    for (var i = 0; i < len; ++i) {
        arr[i] = Number(str[i]);
    }
    var ans = new Array(len);
    var down = false;

    while (true) {
        if (down) {
            if (arr[end] !== 0) {
                --arr[end];
                down = false;
            } else {
                arr[end] = 9;
                down = true;
            }
        }
        if (beg === end) {
            if (down) {
                return false;
            }
            if (type === 1) {
                if (arr[end] % 2 === 0) {
                    ans[end] = String(arr[end] / 2);
                    return ans;
                } else {
                    return false;
                }
            } else {
                if (arr[end] % 2 === 0) {
                    ans[end] = String((arr[end] + 10) / 2);
                    return ans;
                } else {
                    return false;
                }
            }
        }
        if (beg +1 === end) {
            if (down) {
                if (arr[beg] !== 0) {
                    --arr[beg];
                    down = false;
                } else {
                   arr[beg] = 9;
                    down = true;
                }
            }
            if (type === 1 && down || type === 2 && !down) {
                return false;
            }
            if (arr[beg] === arr[end]) {
                ans[beg] = String(arr[end]);
                ans[end] = "0";
                return ans;
            } else {
                return false;
            }
        }
        if (type === 1) {
            if (arr[beg] === arr[end]) {
                type = 1;
                ans[beg] = String(arr[end]);
                ans[end] = "0";
                ++beg, --end;
            } else if (arr[beg] === arr[end] + 1) {
                type = 2;
                ans[beg] = String(arr[end]);
                if (beg === 0 && ans[beg] === "0") {
                    return false;
                }
                ans[end] = "0";
                ++beg, --end;
            } else {
                return false;
            }
        } else {
            if (arr[beg] === arr[end]) {
                if (arr[beg] !== 9) {
                    type = 1;
                    ans[beg] = "9";
                    ans[end] = String(arr[end] + 1);
                    down = true;
                    ++beg, --end;
                } else {
                    return false;
                }
            } else if (arr[beg] === arr[end] + 1) {
                type = 2;
                ans[beg] = "9";
                ans[end] = String(arr[end] + 1);
                down = true;
                ++beg, --end;
            } else if (arr[beg] === 0 && arr[end] === 9) {
                type = 2;
                ans[beg] = "9";
                ans[end] = "0";
                ++beg, --end;
            } else {
                return false;
            }
        }
    }
}

var ans = work(1, 0, len - 1);
if (ans) {
    print(ans.join(""));
} else {
    if (str[0] === "1") {
        ans = work(2, 1, len - 1);
        if (ans) {
            print(ans.join(""));
        } else {
            print(0);
        }
    } else {
        print(0);
    }
}

var str = readline();
var len = str.length;

function work(type, beg, end) {
    var arr = new Array(len);
    for (var i = 0; i < len; ++i) {
        arr[i] = Number(str[i]);
    }
    var ans = new Array(len);
    var down = false;

    while (true) {
        if (down) {
            if (arr[end] !== 0) {
                --arr[end];
                down = false;
            } else {
                arr[end] = 9;
                down = true;
            }
        }
        if (beg > end) {
            if (type === 1 && !down || type === 2 && down) {
                return ans;
            } else {
                return false;
            }
        } else if (beg === end) {
            if (down) {
                return false;
            }
            if (type === 1) {
                if (arr[end] % 2 === 0) {
                    ans[end] = String(arr[end] / 2);
                    return ans;
                } else {
                    return false;
                }
            } else {
                if (arr[end] % 2 === 0) {
                    ans[end] = String((arr[end] + 10) / 2);
                    return ans;
                } else {
                    return false;
                }
            }
        }
        if (type === 1) {
            if (arr[beg] === arr[end]) {
                type = 1;
                ans[beg] = String(arr[end]);
                ans[end] = "0";
                ++beg, --end;
            } else if (arr[beg] === arr[end] + 1) {
                type = 2;
                ans[beg] = String(arr[end]);
                if (beg === 0 && ans[beg] === "0") {
                    return false;
                }
                ans[end] = "0";
                ++beg, --end;
            } else {
                return false;
            }
        } else {
            if (arr[beg] === arr[end]) {
                if (arr[beg] !== 9) {
                    type = 1;
                    ans[beg] = "9";
                    ans[end] = String(arr[end] + 1);
                    down = true;
                    ++beg, --end;
                } else {
                    return false;
                }
            } else if (arr[beg] === arr[end] + 1) {
                type = 2;
                ans[beg] = "9";
                ans[end] = String(arr[end] + 1);
                down = true;
                ++beg, --end;
            } else if (arr[beg] === 0 && arr[end] === 9) {
                type = 2;
                ans[beg] = "9";
                ans[end] = "0";
                ++beg, --end;
            } else {
                return false;
            }
        }
    }
}

var ans = work(1, 0, len - 1);
if (ans) {
    print(ans.join(""));
} else {
    if (str[0] === "1") {
        ans = work(2, 1, len - 1);
        if (ans) {
            print(ans.join(""));
        } else {
            print(0);
        }
    } else {
        print(0);
    }
}

var str = readline();
var len = str.length;

function work(type, beg, end) {
    var arr = new Array(len);
    for (var i = 0; i < len; ++i) {
        arr[i] = Number(str[i]);
    }
    var ans = new Array(len);
    var down = false;

    while (true) {
        if (down) {
            if (arr[end] !== 0) {
                --arr[end];
                down = false;
            } else {
                arr[end] = 9;
                down = true;
            }
        }
        if (beg > end) {
            if (type === 1 && !down || type === 2 && down) {
                return ans;
            } else {
                return false;
            }
        } else if (beg === end) {
            if (down) {
                return false;
            }
            if (type === 1) {
                if (arr[end] % 2 === 0) {
                    ans[end] = String(arr[end] / 2);
                    return ans;
                } else {
                    return false;
                }
            } else {
                if (arr[end] % 2 === 0) {
                    ans[end] = String((arr[end] + 10) / 2);
                    return ans;
                } else {
                    return false;
                }
            }
        }
        if (type === 1) {
            if (arr[beg] === arr[end]) {
                type = 1;
                ans[beg] = String(arr[end]);
                ans[end] = "0";
                ++beg, --end;
            } else if (arr[beg] === arr[end] + 1) {
                type = 2;
                ans[beg] = String(arr[end]);
                ans[end] = "0";
                ++beg, --end;
            } else {
                return false;
            }
        } else {
            if (arr[beg] === arr[end]) {
                if (arr[beg] !== 9) {
                    type = 1;
                    ans[beg] = "9";
                    ans[end] = String(arr[end] + 1);
                    down = true;
                    ++beg, --end;
                } else {
                    return false;
                }
            } else if (arr[beg] === arr[end] + 1) {
                type = 2;
                ans[beg] = "9";
                ans[end] = String(arr[end] + 1);
                down = true;
                ++beg, --end;
            } else if (arr[beg] === 0 && arr[end] === 9) {
                type = 2;
                ans[beg] = "9";
                ans[end] = "0";
                ++beg, --end;
            } else {
                return false;
            }
        }
    }
}

var ans = work(1, 0, len - 1);
if (ans) {
    print(ans.join(""));
} else {
    if (str[0] === "1") {
        ans = work(2, 1, len - 1);
        if (ans) {
            print(ans.join(""));
        } else {
            print(0);
        }
    } else {
        print(0);
    }
}


Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans.Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan.You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on the scales or to say that it can't be done.

The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000).

In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can put m weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales. If there are multiple solutions, you can print any of them.

print(function(h, m) {

	for (var x = 1; x <= 10; x++) {
		if (!h[x]) continue;
		var ans = [x];
		var a = [x, 0];
		var last = x;
		for (var i = 1; i < m; i++) {
			for (var j = 1; j <= 10; j++)
				if (h[j] && j !== last && a[i % 2] + j > a[(i + 1) % 2]) {
					a[i % 2] += j;
					last = j;
					ans.push(j);
					break;
				}

			if (j === 11)
				break;
		}
		if (ans.length === m) {
			return 'YES\n' + ans.join(' ');
		}
	}

	return 'NO';

}(('0' + readline()).split('').map(function(v) {
	return v === '1';
}), +readline()));

print(function(h, m) {
	var q = [];

	function d(step, x) {
		if (step === m) {
			return true;
		}
		for (var i = 1; i <= 10; i++) {
			var y = step % 2 === 1 ? x + i : x - i;
			q[step] = i;
			if (h[i] && i !== q[step - 1] && ((x >= 0 && y < 0) || (x <= 0 && y > 0)) && d(step + 1, y)) {
				return true;
			}
		}

		return false;
	}

	var ans = d(0, 0);
	if (ans) return 'YES\n' + q.join(' ');
	return 'NO';

}(('0' + readline()).split('').map(function(v) {
	return v === '1';
}), +readline()));

print(function(h, m) {

	var ans = [];
	var a = [0, 0];
	var last = 0;
	for (var i = 0; i < m; i++) {
		for (var j = 1; j <= 10; j++)
			if (h[j] && j !== last && a[i % 2] + j > a[(i + 1) % 2]) {
				a[i % 2] += j;
				last = j;
				ans.push(j);
				break;
			}

		if (j === 11)
			return 'NO';
	}
	return 'YES\n' + ans.join(' ');

}(('0' + readline()).split('').map(function(v) {
	return v === '1';
}), +readline()));


This is the easy version of the problem. The difference between the versions is in the number of possible operations that can be made. You can make hacks if and only if you solved both versions of the problem.You are given a binary table of size $$$n \times m$$$. This table consists of symbols $$$0$$$ and $$$1$$$.You can make such operation: select $$$3$$$ different cells that belong to one $$$2 \times 2$$$ square and change the symbols in these cells (change $$$0$$$ to $$$1$$$ and $$$1$$$ to $$$0$$$).Your task is to make all symbols in the table equal to $$$0$$$. You are allowed to make at most $$$3nm$$$ operations. You don't need to minimize the number of operations.It can be proved that it is always possible.

The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 5000$$$) — the number of test cases. The next lines contain descriptions of test cases. The first line of the description of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 \leq n, m \leq 100$$$). Each of the next $$$n$$$ lines contains a binary string of length $$$m$$$, describing the symbols of the next row of the table. It is guaranteed that the sum of $$$nm$$$ for all test cases does not exceed $$$20000$$$.

For each test case print the integer $$$k$$$ ($$$0 \leq k \leq 3nm$$$) — the number of operations. In the each of the next $$$k$$$ lines print $$$6$$$ integers $$$x_1, y_1, x_2, y_2, x_3, y_3$$$ ($$$1 \leq x_1, x_2, x_3 \leq n, 1 \leq y_1, y_2, y_3 \leq m$$$) describing the next operation. This operation will be made with three cells $$$(x_1, y_1)$$$, $$$(x_2, y_2)$$$, $$$(x_3, y_3)$$$. These three cells should be different. These three cells should belong into some $$$2 \times 2$$$ square.

NoteIn the first test case, it is possible to make only one operation with cells $$$(1, 1)$$$, $$$(2, 1)$$$, $$$(2, 2)$$$. After that, all symbols will be equal to $$$0$$$.In the second test case:  operation with cells $$$(2, 1)$$$, $$$(3, 1)$$$, $$$(3, 2)$$$. After it the table will be: 011001000  operation with cells $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$. After it the table will be: 000000000 In the fifth test case:  operation with cells $$$(1, 3)$$$, $$$(2, 2)$$$, $$$(2, 3)$$$. After it the table will be: 010110  operation with cells $$$(1, 2)$$$, $$$(2, 1)$$$, $$$(2, 2)$$$. After it the table will be: 000000

let inputString = "";
let currentLine = 0;
process.stdin.on("data", (data) => {
  inputString += data;
});
process.stdin.on("end", function () {
  inputString = inputString
    .trim()
    .split("\n")
    .map((str) => str.trim());
  main();
});
function readLine() {
  return inputString[currentLine++];
}

// utils
const MOD = 1e9 + 7;
const mod = (n) => ((n % MOD) + MOD) % MOD;
const add = (a, b) => mod(mod(a) + mod(b));
const sub = (a, b) => mod(mod(a) - mod(b));
function mul() {
  for (var a = arguments, r = a[0], i = a.length; --i; )
    r = ((((r >> 16) * a[i]) % MOD) * 65536 + (r & 65535) * a[i]) % MOD;
  return r;
}
function inv(b) {
  for (var a = MOD, u = 0, v = 1, t; b; v = t)
    (t = (a / b) | 0), (a -= t * b), (u -= t * v), (t = a), (a = b), (b = t), (t = u), (u = v);
  u %= MOD;
  return u < 0 ? u + MOD : u;
}
function pow(base, n) {
  if (n === 0) return 1;
  let p = pow(base, Math.floor(n / 2));
  let res = mul(mod(p), mod(p));
  if (n % 2 === 1) res = mul(mod(res), base);
  return res;
}
function highestOneBit(i) {
  i |= i >> 1;
  i |= i >> 2;
  i |= i >> 4;
  i |= i >> 8;
  i |= i >> 16;
  return i - (i >>> 1);
}
function permutation(arr, len, store = "", permutations = []) {
  if (store.length === len) {
    permutations.push(store);
    return;
  }
  for (let i = 0; i < arr.length; i++) {
    const newArr = [...arr];
    const newChar = newArr.splice(i, 1);
    permutation(newArr, len, store.concat(newChar), permutations);
  }
  return permutations;
}

function main() {
  let t = +readLine();
  while (t--) {
    const [rowLen, colLen] = readLine().split(" ").map(Number);
    const arr = [];
    for (let i = 0; i < rowLen; i++) {
      const _r = readLine().split("").map(Number);
      arr.push(_r);
    }
    let count = 0;
    const ans = [];

    for (let row = 1; row <= rowLen; row++) {
      for (let col = 1; col <= colLen; col++) {
        let [dx, dy] = [1, 1];
        if (row === rowLen) dx = -1;
        if (col === colLen) dy = -1;

        if (arr[row - 1][col - 1] === 1) {
          count += 3;
          ans.push([row, col, row, col + dy, row + dx, col]);
          ans.push([row, col, row, col + dy, row + dx, col + dy]);
          ans.push([row, col, row + dx, col, row + dx, col + dy]);
        }
      }
    }
    console.log(count);
    for (let result of ans) console.log(result.join(" "));
  }
}

//Don't have to see. start------------------------------------------
var read = require('readline').createInterface({
	input: process.stdin, output: process.stdout
});
var obj; var inLine = [];
read.on('line', function(input){inLine.push(input);});
read.on('close', function(){
	obj = init(inLine);
	console.error('\n↑入力 ↓出力');
	Main();
});
function makeClone(obj){return (obj instanceof Set) ? new Set(Array.from(obj)) : JSON.parse(JSON.stringify(obj));}
function nextInt(){return myconv(next(),1);} function nextStrArray(){return myconv(next(),2);}
function nextIntArray(){return myconv(next(),4);} function nextCharArray(){return myconv(next(),6);}
function next(){return obj.next();} function hasNext(){return obj.hasNext();}
function init(input){
	return {
		list : input, index : 0, max : input.length,
		hasNext : function(){return (this.index < this.max);},
		next : function(){if(this.hasNext()){return this.list[this.index++];}else{throw 'ArrayIndexOutOfBoundsException ‚There is no more input';}}
	};
}
function myout(s){console.log(s);}
function myerr(s){console.error('debug:' + require('util').inspect(s,false,null));}
//param "no" is
//unknown or outlier : return i. 1: parseInt.
//2: split space. 4: split space and parseInt.
//6: split 1 character. 7: split 1 character and parseInt.
//8: join space. 9: join nextline. 0: join no character.
function myconv(i,no){try{switch(no){case 1:return parseInt(i);case 2:return i.split(' ');case 4:return i.split(' ').map(Number);case 6:return i.split('');case 7:return i.split('').map(Number);case 8:return i.join(' ');case 9:return i.join('\n');case 0:return i.join('');default:return i;}}catch(e){return i;}}

//Don't have to see. end------------------------------------------
function Main(){
	var t = nextInt();
	var output2 = new Array();
	for(var i = 0; i < t; i++){
		var one = nextIntArray();
		var N = one[0];
		var M = one[1];
		var list = new Array(N);
		var count = 0;
		var output = [];
		for(var j = 0; j < N; j++){
			list[j] = new Array(M);
			var tmp = next();
			for(var k = 0; k < M; k++){
				list[j][k] = myconv(tmp[k], 1);
			}
		}
		if(list[N - 1][M - 1] == 1 && list[N - 2][M - 1] == 1 && list[N - 1][M - 2] == 1 && list[N - 2][M - 2] == 1){
			var s = (N - 1) + " " + M + " " + N + " " + (M - 1) + " " + N + " " + M;
			output.push(s);
			count++;
			list[N - 1][M - 1] = 0;
			list[N - 2][M - 1] = 0;
			list[N - 1][M - 2] = 0;
		}
		for(var x = 0; x < N - 1; x++){
			for(var y = 0; y < M - 1; y++){
				var hidariue = {
					v : list[x][y],
					s : (x + 1) + " " + (y + 1),
				};
				var hidarishita = {
					v : list[x + 1][y],
					s : (x + 2) + " " + (y + 1),
				};
				var migiue = {
					v : list[x][y + 1],
					s : (x + 1) + " " + (y + 2),
				};
				var migishita = {
					v : list[x + 1][y + 1],
					s : (x + 2) + " " + (y + 2),
				};

				var check = hidariue.v + hidarishita.v + migiue.v + migishita.v;
				if(check == 4){
					var s = hidariue.s + " " + migiue.s + " " + hidarishita.s;
					output.push(s);
					count++;
					list[x][y] = 0;
					list[x + 1][y] = 0;
					list[x][y + 1] = 0;
				}else if(check == 3){
					if(hidariue.v == 0){
						var s = migiue.s + " " + migishita.s + " " + hidarishita.s;
						output.push(s);
						count++;
						list[x + 1][y + 1] = 0;
						list[x + 1][y] = 0;
						list[x][y + 1] = 0;
					}else if(hidarishita.v == 0){
						var s = hidariue.s + " " + migiue.s + " " + migishita.s;
						output.push(s);
						count++;
						list[x][y] = 0;
						list[x + 1][y + 1] = 0;
						list[x][y + 1] = 0;
					}else if(migiue.v == 0){
						var s = hidariue.s + " " + hidarishita.s + " " + migishita.s;
						output.push(s);
						count++;
						list[x][y] = 0;
						list[x + 1][y] = 0;
						list[x + 1][y + 1] = 0;
					}else{
						var s = hidariue.s + " " + migiue.s + " " + hidarishita.s;
						output.push(s);
						count++;
						list[x][y] = 0;
						list[x + 1][y] = 0;
						list[x][y + 1] = 0;
					}
				}else if(check == 2){
					if(hidariue.v == 1 && migishita.v == 1){
						var s = hidariue.s + " " + migiue.s + " " + hidarishita.s;
						count += 2;
						output.push(s);
						s = migiue.s + " " + migishita.s + " " + hidarishita.s;
						output.push(s);
						list[x][y] = 0;
						list[x + 1][y + 1] = 0;
					}else if(migiue.v == 1 && hidarishita.v == 1){
						var s = hidariue.s + " " + migishita.s + " " + hidarishita.s;
						count += 2;
						output.push(s);
						s = hidariue.s + " " + migiue.s + " " + migishita.s;
						output.push(s);
						list[x][y + 1] = 0;
						list[x + 1][y] = 0;
					}else if(hidariue.v == 1 && migiue.v == 1){
						var s = hidariue.s + " " + hidarishita.s + " " + migishita.s;
						count += 2;
						output.push(s);
						s = migiue.s + " " + hidarishita.s + " " + migishita.s;
						output.push(s);
						list[x][y] = 0;
						list[x][y + 1] = 0;
					}else if(migiue.v == 1 && migishita.v == 1){
						var s = hidariue.s + " " + hidarishita.s + " " + migishita.s;
						count += 2;
						output.push(s);
						s = migiue.s + " " + hidarishita.s + " " + hidariue.s;
						output.push(s);
						list[x][y + 1] = 0;
						list[x + 1][y + 1] = 0;
					}else if(hidarishita.v == 1 && migishita.v == 1){
						var s = hidariue.s + " " + migiue.s + " " + migishita.s;
						count += 2;
						output.push(s);
						s = hidariue.s + " " + migiue.s + " " + hidarishita.s;
						output.push(s);
						list[x + 1][y] = 0;
						list[x + 1][y + 1] = 0;
					}else if(hidariue.v == 1 && hidarishita.v == 1){
						var s = hidariue.s + " " + migiue.s + " " + migishita.s;
						count += 2;
						output.push(s);
						s = hidarishita.s + " " + migiue.s + " " + migishita.s;
						output.push(s);
						list[x][y] = 0;
						list[x + 1][y] = 0;
					}
				}else if(check == 1){
					if(hidariue.v == 1){
						count += 3;
						var s = hidariue.s + " " + migiue.s + " " + hidarishita.s;
						output.push(s);
						s = hidariue.s + " " + hidarishita.s + " " + migishita.s;
						output.push(s);
						s = hidariue.s + " " + migiue.s + " " + migishita.s;
						output.push(s);
						list[x][y] = 0;
					}else if(migiue.v == 1){
						count += 3;
						var s = hidariue.s + " " + migiue.s + " " + migishita.s;
						output.push(s);
						s = hidariue.s + " " + hidarishita.s + " " + migiue.s;
						output.push(s);
						s = hidarishita.s + " " + migiue.s + " " + migishita.s;
						output.push(s);
						list[x][y + 1] = 0;
					}else if(hidarishita.v == 1){
						count += 3;
						var s = hidariue.s + " " + hidarishita.s + " " + migishita.s;
						output.push(s);
						s = hidariue.s + " " + hidarishita.s + " " + migiue.s;
						output.push(s);
						s = hidarishita.s + " " + migiue.s + " " + migishita.s;
						output.push(s);
						list[x + 1][y] = 0;
					}else if(migishita.v == 1){
						count += 3;
						var s = hidarishita.s + " " + migiue.s + " " + migishita.s;
						output.push(s);
						s = hidariue.s + " " + migishita.s + " " + migiue.s;
						output.push(s);
						s = hidariue.s + " " + hidarishita.s + " " + migishita.s;
						output.push(s);
						list[x + 1][y + 1] = 0;
					}
				}
			}
		}
		output.unshift(count);
		output2.push(myconv(output, 9));

	}
	myout(myconv(output2, 9));
}

(()=>{"use strict";var t={675:(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0}),Array.numbersMatrix=(t,e,r)=>{let n=[];null==t&&(e=t),null==r&&(r=0);for(let o=0;o<t;++o){let t=[];for(let n=0;n<e;++n)t.push(r);n.push(t)}return n},Array.prototype.sortAsc=function(){return this.sort(((t,e)=>t-e))},Array.prototype.sortDesc=function(){return this.sort(((t,e)=>e-t))},Array.prototype.max=function(){if(0==this.length)return 0;let t=this[0];for(let e=1;e<this.length;++e)t=Math.max(t,this[e]);return t},Array.prototype.min=function(){if(0==this.length)return 0;let t=this[0];for(let e=1;e<this.length;++e)t=Math.min(t,this[e]);return t},Array.prototype.sum=function(){let t=0;for(let e=0;e<this.length;++e)t+=this[e];return t},Array.prototype.modSum=function(t){let e=0;for(let r=0;r<this.length;++r)e+=this[r],e%=t;return e},Array.prototype.avg=function(){return this.sum()/this.length},Array.prototype.groupBy=function(t){let e=new Map;for(let r=0;r<this.length;++r){const n=t(this[r]);e.has(n)?e.get(n).push(this[r]):e.set(n,[this[r]])}return e},Array.prototype.deepCopy=function(){let t=[];if(0==this.length)return t;let e=!1;Array.isArray(this[0])&&(e=!0);for(let r=0;r<this.length;++r)e?t.push(this[r].deepCopy()):t.push(this[r]);return t},Array.prototype.transpose=function(){let t=[];if(0==this.length)return[[]];for(let e=0;e<this[0].length;++e){let r=[];for(let t=0;t<this.length;++t)r.push(this[t][e]);t.push(r)}return t},Array.prototype.toString=function(){if(0==this.length)return"";if(!Array.isArray(this[0]))return this.join(" ");let t="";for(let e=0;e<this.length;++e)t+=this[e].join(" "),e!=this.length-1&&(t+="\n");return t}},312:function(t,e,r){var n=this&&this.__importDefault||function(t){return t&&t.__esModule?t:{default:t}};Object.defineProperty(e,"__esModule",{value:!0});const o=n(r(747));let s="",i=[],u=0,l="";const p="local"===process.argv[2],f=t=>{if(p){if(i=o.default.readFileSync("input.txt").toString().trim().split("\n").map((t=>t.trim())),t(),o.default.writeFileSync("output.txt",l),console.log(l),o.default.readFileSync("expected.txt").toString().trim()==l.trim()){const{exec:t}=r(129);t("osascript -e 'display notification \"✅ AC!!\"'")}}else process.stdin.resume(),process.stdin.setEncoding("utf-8"),process.stdin.on("data",(t=>{s+=t})),process.stdin.on("end",(e=>{i=s.trim().split("\n").map((t=>t.trim())),t(),console.log(l)}))};function h(){return i[u++]}function a(){return h().split(" ").map((t=>parseFloat(t)))}function c(){return h().split("")}e.default={runMain:f,runEachTest:t=>{f((()=>{let[e]=a();for(;e>0;)t(),e--}))},readline:h,nextNumbers:a,nextBigNumbers:function(){return h().split(" ").map((t=>BigInt(t)))},nextNumbersMatrix:function(t){let e=[];for(let r=0;r<t;++r)e.push(a());return e},nextCharacters:c,nextCharacterMatrix:function(t){let e=[];for(let r=0;r<t;++r)e.push(c());return e},nextIntervalArray:function(t){let e=[];for(let r=0;r<t;++r){const[t,r]=a();e.push({a:t,b:r})}return e},put:function(t){l+=t},puts:function(t){l+=t+"\n"},debug:function(t){p&&console.log(t)}}},965:function(t,e,r){var n=this&&this.__importDefault||function(t){return t&&t.__esModule?t:{default:t}};Object.defineProperty(e,"__esModule",{value:!0}),r(675);const o=n(r(312));o.default.runEachTest((function(){let[t,e]=o.default.nextNumbers(),r=o.default.nextCharacterMatrix(t),n=[],s=(t,e,o,s,i,u)=>{n.push(`${t+1} ${e+1} ${o+1} ${s+1} ${i+1} ${u+1}`),"0"==r[t][e]?r[t][e]="1":r[t][e]="0","0"==r[o][s]?r[o][s]="1":r[o][s]="0","0"==r[i][u]?r[i][u]="1":r[i][u]="0"};if(t%2==1)for(let t=0;t<e-1;t++)"1"==r[0][t]&&"1"==r[0][t+1]?s(0,t,0,t+1,1,t):"1"==r[0][t]?s(0,t,1,t+1,1,t):"1"==r[0][t+1]&&s(0,t+1,1,t+1,1,t);if(e%2==1)for(let e=t%2==0?0:1;e<t-1;++e)"1"==r[e][0]&&"1"==r[e+1][0]?s(e,0,e+1,0,e,1):"1"==r[e][0]?s(e,0,e+1,1,e,1):"1"==r[e+1][0]&&s(e,1,e+1,0,e+1,1);let i=(t,e)=>{"0"!=r[t][e]?"0"!=r[t+1][e]?"0"!=r[t][e+1]?"0"==r[t+1][e+1]&&s(t+1,e,t,e+1,t,e):s(t,e,t+1,e,t+1,e+1):s(t,e,t,e+1,t+1,e+1):s(t+1,e,t,e+1,t+1,e+1)},u=(t,e)=>{let n=[],o=[];for(let s=0;s<2;++s)for(let i=0;i<2;++i)"0"==r[t+s][e+i]?n.push({x1:t+s,x2:e+i}):o.push({x1:t+s,x2:e+i});s(n[0].x1,n[0].x2,n[1].x1,n[1].x2,o[0].x1,o[0].x2)},l=(t,e)=>{let n=[],o=[];for(let s=0;s<2;++s)for(let i=0;i<2;++i)"0"==r[t+s][e+i]?n.push({x1:t+s,x2:e+i}):o.push({x1:t+s,x2:e+i});s(n[0].x1,n[0].x2,n[1].x1,n[1].x2,o[0].x1,o[0].x2)};for(let n=t%2==0?0:1;n<t;n+=2)for(let t=e%2==0?0:1;t<e;t+=2){let e=0,o=0;"1"==r[n][t]?o++:e++,"1"==r[n+1][t]?o++:e++,"1"==r[n][t+1]?o++:e++,"1"==r[n+1][t+1]?o++:e++,0!=o&&(3!=o?2!=o?1!=o?4!=o||(s(n,t,n+1,t,n+1,t+1),u(n,t),l(n,t),i(n,t)):(u(n,t),l(n,t),i(n,t)):(l(n,t),i(n,t)):i(n,t))}o.default.puts(n.length),o.default.puts(n.join(" "))}))},129:t=>{t.exports=require("child_process")},747:t=>{t.exports=require("fs")}},e={};!function r(n){if(e[n])return e[n].exports;var o=e[n]={exports:{}};return t[n].call(o.exports,o,o.exports,r),o.exports}(965)})();

// Solution written in typescript and compiled via webpack. Original source code below
// Happy Hacking 😜

// import './array'
//
// import io from './io'
//
// function main() {
//   let [n, m] = io.nextNumbers()
//   let a = io.nextCharacterMatrix(n)
//
//   let flips = []
//
//   let flip = (x1: number, y1: number, x2: number, y2: number, x3: number, y3: number) => {
//     flips.push(`${x1 + 1} ${y1 + 1} ${x2 + 1} ${y2 + 1} ${x3 + 1} ${y3 + 1}`)
//
//     if (a[x1][y1] == "0") {
//       a[x1][y1] = "1"
//     } else {
//       a[x1][y1] = "0"
//     }
//
//     if (a[x2][y2] == "0") {
//       a[x2][y2] = "1"
//     } else {
//       a[x2][y2] = "0"
//     }
//
//     if (a[x3][y3] == "0") {
//       a[x3][y3] = "1"
//     } else {
//       a[x3][y3] = "0"
//     }
//   }
//
//   if (n % 2 == 1) {
//     for (let i = 0; i < m - 1; i++) {
//       if (a[0][i] == "1" && a[0][i + 1] == "1") {
//         flip(0, i, 0, i + 1, 1, i)
//       } else if (a[0][i] == "1") {
//         flip(0, i, 1, i + 1, 1, i)
//       } else if (a[0][i + 1] == "1") {
//         flip(0, i + 1, 1, i + 1, 1, i)
//       }
//     }
//   }
//
//   if (m % 2 == 1) {
//     for (let i = n % 2 == 0 ? 0 : 1; i < n - 1; ++i) {
//       if (a[i][0] == "1" && a[i + 1][0] == "1") {
//         flip(i, 0, i + 1, 0, i, 1)
//       } else if (a[i][0] == "1") {
//         flip(i, 0, i + 1, 1, i, 1)
//       } else if (a[i + 1][0] == "1") {
//         flip(i, 1, i + 1, 0, i + 1, 1)
//       }
//     }
//   }
//
//   let proc3 = (x1: number, x2: number) => {
//     if (a[x1][x2] == "0") {
//       flip(x1 + 1, x2, x1, x2 + 1, x1 + 1, x2 + 1)
//       return
//     }
//
//     if (a[x1 + 1][x2] == "0") {
//       flip(x1, x2, x1, x2 + 1, x1 + 1, x2 + 1)
//       return
//     }
//
//     if (a[x1][x2 + 1] == "0") {
//       flip(x1, x2, x1 + 1, x2, x1 + 1, x2 + 1)
//       return
//     }
//
//     if (a[x1 + 1][x2 + 1] == "0") {
//       flip(x1 + 1, x2, x1, x2 + 1, x1, x2)
//     }
//   }
//
//   let proc1 = (x1: number, x2: number) => {
//     let zeroCoord = []
//     let oneCoord = []
//
//     for (let i = 0; i < 2; ++i) {
//       for (let j = 0; j < 2; ++j) {
//         if (a[x1 + i][x2 + j] == "0") {
//           zeroCoord.push({ x1: x1 + i, x2: x2 + j })
//         } else {
//           oneCoord.push({ x1: x1 + i, x2: x2 + j })
//         }
//       }
//     }
//
//     flip(
//       zeroCoord[0].x1,
//       zeroCoord[0].x2,
//       zeroCoord[1].x1,
//       zeroCoord[1].x2,
//       oneCoord[0].x1,
//       oneCoord[0].x2
//     )
//   }
//
//   let proc2 = (x1: number, x2: number) => {
//     let zeroCoord = []
//     let oneCoord = []
//
//     for (let i = 0; i < 2; ++i) {
//       for (let j = 0; j < 2; ++j) {
//         if (a[x1 + i][x2 + j] == "0") {
//           zeroCoord.push({ x1: x1 + i, x2: x2 + j })
//         } else {
//           oneCoord.push({ x1: x1 + i, x2: x2 + j })
//         }
//       }
//     }
//
//     flip(
//       zeroCoord[0].x1,
//       zeroCoord[0].x2,
//       zeroCoord[1].x1,
//       zeroCoord[1].x2,
//       oneCoord[0].x1,
//       oneCoord[0].x2
//     )
//   }
//
//   for (let i = n % 2 == 0 ? 0 : 1; i < n; i += 2) {
//     for (let j = m % 2 == 0 ? 0 : 1; j < m; j += 2) {
//       let nr0 = 0
//       let nr1 = 0
//
//       if (a[i][j] == "1") {
//         nr1++
//       } else {
//         nr0++
//       }
//
//       if (a[i + 1][j] == "1") {
//         nr1++
//       } else {
//         nr0++
//       }
//
//       if (a[i][j + 1] == "1") {
//         nr1++
//       } else {
//         nr0++
//       }
//
//       if (a[i + 1][j + 1] == "1") {
//         nr1++
//       } else {
//         nr0++
//       }
//
//       if (nr1 == 0) {
//         continue
//       }
//
//       if (nr1 == 3) {
//         proc3(i, j)
//         continue
//       }
//
//       if (nr1 == 2) {
//         proc2(i, j)
//         proc3(i, j)
//         continue
//       }
//
//       if (nr1 == 1) {
//         proc1(i, j)
//         proc2(i, j)
//         proc3(i, j)
//         continue
//       }
//
//       if (nr1 == 4) {
//         flip(i, j, i + 1, j, i + 1, j + 1)
//         proc1(i, j)
//         proc2(i, j)
//         proc3(i, j)
//         continue
//       }
//     }
//   }
//
//   io.puts(flips.length)
//   io.puts(flips.join(" "))
// }
//
// io.runEachTest(main)
// // io.runMain(main)

//Don't have to see. start------------------------------------------
var read = require('readline').createInterface({
	input: process.stdin, output: process.stdout
});
var obj; var inLine = [];
read.on('line', function(input){inLine.push(input);});
read.on('close', function(){
	obj = init(inLine);
	console.error('\n↑入力 ↓出力');
	Main();
});
function makeClone(obj){return (obj instanceof Set) ? new Set(Array.from(obj)) : JSON.parse(JSON.stringify(obj));}
function nextInt(){return myconv(next(),1);} function nextStrArray(){return myconv(next(),2);}
function nextIntArray(){return myconv(next(),4);} function nextCharArray(){return myconv(next(),6);}
function next(){return obj.next();} function hasNext(){return obj.hasNext();}
function init(input){
	return {
		list : input, index : 0, max : input.length,
		hasNext : function(){return (this.index < this.max);},
		next : function(){if(this.hasNext()){return this.list[this.index++];}else{throw 'ArrayIndexOutOfBoundsException ‚There is no more input';}}
	};
}
function myout(s){console.log(s);}
function myerr(s){console.error('debug:' + require('util').inspect(s,false,null));}
//param "no" is
//unknown or outlier : return i. 1: parseInt.
//2: split space. 4: split space and parseInt.
//6: split 1 character. 7: split 1 character and parseInt.
//8: join space. 9: join nextline. 0: join no character.
function myconv(i,no){try{switch(no){case 1:return parseInt(i);case 2:return i.split(' ');case 4:return i.split(' ').map(Number);case 6:return i.split('');case 7:return i.split('').map(Number);case 8:return i.join(' ');case 9:return i.join('\n');case 0:return i.join('');default:return i;}}catch(e){return i;}}

//Don't have to see. end------------------------------------------
function Main(){
	var t = nextInt();
	var output2 = new Array();
	for(var i = 0; i < t; i++){
		var one = nextIntArray();
		var N = one[0];
		var M = one[1];
		var list = new Array(N);
		var count = 0;
		var output = [];
		for(var j = 0; j < N; j++){
			list[j] = new Array(M);
			var tmp = next();
			for(var k = 0; k < M; k++){
				list[j][k] = myconv(tmp[k], 1);
			}
		}
		if(list[N - 1][M - 1] == 1 && list[N - 2][M - 1] == 1 && list[N - 1][M - 2] == 1 && list[N - 2][M - 2] == 1){
			var s = (N - 1) + " " + M + " " + N + " " + (M - 1) + " " + N + " " + M;
			output.push(s);
			count++;
			list[N - 1][M - 1] = 0;
			list[N - 2][M - 1] = 0;
			list[N - 1][M - 2] = 0;
		}
		for(var x = 0; x < N - 1; x++){
			for(var y = 0; y < M - 1; y++){
				var hidariue = {
					v : list[x][y],
					s : (x + 1) + " " + (y + 1),
				};
				var hidarishita = {
					v : list[x + 1][y],
					s : (x + 2) + " " + (y + 1),
				};
				var migiue = {
					v : list[x][y + 1],
					s : (x + 1) + " " + (y + 2),
				};
				var migishita = {
					v : list[x + 1][y + 1],
					s : (x + 2) + " " + (y + 2),
				};

				var check = hidariue.v + hidarishita.v + migiue.v + migishita.v;
				if(check == 4){
					var s = hidariue.s + " " + migiue.s + " " + hidarishita.s;
					output.push(s);
					count++;
					list[x][y] = 0;
					list[x + 1][y] = 0;
					list[x][y + 1] = 0;
				}else if(check == 3){
					if(hidariue.v == 0){
						var s = migiue.s + " " + migishita.s + " " + hidarishita.s;
						output.push(s);
						count++;
						list[x + 1][y + 1] = 0;
						list[x + 1][y] = 0;
						list[x][y + 1] = 0;
					}else if(hidarishita.v == 0){
						var s = hidariue.s + " " + migiue.s + " " + migishita.s;
						output.push(s);
						count++;
						list[x][y] = 0;
						list[x + 1][x] = 0;
						list[x][y + 1] = 0;
					}else if(migiue.v == 0){
						var s = hidariue.s + " " + hidarishita.s + " " + migishita.s;
						output.push(s);
						count++;
						list[x][y] = 0;
						list[x + 1][y] = 0;
						list[x + 1][y + 1] = 0;
					}else{
						var s = hidariue.s + " " + migiue.s + " " + hidarishita.s;
						output.push(s);
						count++;
						list[x][y] = 0;
						list[x + 1][y] = 0;
						list[x][y + 1] = 0;
					}
				}else if(check == 2){
					if(hidariue.v == 1 && migishita.v == 1){
						var s = hidariue.s + " " + migiue.s + " " + hidarishita.s;
						count += 2;
						output.push(s);
						s = migiue.s + " " + migishita.s + " " + hidarishita.s;
						output.push(s);
						list[x][y] = 0;
						list[x + 1][y + 1] = 0;
					}else if(migiue.v == 1 && hidarishita.v == 1){
						var s = hidariue.s + " " + migishita.s + " " + hidarishita.s;
						count += 2;
						output.push(s);
						s = hidariue.s + " " + migiue.s + " " + migishita.s;
						output.push(s);
						list[x][y + 1] = 0;
						list[x + 1][y] = 0;
					}else if(hidariue.v == 1 && migiue.v == 1){
						var s = hidariue.s + " " + hidarishita.s + " " + migishita.s;
						count += 2;
						output.push(s);
						s = migiue.s + " " + hidarishita.s + " " + migishita.s;
						output.push(s);
						list[x][y] = 0;
						list[x][y + 1] = 0;
					}else if(migiue.v == 1 && migishita.v == 1){
						var s = hidariue.s + " " + hidarishita.s + " " + migishita.s;
						count += 2;
						output.push(s);
						s = migiue.s + " " + hidarishita.s + " " + hidariue.s;
						output.push(s);
						list[x][y + 1] = 0;
						list[x + 1][y + 1] = 0;
					}else if(hidarishita.v == 1 && migishita.v == 1){
						var s = hidariue.s + " " + migiue.s + " " + migishita.s;
						count += 2;
						output.push(s);
						s = hidariue.s + " " + migiue.s + " " + hidarishita.s;
						output.push(s);
						list[x + 1][y] = 0;
						list[x + 1][y + 1] = 0;
					}else if(hidariue.v == 1 && hidarishita.v == 1){
						var s = hidariue.s + " " + migiue.s + " " + migishita.s;
						count += 2;
						output.push(s);
						s = hidarishita.s + " " + migiue.s + " " + migishita.s;
						output.push(s);
						list[x][y] = 0;
						list[x + 1][y] = 0;
					}
				}else if(check == 1){
					if(hidariue.v == 1){
						count += 3;
						var s = hidariue.s + " " + migiue.s + " " + hidarishita.s;
						output.push(s);
						s = hidariue.s + " " + hidarishita.s + " " + migishita.s;
						output.push(s);
						s = hidariue.s + " " + migiue.s + " " + migishita.s;
						output.push(s);
						list[x][y] = 0;
					}else if(migiue.v == 1){
						count += 3;
						var s = hidariue.s + " " + migiue.s + " " + migishita.s;
						output.push(s);
						s = hidariue.s + " " + hidarishita.s + " " + migiue.s;
						output.push(s);
						s = hidarishita.s + " " + migiue.s + " " + migishita.s;
						output.push(s);
						list[x][y + 1] = 0;
					}else if(hidarishita.v == 1){
						count += 3;
						var s = hidariue.s + " " + hidarishita.s + " " + migishita.s;
						output.push(s);
						s = hidariue.s + " " + hidarishita.s + " " + migiue.s;
						output.push(s);
						s = hidarishita.s + " " + migiue.s + " " + migishita.s;
						output.push(s);
						list[x + 1][y] = 0;
					}else if(migishita.v == 1){
						count += 3;
						var s = hidarishita.s + " " + migiue.s + " " + migishita.s;
						output.push(s);
						s = hidariue.s + " " + migishita.s + " " + migiue.s;
						output.push(s);
						s = hidariue.s + " " + migiue.s + " " + migishita.s;
						output.push(s);
						list[x + 1][y + 1] = 0;
					}
				}
			}
		}
		output.unshift(count);
		output2.push(myconv(output, 9));

	}
	myout(myconv(output2, 9));
}

//Don't have to see. start------------------------------------------
var read = require('readline').createInterface({
	input: process.stdin, output: process.stdout
});
var obj; var inLine = [];
read.on('line', function(input){inLine.push(input);});
read.on('close', function(){
	obj = init(inLine);
	console.error('\n↑入力 ↓出力');
	Main();
});
function makeClone(obj){return (obj instanceof Set) ? new Set(Array.from(obj)) : JSON.parse(JSON.stringify(obj));}
function nextInt(){return myconv(next(),1);} function nextStrArray(){return myconv(next(),2);}
function nextIntArray(){return myconv(next(),4);} function nextCharArray(){return myconv(next(),6);}
function next(){return obj.next();} function hasNext(){return obj.hasNext();}
function init(input){
	return {
		list : input, index : 0, max : input.length,
		hasNext : function(){return (this.index < this.max);},
		next : function(){if(this.hasNext()){return this.list[this.index++];}else{throw 'ArrayIndexOutOfBoundsException ‚There is no more input';}}
	};
}
function myout(s){console.log(s);}
function myerr(s){console.error('debug:' + require('util').inspect(s,false,null));}
//param "no" is
//unknown or outlier : return i. 1: parseInt.
//2: split space. 4: split space and parseInt.
//6: split 1 character. 7: split 1 character and parseInt.
//8: join space. 9: join nextline. 0: join no character.
function myconv(i,no){try{switch(no){case 1:return parseInt(i);case 2:return i.split(' ');case 4:return i.split(' ').map(Number);case 6:return i.split('');case 7:return i.split('').map(Number);case 8:return i.join(' ');case 9:return i.join('\n');case 0:return i.join('');default:return i;}}catch(e){return i;}}

//Don't have to see. end------------------------------------------
function Main(){
	var t = nextInt();
	var output2 = new Array();
	for(var i = 0; i < t; i++){
		var one = nextIntArray();
		var N = one[0];
		var M = one[1];
		var list = new Array(N);
		var count = 0;
		var output = [];
		for(var j = 0; j < N; j++){
			list[j] = new Array(M);
			var tmp = next();
			for(var k = 0; k < M; k++){
				list[j][k] = myconv(tmp[k], 1);
			}
		}
		if(list[N - 1][M - 1] == 1 && list[N - 2][M - 1] == 1 && list[N - 1][M - 2] == 1 && list[N - 2][M - 2] == 1){
			var s = (N - 1) + " " + M + " " + N + " " + (M - 1) + " " + N + " " + M;
			output.push(s);
			count++;
			list[N - 1][M - 1] = 0;
			list[N - 2][M - 1] = 0;
			list[N - 1][M - 2] = 0;
		}
		for(var x = 0; x < N - 1; x++){
			for(var y = 0; y < M - 1; y++){
				var hidariue = {
					v : list[x][y],
					s : (x + 1) + " " + (y + 1),
				};
				var hidarishita = {
					v : list[x + 1][y],
					s : (x + 2) + " " + (y + 1),
				};
				var migiue = {
					v : list[x][y + 1],
					s : (x + 1) + " " + (y + 2),
				};
				var migishita = {
					v : list[x + 1][y + 1],
					s : (x + 2) + " " + (y + 2),
				};

				var check = hidariue.v + hidarishita.v + migiue.v + migishita.v;
				if(check == 4){
					var s = hidariue.s + " " + migiue.s + " " + hidarishita.s;
					output.push(s);
					count++;
					list[x][y] = 0;
					list[x + 1][y] = 0;
					list[x][y + 1] = 0;
				}else if(check == 3){
					if(hidariue.v == 0){
						var s = migiue.s + " " + migishita.s + " " + hidarishita.s;
						output.push(s);
						count++;
						list[x + 1][y + 1] = 0;
						list[x + 1][y] = 0;
						list[x][y + 1] = 0;
					}else if(hidarishita.v == 0){
						var s = hidariue.s + " " + migiue.s + " " + migishita.s;
						output.push(s);
						count++;
						list[x][y] = 0;
						list[x + 1][y + 1] = 0;
						list[x][y + 1] = 0;
					}else if(migiue.v == 0){
						var s = hidariue.s + " " + hidarishita.s + " " + migishita.s;
						output.push(s);
						count++;
						list[x][y] = 0;
						list[x + 1][y] = 0;
						list[x + 1][y + 1] = 0;
					}else{
						var s = hidariue.s + " " + migiue.s + " " + hidarishita.s;
						output.push(s);
						count++;
						list[x][y] = 0;
						list[x + 1][y] = 0;
						list[x][y + 1] = 0;
					}
				}else if(check == 2){
					if(hidariue.v == 1 && migishita.v == 1){
						var s = hidariue.s + " " + migiue.s + " " + hidarishita.s;
						count += 2;
						output.push(s);
						s = migiue.s + " " + migishita.s + " " + hidarishita.s;
						output.push(s);
						list[x][y] = 0;
						list[x + 1][y + 1] = 0;
					}else if(migiue.v == 1 && hidarishita.v == 1){
						var s = hidariue.s + " " + migishita.s + " " + hidarishita.s;
						count += 2;
						output.push(s);
						s = hidariue.s + " " + migiue.s + " " + migishita.s;
						output.push(s);
						list[x][y + 1] = 0;
						list[x + 1][y] = 0;
					}else if(hidariue.v == 1 && migiue.v == 1){
						var s = hidariue.s + " " + hidarishita.s + " " + migishita.s;
						count += 2;
						output.push(s);
						s = migiue.s + " " + hidarishita.s + " " + migishita.s;
						output.push(s);
						list[x][y] = 0;
						list[x][y + 1] = 0;
					}else if(migiue.v == 1 && migishita.v == 1){
						var s = hidariue.s + " " + hidarishita.s + " " + migishita.s;
						count += 2;
						output.push(s);
						s = migiue.s + " " + hidarishita.s + " " + hidariue.s;
						output.push(s);
						list[x][y + 1] = 0;
						list[x + 1][y + 1] = 0;
					}else if(hidarishita.v == 1 && migishita.v == 1){
						var s = hidariue.s + " " + migiue.s + " " + migishita.s;
						count += 2;
						output.push(s);
						s = hidariue.s + " " + migiue.s + " " + hidarishita.s;
						output.push(s);
						list[x + 1][y] = 0;
						list[x + 1][y + 1] = 0;
					}else if(hidariue.v == 1 && hidarishita.v == 1){
						var s = hidariue.s + " " + migiue.s + " " + migishita.s;
						count += 2;
						output.push(s);
						s = hidarishita.s + " " + migiue.s + " " + migishita.s;
						output.push(s);
						list[x][y] = 0;
						list[x + 1][y] = 0;
					}
				}else if(check == 1){
					if(hidariue.v == 1){
						count += 3;
						var s = hidariue.s + " " + migiue.s + " " + hidarishita.s;
						output.push(s);
						s = hidariue.s + " " + hidarishita.s + " " + migishita.s;
						output.push(s);
						s = hidariue.s + " " + migiue.s + " " + migishita.s;
						output.push(s);
						list[x][y] = 0;
					}else if(migiue.v == 1){
						count += 3;
						var s = hidariue.s + " " + migiue.s + " " + migishita.s;
						output.push(s);
						s = hidariue.s + " " + hidarishita.s + " " + migiue.s;
						output.push(s);
						s = hidarishita.s + " " + migiue.s + " " + migishita.s;
						output.push(s);
						list[x][y + 1] = 0;
					}else if(hidarishita.v == 1){
						count += 3;
						var s = hidariue.s + " " + hidarishita.s + " " + migishita.s;
						output.push(s);
						s = hidariue.s + " " + hidarishita.s + " " + migiue.s;
						output.push(s);
						s = hidarishita.s + " " + migiue.s + " " + migishita.s;
						output.push(s);
						list[x + 1][y] = 0;
					}else if(migishita.v == 1){
						count += 3;
						var s = hidarishita.s + " " + migiue.s + " " + migishita.s;
						output.push(s);
						s = hidariue.s + " " + migishita.s + " " + migiue.s;
						output.push(s);
						s = hidariue.s + " " + migiue.s + " " + migishita.s;
						output.push(s);
						list[x + 1][y + 1] = 0;
					}
				}
			}
		}
		output.unshift(count);
		output2.push(myconv(output, 9));

	}
	myout(myconv(output2, 9));
}

(()=>{"use strict";var t={675:(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0}),Array.numbersMatrix=(t,e,r)=>{let n=[];null==t&&(e=t),null==r&&(r=0);for(let o=0;o<t;++o){let t=[];for(let n=0;n<e;++n)t.push(r);n.push(t)}return n},Array.prototype.sortAsc=function(){return this.sort(((t,e)=>t-e))},Array.prototype.sortDesc=function(){return this.sort(((t,e)=>e-t))},Array.prototype.max=function(){if(0==this.length)return 0;let t=this[0];for(let e=1;e<this.length;++e)t=Math.max(t,this[e]);return t},Array.prototype.min=function(){if(0==this.length)return 0;let t=this[0];for(let e=1;e<this.length;++e)t=Math.min(t,this[e]);return t},Array.prototype.sum=function(){let t=0;for(let e=0;e<this.length;++e)t+=this[e];return t},Array.prototype.modSum=function(t){let e=0;for(let r=0;r<this.length;++r)e+=this[r],e%=t;return e},Array.prototype.avg=function(){return this.sum()/this.length},Array.prototype.groupBy=function(t){let e=new Map;for(let r=0;r<this.length;++r){const n=t(this[r]);e.has(n)?e.get(n).push(this[r]):e.set(n,[this[r]])}return e},Array.prototype.deepCopy=function(){let t=[];if(0==this.length)return t;let e=!1;Array.isArray(this[0])&&(e=!0);for(let r=0;r<this.length;++r)e?t.push(this[r].deepCopy()):t.push(this[r]);return t},Array.prototype.transpose=function(){let t=[];if(0==this.length)return[[]];for(let e=0;e<this[0].length;++e){let r=[];for(let t=0;t<this.length;++t)r.push(this[t][e]);t.push(r)}return t},Array.prototype.toString=function(){if(0==this.length)return"";if(!Array.isArray(this[0]))return this.join(" ");let t="";for(let e=0;e<this.length;++e)t+=this[e].join(" "),e!=this.length-1&&(t+="\n");return t}},312:function(t,e,r){var n=this&&this.__importDefault||function(t){return t&&t.__esModule?t:{default:t}};Object.defineProperty(e,"__esModule",{value:!0});const o=n(r(747));let s="",i=[],u=0,l="";const p="local"===process.argv[2],f=t=>{if(p){if(i=o.default.readFileSync("input.txt").toString().trim().split("\n").map((t=>t.trim())),t(),o.default.writeFileSync("output.txt",l),console.log(l),o.default.readFileSync("expected.txt").toString().trim()==l.trim()){const{exec:t}=r(129);t("osascript -e 'display notification \"✅ AC!!\"'")}}else process.stdin.resume(),process.stdin.setEncoding("utf-8"),process.stdin.on("data",(t=>{s+=t})),process.stdin.on("end",(e=>{i=s.trim().split("\n").map((t=>t.trim())),t(),console.log(l)}))};function h(){return i[u++]}function a(){return h().split(" ").map((t=>parseFloat(t)))}function c(){return h().split("")}e.default={runMain:f,runEachTest:t=>{f((()=>{let[e]=a();for(;e>0;)t(),e--}))},readline:h,nextNumbers:a,nextBigNumbers:function(){return h().split(" ").map((t=>BigInt(t)))},nextNumbersMatrix:function(t){let e=[];for(let r=0;r<t;++r)e.push(a());return e},nextCharacters:c,nextCharacterMatrix:function(t){let e=[];for(let r=0;r<t;++r)e.push(c());return e},nextIntervalArray:function(t){let e=[];for(let r=0;r<t;++r){const[t,r]=a();e.push({a:t,b:r})}return e},put:function(t){l+=t},puts:function(t){l+=t+"\n"},debug:function(t){p&&console.log(t)}}},965:function(t,e,r){var n=this&&this.__importDefault||function(t){return t&&t.__esModule?t:{default:t}};Object.defineProperty(e,"__esModule",{value:!0}),r(675);const o=n(r(312));o.default.runEachTest((function(){let[t,e]=o.default.nextNumbers(),r=o.default.nextCharacterMatrix(t),n=[],s=(t,e,o,s,i,u)=>{n.push(`${t+1} ${e+1} ${o+1} ${s+1} ${i+1} ${u+1}`),"0"==r[t][e]?r[t][e]="1":r[t][e]="0","0"==r[o][s]?r[o][s]="1":r[o][s]="0","0"==r[i][u]?r[i][u]="1":r[i][u]="0"};if(t%2==1)for(let t=0;t<e-1;t++)"1"==r[0][t]&&"1"==r[0][t+1]?s(0,t,0,t+1,1,t):"1"==r[0][t]?s(0,t,1,t+1,1,t):"1"==r[0][t+1]&&s(0,t+1,1,t+1,1,t);if(e%2==1)for(let e=t%2==0?0:1;e<t-1;++e)"1"==r[e][0]&&"1"==r[e+1][0]?s(e,0,e+1,0,e,1):"1"==r[e][0]?s(e,0,e+1,1,e,1):"1"==r[e+1][0]&&s(e,1,e+1,0,e,1);let i=(t,e)=>{"0"!=r[t][e]?"0"!=r[t+1][e]?"0"!=r[t][e+1]?"0"==r[t+1][e+1]&&s(t+1,e,t,e+1,t,e):s(t,e,t+1,e,t+1,e+1):s(t,e,t,e+1,t+1,e+1):s(t+1,e,t,e+1,t+1,e+1)},u=(t,e)=>{let n=[],o=[];for(let s=0;s<2;++s)for(let i=0;i<2;++i)"0"==r[t+s][e+i]?n.push({x1:t+s,x2:e+i}):o.push({x1:t+s,x2:e+i});s(n[0].x1,n[0].x2,n[1].x1,n[1].x2,o[0].x1,o[0].x2)},l=(t,e)=>{let n=[],o=[];for(let s=0;s<2;++s)for(let i=0;i<2;++i)"0"==r[t+s][e+i]?n.push({x1:t+s,x2:e+i}):o.push({x1:t+s,x2:e+i});s(n[0].x1,n[0].x2,n[1].x1,n[1].x2,o[0].x1,o[0].x2)};for(let n=t%2==0?0:1;n<t;n+=2)for(let t=e%2==0?0:1;t<e;t+=2){let e=0,o=0;"1"==r[n][t]?o++:e++,"1"==r[n+1][t]?o++:e++,"1"==r[n][t+1]?o++:e++,"1"==r[n+1][t+1]?o++:e++,0!=o&&(3!=o?2!=o?1!=o?4!=o||(s(n,t,n+1,t,n+1,t+1),u(n,t),l(n,t),i(n,t)):(u(n,t),l(n,t),i(n,t)):(l(n,t),i(n,t)):i(n,t))}o.default.puts(n.length),o.default.puts(n.join(" "))}))},129:t=>{t.exports=require("child_process")},747:t=>{t.exports=require("fs")}},e={};!function r(n){if(e[n])return e[n].exports;var o=e[n]={exports:{}};return t[n].call(o.exports,o,o.exports,r),o.exports}(965)})();

// Solution written in typescript and compiled via webpack. Original source code below
// Happy Hacking 😜

// import './array'
//
// import io from './io'
//
// function main() {
//   let [n, m] = io.nextNumbers()
//   let a = io.nextCharacterMatrix(n)
//
//   let flips = []
//
//   let flip = (x1: number, y1: number, x2: number, y2: number, x3: number, y3: number) => {
//     flips.push(`${x1 + 1} ${y1 + 1} ${x2 + 1} ${y2 + 1} ${x3 + 1} ${y3 + 1}`)
//
//     if (a[x1][y1] == "0") {
//       a[x1][y1] = "1"
//     } else {
//       a[x1][y1] = "0"
//     }
//
//     if (a[x2][y2] == "0") {
//       a[x2][y2] = "1"
//     } else {
//       a[x2][y2] = "0"
//     }
//
//     if (a[x3][y3] == "0") {
//       a[x3][y3] = "1"
//     } else {
//       a[x3][y3] = "0"
//     }
//   }
//
//   if (n % 2 == 1) {
//     for (let i = 0; i < m - 1; i++) {
//       if (a[0][i] == "1" && a[0][i + 1] == "1") {
//         flip(0, i, 0, i + 1, 1, i)
//       } else if (a[0][i] == "1") {
//         flip(0, i, 1, i + 1, 1, i)
//       } else if (a[0][i + 1] == "1") {
//         flip(0, i + 1, 1, i + 1, 1, i)
//       }
//     }
//   }
//
//   if (m % 2 == 1) {
//     for (let i = n % 2 == 0 ? 0 : 1; i < n - 1; ++i) {
//       if (a[i][0] == "1" && a[i + 1][0] == "1") {
//         flip(i, 0, i + 1, 0, i, 1)
//       } else if (a[i][0] == "1") {
//         flip(i, 0, i + 1, 1, i, 1)
//       } else if (a[i + 1][0] == "1") {
//         flip(i, 1, i + 1, 0, i, 1)
//       }
//     }
//   }
//
//   let proc3 = (x1: number, x2: number) => {
//     if (a[x1][x2] == "0") {
//       flip(x1 + 1, x2, x1, x2 + 1, x1 + 1, x2 + 1)
//       return
//     }
//
//     if (a[x1 + 1][x2] == "0") {
//       flip(x1, x2, x1, x2 + 1, x1 + 1, x2 + 1)
//       return
//     }
//
//     if (a[x1][x2 + 1] == "0") {
//       flip(x1, x2, x1 + 1, x2, x1 + 1, x2 + 1)
//       return
//     }
//
//     if (a[x1 + 1][x2 + 1] == "0") {
//       flip(x1 + 1, x2, x1, x2 + 1, x1, x2)
//     }
//   }
//
//   let proc1 = (x1: number, x2: number) => {
//     let zeroCoord = []
//     let oneCoord = []
//
//     for (let i = 0; i < 2; ++i) {
//       for (let j = 0; j < 2; ++j) {
//         if (a[x1 + i][x2 + j] == "0") {
//           zeroCoord.push({ x1: x1 + i, x2: x2 + j })
//         } else {
//           oneCoord.push({ x1: x1 + i, x2: x2 + j })
//         }
//       }
//     }
//
//     flip(
//       zeroCoord[0].x1,
//       zeroCoord[0].x2,
//       zeroCoord[1].x1,
//       zeroCoord[1].x2,
//       oneCoord[0].x1,
//       oneCoord[0].x2
//     )
//   }
//
//   let proc2 = (x1: number, x2: number) => {
//     let zeroCoord = []
//     let oneCoord = []
//
//     for (let i = 0; i < 2; ++i) {
//       for (let j = 0; j < 2; ++j) {
//         if (a[x1 + i][x2 + j] == "0") {
//           zeroCoord.push({ x1: x1 + i, x2: x2 + j })
//         } else {
//           oneCoord.push({ x1: x1 + i, x2: x2 + j })
//         }
//       }
//     }
//
//     flip(
//       zeroCoord[0].x1,
//       zeroCoord[0].x2,
//       zeroCoord[1].x1,
//       zeroCoord[1].x2,
//       oneCoord[0].x1,
//       oneCoord[0].x2
//     )
//   }
//
//   for (let i = n % 2 == 0 ? 0 : 1; i < n; i += 2) {
//     for (let j = m % 2 == 0 ? 0 : 1; j < m; j += 2) {
//       let nr0 = 0
//       let nr1 = 0
//
//       if (a[i][j] == "1") {
//         nr1++
//       } else {
//         nr0++
//       }
//
//       if (a[i + 1][j] == "1") {
//         nr1++
//       } else {
//         nr0++
//       }
//
//       if (a[i][j + 1] == "1") {
//         nr1++
//       } else {
//         nr0++
//       }
//
//       if (a[i + 1][j + 1] == "1") {
//         nr1++
//       } else {
//         nr0++
//       }
//
//       if (nr1 == 0) {
//         continue
//       }
//
//       if (nr1 == 3) {
//         proc3(i, j)
//         continue
//       }
//
//       if (nr1 == 2) {
//         proc2(i, j)
//         proc3(i, j)
//         continue
//       }
//
//       if (nr1 == 1) {
//         proc1(i, j)
//         proc2(i, j)
//         proc3(i, j)
//         continue
//       }
//
//       if (nr1 == 4) {
//         flip(i, j, i + 1, j, i + 1, j + 1)
//         proc1(i, j)
//         proc2(i, j)
//         proc3(i, j)
//         continue
//       }
//     }
//   }
//
//   io.puts(flips.length)
//   io.puts(flips.join(" "))
// }
//
// io.runEachTest(main)
// // io.runMain(main)


Programmer Vasya is studying a new programming language &K*. The &K* language resembles the languages of the C family in its syntax. However, it is more powerful, which is why the rules of the actual C-like languages are unapplicable to it. To fully understand the statement, please read the language's description below carefully and follow it and not the similar rules in real programming languages.There is a very powerful system of pointers on &K* — you can add an asterisk to the right of the existing type X — that will result in new type X * . That is called pointer-definition operation. Also, there is the operation that does the opposite — to any type of X, which is a pointer, you can add an ampersand — that will result in a type &X, to which refers X. That is called a dereference operation.The &K* language has only two basic data types — void and errtype. Also, the language has operators typedef and typeof. The operator "typedef A B" defines a new data type B, which is equivalent to A. A can have asterisks and ampersands, and B cannot have them. For example, the operator typedef void** ptptvoid will create a new type ptptvoid, that can be used as void**. The operator "typeof A" returns type of A, brought to void, that is, returns the type void**...*, equivalent to it with the necessary number of asterisks (the number can possibly be zero). That is, having defined the ptptvoid type, as shown above, the typeof ptptvoid operator will return void**.An attempt of dereferencing of the void type will lead to an error: to a special data type errtype. For errtype the following equation holds true: errtype* = &errtype = errtype. An attempt to use the data type that hasn't been defined before that will also lead to the errtype.Using typedef, we can define one type several times. Of all the definitions only the last one is valid. However, all the types that have been defined earlier using this type do not change.Let us also note that the dereference operation has the lower priority that the pointer operation, in other words &T *  is always equal to T.Note, that the operators are executed consecutively one by one. If we have two operators "typedef &void a" and "typedef a* b", then at first a becomes errtype, and after that b becomes errtype* = errtype, but not &void* = void (see sample 2).Vasya does not yet fully understand this powerful technology, that's why he asked you to help him. Write a program that analyzes these operators.

The first line contains an integer n (1 ≤ n ≤ 100) — the number of operators. Then follow n lines with operators. Each operator is of one of two types: either "typedef A B", or "typeof A". In the first case the B type differs from void and errtype types, and besides, doesn't have any asterisks and ampersands. All the data type names are non-empty lines of no more than 20 lowercase Latin letters. The number of asterisks and ampersands separately in one type in any operator does not exceed 10, however if we bring some types to void with several asterisks, their number may exceed 10.

For every typeof operator print on the single line the answer to that operator — the type that the given operator returned.

5
typedef void* ptv
typeof ptv
typedef &&ptv node
typeof node
typeof &ptv

17
typedef void* b
typedef b* c
typeof b
typeof c
typedef &b b
typeof b
typeof c
typedef &&b* c
typeof c
typedef &b* c
typeof c
typedef &void b
typeof b
typedef b******* c
typeof c
typedef &&b* c
typeof c

void*
void**
void
void**
errtype
void
errtype
errtype
errtype

NoteLet's look at the second sample.After the first two queries typedef the b type is equivalent to void*, and с — to void**.The next query typedef redefines b — it is now equal to &b = &void* = void. At that, the с type doesn't change.After that the с type is defined as &&b* = &&void* = &void = errtype. It doesn't influence the b type, that's why the next typedef defines c as &void* = void.Then the b type is again redefined as &void = errtype. Please note that the c type in the next query is defined exactly as errtype******* = errtype, and not &void******* = void******. The same happens in the last typedef.

'use strict'

const {EOL} = require('os')

let ipt = ''
process.stdin.on('data', s => ipt += s)
process.stdin.on('end', () => {
  const ils = ipt.split(EOL).slice(1, -1).map(line => line.split(' '))
  let T = {void: 1, errtype: 0}

  for (let i = 0; i < ils.length; i++) {
    let [o, to, tn] = ils[i]
    let [, a, tb, z] = to.match(/(&*)([^*]+)(\**)/)
    a = a.length
    z = z.length

    if (o == 'typedef') {
      if (T[tb] == 0 || T[tb] + z - a <= 0) {
        T[tn] = 0
        continue
      }
      T[tn] = T[tb] + z - a
    } else { // typedef
      if (!T[tb] || T[tb] + z - a <= 0) {
        console.log('errtype')
      } else {
        let out = 'void'
        for (let j = 1; j < T[tb] + z - a; j++) out += '*'
        console.log(out)
      }
    }
  }
})

'use strict'

const {EOL} = require('os')

let ipt = ''
process.stdin.on('data', s => ipt += s)
process.stdin.on('end', () => {
  const ils = ipt.split(EOL).slice(1, -1).map(line => line.split(' '))
  let T = {void: 1, errtype: 0}

  for (let i = 0; i < ils.length; i++) {
    let [o, to, tn] = ils[i]

    if (o == 'typedef') {
      let [, a, tb, z] = to.match(/(&*)([^*]+)(\**)/)
      a = a.length
      z = z.length
      if (T[tb] == 0 || T[tb] + z - a <= 0) {
        T[tn] = 0
        continue
      }
      T[tn] = T[tb] + z - a
    } else { // typedef
      let [, a, tb, z] = to.match(/(&*)([^*]+)(\**)/)
      if (!T[tb] || T[tb + z - a] <= 0) {
        console.log('errtype')
      } else {
        let out = 'void'
        for (let j = 1; j < T[to] + z - a; j++) out += '*'
        console.log(out)
      }
    }
  }
})

'use strict'

const {EOL} = require('os')

let ipt = ''
process.stdin.on('data', s => ipt += s)
process.stdin.on('end', () => {
  const ils = ipt.split(EOL).slice(1, -1).map(line => line.split(' '))
  let T = {void: 1, errtype: 0}

  for (let i = 0; i < ils.length; i++) {
    let [o, to, tn] = ils[i]
    let [, a, tb, z] = to.match(/(&*)([^*]+)(\**)/)
    a = a.length
    z = z.length

    if (o == 'typedef') {
      if (T[tb] == 0 || T[tb] + z - a <= 0) {
        T[tn] = 0
        continue
      }
      T[tn] = T[tb] + z - a
    } else { // typedef
      if (!T[tb] || T[tb + z - a] <= 0) {
        console.log('errtype')
      } else {
        let out = 'void'
        for (let j = 1; j < T[tb] + z - a; j++) out += '*'
        console.log(out)
      }
    }
  }
})


One day little Vasya found mom's pocket book. The book had n names of her friends and unusually enough, each name was exactly m letters long. Let's number the names from 1 to n in the order in which they are written.As mom wasn't home, Vasya decided to play with names: he chose three integers i, j, k (1 ≤ i < j ≤ n, 1 ≤ k ≤ m), then he took names number i and j and swapped their prefixes of length k. For example, if we take names "CBDAD" and "AABRD" and swap their prefixes with the length of 3, the result will be names "AABAD" and "CBDRD".You wonder how many different names Vasya can write instead of name number 1, if Vasya is allowed to perform any number of the described actions. As Vasya performs each action, he chooses numbers i, j, k independently from the previous moves and his choice is based entirely on his will. The sought number can be very large, so you should only find it modulo 1000000007 (109 + 7).

The first input line contains two integers n and m (1 ≤ n, m ≤ 100) — the number of names and the length of each name, correspondingly. Then n lines contain names, each name consists of exactly m uppercase Latin letters.

Print the single number — the number of different names that could end up in position number 1 in the pocket book after the applying the procedures described above. Print the number modulo 1000000007 (109 + 7).

NoteIn the first sample Vasya can get the following names in the position number 1: "AAB", "AAA", "BAA" and "BAB".

function trim(s) {
	return s.replace(/^\s+|\s+$/gm, '');
}

function tokenize(s) {
	return trim(s).split(/\s+/);
}

function tokenizeIntegers(s) {
	var tokens = tokenize(s);
	for (var i = 0; i < tokens.length; i += 1) {
		tokens[i] = parseInt(tokens[i], 10);
	};
	return tokens;
}

function printValues() {
	var parts = [];
	for (var i = 0; i < arguments.length; i += 2) {
		parts.push(arguments[i]+' = '+arguments[i+1]);
	}
	print(parts.join(', '));
}

function main() {
	var data = tokenizeIntegers(readline()),
		n = data[0], m = data[1],
    words = [],
    result = 1;
  for (var i = 0; i < n; ++i) {
    words.push(trim(readline()));
  }
  for (var i = 0; i < m; ++i) {
    var seen = {}, count = 0;
    for (var j = 0; j < n; ++j) {
      var ch = words[j][i];
      if (seen[ch] === undefined) {
        seen[ch] = true;
        ++count;
      }
    }
    result = (count*result) % 1000000007;
  }
  print(result);
}

main();


Dmitry has an array of $$$n$$$ non-negative integers $$$a_1, a_2, \dots, a_n$$$.In one operation, Dmitry can choose any index $$$j$$$ ($$$1 \le j \le n$$$) and increase the value of the element $$$a_j$$$ by $$$1$$$. He can choose the same index $$$j$$$ multiple times.For each $$$i$$$ from $$$0$$$ to $$$n$$$, determine whether Dmitry can make the $$$\mathrm{MEX}$$$ of the array equal to exactly $$$i$$$. If it is possible, then determine the minimum number of operations to do it.The $$$\mathrm{MEX}$$$ of the array is equal to the minimum non-negative integer that is not in the array. For example, the $$$\mathrm{MEX}$$$ of the array $$$[3, 1, 0]$$$ is equal to $$$2$$$, and the array $$$[3, 3, 1, 4]$$$ is equal to $$$0$$$.

The first line of input data contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input.  The descriptions of the test cases follow. The first line of the description of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of the array $$$a$$$. The second line of the description of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le n$$$) — elements of the array $$$a$$$. It is guaranteed that the sum of the values $$$n$$$ over all test cases in the test does not exceed $$$2\cdot10^5$$$.

For each test case, output $$$n + 1$$$ integer — $$$i$$$-th number is equal to the minimum number of operations for which you can make the array $$$\mathrm{MEX}$$$ equal to $$$i$$$ ($$$0 \le i \le n$$$), or -1 if this cannot be done.

NoteIn the first set of example inputs, $$$n=3$$$:  to get $$$\mathrm{MEX}=0$$$, it is enough to perform one increment: $$$a_1$$$++;  to get $$$\mathrm{MEX}=1$$$, it is enough to perform one increment: $$$a_2$$$++;  $$$\mathrm{MEX}=2$$$ for a given array, so there is no need to perform increments;  it is impossible to get $$$\mathrm{MEX}=3$$$ by performing increments.

'use strict';

function solve(n, a) {
  let map = new Map();
  const res = [];
  const heap = [];
  for (let num of a) {
    var _map$get;
    map.set(num, ((_map$get = map.get(num)) !== null && _map$get !== void 0 ? _map$get : 0) + 1);
  }
  let cur = 0,
      op = 0;
  for (let i = 0; i <= n; i++) {
    if (cur === i) {
      if (map.has(i)) {
        res[i] = op + map.get(i);
        if (map.get(i) > 1) {
          heap.push([i, map.get(i) - 1]);
        }
        cur++;
      } else {
        res[i] = op;
        if (heap.length) {
          const top = heap[heap.length - 1];
          op += i - top[0];
          top[1]--;
          while (heap.length && heap[heap.length - 1][1] === 0) heap.pop();
          cur++;
        }
      }
    } else {
      res[i] = -1;
    }
  }
  return res.join(' ');
}

async function main(read) {
  try {
    let t = Number(await read());
    let res = new Array(t);
    for (let i = 0; i < t; i++) {
      const n = Number(await read());
      const a = (await read()).split(' ').map(Number);
      res[i] = solve(n, a);
    }
    return res.join('\n');
  } catch (error) {
    console.log(error);
  }
}

let inputs,
    str = '';
function read() {
  return inputs.next().value.trim();
}
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', input => str += input);
process.stdin.on('end', async () => {
  inputs = str.split('\n').values();
  const output = await main(read);
  process.stdout.write(output);
});

'use strict';

function _defineProperty(obj, key, value) {
  if (key in obj) {
    Object.defineProperty(obj, key, {
      value: value,
      enumerable: true,
      configurable: true,
      writable: true
    });
  } else {
    obj[key] = value;
  }

  return obj;
}

function solve(n, a) {
  let map = new Map();
  const res = [];
  const heap = new Heap((a, b) => b[0] - a[0]);
  for (let num of a) {
    var _map$get;
    map.set(num, ((_map$get = map.get(num)) !== null && _map$get !== void 0 ? _map$get : 0) + 1);
  }
  let cur = 0,
      op = 0;
  for (let i = 0; i <= n; i++) {
    if (cur === i) {
      if (map.has(i)) {
        res[i] = op + map.get(i);
        if (map.get(i) > 1) {
          heap.push([i, map.get(i) - 1]);
        }
        cur++;
      } else {
        res[i] = op;
        if (heap.size()) {
          const top = heap.top();
          op += i - top[0];
          top[1]--;
          if (top[1] === 0) heap.pop();
          cur++;
        }
      }
    } else {
      res[i] = -1;
    }
  }
  return res.join(' ');
}
class Heap {
  constructor(_comparator, arr) {
    _defineProperty(this, "_heap", []);
    this._comparator = _comparator;
    if (arr) {
      this._heap = [...arr];
      for (let i = arr.length >> 1; i; i--) this.down(i);
    }
  }
  swap(i, j) {
    [this._heap[i], this._heap[j]] = [this._heap[j], this._heap[i]];
  }
  _has(i) {
    return Object.prototype.hasOwnProperty.call(this._heap, i);
  }
  comparator(i, j) {
    return this._comparator(this._heap[i], this._heap[j]) < 0;
  }
  parent(i) {
    return i - 1 >> 1;
  }
  children(i) {
    return [2 * i + 1, 2 * i + 2];
  }
  up(i) {
    let parent = this.parent(i);
    while (i > 0 && this.comparator(i, parent)) {
      this.swap(i, parent);
      [i, parent] = [parent, parent - 1 >> 1];
    }
  }
  down(i) {
    let [left, right] = this.children(i);
    while (left < this._heap.length) {
      if (!this._has(right)) right = left;
      if (this.comparator(right, left)) [left, right] = [right, left];
      if (this.comparator(i, left)) break;
      this.swap(i, left);
      i = left;
      [left, right] = [2 * i + 1, 2 * i + 2];
    }
  }
  push(node) {
    this._heap.push(node);
    this.up(this._heap.length - 1);
  }
  pop() {
    if (this.size() === 0) return null;
    let res = this._heap[0];
    this.swap(0, this._heap.length - 1);
    this._heap.pop();
    this.down(0);
    return res;
  }
  remove(i) {
    if (i >= this.size()) return;
    let res = this._heap[i];
    this.swap(i, this._heap.length - 1);
    this._heap.pop();
    this.down(i);
    this.up(i);
    return res;
  }
  modify(i, val) {
    if (i >= this.size()) return;
    let res = this._heap[i];
    this._heap[i] = val;
    this.down(i);
    this.up(i);
    return res;
  }
  top() {
    if (this.size() === 0) return null;
    return this._heap[0];
  }
  size() {
    return this._heap.length;
  }
}

async function main(read) {
  try {
    let t = Number(await read());
    let res = new Array(t);
    for (let i = 0; i < t; i++) {
      const n = Number(await read());
      const a = (await read()).split(' ').map(Number);
      res[i] = solve(n, a);
    }
    return res.join('\n');
  } catch (error) {
    console.log(error);
  }
}

let inputs,
    str = '';
function read() {
  return inputs.next().value.trim();
}
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', input => str += input);
process.stdin.on('end', async () => {
  inputs = str.split('\n').values();
  const output = await main(read);
  process.stdout.write(output);
});

var dataitr;
var stdin_input = "";
process.stdin.resume();
process.stdin.setEncoding("ascii");
process.stdin.on("data", function (input) { stdin_input += input; });
process.stdin.on("end", function () { dataitr = stdin_input.split('\n').values(); main(); });

const rl = () => dataitr.next().value.trim();
const rn = () => Number(rl());
const ra = () => rl().split(' ');
const rna = () => ra().map(x => Number(x));

const mxN = 2e5+1;

function main () {
	let T = rn();
	while (T--) {
		const n = rn();
		let a = rna();

		const frq = []; //Array(mxN).fill(0);
		for (const x of a) {
			if (!frq[x]) {
				frq[x] = 0;
			}
			frq[x]++;
		}

		a = [...new Set(a)];
		a.sort((x, y) => x - y);

		const ans = [];
		let cost = 0, extras = [], i = 0;
		for (let x = 0; x <= n; x++) {
			if (a[i] == x) {
				ans[x] = cost + frq[x];
				extras.push(...Array(frq[x]-1).fill(x));
				i++;
			} else { // a[i] > x
				ans[x] = cost;
				if (extras.length) {
					cost += x - extras.pop();
				} else {
					break;
				}
			}
		}

		while (ans.length < n+1) {
			ans.push(-1);
		}

		console.log(ans.join(' '));
	}
}

'use strict';
// HEADER:
const print = console.log;
let IS_MULTITEST, testN;
const pcalc = testN=>{
  let res = calc(testN);
  if (Array.isArray(res)){
    pa(res);
  } else if (Number.isFinite(res) || typeof res=='string'){
    print(res);
  }
};

const main = ()=>{
  if (IS_MULTITEST){
    let T = +readline()
    for (testN=1; testN<=T; testN++)
      pcalc(T);
  } else
   pcalc();
};

const lines = [];
const rl = require('readline').createInterface({
   input: process.stdin, output: process.stdout});
rl.on('line', line=>{
  lines.push(line);
});
rl.on('close', main);
let rli = 0;
const readline = ()=>lines[rli++];
const L = process.env.DEBUG ? console.log : ()=>{};
const LT = obj=>L(`Test #${testN}`, obj);

const ra = ()=>readline().split(' ').map(a=>+a)
const pa = arr=>print(arr.join(' '));

// SOLUTION:
IS_MULTITEST = 1;

const calc = ()=>{
  // READING:
  let [n] = ra();
  let A = ra();
  LT({n, A});
  // PROCESSING:
  let cnt = {};
  for (let i=0; i<n; i++)
    cnt[i] = 0;
  for (let i=0; i<n; i++){
    cnt[A[i]]++;
  }
  let ans = [];
  let stack = [];
  let fix_zeros_price = 0;
  for (let i=0; i<=n; i++){
    ans.push(fix_zeros_price+(i<n ? cnt[i] : 0));
    while (cnt[i]>1){
      stack.push(i);
      cnt[i]--;
    }
    if (cnt[i]==0){
      if (stack.length){
        let top = stack.pop();
        fix_zeros_price += i-top;
      } else {
        ans.push(-1);
        break;
      }
    }
  }
  while (ans.length<=n) ans.push(-1)
  L(cnt)

  return ans;
};

'use strict';
const readline = require('readline');
const util = require('util');
const rl = readline.createInterface({
    input: process.stdin,
});
let pos = 0;
const words = [];
rl.on('line', (line) => {
    words.push.apply(words, line.split(' ').filter(s => s != ''));
});
rl.on('close', main);
function check(b) {
    if (!b) {
        process.stderr.write(`${new Error().stack}\n`);
        process.exit(-1);
    }
}
function printf(...args) {
    process.stdout.write(util.format(...args));
}
function nextInt() {
    return Math.trunc(Number(words[pos++]));
}
function nextStr() {
    return words[pos++];
}
function af(l) {
    return Array.from({ length: l });
}
const INIT = -1;
function main() {
    let tc = nextInt();
    while (tc-- > 0) {
        let n = nextInt();
        let cnt = af(n + 1).fill(0);
        for (let i = 0; i < n; i++) {
            let x = nextInt();
            cnt[x]++;
        }
        let sum = 0; // sum to complete j < i
        let rem = []; // indices with extra
        let res = af(n + 1).fill(0);
        let i = 0;
        for (; i <= n; i++) {
            let ans = sum;
            if (cnt[i] > 0) {
                ans += cnt[i];
            }
            printf("%d ", ans);
            if (cnt[i] > 1) {
                rem.push(i);
                res[i] = cnt[i] - 1;
            }
            else if (cnt[i] == 0) {
                if (rem.length > 0) {
                    let last = rem[rem.length - 1];
                    sum += i - last;
                    res[last]--;
                    if (res[last] == 0) {
                        rem.pop();
                    }
                }
                else {
                    i++;
                    break;
                }
            }
        }
        for (; i <= n; i++) {
            printf("-1 ");
        }
        printf("\n");
    }
}

// Generated by CoffeeScript 1.8.0
;(function() {
  var Heap, defaultCmp, floor, heapify, heappop, heappush, heappushpop, heapreplace, insort, min, nlargest, nsmallest, updateItem, _siftdown, _siftup;

  floor = Math.floor, min = Math.min;

  /*
  Default comparison function to be used
   */

  defaultCmp = function(x, y) {
    if (x < y) {
      return -1;
    }
    if (x > y) {
      return 1;
    }
    return 0;
  };

  /*
  Insert item x in list a, and keep it sorted assuming a is sorted.

  If x is already in a, insert it to the right of the rightmost x.

  Optional args lo (default 0) and hi (default a.length) bound the slice
  of a to be searched.
   */

  insort = function(a, x, lo, hi, cmp) {
    var mid;
    if (lo == null) {
      lo = 0;
    }
    if (cmp == null) {
      cmp = defaultCmp;
    }
    if (lo < 0) {
      throw new Error('lo must be non-negative');
    }
    if (hi == null) {
      hi = a.length;
    }
    while (lo < hi) {
      mid = floor((lo + hi) / 2);
      if (cmp(x, a[mid]) < 0) {
        hi = mid;
      } else {
        lo = mid + 1;
      }
    }
    return ([].splice.apply(a, [lo, lo - lo].concat(x)), x);
  };

  /*
  Push item onto heap, maintaining the heap invariant.
   */

  heappush = function(array, item, cmp) {
    if (cmp == null) {
      cmp = defaultCmp;
    }
    array.push(item);
    return _siftdown(array, 0, array.length - 1, cmp);
  };

  /*
  Pop the smallest item off the heap, maintaining the heap invariant.
   */

  heappop = function(array, cmp) {
    var lastelt, returnitem;
    if (cmp == null) {
      cmp = defaultCmp;
    }
    lastelt = array.pop();
    if (array.length) {
      returnitem = array[0];
      array[0] = lastelt;
      _siftup(array, 0, cmp);
    } else {
      returnitem = lastelt;
    }
    return returnitem;
  };

  /*
  Pop and return the current smallest value, and add the new item.

  This is more efficient than heappop() followed by heappush(), and can be
  more appropriate when using a fixed size heap. Note that the value
  returned may be larger than item! That constrains reasonable use of
  this routine unless written as part of a conditional replacement:
      if item > array[0]
        item = heapreplace(array, item)
   */

  heapreplace = function(array, item, cmp) {
    var returnitem;
    if (cmp == null) {
      cmp = defaultCmp;
    }
    returnitem = array[0];
    array[0] = item;
    _siftup(array, 0, cmp);
    return returnitem;
  };

  /*
  Fast version of a heappush followed by a heappop.
   */

  heappushpop = function(array, item, cmp) {
    var _ref;
    if (cmp == null) {
      cmp = defaultCmp;
    }
    if (array.length && cmp(array[0], item) < 0) {
      _ref = [array[0], item], item = _ref[0], array[0] = _ref[1];
      _siftup(array, 0, cmp);
    }
    return item;
  };

  /*
  Transform list into a heap, in-place, in O(array.length) time.
   */

  heapify = function(array, cmp) {
    var i, _i, _j, _len, _ref, _ref1, _results, _results1;
    if (cmp == null) {
      cmp = defaultCmp;
    }
    _ref1 = (function() {
      _results1 = [];
      for (var _j = 0, _ref = floor(array.length / 2); 0 <= _ref ? _j < _ref : _j > _ref; 0 <= _ref ? _j++ : _j--){ _results1.push(_j); }
      return _results1;
    }).apply(this).reverse();
    _results = [];
    for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
      i = _ref1[_i];
      _results.push(_siftup(array, i, cmp));
    }
    return _results;
  };

  /*
  Update the position of the given item in the heap.
  This function should be called every time the item is being modified.
   */

  updateItem = function(array, item, cmp) {
    var pos;
    if (cmp == null) {
      cmp = defaultCmp;
    }
    pos = array.indexOf(item);
    if (pos === -1) {
      return;
    }
    _siftdown(array, 0, pos, cmp);
    return _siftup(array, pos, cmp);
  };

  /*
  Find the n largest elements in a dataset.
   */

  nlargest = function(array, n, cmp) {
    var elem, result, _i, _len, _ref;
    if (cmp == null) {
      cmp = defaultCmp;
    }
    result = array.slice(0, n);
    if (!result.length) {
      return result;
    }
    heapify(result, cmp);
    _ref = array.slice(n);
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      elem = _ref[_i];
      heappushpop(result, elem, cmp);
    }
    return result.sort(cmp).reverse();
  };

  /*
  Find the n smallest elements in a dataset.
   */

  nsmallest = function(array, n, cmp) {
    var elem, i, los, result, _i, _j, _len, _ref, _ref1, _results;
    if (cmp == null) {
      cmp = defaultCmp;
    }
    if (n * 10 <= array.length) {
      result = array.slice(0, n).sort(cmp);
      if (!result.length) {
        return result;
      }
      los = result[result.length - 1];
      _ref = array.slice(n);
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        elem = _ref[_i];
        if (cmp(elem, los) < 0) {
          insort(result, elem, 0, null, cmp);
          result.pop();
          los = result[result.length - 1];
        }
      }
      return result;
    }
    heapify(array, cmp);
    _results = [];
    for (i = _j = 0, _ref1 = min(n, array.length); 0 <= _ref1 ? _j < _ref1 : _j > _ref1; i = 0 <= _ref1 ? ++_j : --_j) {
      _results.push(heappop(array, cmp));
    }
    return _results;
  };

  _siftdown = function(array, startpos, pos, cmp) {
    var newitem, parent, parentpos;
    if (cmp == null) {
      cmp = defaultCmp;
    }
    newitem = array[pos];
    while (pos > startpos) {
      parentpos = (pos - 1) >> 1;
      parent = array[parentpos];
      if (cmp(newitem, parent) < 0) {
        array[pos] = parent;
        pos = parentpos;
        continue;
      }
      break;
    }
    return array[pos] = newitem;
  };

  _siftup = function(array, pos, cmp) {
    var childpos, endpos, newitem, rightpos, startpos;
    if (cmp == null) {
      cmp = defaultCmp;
    }
    endpos = array.length;
    startpos = pos;
    newitem = array[pos];
    childpos = 2 * pos + 1;
    while (childpos < endpos) {
      rightpos = childpos + 1;
      if (rightpos < endpos && !(cmp(array[childpos], array[rightpos]) < 0)) {
        childpos = rightpos;
      }
      array[pos] = array[childpos];
      pos = childpos;
      childpos = 2 * pos + 1;
    }
    array[pos] = newitem;
    return _siftdown(array, startpos, pos, cmp);
  };

  Heap = (function() {
    Heap.push = heappush;

    Heap.pop = heappop;

    Heap.replace = heapreplace;

    Heap.pushpop = heappushpop;

    Heap.heapify = heapify;

    Heap.updateItem = updateItem;

    Heap.nlargest = nlargest;

    Heap.nsmallest = nsmallest;

    function Heap(cmp) {
      this.cmp = cmp != null ? cmp : defaultCmp;
      this.nodes = [];
    }

    Heap.prototype.push = function(x) {
      return heappush(this.nodes, x, this.cmp);
    };

    Heap.prototype.pop = function() {
      return heappop(this.nodes, this.cmp);
    };

    Heap.prototype.peek = function() {
      return this.nodes[0];
    };

    Heap.prototype.contains = function(x) {
      return this.nodes.indexOf(x) !== -1;
    };

    Heap.prototype.replace = function(x) {
      return heapreplace(this.nodes, x, this.cmp);
    };

    Heap.prototype.pushpop = function(x) {
      return heappushpop(this.nodes, x, this.cmp);
    };

    Heap.prototype.heapify = function() {
      return heapify(this.nodes, this.cmp);
    };

    Heap.prototype.updateItem = function(x) {
      return updateItem(this.nodes, x, this.cmp);
    };

    Heap.prototype.clear = function() {
      return this.nodes = [];
    };

    Heap.prototype.empty = function() {
      return this.nodes.length === 0;
    };

    Heap.prototype.size = function() {
      return this.nodes.length;
    };

    Heap.prototype.clone = function() {
      var heap;
      heap = new Heap();
      heap.nodes = this.nodes.slice(0);
      return heap;
    };

    Heap.prototype.toArray = function() {
      return this.nodes.slice(0);
    };

    Heap.prototype.insert = Heap.prototype.push;

    Heap.prototype.top = Heap.prototype.peek;

    Heap.prototype.front = Heap.prototype.peek;

    Heap.prototype.has = Heap.prototype.contains;

    Heap.prototype.copy = Heap.prototype.clone;

    return Heap;

  })();

  (function(root, factory) {
    // if (typeof define === 'function' && define.amd) {
    //   return define([], factory);
    // } else if (typeof exports === 'object') {
    //   return module.exports = factory();
    // } else {
      return root.Heap = factory();
    // }
  })(this, function() {
    return Heap;
  });

}).call(this);
const Heap = this.Heap

const readline = require('readline')

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
})
const lines = []
rl.on('line', (input) => {
    lines.push(input);
})
rl.on('close', () => {
// (function() {
    // const lines = require('fs').readFileSync('test.in', 'utf8').split('\n')
    let l = 0;
    let t = +lines[l++]
    const output = []
    for (let i = 0; i < t; i++) {
        const n = +lines[l++]
        const arr = lines[l++].trim().split(' ').map(Number)
        output[i] = solve(n, arr)
    }
    console.log(output.join('\n'))
// })()
})

function solve(n, arr) {
    const map = arr.reduce((o, x) => {
        o[x] = (o[x] || 0) + 1
        return o
    }, {})
    let mex = -1
    for (let i = 0; i <= n; i++) {
        if (!map[i]) {
            mex = i
            break
        }
    }
    const ans = Array(n + 1)
    let agg = 0
    const h = new Heap((a, b) => b - a)
    let fail = 0
    for (let i = 0; i <= n; i++) {
        if (i < mex) {
            // i -> i + 1
            ans[i] = map[i]
        } else if (i > mex) {
            if (fail) {
                ans[i] = -1
                continue
            }
            let now = agg
            // i -> i + 1
            if (map[i]) now += map[i]
            ans[i] = now
        } else {
            ans[i] = 0
        }
        //
        if (!map[i]) {
            if (h.empty()) {
                fail = 1
                continue
            }
            const p = h.peek()
            if (--map[p] <= 1) h.pop()
            agg += i - p
        } else if (map[i] > 1) {
            h.push(i)
        }
    }
    return ans.join(' ')
}