Lock Patterns and Spanning Trees

No attempts yetTime limit1sMemory limit128 MB

Problem

Android uses a pattern to lock a smartphone. The common lock pattern is a 3×3 grid of 9 nodes. Sanghyun wants stronger security, so he is building an app that offers the lock patterns described here.

In his app a lock pattern has a size from 2×2 up to m×mm \times m, and only adjacent nodes can be connected. Two nodes are adjacent when one is directly next to the other horizontally, vertically, or diagonally. A node at one of the four corners is connected to 3 nodes, and every other node is connected to at most 8.

A lock pattern always has to form a spanning tree. A spanning tree is a set of edges of the graph that contains no closed loop. It also has to give a path between any two nodes, and it consists of m2=nm^2 = n vertices and n1n-1 edges. A single lock pattern admits many different spanning trees.

To count the spanning trees of a graph GG, first number every vertex v1,,vnv_1, \dots, v_n. Then build the matrix T=[tij]T = [t_{ij}] this way.

  • if i=ji = j, then tijt_{ij} is the number of edges incident to viv_i
  • if iji \ne j, then tijt_{ij} is 1-1 when there is an edge between viv_i and vjv_j, and 00 when there is none

The number of spanning trees is a cofactor of TT.

cofactor of tij=(1)i+jMij\text{cofactor of } t_{ij} = (-1)^{i+j} M_{ij}

Here MijM_{ij} is the determinant of the (n1)×(n1)(n-1) \times (n-1) matrix obtained by deleting row ii and column jj from TT. Every cofactor of TT has the same value, whichever ii and jj you pick.

For example, the matrix TT of the 2×2 pattern is as follows.

T=(3111131111311113)T = \begin{pmatrix} 3 & -1 & -1 & -1 \\ -1 & 3 & -1 & -1 \\ -1 & -1 & 3 & -1 \\ -1 & -1 & -1 & 3 \end{pmatrix}

Taking i=1i = 1 and j=1j = 1, the cofactor comes out like this.

(1)1+1M11=311131113=16(-1)^{1+1} M_{11} = \begin{vmatrix} 3 & -1 & -1 \\ -1 & 3 & -1 \\ -1 & -1 & 3 \end{vmatrix} = 16

So the 2×2 pattern has 16 spanning trees.

The matrix TT of the 3×3 pattern is as follows.

T=(310110000151111000013011000110510110111181111011015011000110310000111151000011013)T = \begin{pmatrix} 3&-1&0&-1&-1&0&0&0&0 \\ -1&5&-1&-1&-1&-1&0&0&0 \\ 0&-1&3&0&-1&-1&0&0&0 \\ -1&-1&0&5&-1&0&-1&-1&0 \\ -1&-1&-1&-1&8&-1&-1&-1&-1 \\ 0&-1&-1&0&-1&5&0&-1&-1 \\ 0&0&0&-1&-1&0&3&-1&0 \\ 0&0&0&-1&-1&-1&-1&5&-1 \\ 0&0&0&0&-1&-1&0&-1&3 \end{pmatrix}

The cofactor of this matrix is the number of spanning trees of the 3×3 pattern.

Write a program that counts the spanning trees you can make on an m×mm \times m pattern.

Input

The first line contains the number of test cases NN (1N51 \le N \le 5). Each of the next NN lines holds one test case, the pattern size mm. (2m62 \le m \le 6)

Output

For each test case, print on its own line the number of spanning trees you can make on the m×mm \times m pattern.

Hint

The m=2m = 2 pattern is a graph of 4 vertices that are all connected to each other, and it has 16 spanning trees.