You are given two integers l and r, l≤ r. Find the largest possible value of a mod b over all pairs (a, b) of integers for which r≥ a ≥ b ≥ l.

As a reminder, a mod b is a remainder we get when dividing a by b. For example, 26 mod 8 = 2.

Input

Each test contains multiple test cases.

The first line contains one positive integer t (1≤ t≤ 10^4), denoting the number of test cases. Description of the test cases follows.

The only line of each test case contains two integers l, r (1≤ l ≤ r ≤ 10^9).

Output

For every test case, output the largest possible value of a mod b over all pairs (a, b) of integers for which r≥ a ≥ b ≥ l.

Example

Input


4
1 1
999999999 1000000000
8 26
1 999999999


Output


0
1
12
499999999

Note

In the first test case, the only allowed pair is (a, b) = (1, 1), for which a mod b = 1 mod 1 = 0.

In the second test case, the optimal choice is pair (a, b) = (1000000000, 999999999), for which a mod b = 1.

import java.io.*;
import java.util.*;
 
public class TEMP {
	public static PrintWriter out;
    
	   public static class MyScanner {
	      BufferedReader br;
	      StringTokenizer st;
	 
	      public MyScanner() {
	         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());
	      }
	 
	      String nextLine(){
	          String str = "";
		  try {
		     str = br.readLine();
		  } catch (IOException e) {
		     e.printStackTrace();
		  }
		  return str;
	      }
 
	   }
 
 
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		
		  MyScanner sc = new MyScanner();
	      out = new PrintWriter(new BufferedOutputStream(System.out));
//	      long k     = sc.nextLong();       // read input as long
//	      double d   = sc.nextDouble();     // read input as double
//	      String str = sc.next();           // read input as String
//	      String s   = sc.nextLine();       // read whole line as String
//	      out.println(result); // print via PrintWriter
	      int T = sc.nextInt();
	      while(T-->0) {
	    	  long max =0;
	    	  long l = sc.nextLong();
	    	  long r = sc.nextLong();
	    	  for(Long i=l;i<=r;i++) {
	    		  if(r==2*i) {
	    			  max = Math.max(max, (r-1)%i);
	    			  break;
	    			  }
	    		  else if(r>2*i){
	    			  
	    			  
	    			  long mid = (r+1)/2;
	    			  if(mid>=l) {
	    				  max = Math.max(r%mid, (r-1)%mid);
	    				  break;
	    				  
	    			  }else {
	    				  max = Math.max(max, r%l);
	    			  }
	    		
	    			  
	    			  
	    			  
	    		  }else {
	    			  max = Math.max(max, r%i);
	    			  break;
	    			  
	    		  }
	    		  
	    	  }
	    	  System.out.println(max);
	    	  
	      }
	 

	      out.close();
		
 
	}
	
	public static int[] seive(int n) {
		int r = (int)Math.sqrt(n);
		int arr[] = new int[n+1];
		Arrays.fill(arr, 1);
		for(int i=2;i<=r;i++){
			for(int j=2*i;j<=n;j+=i){
				arr[j]=0;
				
			}
		}
		return arr;
		
	}
	
	public static boolean isPrime(long arr) {
		long div =0;
		for(long i=1;i*i<=arr;i++) {
			if(arr%i==0) {
				div+=2;
			}
			if(i*i==arr) div-=1;
		
			if(div>2) return false;
		}
		if(div==2) return true;
		return false;
	}
	
    static void shuffle(long a[], int n){ 
        for (int i = 0; i < n; i++) { 
  
            // getting the random index 
            int t = (int)Math.random() * a.length; 
  
            // and swapping values a random index 
            // with the current index 
            long x = a[t]; 
            a[t] = a[i]; 
            a[i] = x; 
        } 
    } 
	
    public static ArrayList<Integer> get_divisor(int n) {
		ArrayList<Integer> arr = new ArrayList<>();
		for(int i=1;i*i<=n;i++) {
			if(n%i==0) {
				arr.add(i);
				if(i*i!=n) arr.add(n/i);  
			}
		}
		return arr;
	}
	
	public static int get_floor(int arr[],int target,int i,int j) {
		int l =i;
		int r =j;
		int ind =-1;
		while(l<=r) {
			int mid = l+(r-l)/2;
			if(arr[mid]==target) {
				r = mid-1;
			}else if(arr[mid]<target) {
				ind = mid;
				l = mid+1;
			}else {
				r = mid-1;
			}
		}
		
		return ind;
		
	}
	
	public static int get_ceil(int arr[],int target,int i,int j) {
		int l =i;
		int r =j;
		int ind =-1;
		while(l<=r) {
			int mid = l+(r-l)/2;
			if(arr[mid]==target) {
				l = mid+1;
			}else if(arr[mid]>target) {
				ind = mid;
				r = mid-1;
			}else {
				l = mid+1;
			}
		}
		
		return ind;
		
	}
	
	
	
}