#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
bool col[11] = {};
bool d1[21] = {};
bool d2[21] = {};
int backtrack(int i, int n, bool col[], bool d1[], bool d2[])
{
int cnt = 0;
if(i == (n+1))
{
return 1;
}
for(int j = 1; j <= n; j++)
{
if(!col[j] && !d1[n + i - j] && !d2[i + j - 1])
{
col[j] = d1[n + i - j] = d2[i + j - 1] = 1;
cnt += backtrack(i + 1, n, col, d1, d2);
col[j] = d1[n + i - j] = d2[i + j - 1] = 0;
}
}
return cnt;
}
int totalNQueens(int n)
{
if(n == 1) return 1;
return backtrack(1, n, col, d1, d2);
}
};
int total_test;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
Solution Solver;
cin>>total_test;
while(total_test--)
{
int n;
cin>>n;
cout<<Solver.totalNQueens(n)<<endl;
}
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgpjbGFzcyBTb2x1dGlvbgp7CnB1YmxpYzoKICAgIGJvb2wgY29sWzExXSA9IHt9OwogICAgYm9vbCBkMVsyMV0gPSB7fTsKICAgIGJvb2wgZDJbMjFdID0ge307CiAgICBpbnQgYmFja3RyYWNrKGludCBpLCBpbnQgbiwgYm9vbCBjb2xbXSwgYm9vbCBkMVtdLCBib29sIGQyW10pCiAgICB7CiAgICAgICAgaW50IGNudCA9IDA7CiAgICAgICAgaWYoaSA9PSAobisxKSkKICAgICAgICB7CiAgICAgICAgICAgIHJldHVybiAxOwogICAgICAgIH0KCiAgICAgICAgZm9yKGludCBqID0gMTsgaiA8PSBuOyBqKyspCiAgICAgICAgewogICAgICAgICAgICBpZighY29sW2pdICYmICFkMVtuICsgaSAtIGpdICYmICFkMltpICsgaiAtIDFdKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBjb2xbal0gPSBkMVtuICsgaSAtIGpdID0gZDJbaSArIGogLSAxXSA9IDE7CiAgICAgICAgICAgICAgICBjbnQgKz0gYmFja3RyYWNrKGkgKyAxLCBuLCBjb2wsIGQxLCBkMik7CiAgICAgICAgICAgICAgICBjb2xbal0gPSBkMVtuICsgaSAtIGpdID0gZDJbaSArIGogLSAxXSA9IDA7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgcmV0dXJuIGNudDsKICAgIH0KCiAgICBpbnQgdG90YWxOUXVlZW5zKGludCBuKQogICAgewogICAgICAgIGlmKG4gPT0gMSkgcmV0dXJuIDE7CiAgICAgICAgcmV0dXJuIGJhY2t0cmFjaygxLCBuLCBjb2wsIGQxLCBkMik7CiAgICB9Cn07CgppbnQgdG90YWxfdGVzdDsKCmludCBtYWluKCkKewogICAgaW9zOjpzeW5jX3dpdGhfc3RkaW8oMCk7CiAgICBjaW4udGllKDApOyBjb3V0LnRpZSgwKTsKCiAgICBTb2x1dGlvbiBTb2x2ZXI7CgogICAgY2luPj50b3RhbF90ZXN0OwogICAgd2hpbGUodG90YWxfdGVzdC0tKQogICAgewogICAgICAgIGludCBuOwogICAgICAgIGNpbj4+bjsKICAgICAgICBjb3V0PDxTb2x2ZXIudG90YWxOUXVlZW5zKG4pPDxlbmRsOwogICAgfQp9Cg==