#include<bits/stdc++.h>
using namespace std;
long long n,m,t;
int main() {
cin>>t;
while(t--) {
scanf("%lld %lld",&n,&m);
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) {
if(m==n*2)
puts("3");
else{
int ans;
long long dif=m-n*2;
long long val=n/2;
if(n%2==0){
ans=3+ceil((double)dif/val);
}else{
long long frac1=dif/(2*val+1);
long long frac2=ceil(((double)m-(n*2+frac1*(2*val+1)))/val);
ans=frac1*2+3+frac2;
}
printf("%d\n",ans);
}
} 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;
}
/*
22 242
ans=22
my output=21
*/
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCmxvbmcgbG9uZyBuLG0sdDsKCmludCBtYWluKCkgewoKICAgIGNpbj4+dDsKICAgIHdoaWxlKHQtLSkgewogICAgICAgIHNjYW5mKCIlbGxkICVsbGQiLCZuLCZtKTsKCiAgICAgICAgaWYobj09MSkgewogICAgICAgICAgICBpZihtPT0wKQogICAgICAgICAgICAgICAgcHV0cygiMCIpOwogICAgICAgICAgICBlbHNlIGlmKG09PTEpCiAgICAgICAgICAgICAgICBwdXRzKCIxIik7CiAgICAgICAgICAgIGVsc2UKICAgICAgICAgICAgICAgIHB1dHMoIi0xIik7CgogICAgICAgICAgICBjb250aW51ZTsKICAgICAgICB9CgogICAgICAgIGlmKG0+PW4tMSAmJiBtPD0gbioobi0xKS8yK24pIHsKCiAgICAgICAgICAgIGlmKG0tKG4tMSk+bikgewoKICAgICAgICAgICAgICAgIGlmKG09PW4qMikKICAgICAgICAgICAgICAgICAgICBwdXRzKCIzIik7CiAgICAgICAgICAgICAgICBlbHNlewoKICAgICAgICAgICAgICAgICAgIGludCBhbnM7CiAgICAgICAgICAgICAgICAgICBsb25nIGxvbmcgZGlmPW0tbioyOwogICAgICAgICAgICAgICAgICAgbG9uZyBsb25nIHZhbD1uLzI7CgogICAgICAgICAgICAgICAgICAgaWYobiUyPT0wKXsKICAgICAgICAgICAgICAgICAgICAgYW5zPTMrY2VpbCgoZG91YmxlKWRpZi92YWwpOwogICAgICAgICAgICAgICAgICAgfWVsc2V7CgogICAgICAgICAgICAgICAgICAgICBsb25nIGxvbmcgZnJhYzE9ZGlmLygyKnZhbCsxKTsKICAgICAgICAgICAgICAgICAgICAgbG9uZyBsb25nIGZyYWMyPWNlaWwoKChkb3VibGUpbS0obioyK2ZyYWMxKigyKnZhbCsxKSkpL3ZhbCk7CiAgICAgICAgICAgICAgICAgICAgIGFucz1mcmFjMSoyKzMrZnJhYzI7CiAgICAgICAgICAgICAgICAgICB9CgogICAgICAgICAgICAgICAgICAgcHJpbnRmKCIlZFxuIixhbnMpOwoKICAgICAgICAgICAgICAgIH0KCgogICAgICAgICAgICB9IGVsc2UgewoKICAgICAgICAgICAgICAgIGlmKG0tKG4tMSk9PTApewogICAgICAgICAgICAgICAgICAgIGlmKG49PTIpCiAgICAgICAgICAgICAgICAgICAgIHB1dHMoIjEiKTsKICAgICAgICAgICAgICAgICAgICBlbHNlCiAgICAgICAgICAgICAgICAgICAgcHV0cygiMiIpOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgZWxzZSBpZihtLShuLTEpLTI+MCkKICAgICAgICAgICAgICAgICAgICBwdXRzKCIzIik7CiAgICAgICAgICAgICAgICBlbHNlCiAgICAgICAgICAgICAgICAgICAgcHV0cygiMiIpOwoKICAgICAgICAgICAgfQoKCiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgcHV0cygiLTEiKTsKICAgICAgICB9CgoKICAgIH0KCgogICAgcmV0dXJuIDA7Cn0KLyoKMjIgMjQyCmFucz0yMgpteSBvdXRwdXQ9MjEKCiovCg==