#include<cstdio>
#include<queue>
using namespace std;
struct point{
int n,step;
};
queue<point> q;
int main(){
int T,n,k; point u;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&k);
q.push((point){n,0});
while(1){
u=q.front(); q.pop();
if(u.n==k) {printf("%d",u.step);break;}
if(u.n+1<=k) q.push((point){u.n+1,u.step+1});
if(u.n-1>0) q.push((point){u.n-1,u.step+1});
if(u.n*2<=1e6) q.push((point){u.n*2,u.step+1});
}
}
return 0;
}