package no3642; import java.util.*; public class Main { public static void main(String[] args) { // TODO 自動生成されたメソッド・スタブ Scanner sc = new Scanner(System.in); int t = sc.nextInt(); StringBuilder sb = new StringBuilder(); while(t > 0) { t--; int l = sc.nextInt(); int r = sc.nextInt(); if(r == 0) { sb.append(0 +"\n"); continue; } long v = 1; while(v * 2L <= r) { v *= 2L; }if((l & v) != 0) { sb.append(-1 + "\n"); continue; }sb.append((long)v * (long)(v - 1) + "\n"); }System.out.print(sb); } }