Welcome to Sevenkplus' perfect math class! In this class we will study an algebraic structure called magma.
A magma is a nonempty set equipped with a binary operation . We write for the application of the operator on the two elements . Note that there are no restrictions on the binary operation. For example, we cannot assume that always holds.
There are many different types of magmas. Some are interesting, others are even more interesting. Studying specific types of magmas is an important part of mathematics. Below we introduce the following types of magmas.
A quasigroup is a magma such that for all , there exists a unique such that and a unique such that .
A loop is a quasigroup such that there is a unique element such that for all , .
A semigroup is a magma such that for all , we have .
A monoid is a semigroup such that there is a uniqe element such that for all , .
A group is a monoid such that for all , there exists such that .
An abelian group is a group such that for all , we have .
A rack is a magma such that (1) for all , we have and (2) for all , there exists a unique such that .
A quandle is a rack such that for all , we have .
In this problem, you are given several magmas. You have to tell us what types they are.
Input Format
The first line is a integer , the number of magmas given to you. Following are magmas.
For each magma, the first line is an integer , the size of the set . (Yes, you only need to consider finite algebraic structures here, which greatly simplifies the problem.) Without loss of generality, we assume that .
Following are lines. Each line contains space-separated integers. The -th number on the -th line is where is the binary operation of the magma.
Constraints
.
Output Format
For each magma, output one line, which is the magic number of the magma (denoted by henceforth).
The magic number of a magma is defined as:
- Initially, let .
- If the magma is a quasigroup, add to .
- If the magma is a loop, add to .
- If the magma is a semigroup, add to .
- If the magma is a monoid, add to .
- If the magma is a group, add to .
- If the magma is an abelian group, add to .
- If the magma is a rack, add to .
- If the magma is a quandle, add to .
- Then is the magic number of the magma.
Sample Input
2
1
0
2
1 1
1 0
Sample Output
255
0
Explanation
The first magma satisfies all rules while the second one satisfies none.