Slides! (Large)

Decide whether a building slide graph can have exactly M paths from building 1 to B, then print the fixed canonical matrix when possible.

Medium7CombinatoricsBit manipulationImplementationNo attempts yetTime limit5sMemory limit512 MB

Problem

Gooli is a huge company that owns BB buildings in a hilly area. The buildings are numbered from 1 to BB.

The CEO wants to build a set of slides between buildings that she can use to travel from her office in building 1 to her favorite cafe in building BB. Slides are one-way only, but the buildings are tall and have elevators, so a slide can start in any building and end in any other building, and can go in either direction. Specifically, for any two buildings xx and yy, you can build either zero or one slides from xx to yy, and you can build either zero or one slides from yy to xx. The exception is that no slides are allowed to originate in building BB, since once the CEO reaches that building, she has no need to slide any further.

In honor of Gooli becoming exactly MM milliseconds old, the design must give the CEO exactly MM different ways to travel from building 1 to building BB using the new slides. A way is a sequence of buildings that starts with building 1, ends with building BB, and has the property that for each pair of consecutive buildings xx and yy in the sequence, a slide exists from xx to yy. The CEO does not require that every building be reachable from every other building via slides.

Build a set of one or more slides that satisfies the CEO's requirements, or determine that it is impossible.

Input

The first line of the input gives the number of test cases, TT. TT lines follow; each consists of two integers BB and MM, as described above.

Output

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is POSSIBLE if the CEO's requirements can be fulfilled and IMPOSSIBLE otherwise.

If it is possible, output an additional BB lines containing BB characters each, representing a matrix of slides. The jj-th character of the ii-th of these lines (with both ii and jj counting from 1) is 1 if a slide is built from building ii to building jj, and 0 otherwise.

So that the answer is unique, the matrix must be built by this rule:

  • For every pair 2i<jB2 \le i < j \le B, build a slide from building ii to building jj. Build no other slides among buildings 22 to BB.
  • If M=2B2M = 2^{B-2}, build a slide from building 1 to each of buildings 2,3,,B2, 3, \ldots, B.
  • If M<2B2M < 2^{B-2}, then for each kk with 0kB30 \le k \le B-3, build a slide from building 1 to building B1kB-1-k exactly when the bit of value 2k2^k in the binary representation of MM is 1.
  • Build no other slides. In particular, the ii-th character of the ii-th line and every character of the last line are 0.

The matrix built by this rule has exactly MM ways from building 1 to building BB.

Constraints

  • 1T1001 \le T \le 100
  • 2B502 \le B \le 50
  • 1M10181 \le M \le 10^{18}

Hint

In Case #1 (B=5B = 5, M=4M = 4), M<23M < 2^3 and only the bit of value 222^2 is set in MM, so building 1 gets a single slide, to building 512=25-1-2 = 2. The four ways to get from building 1 to building 5 are:

  • 1, 2, 5
  • 1, 2, 3, 5
  • 1, 2, 4, 5
  • 1, 2, 3, 4, 5

In Case #3, building slides from 1 to 2, 2 to 3, 3 to 1, and 1 to 4 would create infinitely many ways for the CEO to reach building 4 (she could go directly to 4, or go around the loop once and then go to 4, or go around the loop twice, and so on), but the CEO requested exactly 20 ways.