#include "bits/stdc++.h"

using namespace std;
using ll = long long;
using pii = pair<int,int>;

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int tt;
  cin >> tt;
  while(tt--) {
    int n;
    cin>> n;
    int m = n*(n-1)/2;
    vector<vector<int>> ans(n,vector<int>(n));
    if (n==2)
    {
      cout << 0 << '\n';
    }
    else if (n%2 == 0) {
      for (int i = 0; i < n; i++) {
        int cnt =0;
        for (int j = i+1; j < n; j++,cnt++) {
          if (cnt < (n-1)/2) {
            cout << 1 << " ";
          }
          else if (cnt==(n-1)/2) {
            cout << 0 << " ";
          }
          else
            cout << -1 << " ";
        }
      }
      cout << '\n';
    }
    else {
      int curr = 1;
      for (int i = 0; i < m; i++) {
        cout << curr << ' ';
        curr = -curr;
      }
      cout << '\n';
    }
  }
}

// Tie is 1 point or every team
// Win gives 3
// Loss gives 0
//
// Construct a sequence of games between all the players that will give equal
// score with minimal ties

