/* package codechef; // don’t place package name! */
import java.util.;
import java.lang.;
import java.io.*;
/* Name of the class has to be “Main” only if the class is public. */
class Codechef
{
public static void main (String[] args)
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t–>0){
long x=sc.nextLong();
long y=sc.nextLong();
long l=sc.nextLong();
long r=sc.nextLong();
long z=0;
long min=Long.MIN_VALUE;
for(long i=r;i>=l;i–){
long h=(x&i)*(y&i);
if(h>min){
min =h;
z=i;
if((x|y)<z){
z=x|y;
}
}
}
System.out.println(z);
}
}
}