#include<bits/stdc++.h>
using namespace std;
long long n,m,t;
bool taken[100][100];
long long val[1000006];
bool cmp(vector<int> L,vector<int> R) {
if(L.size()==R.size())
return L<R;
else
return L.size()<R.size();
}
int main() {
// freopen("o.txt","w",stdout);
cin>>t;
while(t--) {
scanf("%lld %lld",&n,&m);
memset(taken,0,sizeof(taken));
if(n==1) {
if(m==0)
puts("0");
else if(m==1)
puts("1");
else
puts("-1");
continue;
}
if(m>=n-1 && m<= n*(n-1)/2+n) {
if(m-(n-1)>n) {
m=m-(n-1)-n;
val[1]=val[n]=2;
for(int i=2; i<n; i++)
val[i]=3;
for(int i=1; i<n; i++) {
taken[i][i+1]=taken[i+1][i]=1;
}
while(m--) {
int mn=INT_MAX;
vector<pair<int,int> > min_edges;
for(int i=1; i<=n; i++)
for(int j=i+1; j<=n; j++) {
if(taken[i][j]==0 && max(val[i]+1,val[j]+1)<mn) {
mn=max(val[i]+1,val[j]+1);
min_edges.clear();
}
if(taken[i][j]==0 && max(val[i]+1,val[j]+1)==mn) {
min_edges.push_back({i,j});
}
}
vector<vector<int> > pile;
vector<vector<int> > pile2;
int mn2=INT_MAX;
int indx=0;
for(auto von: min_edges) {
vector<int> boom;
for(int i=1; i<=n; i++)
if(i!=von.first && i!=von.second && (taken[i][von.first]==0 || taken[von.first][i]==0 )) {
if(max(val[i],val[von.first]+1)!=max(val[i],val[von.first])) {
boom.push_back(max(val[i],val[von.first]+1));
}
}
for(int i=1; i<=n; i++)
if(i!=von.second && i!=von.first && (taken[i][von.second]==0 || taken[von.second][i]==0)) {
if(max(val[i],val[von.second]+1)!=max(val[i],val[von.second])) {
boom.push_back(max(val[i],val[von.second]+1));
}
}
sort(boom.begin(),boom.end());
if(boom.size()==0) {
auto pr=von;
taken[pr.first][pr.second]=taken[pr.second][pr.first]=1;
val[pr.first]++;
val[pr.second]++;
goto jump;
}
pile.push_back(boom);
if(mn2>boom.back()) {
mn2=boom.back();
pile2.clear();
}
if(mn2==boom.back()) {
pile2.push_back(boom);
}
indx++;
}
sort(pile2.begin(),pile2.end(),cmp);
for(int i=0; i<pile.size(); i++)
if(pile2[0]==pile[i]) {
auto pr=min_edges[i];
taken[pr.first][pr.second]=taken[pr.second][pr.first]=1;
val[pr.first]++;
val[pr.second]++;
break;
}
jump:
;
}
long long mx=LLONG_MIN;
for(int i=1; i<=n; i++)
if(val[i]>mx) {
mx=val[i];
}
printf("%lld\n",mx);
} else {
if(m-(n-1)==0) {
if(n==2)
puts("1");
else
puts("2");
} else if(m-(n-1)-2>0)
puts("3");
else
puts("2");
}
} else {
puts("-1");
}
}
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCmxvbmcgbG9uZyBuLG0sdDsKYm9vbCB0YWtlblsxMDBdWzEwMF07CmxvbmcgbG9uZyB2YWxbMTAwMDAwNl07Cgpib29sIGNtcCh2ZWN0b3I8aW50PiBMLHZlY3RvcjxpbnQ+IFIpIHsKICAgIGlmKEwuc2l6ZSgpPT1SLnNpemUoKSkKICAgICAgICByZXR1cm4gTDxSOwogICAgZWxzZQogICAgICAgIHJldHVybiBMLnNpemUoKTxSLnNpemUoKTsKfQoKaW50IG1haW4oKSB7CgovLyAgICBmcmVvcGVuKCJvLnR4dCIsInciLHN0ZG91dCk7CgogICAgY2luPj50OwogICAgd2hpbGUodC0tKSB7CiAgICAgICAgc2NhbmYoIiVsbGQgJWxsZCIsJm4sJm0pOwogICAgICAgIG1lbXNldCh0YWtlbiwwLHNpemVvZih0YWtlbikpOwoKICAgICAgICBpZihuPT0xKSB7CiAgICAgICAgICAgIGlmKG09PTApCiAgICAgICAgICAgICAgICBwdXRzKCIwIik7CiAgICAgICAgICAgIGVsc2UgaWYobT09MSkKICAgICAgICAgICAgICAgIHB1dHMoIjEiKTsKICAgICAgICAgICAgZWxzZQogICAgICAgICAgICAgICAgcHV0cygiLTEiKTsKCiAgICAgICAgICAgIGNvbnRpbnVlOwogICAgICAgIH0KCiAgICAgICAgaWYobT49bi0xICYmIG08PSBuKihuLTEpLzIrbikgewoKICAgICAgICAgICAgaWYobS0obi0xKT5uKSB7CgogICAgICAgICAgICAgICAgbT1tLShuLTEpLW47CiAgICAgICAgICAgICAgICB2YWxbMV09dmFsW25dPTI7CiAgICAgICAgICAgICAgICBmb3IoaW50IGk9MjsgaTxuOyBpKyspCiAgICAgICAgICAgICAgICAgICAgdmFsW2ldPTM7CgogICAgICAgICAgICAgICAgZm9yKGludCBpPTE7IGk8bjsgaSsrKSB7CiAgICAgICAgICAgICAgICAgICAgdGFrZW5baV1baSsxXT10YWtlbltpKzFdW2ldPTE7CiAgICAgICAgICAgICAgICB9CgogICAgICAgICAgICAgICAgd2hpbGUobS0tKSB7CiAgICAgICAgICAgICAgICAgICAgaW50IG1uPUlOVF9NQVg7CiAgICAgICAgICAgICAgICAgICAgdmVjdG9yPHBhaXI8aW50LGludD4gPiBtaW5fZWRnZXM7CgogICAgICAgICAgICAgICAgICAgIGZvcihpbnQgaT0xOyBpPD1uOyBpKyspCiAgICAgICAgICAgICAgICAgICAgICAgIGZvcihpbnQgaj1pKzE7IGo8PW47IGorKykgewoKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmKHRha2VuW2ldW2pdPT0wICYmIG1heCh2YWxbaV0rMSx2YWxbal0rMSk8bW4pIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBtbj1tYXgodmFsW2ldKzEsdmFsW2pdKzEpOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIG1pbl9lZGdlcy5jbGVhcigpOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgfQoKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmKHRha2VuW2ldW2pdPT0wICYmIG1heCh2YWxbaV0rMSx2YWxbal0rMSk9PW1uKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgbWluX2VkZ2VzLnB1c2hfYmFjayh7aSxqfSk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CgogICAgICAgICAgICAgICAgICAgICAgICB9CgogICAgICAgICAgICAgICAgICAgIHZlY3Rvcjx2ZWN0b3I8aW50PiA+IHBpbGU7CiAgICAgICAgICAgICAgICAgICAgdmVjdG9yPHZlY3RvcjxpbnQ+ID4gcGlsZTI7CgogICAgICAgICAgICAgICAgICAgIGludCBtbjI9SU5UX01BWDsKICAgICAgICAgICAgICAgICAgICBpbnQgaW5keD0wOwoKICAgICAgICAgICAgICAgICAgICBmb3IoYXV0byB2b246IG1pbl9lZGdlcykgewogICAgICAgICAgICAgICAgICAgICAgICB2ZWN0b3I8aW50PiBib29tOwoKICAgICAgICAgICAgICAgICAgICAgICAgZm9yKGludCBpPTE7IGk8PW47IGkrKykKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmKGkhPXZvbi5maXJzdCAmJiBpIT12b24uc2Vjb25kICYmICh0YWtlbltpXVt2b24uZmlyc3RdPT0wIHx8IHRha2VuW3Zvbi5maXJzdF1baV09PTAgKSkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmKG1heCh2YWxbaV0sdmFsW3Zvbi5maXJzdF0rMSkhPW1heCh2YWxbaV0sdmFsW3Zvbi5maXJzdF0pKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGJvb20ucHVzaF9iYWNrKG1heCh2YWxbaV0sdmFsW3Zvbi5maXJzdF0rMSkpOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KCiAgICAgICAgICAgICAgICAgICAgICAgIGZvcihpbnQgaT0xOyBpPD1uOyBpKyspCiAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZihpIT12b24uc2Vjb25kICYmIGkhPXZvbi5maXJzdCAmJiAodGFrZW5baV1bdm9uLnNlY29uZF09PTAgfHwgdGFrZW5bdm9uLnNlY29uZF1baV09PTApKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYobWF4KHZhbFtpXSx2YWxbdm9uLnNlY29uZF0rMSkhPW1heCh2YWxbaV0sdmFsW3Zvbi5zZWNvbmRdKSkgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBib29tLnB1c2hfYmFjayhtYXgodmFsW2ldLHZhbFt2b24uc2Vjb25kXSsxKSk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICAgICAgfQoKCiAgICAgICAgICAgICAgICAgICAgICAgIHNvcnQoYm9vbS5iZWdpbigpLGJvb20uZW5kKCkpOwoKICAgICAgICAgICAgICAgICAgICAgICAgaWYoYm9vbS5zaXplKCk9PTApIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGF1dG8gcHI9dm9uOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgdGFrZW5bcHIuZmlyc3RdW3ByLnNlY29uZF09dGFrZW5bcHIuc2Vjb25kXVtwci5maXJzdF09MTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHZhbFtwci5maXJzdF0rKzsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHZhbFtwci5zZWNvbmRdKys7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBnb3RvIGp1bXA7CiAgICAgICAgICAgICAgICAgICAgICAgIH0KCiAgICAgICAgICAgICAgICAgICAgICAgIHBpbGUucHVzaF9iYWNrKGJvb20pOwoKICAgICAgICAgICAgICAgICAgICAgICAgaWYobW4yPmJvb20uYmFjaygpKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBtbjI9Ym9vbS5iYWNrKCk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBwaWxlMi5jbGVhcigpOwogICAgICAgICAgICAgICAgICAgICAgICB9CgogICAgICAgICAgICAgICAgICAgICAgICBpZihtbjI9PWJvb20uYmFjaygpKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBwaWxlMi5wdXNoX2JhY2soYm9vbSk7CiAgICAgICAgICAgICAgICAgICAgICAgIH0KCiAgICAgICAgICAgICAgICAgICAgICAgIGluZHgrKzsKCiAgICAgICAgICAgICAgICAgICAgfQoKICAgICAgICAgICAgICAgICAgICBzb3J0KHBpbGUyLmJlZ2luKCkscGlsZTIuZW5kKCksY21wKTsKCiAgICAgICAgICAgICAgICAgICAgZm9yKGludCBpPTA7IGk8cGlsZS5zaXplKCk7IGkrKykKICAgICAgICAgICAgICAgICAgICAgICAgaWYocGlsZTJbMF09PXBpbGVbaV0pIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGF1dG8gcHI9bWluX2VkZ2VzW2ldOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgdGFrZW5bcHIuZmlyc3RdW3ByLnNlY29uZF09dGFrZW5bcHIuc2Vjb25kXVtwci5maXJzdF09MTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHZhbFtwci5maXJzdF0rKzsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHZhbFtwci5zZWNvbmRdKys7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgICAgICAgICAgfQoKanVtcDoKICAgICAgICAgICAgICAgICAgICA7CgogICAgICAgICAgICAgICAgfQoKICAgICAgICAgICAgICAgIGxvbmcgbG9uZyBteD1MTE9OR19NSU47CiAgICAgICAgICAgICAgICBmb3IoaW50IGk9MTsgaTw9bjsgaSsrKQogICAgICAgICAgICAgICAgICAgIGlmKHZhbFtpXT5teCkgewogICAgICAgICAgICAgICAgICAgICAgICBteD12YWxbaV07CiAgICAgICAgICAgICAgICAgICAgfQoKICAgICAgICAgICAgICAgIHByaW50ZigiJWxsZFxuIixteCk7CgogICAgICAgICAgICB9IGVsc2UgewoKICAgICAgICAgICAgICAgIGlmKG0tKG4tMSk9PTApIHsKICAgICAgICAgICAgICAgICAgICBpZihuPT0yKQogICAgICAgICAgICAgICAgICAgICAgICBwdXRzKCIxIik7CiAgICAgICAgICAgICAgICAgICAgZWxzZQogICAgICAgICAgICAgICAgICAgICAgICBwdXRzKCIyIik7CiAgICAgICAgICAgICAgICB9IGVsc2UgaWYobS0obi0xKS0yPjApCiAgICAgICAgICAgICAgICAgICAgcHV0cygiMyIpOwogICAgICAgICAgICAgICAgZWxzZQogICAgICAgICAgICAgICAgICAgIHB1dHMoIjIiKTsKCiAgICAgICAgICAgIH0KCgogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIHB1dHMoIi0xIik7CiAgICAgICAgfQoKCiAgICB9CgoKICAgIHJldHVybiAwOwp9Cg==