#include<bits/stdc++.h>
using namespace std;
const int maxn=10000011;
bool vis[maxn];
queue<pair<int,int> >q;
int main()
{
int n;
scanf("%d",&n);
while(n--)
{
int s,t;
scanf("%d%d",&s,&t);
while(!q.empty())q.pop();
int mx=2*max(s,t);
memset(vis,0,mx*sizeof(bool));
q.push(make_pair(s,0));
vis[s]=1;
int ans=-1;
while(!q.empty())
{
int x=q.front().first;
int v=q.front().second;
q.pop();
if(x-1>0&&!vis[x-1])
{
if(x-1==t)
{
ans=v+1;
break;
}
vis[x-1]=1;
q.push(make_pair(x-1,v+1));
}
if(x+1<=mx&&!vis[x+1])
{
if(x+1==t)
{
ans=v+1;
break;
}
vis[x+1]=1;
q.push(make_pair(x+1,v+1));
}
if(x%2==0&&!vis[x/2])
{
if(x/2==t)
{
ans=v+1;
break;
}
vis[x/2]=1;
q.push(make_pair(x/2,v+1));
}
if(x*2<=mx&&!vis[x*2])
{
if(x*2==t)
{
ans=v+1;
break;
}
vis[x*2]=1;
q.push(make_pair(x*2,v+1));
}
}
printf("%d\n",ans*2);
}
return 0;
}