t = int(input())
while t:
a, b = list(map(int, input().split(' ')))
count = b - a
for i in range(b): if b == b | i:
count = min(count, 1 + abs(i - a))
print(count)
t -= 1
//************ || MD. SAJID ALAM CHOWDHURY || *************//
//**************** || 25-Mar-2023(Sat) || *****************//
//******************** || 18:38:03 || *********************//
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include<functional>
using namespace std;
using namespace __gnu_pbds;
//********************* || DEFINES || *********************//
#define BOOST ios_base::sync_with_stdio(false);cin.tie(NULL);
#define ff first
#define ss second
#define IN insert
#define MP make_pair
#define PB push_back
#define PF push_front
#define LB lower_bound
#define UB upper_bound
#define NP next_permutation
#define PP prev_permutation
#define all(v) v.begin(),v.end()
#define Fo(i,a,b) for(ll i=a;i<=b;i++)
#define Fb(i,b,a) for(ll i=b;i>=a;i--)
#define mem_0(arr) memset(arr,0,sizeof(arr))
#define mem_1(arr) memset(arr,-1,sizeof(arr))
#define precise_impact(x) fixed<<showpoint<<setprecision(x)
//******************** || TYPEDEFS || *********************//
typedef double db;
typedef long long ll;
typedef long double ldb;
typedef unsigned long long ull;
typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
typedef trie<string,null_type,trie_string_access_traits<>,pat_trie_tag,trie_prefix_search_node_update> pref_trie;
//******************** || CONSTANTS || ********************//
const ll MAX=2e5+5;
const ll MOD=1e9+7; // 998244353 //
const ll INF=1e18+5;
const db PI=acos(-1);
//--------- Up-Down,Left-Right ---------//
//const ll dx[]={ 0, 0,-1, 1};
//const ll dy[]={-1, 1, 0, 0};
//------------ King's Moves ------------//
//const ll dx[]={ 0, 0,-1,-1,-1, 1, 1, 1};
//const ll dy[]={-1, 1, 0,-1, 1, 0,-1, 1};
//----------- Knight's Moves -----------//
//const ll dx[]={-2,-2,-1,-1, 1, 1, 2, 2};
//const ll dy[]={-1, 1,-2, 2,-2, 2,-1, 1};
//***************** || INLINE FUNCTIONS || ****************//
inline ll norm(ll &a) { a%=MOD; return (a<0)?(a+=MOD):a; }
inline ll modAdd(ll a,ll b) { a%=MOD,b%=MOD; norm(a),norm(b); a+=b; return norm(a); }
inline ll modSub(ll a,ll b) { a%=MOD,b%=MOD; norm(a),norm(b); a-=b; return norm(a); }
inline ll modMul(ll a,ll b) { a%=MOD,b%=MOD; norm(a),norm(b); a*=b; return norm(a); }
inline ll modPow(ll a,ll p) { ll c=1; while(p){ if(p%2){ c=modMul(c,a); } a=modMul(a,a); p/=2; } return c; }
inline ll modInverse( ll a) { return modPow(a,MOD-2); }
inline ll modDiv(ll a,ll b) { a%=MOD,b%=MOD; norm(a),norm(b); return modMul(a,modInverse(b)); }
//******************** || FUNCTIONS || ********************//
ll gcd(ll m,ll n)
{
while(n!=0)
{
m%=n;
swap(m,n);
}
return m;
}
ll lcm(ll m,ll n)
{
return ((m*n)/gcd(m,n));
}
ll bit_on(ll pos,ll mask)
{
return mask|(1<<pos);
}
bool bit_check(ll pos,ll mask)
{
return mask&(1<<pos);
}
bool cmp(pair<ll,ll>a,pair<ll,ll>b)
{
if(a.ff==b.ff)
{
return (a.ss>b.ss);
}
return (a.ff<b.ff);
}
int main()
{
#ifndef ONLINE_JUDGE
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
#endif
/***********************/ BOOST /***********************/
//***************** || READY TO GO || *****************//
ll t;
cin>>t;
while(t--)
{
ll a,b;
cin>>a>>b;
ll ans=INF;
Fo(i,0,b)
{
if((i|b)==b)
{
ll k=abs(i-a);
if(i!=b)
{
k++;
}
ans=min(ans,k);
}
}
cout<<ans<<endl;
}
return 0;
//*************** || DONE AND DUSTED || ***************//
}
1302. Deepest Leaves Sum | 1209. Remove All Adjacent Duplicates in String II |
994. Rotting Oranges | 983. Minimum Cost For Tickets |
973. K Closest Points to Origin | 969. Pancake Sorting |
967. Numbers With Same Consecutive Differences | 957. Prison Cells After N Days |
946. Validate Stack Sequences | 921. Minimum Add to Make Parentheses Valid |
881. Boats to Save People | 497. Random Point in Non-overlapping Rectangles |
528. Random Pick with Weight | 470. Implement Rand10() Using Rand7() |
866. Prime Palindrome | 1516A - Tit for Tat |
622. Design Circular Queue | 814. Binary Tree Pruning |
791. Custom Sort String | 787. Cheapest Flights Within K Stops |
779. K-th Symbol in Grammar | 701. Insert into a Binary Search Tree |
429. N-ary Tree Level Order Traversal | 739. Daily Temperatures |
647. Palindromic Substrings | 583. Delete Operation for Two Strings |
518. Coin Change 2 | 516. Longest Palindromic Subsequence |
468. Validate IP Address | 450. Delete Node in a BST |