#include <bits/stdc++.h>
using namespace std;
#define int long long
int T,n,a[100000009];
queue<pair<int,int> > q;
map<int,bool> v;
signed main()
{
// freopen("qiaqia.in","r",stdin);
// freopen("qiaqia.out","w",stdout);
q.push({2,1});
while(!q.empty())
{
pair<int,int> x=q.front();
q.pop();
if(x.first>1e9) continue ;
a[++a[0]]=x.first;
for(int i=x.second+1;i<=30;i++)
{
q.push({(x.first+(int)pow(2,i))<<1,i+1});
}
}
sort(a+1,a+a[0]+1);
// cout<<a[0]<<'\n';
cin>>T;
while(T--)
{
int l,r;
cin>>l>>r;
int x=lower_bound(a+1,a+a[0]+1,l)-a;
if(r>=a[x]&&a[x]) cout<<a[x]<<'\n';
else cout<<-1<<'\n';
}
return 0;
}