description: 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.
solution: import java.util.*; // Implicit import
  import java.io.*;
  import java.math.BigInteger; // Explicit import
  
  public class A {
  
  	static FastReader		sc	= new FastReader();
  	static PrintWriter	out	= new PrintWriter(System.out);
  
  
  	public static void main(String[] args) throws Exception {
  
  		int t = sc.ni();
  		while (t-- > 0) {
  			A.go();
  		}
  		out.flush();
  
  	}
  
  	// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Code Starts <<<<<<<<<<<<<<<<<<<<<<<<<<<<< //
   static void go() {
  	 long l=sc.ni();
  	 long r=sc.ni();
  	 if(l==r) {
  		 out.println(0);
  		 return;
  	 }
  	// a=b+x;
  	if((r-l)==1) {
  		out.println(r%l);
  		return;
  	}
  	if(r%2==0) {
  		if(l>r/2) {
  		out.println(r%l);
  		}else {
  			l=r/2+1;
  			out.println(r%l);
  		}
  	}else {
  		if(l>r/2) {
  			out.println(r%(l));
  		}else {
  			out.println(r%(r/2+1));
  		}
  	}
  	
  }
  
  	// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Code Ends <<<<<<<<<<<<<<<<<<<<<<<<<<<<< //
  	static long	fact[];
  	static long	invfact[];
  
  	static long ncr(int n, int k) {
  		if (k < 0 || k > n) {
  			return 0;
  		}
  		long x = fact[n] % mod;
  		long y = invfact[k] % mod;
  		long yy = invfact[n - k] % mod;
  		long ans = (x * y) % mod;
  		ans = (ans * yy) % mod;
  		return ans;
  	}
  
  	static long gcd(long a, long b) {
  		if (b == 0) {
  			return a;
  		}
  		return gcd(b, a % b);
  	}
  
  	static int	prime[]	= new int[200005];
  	static int	N				= 200005;
  
  	static void sieve() {
  
  		for (int i = 0; i < N; i++) {
  			prime[i] = i;
  		}
  		for (int i = 2; i * i <= N; i++) {
  			if (prime[i] == i) {
  				prime[i] = i;
  				for (int j = i; j < N; j += i) {
  					prime[j] = i;
  				}
  			}
  		}
  	}
  
  	static int gcd(int a, int b) {
  		if (b == 0) {
  			return a;
  		}
  		return gcd(b, a % b);
  	}
  
  	static void sort(int[] a) {
  		ArrayList<Integer> aa = new ArrayList<>();
  		for (Integer i : a) {
  			aa.add(i);
  		}
  		Collections.sort(aa);
  		for (int i = 0; i < a.length; i++)
  			a[i] = aa.get(i);
  	}
  
  	static void sort(long[] a) {
  		ArrayList<Long> aa = new ArrayList<>();
  		for (Long i : a) {
  			aa.add(i);
  		}
  		Collections.sort(aa);
  		for (int i = 0; i < a.length; i++)
  			a[i] = aa.get(i);
  	}
  
  	static int mod = (int) 1000000007;
  
  	static long pow(long x, long y) {
  		long res = 1l;
  		while (y != 0) {
  			if (y % 2 == 1) {
  				res = (x % mod * res % mod) % mod;
  			}
  			y /= 2;
  			x = (x % mod * x % mod) % mod;
  		}
  		return res % mod;
  	}
  
  	// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Fast IO <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< //
  
  	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 ni() {
  			return Integer.parseInt(next());
  		}
  
  		long nl() {
  			return Long.parseLong(next());
  		}
  
  		double nextDouble() {
  			return Double.parseDouble(next());
  		}
  
  		int[] intArray(int n) {
  			int a[] = new int[n];
  			for (int i = 0; i < n; i++)
  				a[i] = sc.ni();
  			return a;
  		}
  
  		long[] longArray(int n) {
  			long a[] = new long[n];
  			for (int i = 0; i < n; i++)
  				a[i] = sc.nl();
  			return a;
  		}
  
  		String nextLine() {
  			String str = "";
  			try {
  				str = br.readLine();
  			} catch (IOException e) {
  				e.printStackTrace();
  			}
  			return str;
  		}
  	}
  }