description: Alice gave Bob two integers a and b (a > 0 and b ≥ 0). Being a curious boy, Bob wrote down an array of non-negative integers with \operatorname{MEX} value of all elements equal to a and \operatorname{XOR} value of all elements equal to b.
  
  What is the shortest possible length of the array Bob wrote?
  
  Recall that the \operatorname{MEX} (Minimum EXcluded) of an array is the minimum non-negative integer that does not belong to the array and the \operatorname{XOR} of an array is the [bitwise XOR](https://en.wikipedia.org/wiki/Bitwise_operation#XOR) of all the elements of the array.
  
  Input
  
  The input consists of multiple test cases. The first line contains an integer t (1 ≤ t ≤ 5 ⋅ 10^4) — the number of test cases. The description of the test cases follows.
  
  The only line of each test case contains two integers a and b (1 ≤ a ≤ 3 ⋅ 10^5; 0 ≤ b ≤ 3 ⋅ 10^5) — the \operatorname{MEX} and \operatorname{XOR} of the array, respectively.
  
  Output
  
  For each test case, output one (positive) integer — the length of the shortest array with \operatorname{MEX} a and \operatorname{XOR} b. We can show that such an array always exists.
  
  Example
  
  Input
  
  
  5
  1 1
  2 1
  2 0
  1 10000
  2 10000
  
  
  Output
  
  
  3
  2
  3
  2
  3
  
  Note
  
  In the first test case, one of the shortest arrays with \operatorname{MEX} 1 and \operatorname{XOR} 1 is [0, 2020, 2021].
  
  In the second test case, one of the shortest arrays with \operatorname{MEX} 2 and \operatorname{XOR} 1 is [0, 1].
  
  It can be shown that these arrays are the shortest arrays possible.
solution: 
  
  import java.util.*;
  import java.io.*;
  import java.math.BigInteger;
  
  public class P04 {
  	static class FastReader{
  		BufferedReader br ;
  		StringTokenizer st;
  		public FastReader() {
  			br = new BufferedReader(new InputStreamReader(System.in));
  			
  		}
  		String next() {
  			while(st==null || !st.hasMoreElements()) {
  				try {
  					st = new StringTokenizer(br.readLine());
  				}catch(IOException e) {
  					e.printStackTrace();
  				}
  			}
  			return st.nextToken();
  		}
  		
  		int nextInt() {
  			return Integer.parseInt(next());
  		}
  		
  		long nextLong() {
  			return  Long.parseLong(next());
  		}
  		
  		double nextDouble() {
  			return Double.parseDouble(next());
  		}
  		
  		float nextFloat() {
  			return Float.parseFloat(next());
  		}
  		String nextLine() {
  			String str = "";
  			try {
  				str = br.readLine();
  			}catch(IOException e) {
  				e.printStackTrace();
  			}
  			return str ;
  		}
  	}
  	static void swap(int[] arr, int i, int j)
  	{
  	    int temp = arr[i];
  	    arr[i] = arr[j];
  	    arr[j] = temp;
  	}
  
  	static int partition(int[] arr, int low, int high)
  	{	   
  	    long pivot = arr[high];   
  	    int i = (low - 1);
  	    for(int j = low; j <= high - 1; j++)
  	    {    
  	        if (arr[j] < pivot)
  	        {          
  	            i++;
  	            swap(arr, i, j);
  	        }
  	    }
  	    swap(arr, i + 1, high);
  	    return (i + 1);
  	}
  	static void quickSort(int[] arr, int low, int high)
  	{
  	    if (low < high)
  	    {  
  	        int pi = partition(arr, low, high);
  	        quickSort(arr, low, pi - 1);
  	        quickSort(arr, pi + 1, high);
  	    }
  	}
  	public static long binarySearch(long arr[], long key){  
  		  int  first =0,last =arr.length-1;
  		   int mid = (first + last)/2;  
  		   while( first <= last ){  
  		      if ( arr[mid] < key ){  
  		        first = mid + 1;     
  		      }else if ( arr[mid] == key ){  
  		    	 return mid;  
  		      }else{  
  		         last = mid - 1;  
  		      }  
  		      mid = (first + last)/2;  
  		   }  
  		   if ( first > last ){ 
  			   return -1;
  		       
  		   } 
  		   return -1;
  		 }  
  	static long fp(long a , long b , int n) {
  		long res =1;
  		while(b>0) {
  			if((b&1)!=0) {             //when b is odd 
  				res=(res*a%n)%n;
  			}
  			a=(a%n*a%n)%n;
  			b=b>>1;
  		}
  		return res;
  	}
  	static long gcd (long a ,long b) {
  		if(b==0) {
  			return a;
  		}
  		return gcd(b,a%b);
  	}
  	public static void main(String[] args) {
  		Scanner sc = new Scanner (System.in);
  		FastReader fs = new FastReader();
  		int sr []= new int [300001];
  		sr[1]=0;
  		for(int i =2;i<300001;i++) {
  			sr[i]=sr[i-1]^i-1;
  		}
  		int t = fs.nextInt();
  		while(t-->0) {
  			int a = fs.nextInt();
  			int b = fs.nextInt();
  			if(sr[a]==b)System.out.println(a);
  			else {
  				if((sr[a]^b)!=a)System.out.println(a+1);
  				else System.out.println(a+2);
  			}
  		}
  		
      }
  	
  
  	private static String reduce(String s) {
  		String ans ="";
  		ans+=s.charAt(0);
  		for(int i =1;i<s.length();i++) {
  			if(s.charAt(i)!=s.charAt(i-1)) {
  				ans+=s.charAt(i);
  			}
  		}
  	//	if(s.charAt(s.length()-1)!=s.charAt(s.length()-2))ans+=s.charAt(s.length()-1);
  		return ans;
  	}
  
  	private static int solve(int[] a,int b[], int n) {
  		int flag =0;
  		for(int j =0;j<n;j++) {
  			if(a[j]!=b[j]) {
  				flag=1;
  			}
  		}
  		if(flag==0)return 0;
  		int i=1;
  		while(true) {
  			if(i%2==0) {
  				for(int j=1;j<n-1;j+=2) {
  					if(a[j]>a[j+1]) {
  						int temp =a[j];
  						a[j]=a[j+1];
  						a[j+1]=temp;
  					}
  				}
  			}
  			else {
  				for(int j =0;j<n-2;j+=2) {
  					if(a[j]>a[j+1]) {
  						int temp =a[j];
  						a[j]=a[j+1];
  						a[j+1]=temp;
  					}
  				}
  			}
  			flag =0;
  			for(int j =0;j<n;j++) {
  				if(a[j]!=b[j])flag=1;
  			}
  			if(flag==0)return i;
  			i++;
  		}
         
  		
  		
  	}
  
  	private static long sumofn(long n) {
  		int ans = 0;
  		while(n>9) {
  			ans+=n%10;
  			n/=10;
  		}
  		ans+=n;
  		return ans;
  	}
  	private static boolean palin(String s) {
  		int i =0,j=s.length()-1;
  		while(i<j) {
  			if(s.charAt(i)!=s.charAt(j))return false;
  			else {
  				i++;
  				j--;
  			}
  		}
  		return true;
  	}
  }
  			
  
  
  