#include using namespace std; using ll = long long; int main(){ ios::sync_with_stdio(false); cin.tie(0); int T; cin >> T; while(T--){ ll y, x; cin >> y >> x; x--; auto rec = [&](auto rec, ll y, ll x) -> ll { ll nx = x, msb = __lg(y); while(y >> (nx + 1) & 1) nx++; if(nx == msb) return y - 1; nx++; ll msk = (1ll << nx) - 1; ll ny = (y & ~msk) + (1ll << nx); return max(ny - y, nx - x) + rec(rec, ny, nx); }; cout << rec(rec, y, x) << '\n'; } }