#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define all(x) (x).begin(), (x).end()
typedef long long ll;
int readInt() { int x; return cin >> x, x; }
int n, k;
vector<ll> BIT;
void update(int p) {
for (; p <= n; p += p & -p)
BIT[p]++;
}
void reverse(int p) {
for (; p <= n; p += p & -p)
BIT[p]--;
}
ll getValue(int p) {
ll res = 0;
for (p--; p > 0; p -= p & -p)
res += BIT[p];
return res;
}
int query()
{
cin >> n;
vector<int> perm(n + 1);
for (int i = 1; i <= n; ++i)
perm[i] = i;
vector<ll> cal(n + 1, 0);
BIT.assign(n + 1, 0);
for (int i = 1; i <= n; ++i)
{
cal[i] = cal[i - 1] + 1LL * perm[i] * getValue(perm[i]);
update(perm[i]);
}
ll res = cal[n];
vector<int> n_perm(perm);
while (true) {
if (!next_permutation(1 + all(n_perm))) break;
int p = 1;
for (int l = 1, r = n; l <= r; )
{
int m = (l + r)/ 2;
if (perm[m] == n_perm[m] && perm[m - 1] == n_perm[m - 1])
{
p = m;
l = m + 1;
}
else
r = m - 1;
}
for (int i = p; i <= n; ++i)
reverse(perm[i]);
for (int i = p; i <= n; ++i)
{
cal[i] = cal[i - 1] + 1LL * n_perm[i] * getValue(n_perm[i]);
update(n_perm[i]);
}
res += cal[n];
next_permutation(1 + all(perm));
}
cout << res << '\n';
return 0;
}
int main()
{
for (int q = readInt(); q--; )
{
if (query());
}
}
I2luY2x1ZGUgPGFsZ29yaXRobT4KI2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKI2RlZmluZSBhbGwoeCkgKHgpLmJlZ2luKCksICh4KS5lbmQoKQp0eXBlZGVmIGxvbmcgbG9uZyBsbDsKaW50IHJlYWRJbnQoKSB7IGludCB4OyByZXR1cm4gY2luID4+IHgsIHg7IH0KCmludCBuLCBrOwp2ZWN0b3I8bGw+IEJJVDsKCnZvaWQgdXBkYXRlKGludCBwKSB7CiAgICBmb3IgKDsgcCA8PSBuOyBwICs9IHAgJiAtcCkKICAgICAgICBCSVRbcF0rKzsKfQoKdm9pZCByZXZlcnNlKGludCBwKSB7CiAgICBmb3IgKDsgcCA8PSBuOyBwICs9IHAgJiAtcCkKICAgICAgICBCSVRbcF0tLTsKfQoKbGwgZ2V0VmFsdWUoaW50IHApIHsKICAgIGxsIHJlcyA9IDA7CiAgICBmb3IgKHAtLTsgcCA+IDA7IHAgLT0gcCAmIC1wKQogICAgICAgIHJlcyArPSBCSVRbcF07CiAgICAKICAgIHJldHVybiByZXM7Cn0KCmludCBxdWVyeSgpCnsKICAgIGNpbiA+PiBuOwogICAgdmVjdG9yPGludD4gcGVybShuICsgMSk7CiAgICBmb3IgKGludCBpID0gMTsgaSA8PSBuOyArK2kpCiAgICAgICAgcGVybVtpXSA9IGk7CgogICAgdmVjdG9yPGxsPiBjYWwobiArIDEsIDApOwogICAgQklULmFzc2lnbihuICsgMSwgMCk7CiAgICBmb3IgKGludCBpID0gMTsgaSA8PSBuOyArK2kpCiAgICB7CiAgICAgICAgY2FsW2ldID0gY2FsW2kgLSAxXSArIDFMTCAqIHBlcm1baV0gKiBnZXRWYWx1ZShwZXJtW2ldKTsKICAgICAgICB1cGRhdGUocGVybVtpXSk7CiAgICB9CgogICAgbGwgcmVzID0gY2FsW25dOwogICAgdmVjdG9yPGludD4gbl9wZXJtKHBlcm0pOwogICAgd2hpbGUgKHRydWUpIHsKICAgICAgICBpZiAoIW5leHRfcGVybXV0YXRpb24oMSArIGFsbChuX3Blcm0pKSkgYnJlYWs7CiAgICAgICAgaW50IHAgPSAxOwogICAgICAgIGZvciAoaW50IGwgPSAxLCByID0gbjsgbCA8PSByOyApCiAgICAgICAgewogICAgICAgICAgICBpbnQgbSA9IChsICsgcikvIDI7CiAgICAgICAgICAgIGlmIChwZXJtW21dID09IG5fcGVybVttXSAmJiBwZXJtW20gLSAxXSA9PSBuX3Blcm1bbSAtIDFdKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBwID0gbTsKICAgICAgICAgICAgICAgIGwgPSBtICsgMTsKICAgICAgICAgICAgfQogICAgICAgICAgICBlbHNlIAogICAgICAgICAgICAgICAgciA9IG0gLSAxOwogICAgICAgIH0KCiAgICAgICAgZm9yIChpbnQgaSA9IHA7IGkgPD0gbjsgKytpKQogICAgICAgICAgICByZXZlcnNlKHBlcm1baV0pOwoKICAgICAgICBmb3IgKGludCBpID0gcDsgaSA8PSBuOyArK2kpCiAgICAgICAgewogICAgICAgICAgICBjYWxbaV0gPSBjYWxbaSAtIDFdICsgMUxMICogbl9wZXJtW2ldICogZ2V0VmFsdWUobl9wZXJtW2ldKTsKICAgICAgICAgICAgdXBkYXRlKG5fcGVybVtpXSk7CiAgICAgICAgfQogICAgICAgIHJlcyArPSBjYWxbbl07CiAgICAgICAgbmV4dF9wZXJtdXRhdGlvbigxICsgYWxsKHBlcm0pKTsKICAgIH0KICAgIGNvdXQgPDwgcmVzIDw8ICdcbic7CiAgICByZXR1cm4gMDsKfQoKaW50IG1haW4oKQp7CiAgICBmb3IgKGludCBxID0gcmVhZEludCgpOyBxLS07ICkKICAgIHsKICAgICAgICBpZiAocXVlcnkoKSk7CgoKCiAgICB9Cn0=