#include <bits/stdc++.h>
using namespace std;
#define int long long
const int INF = 1e12;
int comb2(int x)
{
return x * (x-1) / 2;
}
int n,m;
bool verif(vector<int> v)
{
int pref = 0;
for(int i=1;i<=n;i++)
{
if(i > 1 && v[i-1] > v[i])
return 0;
pref += v[i];
if(pref < comb2(i))
return 0;
}
if(pref != comb2(n))
return 0;
return 1;
}
int calc(vector<int> v)
{
int tot = 0;
for(int i=1;i<=n;i++)
tot += comb2(v[i]);
return tot;
}
bool done;
vector<int> sol;
map<vector<int>, bool> visited;
void recursiv(vector<int> v)
{
if(done)
return;
if(visited[v])
return;
visited[v] = 1;
if(calc(v) == m)
{
done = 1;
sol = v;
return;
}
vector<pair<int,vector<int>>> ord;
for(int from=2;from<=n;from++)
{
for(int to=1;to<from;to++)
{
vector<int> newv = v;
newv[from]--;
newv[to]++;
if(newv[from] >= 0 && verif(newv))
{
int mydist = abs(calc(newv) - m);
ord.push_back({mydist, newv});
}
}
}
sort(ord.begin(), ord.end());
for(auto [_,newv]:ord)
recursiv(newv);
}
int mat[5005][5005];
void reconstruct(vector<int> v)
{
assert(verif(v));
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
mat[i][j] = 0;
vector<pair<int,int>> aux;
for(int i=1;i<=n;i++)
aux.push_back({v[i], i});
while(1)
{
sort(aux.begin(), aux.end());
if(aux.back().first == 0)
break;
for(int i=0;i<aux.back().first;i++)
{
mat[aux.back().second][aux[i].second] = 1;
}
for(int i=aux.back().first;i<n-1;i++)
{
aux[i].first--;
mat[aux[i].second][aux.back().second] = 1;
}
aux.back().first = 0;
}
for(int i=2;i<=n;i++)
{
for(int j=1;j<i;j++)
cout<<mat[i][j];
cout<<"\n";
}
}
void solve()
{
cin>>n>>m;
m = n * (n-1) * (n-2) / 6 - m;
//get close to m then do backtrack
done = 0;
visited.clear();
vector<int> init(n+2,0);
for(int i=1;i<=n;i++)
init[i] = i - 1;
assert(verif(init));
if(m > calc(init))
{
cout<<"No\n";
return;
}
recursiv(init);
if(!done)
{
cout<<"No\n";
return;
}
cout<<"Yes\n";
reconstruct(sol);
}
signed main()
{
ios_base::sync_with_stdio(0);cin.tie(0);
int t;
cin>>t;
while(t--)
solve();
return 0;
}
/*
for each "bad" tuple (x,y,z) of nodes,
exactly one of x,y,z will have outgoing edges towards the other 2
=> M = total - sum(comb(cnt_outgoing(x), 2))
*/