You are given two integers $l$ and $r$, $l\le r$. Find the largest possible value of $a mod b$ over all pairs $(a, b)$ of integers for which $r\ge a \ge b \ge 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\le t\le 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\le l \le r \le 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\ge a \ge b \ge l$.


-----Examples-----

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$.

Difficulty: EASY
Tags: Mathematics

Greedy algorithms
Skill types: Greedy algorithms

Starter code:

Solutions:
import sys
import math
for t in range(int(input())):
	(l, r) = map(int, sys.stdin.readline().split())
	if l <= math.ceil(r / 2):
		print(math.ceil(r / 2) - 1)
	else:
		print(r % l)


t = int(input())
arrow = []
while t > 0:
	w = input()
	w = w.split()
	a = int(w[0])
	b = int(w[1])
	n = max(a, b) // 2 + 1
	ans = max(a, b) % max(min(a, b), n)
	arrow.append(ans)
	t -= 1
for i in range(len(arrow)):
	print(arrow[i])


import math
n = int(input())
for _ in range(n):
	(l, r) = map(int, input().split(' '))
	if l == r:
		print(0)
	else:
		m = math.ceil(r % (r / 2 + 1))
		if l <= r / 2 + 1:
			print(m)
		else:
			print(r - l)


import sys
input = sys.stdin.readline
n = int(input())
for _ in range(n):
	(x, y) = map(int, input().split())
	x = max(x, y // 2 + 1)
	(lt, rt) = (x, y)
	answer = 0
	while lt <= rt:
		mid = (lt + rt) // 2
		if y % mid > answer:
			answer = y % mid
			rt = mid - 1
		else:
			lt = mid + 1
	print(answer)


for s in [*open(0)][1:]:
	(l, r) = map(int, s.split())
	print(r % max(l, r // 2 + 1))


import math

def find_result(l, r):
	if math.floor(r / 2) >= l:
		return r - (math.floor(r / 2) + 1)
	return r - l

def solve():
	(l, r) = map(int, input().split())
	result = find_result(l, r)
	print(int(result))
t = int(input())
for _ in range(t):
	solve()


import sys
import math

def maximize_modulo(constr):
	minimum = constr[0]
	maximum = constr[1]
	if maximum - minimum == 1 or maximum == minimum:
		print(maximum % minimum)
		return
	a = maximum
	b = math.ceil(maximum / 2)
	if a % 2 == 0:
		b = math.ceil(maximum / 2) + 1
	if b < minimum:
		b = minimum
	print(a % b)
infile = []
for line in sys.stdin:
	if '' == line.rstrip():
		break
	infile.append(list(map(int, line.rstrip().split())))
for x in range(1, len(infile)):
	maximize_modulo(infile[x])


_ = 1
_ = int(input())
while _ > 0:
	_ -= 1
	(x, y) = map(int, input().split())
	if x == y:
		print(0)
		continue
	print(y % max(y // 2 + 1, x))


t = int(input())
arres = []
for i in range(t):
	(l, r) = map(int, input().split())
	if l <= r // 2:
		res = (r + 1) // 2 - 1
	else:
		res = r - l
	arres.append(res)
for i in range(t):
	print(arres[i], end='\n')


for i in range(int(input())):
	(l, r) = map(int, input().split())
	a = max(l, r)
	b = min(l, r)
	if l == 1 and r == 1:
		print(0)
	elif a % 2 == 0:
		t = a // 2
		if t >= l:
			print(t - 1)
		else:
			print(a % b)
	else:
		t = (a - 1) // 2
		if t >= l:
			print(t)
		else:
			print(a % b)


t = int(input())
for i in range(t):
	(l, r) = map(int, input().split())
	if l <= r // 2:
		print(r % (r // 2 + 1))
	else:
		print(r % l)


for _ in range(int(input())):
	(l, r) = map(int, input().split())
	a = r // 2
	ans = 0
	if a >= l and a != 0:
		ans = max(ans, r % a)
	if a - 1 >= l and a - 1 != 0:
		ans = max(ans, r % (a - 1))
	if a + 1 >= l and a + 1 <= r:
		ans = max(ans, r % (a + 1))
	ans = max(ans, r % l)
	print(ans)


from collections import defaultdict
import math
from functools import reduce
from math import comb, gcd
from collections import defaultdict
from sys import stdout
NO = 'NO'
YES = 'YES'
MOD = 1000000007
ALPH = 'abcdefghijklmnopqrstuvwxyz'

def input_int():
	return int(input())

def input_list():
	return input().split(' ')

def input_list_int():
	return list(map(int, input_list()))

def list_int_to_str(a, sep=' '):
	return sep.join((str(x) for x in a))
global_ans = []

def pr(a):
	global_ans.append(a)

def z_func(s):
	n = len(s)
	z = [0] * n
	l = r = 0
	for i in range(1, n):
		if r >= i:
			z[i] = min(z[i - l], r - i + 1)
		while z[i] + i < n and s[z[i]] == s[z[i] + i]:
			z[i] += 1
		if i + z[i] - 1 > r:
			l = i
			r = i + z[i] - 1
	return z

def solve():
	(l, r) = input_list_int()
	ans = r // 2 + 1
	ans = max(l, ans)
	pr(r % ans)
query_count = 1
query_count = input_int()
while query_count:
	query_count -= 1
	solve()
for global_ans_line in global_ans:
	print(global_ans_line)


n = int(input())
z = []
for i in range(n):
	(l, r) = [int(x) for x in input().split(' ')]
	if l == r:
		z.append('0')
	elif l < r and l > r // 2:
		z.append(str(r - l))
	else:
		z.append(str(r - (r // 2 + 1)))
for i in z:
	print(i)


import sys
input = iter(sys.stdin.read().splitlines()).__next__

def solve(L, R):
	if L == R:
		return 0
	a = R
	b = max(R // 2 + 1, L)
	return a % b
TC = int(input())
for tc in range(TC):
	(L, R) = map(int, input().split())
	res = solve(L, R)
	print(res)


from os import path
from io import BytesIO, IOBase
import sys
from heapq import heappush, heappop
from functools import cmp_to_key as ctk
from collections import deque, Counter, defaultdict as dd
from bisect import bisect, bisect_left, bisect_right, insort, insort_left, insort_right
from itertools import permutations
from datetime import datetime
from math import ceil, sqrt, log, gcd

def ii():
	return int(input())

def si():
	return input().rstrip()

def mi():
	return map(int, input().split())

def li():
	return list(mi())
abc = 'abcdefghijklmnopqrstuvwxyz'
mod = 1000000007
inf = float('inf')
vow = ['a', 'e', 'i', 'o', 'u']
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def bo(i):
	return ord(i) - ord('0')
file = 1

def ceil(a, b):
	return (a + b - 1) // b

def solve():
	for _ in range(1, ii() + 1):
		(l, r) = mi()
		l = max(l, r // 2 + 1)
		print(r - l)
if file:
	if path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	else:
		input = sys.stdin.readline
solve()


t = int(input())
for i in range(t):
	(n, m) = map(int, input().split())
	k = max(m // 2 + 1, n)
	k = min(m, k)
	print(m % k)


T = int(input())
for t in range(0, T):
	(l, r) = map(int, input().split(' '))
	if l == r:
		print(0)
	else:
		ans = r // 2 + 1
		if ans > l:
			print(r % ans)
		else:
			print(r % l)


for i in range(int(input())):
	[l, r] = list(map(int, input().split()))
	divider = int(r / 2) + 1
	if int(r / 2) < l:
		divider = l
	elif int(r / 2) == l:
		divider = l + 1
	print(r % divider)


for iii in range(int(input())):
	(q, w) = list(map(int, input().split()))
	if w - q > (w - 1) // 2:
		print((w - 1) // 2)
	else:
		print(w - q)


class Solution(object):

	def __init__(self):
		pass

	def solve(self, lf, rg):
		md = int(rg / 2)
		return rg % max(lf, md + 1)
test_case_num = int(input())
results = list()
for test in range(test_case_num):
	(lf, rg) = input().split(' ')
	results.append(Solution().solve(int(lf), int(rg)))
for r in results:
	print(r)


t = input()
t = int(t)
for i in range(0, t):
	(r, l) = map(int, input().split())
	if r == l:
		ans = 0
	elif l % 2 == 0:
		m = l / 2
		m = int(m)
		m = m + 1
		if m in range(r, l + 1):
			ans = l % m
		else:
			ans = l % r
	else:
		m = int(l / 2)
		if m in range(r, l + 1):
			ans = m
		else:
			ans = l % r
	print(ans)


t = int(input())
for _ in range(t):
	(l, r) = map(int, input().split())
	best = (r + 2) // 2
	if best >= l:
		print(r % best)
	else:
		print(r % l)


for _t in range(int(input())):
	(l, r) = map(int, input().split())
	if l <= r // 2:
		ans = r % (r // 2 + 1)
	else:
		ans = r % l
	print(ans)


t = int(input())
for k in range(t):
	(l, r) = map(int, input().split())
	if l <= r // 2:
		x = r % (r // 2 + 1)
	else:
		x = r % l
	print(x)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	if a == b:
		print(0)
	elif b // 2 + 1 > a:
		print(b % (b // 2 + 1))
	else:
		print(b % a)


import sys

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def get_list():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_string():
	return sys.stdin.readline().strip()

def get_int():
	return int(sys.stdin.readline().strip())

def get_list_strings():
	return list(map(str, sys.stdin.readline().strip().split()))

def solve(l, r):
	if l == r:
		print(0)
		return
	req = r // 2 + 1
	if l <= req:
		ans = r % req
		print(ans)
		return
	ans = r % l
	print(ans)
	return
T = get_int()
while T:
	(l, r) = get_ints()
	solve(l, r)
	T -= 1


def solve():
	(l, r) = map(int, input().split())
	if r <= 2 * l - 1:
		return r - l
	return (r - 1) // 2
import sys
input = lambda : sys.stdin.readline().rstrip()
t = int(input())
for i in range(t):
	print(solve())


import os, sys
from io import BytesIO, IOBase

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, 8192))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, 8192))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

class dict(dict):

	def __missing__(self, key):
		return 0
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
inp = lambda dtype: [dtype(x) for x in input().split()]
inp_2d = lambda dtype, n: [dtype(input()) for _ in range(n)]
inp_2ds = lambda dtype, n: [inp(dtype) for _ in range(n)]
inp_enu = lambda dtype: [(i, x) for (i, x) in enumerate(inp(dtype))]
inp_enus = lambda dtype, n: [[i] + [inp(dtype)] for i in range(n)]
ceil1 = lambda a, b: (a + b - 1) // b
for _ in range(int(input())):
	(l, r) = inp(int)
	print(r - max(ceil1(r, 2) + (r & 1 == 0), l))


testcases = int(input())
while testcases:
	(l, r) = [int(i) for i in input().split()]
	if l <= r // 2:
		print(r % (r // 2 + 1))
	else:
		print(r % l)
	testcases -= 1


T = int(input())
for t in range(T):
	(l, r) = map(int, input().split())
	if (r + 1) // 2 >= l and (r + 1) // 2 <= r:
		print((r + 1) // 2 - 1)
	else:
		print(r - l)


T = int(input())
for _ in range(T):
	(l, r) = map(int, input().split())
	x = r + 1
	if x % 2:
		x += 1
	if x // 2 >= l:
		print(r % (x // 2))
	else:
		print(r % l)


import sys

def get_string():
	return sys.stdin.readline().strip()

def get_array():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def get_num():
	return int(sys.stdin.readline())
t = get_num()
for test in range(t):
	lr = get_array()
	l = lr[0]
	r = lr[1]
	if l > r // 2:
		print(r % l)
	else:
		print(r % (r // 2 + 1))


def solve(l, r):
	b = (r + 1) // 2
	if l <= b:
		return b - 1
	else:
		return r - l
t = int(input())
for i in range(t):
	(a, b) = map(int, input().split())
	print(solve(a, b))


def solve():
	(l, r) = [int(i) for i in input().split()]
	return r % max(l, r // 2 + 1)
t = int(input())
ans = []
while t:
	ans.append(str(solve()))
	t -= 1
print('\n'.join(ans))


t = int(input())
for _ in range(t):
	(l, r) = list(map(int, input().split()))
	if r // 2 + 1 >= l:
		print((r - 1) // 2)
	else:
		print(r % l)


from sys import *
import math

def solve_case():
	(l, r) = [int(x) for x in stdin.readline().rstrip().split()]
	if l < r / 2 + 1:
		print(r % math.floor(r / 2 + 1))
	else:
		print(r % l)

def main():
	T = int(stdin.readline().rstrip())
	for t in range(T):
		solve_case()
main()


a = int(input())
import math
i = 0
while i < a:
	b = input().split(' ')
	c = int(b[0])
	e = int(b[1])
	if e % 2 == 0:
		if math.floor(e // 2) + 1 >= c:
			print(math.floor(e // 2) - 1)
		else:
			print(e - c)
	if e % 2 == 1:
		if math.floor(e // 2) >= c:
			print(math.floor(e // 2))
		else:
			print(e - c)
	i = i + 1


try:
	lst = []
	t = int(input())
	for _ in range(t):
		(l, r) = map(int, input().split(' '))
		mid = r // 2
		if l > mid:
			print(r % l)
		else:
			print(r % (r // 2 + 1))
except:
	pass


def solve(l, r):
	if r < l * 2:
		array_answer.append(int(r - l))
	else:
		array_answer.append(int((r - 1) / 2))
t = int(input())
array_answer = []
array_input = []
while t > 0:
	value = input()
	array_input = value.split(' ')
	array_input[0] = int(array_input[0])
	array_input[1] = int(array_input[1])
	if array_input[0] <= array_input[1]:
		solve(array_input[0], array_input[1])
	t -= 1
for i in array_answer:
	print(i)


for _ in range(int(input())):
	(lo, hi) = map(int, input().split())
	if lo * 2 > hi:
		print(hi - lo)
	else:
		if hi % 2 == 0:
			hi -= 1
		print(hi // 2)


import sys
from heapq import heappush, heappop, heapify
from math import ceil

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def get_array():
	return list(get_ints())

def input():
	return sys.stdin.readline().strip()

def solve(l, r):
	h = int(ceil(r / 2))
	if h >= l:
		return h - 1
	else:
		return r % l
T = int(input())
while T > 0:
	(l, r) = get_ints()
	print(solve(l, r))
	T -= 1


test_cases = int(input())
for t in range(test_cases):
	(l, r) = list(map(int, input().split()))
	if r % 2 == 0:
		max_nr = min(r / 2 - 1, r - l)
	else:
		max_nr = min((r - 1) / 2, r - l)
	print(int(max_nr))


t = int(input())
for _ in range(t):
	(l, r) = map(int, input().split())
	print(min(r - l, (r - 1) // 2))


def solve(Test_cases):
	Resultant_list = []
	for i in range(Test_cases):
		string = input()
		list_me = list(string.split())
		map_object = map(int, list_me)
		list_me = list(map_object)
		(l, r) = (list_me[0], list_me[1])
		if r == l:
			Resultant_list.append(0)
		elif r % 2 != 0:
			value_1 = (r + 1) // 2
			if value_1 >= l:
				Resultant_list.append(r % value_1)
			else:
				Resultant_list.append(r % l)
		else:
			value_2 = r // 2 + 1
			if value_2 >= l:
				Resultant_list.append(r % value_2)
			else:
				Resultant_list.append(r % l)
	return Resultant_list
Test_cases = int(input())
if Test_cases < 1 or Test_cases > pow(10, 4):
	exit()
Result = solve(Test_cases)
for i in Result:
	print(i)


ans = ''
for _ in range(int(input())):
	(l, r) = list(map(int, input().split()))
	if l <= r // 2 + 1:
		ans += str(r % (r // 2 + 1))
	else:
		ans += str(r - l)
	ans += '\n'
print(ans)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	print(b % max(a, b // 2 + 1))


import sys
import math
import bisect
from sys import stdin, stdout
from math import gcd, floor, sqrt, log
from collections import defaultdict as dd
from bisect import bisect_left as bl, bisect_right as br
sys.setrecursionlimit(100000000)
input = sys.stdin.buffer.readline
inp = lambda : int(input())
strng = lambda : input().strip()
jn = lambda x, l: x.join(map(str, l))
strl = lambda : list(input().strip())
mul = lambda : map(int, input().strip().split())
mulf = lambda : map(float, input().strip().split())
seq = lambda : list(map(int, input().strip().split()))
seq_nll = lambda : list(map(int, input().strip()))
ceil = lambda x: int(x) if x == int(x) else int(x) + 1
ceildiv = lambda x, d: x // d if x % d == 0 else x // d + 1
flush = lambda : stdout.flush()
stdstr = lambda : stdin.readline()
stdint = lambda : int(stdin.readline())
stdpr = lambda x: stdout.write(str(x))
mod = 1000000007
for _ in range(int(input())):
	(l, r) = input().split()
	l = int(l)
	r = int(r)
	hf = r // 2
	if hf == 0:
		print(r % l)
	elif l <= hf:
		if r % hf == 0:
			print(r % (hf + 1))
		else:
			print(hf)
	else:
		print(r % l)


t = int(input())
for i in range(t):
	(l, r) = map(int, input().split())
	a = r
	b = r // 2 + 1
	if b < l:
		b = l
	print(a % b)


for _ in range(int(input())):
	(l, r) = map(int, input().split())
	if l > r // 2:
		ans = r - l
	elif r == l:
		ans = 0
	else:
		if r % 2 == 1:
			r += 1
		ans = r // 2 - 1
	print(ans)


import copy
import math
from sys import stdin

def A():
	t = int(stdin.readline())
	while t:
		t -= 1
		(l, r) = [int(x) for x in stdin.readline().split()]
		k = r // 2
		if k < l:
			print(r - l)
		else:
			print(r - k - 1)

def B():
	t = int(stdin.readline())
	while t:
		t -= 1
		n = int(stdin.readline())

def C():
	t = int(stdin.readline())
	while t:
		t -= 1
		a = [int(x) for x in stdin.readline().split()]

def D():
	t = int(stdin.readline())
	while t:
		t -= 1

def E():
	t = int(stdin.readline())
	while t:
		t -= 1
A()


def main():
	t = int(input())
	for i in range(t):
		(n, m) = map(int, input().split())
		if n > (m + 1) // 2:
			print(m % n)
		else:
			x = (m + 1) // 2
			print(m % x if m % x > m % (x + 1) or x + 1 > m else m % (x + 1))
main()


TEST_CASES = int(input(''))
for x in range(TEST_CASES):
	RANGE = input('').split(' ')
	LR = int(RANGE[0])
	HR = int(RANGE[1])
	if LR > HR / 2:
		print(int(HR % LR))
	elif LR <= HR / 2:
		print(HR % int(HR / 2 + 1))


t = int(input())
lista = []
for i in range(t):
	(l, r) = map(int, input().split())
	if r % 2 == 0:
		if l <= r / 2:
			lista.append(r % (r // 2 + 1))
		else:
			lista.append(r % l)
	elif l <= r / 2:
		lista.append(r % (r // 2 + 1))
	else:
		lista.append(r % l)
for i in lista:
	print(i)


a = int(input())
for x in range(a):
	(b, c) = map(int, input().split())
	d = c // 2
	if d >= b:
		e = c % (d + 1)
		print(e)
	else:
		print(c % b)


def main():
	test = int(input())
	i = 0
	while i < test:
		(l, r) = map(int, input().split(' '))
		rem = max_mod(l, r)
		print(rem)
		i += 1

def max_mod(min, max):
	if max // 2 >= min:
		max_rem = max - 1 - max // 2
	else:
		max_rem = max - min
	return max_rem
main()


t = int(input())
for i in range(t):
	(l, r) = map(int, input().split())
	maxi = max(l, r // 2 + 1)
	print(r % maxi)


test_cases = input()

def inlt():
	return list(map(int, input().split()))

def solve(l, r):
	if r // 2 + 1 >= l:
		return r % (r // 2 + 1)
	elif r // 2 + 1 < l:
		return r % l
ans = []
for i in range(int(test_cases)):
	lis = inlt()
	ans.append(solve(lis[0], lis[1]))
for i in ans:
	print(i, end='\n')


testcase = int(input())
for i in range(testcase):
	(l, r) = [int(i) for i in input().split()]
	if int(r / 2) + 1 >= l:
		print(r % (int(r / 2) + 1))
	else:
		print(r % l)


import math

def fun(a, b):
	if b % 2 == 0:
		temp = b // 2 + 1
	else:
		temp = math.ceil(b / 2)
	if temp < a:
		print(b % a)
	else:
		print(b % temp)
n = int(input())
l1 = []
for i in range(n):
	(a, b) = input().split()
	a = int(a)
	b = int(b)
	l1.append([a, b])
for i in l1:
	fun(i[0], i[1])


import sys
input = sys.stdin.readline
ins = lambda : input().rstrip()
ini = lambda : int(input().rstrip())
inm = lambda : map(int, input().rstrip().split())
inl = lambda : list(map(int, input().split()))
out = lambda x, s='\n': print(s.join(map(str, x)))
t = ini()
for _ in range(t):
	(l, r) = inm()
	if r // 2 + 1 >= l:
		print(r % (r // 2 + 1))
	else:
		print(r % l)


def largest_modulo(l, r):
	if l > r // 2:
		return r % l
	else:
		a = r // 2 + 1
		return r % a
t = int(input())
a = []
while t > 0:
	arr = [int(i) for i in input().split()]
	(l, r) = (arr[0], arr[1])
	a.append(largest_modulo(l, r))
	t -= 1
for i in a:
	print(i)


import sys, os, io
import math
from collections import defaultdict

def ii():
	return int(input())

def li():
	return list(map(int, input().split()))
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
else:
	input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def solve():
	(l, r) = li()
	x = r // 2 + 1
	if l <= x:
		print(r % x)
	else:
		print(r % l)
t = 1
t = ii()
for _ in range(t):
	solve()


from collections import deque, Counter
from collections import defaultdict as dfd
from bisect import bisect, bisect_left
from math import sqrt, gcd, ceil, factorial
from heapq import heapify, heappush, heappop
import sys
sys.setrecursionlimit(10 ** 7)
MOD = 10 ** 9 + 7
inf = float('inf')
ans_ = []

def nin():
	return int(input())

def ninf():
	return int(file.readline())

def st():
	return input().strip()

def stf():
	return file.readline().strip()

def read():
	return list(map(int, input().strip().split()))

def readf():
	return list(map(int, file.readline().strip().split()))

def readfl():
	return list(map(float, input().strip().split()))

def readffl():
	return list(map(float, file.readline().strip().split()))

def solve():
	for _ in range(nin()):
		(l, r) = read()
		x = max(l, r // 2 + 1)
		ans_.append(r % x)
solve()
for i in ans_:
	print(i)


for _ in range(int(input())):
	(l, r) = map(int, input().split())
	if r % 2:
		div = (r + 1) // 2
	else:
		div = r // 2 + 1
	if l > div:
		div = l
	print(r - div)


import sys
I = lambda x=' ': map(int, sys.stdin.readline().split(x))
pr = lambda *x, sep=' ', end='\n': sys.stdout.write(f'{sep.join(map(str, x))}{end}')

def solve():
	(l, r) = I()
	a = r
	b = max(l, r // 2 + 1)
	pr(a % b)

def main():
	(t,) = I()
	for _ in range(t):
		solve()
main()


t = int(input())
for i in range(0, t):
	(l, r) = map(int, input().split())
	max = 0
	q = r // l
	if l == r:
		max = 0
	elif r < l * 2:
		max = r - l
	else:
		max = (r - 1) // 2
	print(max)


def f(l, r):
	if l <= r // 2:
		return r % (r // 2 + 1)
	else:
		return r % l
n = int(input())
lst = []
for item in range(n):
	lst.append(f(*[int(item) for item in input().split()]))
print(*lst, sep='\n')


n = int(input())
for i in range(n):
	t = list(input().split())
	t = [int(j) for j in t]
	t = sorted(t)
	if t[0] <= t[1] // 2:
		res = t[1] % (t[1] // 2 + 1)
	else:
		res = t[1] % t[0]
	print(res)


import math
import sys
import collections
import bisect
import time
import random
import itertools
from collections import Counter, defaultdict

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def get_list():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_string():
	return sys.stdin.readline().strip()
for t in range(int(input())):
	(l, r) = get_ints()
	if 2 * l <= r:
		print(math.ceil(r / 2) - 1)
	else:
		print(r % l)


t = int(input())
for _ in range(t):
	(l, r) = [int(i) for i in input().split()]
	a = r
	b = a // 2 + 1 if a % 2 == 0 else a // 2
	if b < l:
		print(r - l)
	elif a % 2 == 0:
		print(a - b)
	else:
		print(a - b - 1)


for i in range(int(input())):
	(l, r) = map(int, input().split())
	a = r // 2 + 1
	print(r - max(l, a))


from sys import stdin
input = stdin.readline
t = int(input())
for _ in range(t):
	(l, r) = [int(x) for x in input().split()]
	x = max(r // 2 + 1, l)
	ans = r % x
	print(ans)


for _ in range(int(input())):
	(l, r) = map(int, input().split())
	print(r % max(l, r // 2 + 1))


from bisect import insort, bisect_right, bisect_left
from sys import stdout, stdin, setrecursionlimit
from heapq import heappush, heappop, heapify
from io import BytesIO, IOBase
from collections import *
from itertools import *
from random import *
from string import *
from queue import *
from math import *
from re import *
from os import *

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = read(self._fd, max(fstat(self._fd).st_size, 8192))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = read(self._fd, max(fstat(self._fd).st_size, 8192))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(stdin, stdout) = (IOWrapper(stdin), IOWrapper(stdout))
(graph, mod, szzz) = ({}, 10 ** 9 + 7, lambda : sorted(zzz()))

def getStr():
	return input()

def getInt():
	return int(input())

def listStr():
	return list(input())

def getStrs():
	return input().split()

def isInt(s):
	return '0' <= s[0] <= '9'

def input():
	return stdin.readline().strip()

def zzz():
	return [int(i) for i in input().split()]

def output(answer, end='\n'):
	stdout.write(str(answer) + end)

def lcd(xnum1, xnum2):
	return xnum1 * xnum2 // gcd(xnum1, xnum2)

def getPrimes(N=10 ** 5):
	SN = int(sqrt(N))
	sieve = [i for i in range(N + 1)]
	sieve[1] = 0
	for i in sieve:
		if i > SN:
			break
		if i == 0:
			continue
		for j in range(2 * i, N + 1, i):
			sieve[j] = 0
	prime = [i for i in range(N + 1) if sieve[i] != 0]
	return prime

def primeFactor(n, prime=getPrimes()):
	lst = []
	mx = int(sqrt(n)) + 1
	for i in prime:
		if i > mx:
			break
		while n % i == 0:
			lst.append(i)
			n //= i
	if n > 1:
		lst.append(n)
	return lst
dx = [-1, 1, 0, 0, 1, -1, 1, -1]
dy = [0, 0, 1, -1, 1, -1, -1, 1]
daysInMounth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
num = getInt()
for _ in range(num):
	(l, r) = zzz()
	mn = ceil(r / 2) - 1
	if r >= 2 * l:
		print(mn)
	else:
		print(r % l)


import math
for t in range(int(input())):
	(l, r) = map(int, input().split())
	if r // 2 >= l:
		print(r - r // 2 - 1)
	else:
		print(r % l)


def main():
	t = input()
	for _ in range(int(t)):
		(l, r) = input().split(' ')
		(l, r) = (int(l), int(r))
		a = r
		if 2 * l > r:
			b = l
		else:
			b = a // 2 + 1
		print(a - b)
main()


for i in range(int(input())):
	(a, b) = input().split()
	a = int(a)
	b = int(b)
	x = int(b / 2) + 1
	x = int(x)
	if x >= a:
		print(b % x)
	else:
		print(b % a)


m = int(input())
l = []
for i in range(m):
	(a, b) = map(int, input().split())
	c = b % (b // 2 + 1)
	if c < a:
		if b % a == 0 and a != b:
			l.append(b % (a + 1))
		else:
			l.append(b % a)
	else:
		l.append(c)
for i in l:
	print(i)


t = int(input())
firstn = []
secondn = []
answers = []
for i in range(0, t, 1):
	(x, y) = input().split()
	firstn.append(x)
	secondn.append(y)
for (n1, n2) in zip(firstn, secondn):
	n1 = int(n1)
	n2 = int(n2)
	if n1 > n2:
		if n1 // 2 + 1 >= n2:
			mod = n1 % (n1 // 2 + 1)
			answers.append(mod)
		else:
			answers.append(n1 % n2)
	elif n2 // 2 + 1 >= n1:
		mod = n2 % (n2 // 2 + 1)
		answers.append(mod)
	else:
		answers.append(n2 % n1)
for answer in answers:
	print(answer)


for i in range(int(input())):
	(l, r) = tuple(map(int, input().split()))
	if r % 2 == 0:
		if r // 2 < l:
			print(r % l)
		else:
			print(r // 2 - 1)
	elif r // 2 < l:
		print(r % l)
	else:
		print(r // 2)


import bisect
import heapq
import math
import collections
import sys
import copy
from functools import reduce
import decimal
from io import BytesIO, IOBase
import os
import itertools
import functools
from types import GeneratorType
sys.setrecursionlimit(10 ** 9)
decimal.getcontext().rounding = decimal.ROUND_HALF_UP
graphDict = collections.defaultdict
queue = collections.deque

def bootstrap(f, stack=[]):

	def wrappedfunc(*args, **kwargs):
		to = f(*args, **kwargs)
		if stack:
			return to
		else:
			while True:
				if type(to) is GeneratorType:
					stack.append(to)
					to = next(to)
				else:
					stack.pop()
					if not stack:
						return to
					to = stack[-1].send(to)
	return wrappedfunc

class Graphs:

	def __init__(self):
		self.graph = graphDict(set)

	def add_edge(self, u, v):
		self.graph[u].add(v)
		self.graph[v].add(u)

	def dfs_utility(self, nodes, visited_nodes, psf):
		global ans
		if len(visited_nodes.keys()) == n:
			ans = psf
			return
		visited_nodes[nodes] = None
		for neighbour in self.graph[nodes]:
			if neighbour not in visited_nodes:
				self.dfs_utility(neighbour, visited_nodes, psf + [neighbour])
		visited_nodes.pop(nodes)
		return

	def dfs(self, node):
		global ans
		Visited = collections.OrderedDict()
		self.dfs_utility(node, Visited, [node])

	def bfs(self, node):
		visited = set()
		if node not in visited:
			queue.append(node)
			visited.add(node)
		while queue:
			parent = queue.popleft()
			print(parent)
			for item in self.graph[parent]:
				if item not in visited:
					queue.append(item)
					visited.add(item)

class Tree:

	def __init__(self, data):
		self.data = data
		self.left = None
		self.right = None

def inorder(node, lis):
	if node:
		inorder(node.left, lis)
		lis.append(node.data)
		inorder(node.right, lis)
	return lis

def leaf_node_sum(root):
	if root is None:
		return 0
	if root.left is None and root.right is None:
		return root.data
	return leaf_node_sum(root.left) + leaf_node_sum(root.right)

def hight(root):
	if root is None:
		return -1
	if root.left is None and root.right is None:
		return 0
	return max(hight(root.left), hight(root.right)) + 1

def rounding(n):
	return int(decimal.Decimal(f'{n}').to_integral_value())

def factors(n):
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0), []))

def p_sum(array):
	return list(itertools.accumulate(array))
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self, **kwargs):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def inp():
	return sys.stdin.readline().strip()

def map_inp(v_type):
	return map(v_type, inp().split())

def list_inp(v_type):
	return list(map_inp(v_type))
for _ in range(int(inp())):
	(l, r) = map_inp(int)
	if r & 1:
		if r // 2 >= l:
			print(r // 2)
		else:
			print(r % l)
	elif r // 2 - 1 >= l:
		print(r // 2 - 1)
	elif l == r // 2:
		print(r % (l + 1))
	else:
		print(r % l)


def mex(L, R):
	if L == R:
		T = 0
	elif L * 2 < R:
		if R % 2:
			T = R // 2
		else:
			T = R // 2 - 1
	else:
		if R % L == 0:
			L += 1
		T = R % L
	return T
N = int(input())
Z = []
for q in range(N):
	(l, r) = list(map(int, input().split()))
	Z.append(mex(l, r))
for me in Z:
	print(me)


t = int(input())
ans = []
for i in range(t):
	inp = input().split()
	l = int(inp[0])
	r = int(inp[1])
	a = r // 2 + 1
	if l <= a:
		ans.append(int(r - a))
	else:
		ans.append(int(r - l))
	i += 1
for b in range(len(ans)):
	print(ans[b])


t = int(input())
out = []
for i in range(t):
	(l, r) = [int(j) for j in input().split()]
	if r % 2 == 0:
		if l <= r // 2 + 1 <= r:
			res = r // 2 + 1
		else:
			res = l
	elif l <= (r + 1) // 2 <= r:
		res = (r + 1) // 2
	else:
		res = l
	out.append(r % res)
for i in out:
	print(i)


t = int(input())
ans = []
for _ in range(t):
	(l, r) = [int(x) for x in input().split()]
	if r >= l << 1:
		ans.append(str((r + 1 >> 1) - 1))
	else:
		ans.append(str(r - l))
print('\n'.join(ans))


import sys
testcases = int(input())
while testcases > 0:
	(r, l) = map(int, sys.stdin.readline().split())
	if l >= 2 * r:
		c = (l - 1) / 2
	else:
		c = l - r
	print(int(c))
	testcases -= 1


test_cases = int(input())
for i in range(test_cases):
	[l, r] = input().split(' ')
	l = int(l)
	r = int(r)
	if r % 2 == 0:
		temp = r / 2
		if temp >= l:
			maxRemain = temp - 1
		else:
			maxRemain = r % l
	else:
		temp = (r + 1) / 2
		if temp >= l:
			maxRemain = temp - 1
		else:
			maxRemain = r % l
	print(int(maxRemain))


def solve():
	(l, r) = map(int, input().split())
	if r < l * 2:
		print(r - l)
	else:
		print((r - 1) // 2)
t = int(input())
for _ in range(t):
	solve()


import sys

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def get_list():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_list_string():
	return list(map(str, sys.stdin.readline().strip().split()))

def get_string():
	return sys.stdin.readline().strip()

def get_int():
	return int(sys.stdin.readline().strip())

def get_print_int(x):
	sys.stdout.write(str(x) + '\n')

def get_print(x):
	sys.stdout.write(x + '\n')

def get_print_int_same(x):
	sys.stdout.write(str(x) + ' ')

def get_print_same(x):
	sys.stdout.write(x + ' ')
from sys import maxsize
from math import ceil

def solve():
	for _ in range(get_int()):
		(l, r) = get_ints()
		x = ceil(r / 2)
		if x >= l:
			get_print_int(x - 1)
		else:
			get_print_int(r % l)
solve()


t = int(input())
while t:
	(l, r) = map(int, input().split())
	max = 0
	if r // 2 + 1 >= l:
		l = r // 2 + 1
	print(r % l)
	t -= 1


t = int(input())
for i in range(t):
	(a, b) = map(int, input().split())
	if b // 2 + b % 2 >= a:
		ans = b // 2 + 1
		print(b % ans)
	else:
		print(b % a)


read = lambda : map(int, input().split())
t = int(input())
for i in range(t):
	(l, r) = read()
	a = r - (r % 2 == 0)
	b = max(l, (a + 1) // 2)
	if a < l:
		a = l
	res1 = max(a % b, r % l)
	print(res1)


t = int(input())
for tt in range(t):
	(l, r) = map(int, input().split())
	l = max(l, (r + 1) // 2 + int(r % 2 == 0))
	print(r % l)


import sys
for _ in range(int(sys.stdin.readline())):
	(l, r) = map(int, sys.stdin.readline().split())
	if l == r:
		print(0)
		continue
	start = r // 2
	if start < l:
		print(r % l)
		continue
	print(r % (start + 1))


def getlistint():
	return list(map(int, stdin.readline().split()))

def getmapint():
	return map(int, stdin.readline().split())

def getliststr():
	return list(stdin.readline().split())

def getint():
	return int(stdin.readline().strip())

def getstr():
	return stdin.readline().strip()

def pr(s):
	stdout.write(str(s) + '\n')

def prlist(l):
	stdout.write(' '.join(list(map(str, l))) + '\n')
from sys import stdin, stdout
for _ in range(getint()):
	(l, r) = getmapint()
	mid = r // 2 + 1
	l = max(l, mid)
	pr(r % l)


import sys, os, io
input = sys.stdin.readline
PI = 3.141592653589793
INF = float('inf')
MOD = 1000000007

def bin32(num):
	return '{0:032b}'.format(num)

def add(x, y):
	return (x + y) % MOD

def sub(x, y):
	return (x - y + MOD) % MOD

def mul(x, y):
	return x * y % MOD

def gcd(x, y):
	if y == 0:
		return x
	return gcd(y, x % y)

def lcm(x, y):
	return x * y // gcd(x, y)

def power(x, y):
	res = 1
	x %= MOD
	while y != 0:
		if y & 1:
			res = mul(res, x)
		y >>= 1
		x = mul(x, x)
	return res

def mod_inv(n):
	return power(n, MOD - 2)

def prob(p, q):
	return mul(p, power(q, MOD - 2))

def ii():
	return int(input())

def li():
	return [int(i) for i in input().split()]

def ls():
	return [i for i in input().split()]
for t in range(ii()):
	t += 1
	(l, r) = li()
	if r % 2 == 1:
		if l > r // 2:
			print(r % l)
		else:
			print(r % ((r + 1) // 2))
	else:
		x = r // 2
		x += 1
		if x >= l:
			print(r % x)
		else:
			print(r % l)


for s in [*open(0)][1:]:
	(a, b) = map(int, s.split())
	print(b % max(a, b // 2 + 1))


import sys, math, io, os, time, itertools, collections
mod = 10 ** 9 + 7
sys.setrecursionlimit(10000)
i = sys.stdin.readline
p = sys.stdout.write
global start, end

def isin(l, x):
	left = 0
	right = len(l) - 1
	if x < l[0]:
		return -1
	while left <= right:
		mid = left + (right - left) // 2
		if l[mid] == x:
			return mid
		elif l[mid] < x:
			ans = mid
			left = mid + 1
		else:
			right = mid - 1
	return ans

def ispal(l):
	n = len(l)
	for i in range(n // 2 + 1):
		if l[i] != l[n - i - 1]:
			return False
	return True

def ccarray(l):
	d = {l[k]: k for k in range(len(l))}
	m = sorted(d)
	return [d[m[k]] for k in range(len(l))]

def is_prime(n):
	if n <= 3:
		return n > 1
	if n % 2 == 0 or n % 3 == 0:
		return False
	k = 5
	while k ** 2 <= n:
		if n % k == 0 or n % (k + 2) == 0:
			return False
		k += 6
	return True

def sieve(n):
	prime = [True for k in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for k in range(p * p, n + 1, p):
				prime[k] = False
		p += 1

def main():
	(l, r) = [int(k) for k in i().split()]
	z = r // 2 + 1
	l = int(max(l, z))
	ans = r % l
	p(str(ans) + '\n')
t = 1
t = int(i())
start = time.perf_counter()
for _ in range(t):
	main()
end = time.perf_counter()


t = int(input())
while t:
	t -= 1
	(l, r) = [int(x) for x in input().split()]
	if l > r // 2:
		print(r % l)
	else:
		print(r % (r // 2 + 1))


n = int(input())
for i in range(n):
	(l, r) = map(int, input().split())
	if 2 * l - r > 0:
		print(r - l)
	else:
		print((r - 1) // 2)


def find_max_modular(l, r):
	div_two = (r + 1) // 2
	if div_two < l:
		return r - l
	else:
		mod = r - 1 - div_two if r % 2 == 0 else r - div_two
		return mod
num_test_case = int(input())
test_cases = []
for i in range(num_test_case):
	lef_and_right = input().split()
	left = int(lef_and_right[0])
	right = int(lef_and_right[1])
	test_cases.append((left, right))
for test_case in test_cases:
	print(find_max_modular(test_case[0], test_case[1]))


n = int(input())
for x in range(n):
	(a, b) = map(int, input().split())
	if a == b:
		print('0')
	else:
		if a > b / 2:
			print(b % a)
		if a <= b / 2:
			r = int(b / 2)
			k = int(b % r)
			t = int(b % (r + 1))
			print(int(max(k, t)))


t = int(input())
l = []
o = []
for i in range(t):
	t = input()
	l.append([int(i) for i in t.split(' ')])
for i in l:
	o.append(i[1] % max(i[0], i[1] // 2 + 1))
print('\n'.join([str(i) for i in o]))


def solve(l, r):
	if r % 2 == 0:
		if l <= r // 2:
			return r // 2 - 1
		else:
			return r - l
	elif l <= r // 2:
		return r // 2
	else:
		return r - l
for _ in range(int(input())):
	(l, r) = map(int, input().split())
	print(solve(l, r))


import os
import math
import sys
parse_input = lambda : sys.stdin.readline().rstrip('\r\n')

def func(l, r):
	if l == r:
		return 0
	if r // 2 > l:
		if r % 2 == 0:
			return r - (r // 2 + 1)
		else:
			return r - (r + 1) // 2
	elif r // 2 < l:
		return r % l
	elif r // 2 == l:
		return r % (l + 1)

def main():
	n_cases = int(parse_input())
	for i in range(n_cases):
		(l, r) = [int(i) for i in parse_input().split()]
		print(func(l, r))
main()


import sys
input = sys.stdin.readline
t = int(input())
while t > 0:
	(a, b) = map(int, input().split())
	(a, b) = (min(a, b), max(a, b))
	if b // 2 >= a:
		print(b % (b // 2 + 1))
	else:
		print(b % a)
	t -= 1


for _ in range(int(input())):
	(l, r) = input().split()
	(l, r) = (int(l), int(r))
	x = r // 2 + 1
	if x > r // 2:
		x = r // 2 + 1
	x = max(l, x)
	print(r % x)


import sys
import math
import itertools
import functools
import collections
import operator
import fileinput
import copy
import string
ORDA = 97

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return list(map(int, input().split()))

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def revn(n):
	return str(n)[::-1]

def dd():
	return collections.defaultdict(int)

def ddl():
	return collections.defaultdict(list)

def sieve(n):
	if n < 2:
		return list()
	prime = [True for _ in range(n + 1)]
	p = 3
	while p * p <= n:
		if prime[p]:
			for i in range(p * 2, n + 1, p):
				prime[i] = False
		p += 2
	r = [2]
	for p in range(3, n + 1, 2):
		if prime[p]:
			r.append(p)
	return r

def divs(n, start=2):
	r = []
	for i in range(start, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n // i == i:
				r.append(i)
			else:
				r.extend([i, n // i])
	return r

def divn(n, primes):
	divs_number = 1
	for i in primes:
		if n == 1:
			return divs_number
		t = 1
		while n % i == 0:
			t += 1
			n //= i
		divs_number *= t

def prime(n):
	if n == 2:
		return True
	if n % 2 == 0 or n <= 1:
		return False
	sqr = int(math.sqrt(n)) + 1
	for d in range(3, sqr, 2):
		if n % d == 0:
			return False
	return True

def convn(number, base):
	new_number = 0
	while number > 0:
		new_number += number % base
		number //= base
	return new_number

def cdiv(n, k):
	return n // k + (n % k != 0)

def ispal(s):
	for i in range(len(s) // 2 + 1):
		if s[i] != s[-i - 1]:
			return False
	return True
for _ in range(ii()):
	(l, r) = mi()
	if 2 * l > r:
		print(r % l)
	else:
		print(r // 2 if r & 1 else r // 2 - 1)


import sys
input = sys.stdin.readline
T = int(input())
for _ in range(T):
	(l, r) = map(int, input().split())
	if (r + 1) / 2 >= l:
		print((r + 1) // 2 - 1)
	else:
		print(r % l)


t = int(input(''))
while t > 0:
	(a, b) = map(int, input().split())
	if b % 2 == 1:
		term2 = (b + 1) // 2
	else:
		term2 = b // 2
	if term2 < a:
		print(b % a)
	else:
		print(max(b % a, term2 - 1))
	t -= 1


import bisect
import math
mod = 998244353
N = 200005

def mul(a, b):
	return a * b % mod

def add(a, b):
	return a + b if a + b < mod else a + b - mod

def sub(a, b):
	return a - b if a - b > 0 else a - b + mod

def powr(a, b):
	ans = 1
	while b > 0:
		if b & 1:
			ans = mul(ans, a)
		a = mul(a, a)
		b //= 2
	return ans

def inv(n):
	return powr(n, mod - 2)

def factlist():
	fact = [1]
	for i in range(1, N):
		fact.append(mul(fact[i - 1], i))
	return fact

def invfactlist(fact):
	invfact = [0] * N
	invfact[0] = 1
	invfact[N - 1] = inv(fact[N - 1])
	for i in range(N - 2, 0, -1):
		invfact[i] = mul(invfact[i + 1], i + 1)
	return invfact

def rot(S):
	return list(zip(*S[::-1]))

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def main():
	t = int(input())
	while t:
		t -= 1
		(l, r) = map(int, input().split())
		if l == r:
			print('0')
			continue
		if l > (r + 1) // 2:
			print(r - l)
			continue
		else:
			k = (r + 1) // 2
			print(k - 1)
main()


def f(a, b):
	if a <= b // 2:
		return max(b % (b // 2), b % (b // 2 + 1))
	return b % a
for i in range(int(input())):
	(a, b) = map(int, input().split())
	print(f(a, b))


import math
t = int(input())
for _ in range(t):
	(l, r) = map(int, input().split())
	b = max(l, math.ceil((r + 1) / 2))
	print(r % b)


for _ in range(int(input())):
	(l, r) = map(int, input().split())
	a = r
	b = r // 2 + 1
	if b < l:
		b = l
	print(a % b)


import sys
input = sys.stdin.readline
n = int(input())
for i in range(n):
	(a, b) = list(map(int, input().split()))
	if a == b:
		print(0)
	elif a <= b // 2 + 1:
		tmp = b % (b // 2 + 1)
		print(tmp)
	else:
		tmp = b % a
		print(tmp)


import sys, os
import time
t = int(input())
while t > 0:
	(l, r) = list(map(int, input().split()))
	if l == r:
		print(0)
	elif l <= r / 2:
		if r % 2 == 0:
			print(r // 2 - 1)
		else:
			print(r // 2)
	else:
		print(r - l)
	t -= 1


import math
for _ in range(int(input())):
	(l, r) = map(int, input().split())
	k = r // 2 + 1
	if k >= l:
		print(r % k)
	else:
		print(r % l)


num = int(input())
bounds = []
for _ in range(num):
	(l, r) = map(int, input().split())
	bounds.append((l, r))
results = []

def find_max(edges):
	(l, r) = edges
	if l <= r // 2:
		if r % 2 == 0:
			return r // 2 - 1
		else:
			return (r - 1) // 2
	else:
		return r % l
for bound in bounds:
	results.append(find_max(bound))
print('\n'.join(map(str, results)))


import math
for _ in range(int(input())):
	(a, b) = map(int, input().split())
	ans = 0
	if int(b / 2) >= a:
		if int(b / 2) == b / 2:
			ans = int(b / 2) - 1
			print(ans)
		else:
			ans = math.floor(b / 2)
			print(ans)
	else:
		ans = b % a
		print(ans)


n = int(input())
for i in range(n):
	(a, m) = map(int, input().split())
	if m % 2 == 0:
		if m // 2 >= a:
			print(m // 2 - 1)
		else:
			print(m - a)
	elif m % 2 != 0:
		if m // 2 + 1 >= a:
			print(m // 2)
		else:
			print(m - a)


t = int(input(''))
l = []
if t >= 1 and t <= 10000:
	for i in range(t):
		a = 0
		b = 0
		c = 0
		(p, q) = map(int, input().split())
		if p >= 1 and q <= 1000000000 and (p <= q):
			if q / 2 >= p:
				a = q / 2
				b = int(a) + 1
				c = q % b
				l.append(int(c))
			else:
				c = q % p
				l.append(c)
		else:
			break
	for i in l:
		print(i)


t = int(input())

def get_sol(a, b):
	if a <= b * 0.5:
		return int((b + 1) * 0.5 - 1)
	else:
		return int(b % a)
tests = []
for i in range(t):
	tests.append(list(map(int, input().split())))
for i in range(t):
	print(get_sol(tests[i][0], tests[i][1]))


import sys
input = lambda : sys.stdin.readline().strip()
for i in range(int(input())):
	(l, r) = map(int, input().split())
	if l <= r // 2 + 1:
		print(r % (r // 2 + 1))
	else:
		print(r % l)


T = int(input())
myinp = []
for i in range(T):
	myinp.append(input())
for i in range(T):
	inp = myinp[i].split()
	for i in range(len(inp)):
		inp[i] = int(inp[i])
	l = inp[0]
	r = inp[1]
	a = r
	b = int(r / 2) + 1
	if b >= l:
		print(a % b)
	else:
		print(a % l)


for _ in range(int(input())):
	x = list(map(int, input().split()))
	x.sort(reverse=True)
	a = x[0]
	b = x[1]
	if a // 2 < b:
		print(a % b)
	else:
		print(a % (a // 2 + 1))


import sys
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep='\n')

def II():
	return int(sys.stdin.readline())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI(rows_number):
	return [LI() for _ in range(rows_number)]

def LI1():
	return list(map(int1, sys.stdin.readline().split()))

def LLI1(rows_number):
	return [LI1() for _ in range(rows_number)]

def SI():
	return sys.stdin.readline().rstrip()
inf = 10 ** 16
md = 10 ** 9 + 7

def solve():
	(l, r) = LI()
	l = max(l, r // 2 + 1)
	print(r % l)
for testcase in range(II()):
	solve()


for c in range(int(input())):
	(l, n) = map(int, input().split())
	k = 0
	if l == n:
		print(0)
		continue
	if n % 2 == 0:
		k = n / 2 - 1
	else:
		k = int(n / 2)
	if l > k:
		while n % l == 0 and n != l:
			l += 1
		print(n % l)
	else:
		print(int(k))


x = int(input())
l = []
for i in range(x):
	l.append(list(map(int, input().split())))
for i in l:
	a = 0
	b = 0
	if i[1] % 2 == 0:
		b = int(i[1] / 2 + 1)
		a = i[1]
	elif i[1] % 2 != 0:
		b = int((i[1] + 1) / 2)
		a = i[1]
	ans = a % b
	if b < i[0]:
		ans -= i[0] - b
	print(ans)


n = int(input())
for i in range(1, n + 1):
	(l, r) = map(int, input().split())
	if r // 2 + 1 < l:
		ans = r - l
	else:
		x = r // 2 + 1
		ans = r - x
	print(ans)


import sys

def max_mod(a, b):
	best_b = max(a // 2 + 1, b)
	return a % best_b

def solve(l, r):
	return max_mod(r, l)

def main():
	test_cases = int(input())
	answers = []
	for _ in range(test_cases):
		(l, r) = map(int, input().split())
		a = solve(l, r)
		answers.append(a)
	print(*answers, sep='\n')
main()


ll = lambda : map(int, input().split())
t = lambda : int(input())
ss = lambda : input()
lx = lambda x: map(int, input().split(x))
yy = lambda : print('YES')
nn = lambda : print('NO')
from math import log10, log2, ceil, factorial as fac, gcd, inf, sqrt, log
from bisect import bisect_right as br, bisect_left as bl
from collections import Counter
mod = 10 ** 9 + 7
for _ in range(t()):
	(a, b) = ll()
	x = b // 2 + (1 if b % 2 == 0 else 0)
	if 2 * a > b:
		print(b - a)
	else:
		print((b - 1) // 2)


for _ in range(int(input().strip())):
	(a, b) = map(int, input().strip().split())
	ans = 0
	num = b // 2 + 1
	if a == b:
		ans = 0
	elif num >= a and num <= b:
		ans = max(b % a, b % num)
	else:
		num = (a + b) // 2 + 1
		ans = max(b % a, b % num)
	print(ans)


t = int(input())
ls = []
for i in range(t):
	(l, r) = map(int, input().split())
	if r // 2 >= l and r // 2 == r / 2:
		ls.append((r - 1) % (r // 2))
	elif r // 2 >= l and (not r // 2 == r / 2):
		ls.append(r % (r // 2 + 1))
	else:
		ls.append(r % l)
for i in range(t):
	print(ls[i])


from sys import stdin, setrecursionlimit, maxsize
from collections import deque
from heapq import *
from math import sqrt, gcd
input = stdin.readline
setrecursionlimit(10 ** 5)
for _ in range(int(input())):
	(l, r) = map(int, input().split())
	if r < l * 2:
		print(r - l)
	else:
		print((r - 1) // 2)


t = int(input())
for i in range(t):
	(l, r) = map(int, input().split())
	ans = 0
	a = max(l, (r + 1) // 2)
	for j in range(max(l, r - 1), r + 1):
		ans = max(ans, j % a)
	print(ans)


import sys
input = sys.stdin.readline
t = int(input())
for tests in range(t):
	(l, r) = map(int, input().split())
	ANS = r % l
	for x in range(r // 2 - 1, r // 2 + 3):
		if r >= x >= l:
			ANS = max(ANS, r % x)
	print(ANS)


import sys
for i in range(int(input())):
	t = list(map(int, input().split()))
	l = t[0]
	r = t[1]
	if l <= r / 2:
		if r % 2 == 0:
			ans = r / 2 - 1
		else:
			ans = (r - 1) / 2
	else:
		ans = r - l
	print(int(ans))


from math import gcd
from bisect import bisect_left, bisect_right
from itertools import combinations
from itertools import permutations
from bisect import bisect_left
from math import ceil
from heapq import heapify, heappush, heappop, nsmallest
import bisect
from math import pi
from collections import deque
from math import factorial
from math import log, ceil
from collections import defaultdict
from math import *
from sys import stdin, stdout
import itertools
import os
import sys
import threading
from collections import deque, Counter, OrderedDict, defaultdict
from heapq import *
from fractions import Fraction
mod = int(pow(10, 9) + 7)

def ii():
	return int(input())

def si():
	return str(input())

def mi():
	return map(int, input().split())

def li1():
	return list(mi())

def fii():
	return int(stdin.readline())

def fsi():
	return str(stdin.readline())

def fmi():
	return map(int, stdin.readline().split())

def fli():
	return list(fmi())
abd = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}

def getKey(item):
	return item[0]

def sort2(l):
	return sorted(l, key=getKey)

def d2(n, m, num):
	return [[num for x in range(m)] for y in range(n)]

def isPowerOfTwo(x):
	return x and (not x & x - 1)

def decimalToBinary(n):
	return bin(n).replace('0b', '')

def ntl(n):
	return [int(i) for i in str(n)]

def powerMod(x, y, p):
	res = 1
	x %= p
	while y > 0:
		if y & 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res
graph = defaultdict(list)
visited = [0] * 1000000
col = [-1] * 1000000

def bfs(d, v):
	q = []
	q.append(v)
	visited[v] = 1
	while len(q) != 0:
		x = q[0]
		q.pop(0)
		for i in d[x]:
			if visited[i] != 1:
				visited[i] = 1
				q.append(i)
		print(x)

def make_graph(e):
	d = {}
	for i in range(e):
		(x, y) = mi()
		if x not in d:
			d[x] = [y]
		else:
			d[x].append(y)
		if y not in d:
			d[y] = [x]
		else:
			d[y].append(x)
	return d

def gr2(n):
	d = defaultdict(list)
	for i in range(n):
		(x, y) = mi()
		d[x].append(y)
	return d

def connected_components(graph):
	seen = set()

	def dfs(v):
		vs = set([v])
		component = []
		while vs:
			v = vs.pop()
			seen.add(v)
			vs |= set(graph[v]) - seen
			component.append(v)
		return component
	ans = []
	for v in graph:
		if v not in seen:
			d = dfs(v)
			ans.append(d)
	return ans

def primeFactors(n):
	s = set()
	while n % 2 == 0:
		s.add(2)
		n = n // 2
	for i in range(3, int(sqrt(n)) + 1, 2):
		while n % i == 0 and i % 2 == 1:
			s.add(i)
			n = n // i
	if n > 2 and n % 2 == 1:
		s.add(n)
	return s

def find_all(a_str, sub):
	start = 0
	while True:
		start = a_str.find(sub, start)
		if start == -1:
			return
		yield start
		start += len(sub)

def SieveOfEratosthenes(n, isPrime):
	isPrime[0] = isPrime[1] = False
	for i in range(2, n):
		isPrime[i] = True
	p = 2
	while p * p <= n:
		if isPrime[p] == True:
			i = p * p
			while i <= n:
				isPrime[i] = False
				i += p
		p += 1
	return isPrime

def dijkstra(edges, f, t):
	g = defaultdict(list)
	for (l, r, c) in edges:
		g[l].append((c, r))
	(q, seen, mins) = ([(0, f, ())], set(), {f: 0})
	while q:
		(cost, v1, path) = heappop(q)
		if v1 not in seen:
			seen.add(v1)
			path = (v1, path)
			if v1 == t:
				return (cost, path)
			for (c, v2) in g.get(v1, ()):
				if v2 in seen:
					continue
				prev = mins.get(v2, None)
				next = cost + c
				if prev is None or next < prev:
					mins[v2] = next
					heappush(q, (next, v2, path))
	return float('inf')

def binsearch(a, l, r, x):
	while l <= r:
		mid = l + (r - 1) // 2
		if a[mid]:
			return mid
		elif a[mid] > x:
			l = mid - 1
		else:
			r = mid + 1
	return -1

def readTree(n):
	adj = [set() for _ in range(n)]
	for _ in range(n - 1):
		(u, v) = map(int, input().split())
		adj[u - 1].add(v - 1)
		adj[v - 1].add(u - 1)
	return adj

def treeOrderByDepth(n, adj, root=0):
	parent = [-2] + [-1] * (n - 1)
	ordered = []
	q = deque()
	q.append(root)
	depth = [0] * n
	while q:
		c = q.popleft()
		ordered.append(c)
		for a in adj[c]:
			if parent[a] == -1:
				parent[a] = c
				depth[a] = depth[c] + 1
				q.append(a)
	return (ordered, parent, depth)

def isSubSequence(str1, str2):
	m = len(str1)
	n = len(str2)
	j = 0
	i = 0
	while j < m and i < n:
		if str1[j] == str2[i]:
			j = j + 1
		i = i + 1
	return j == m

def nextPowerOf2(n):
	count = 0
	if n and (not n & n - 1):
		return n
	while n != 0:
		n >>= 1
		count += 1
	return 1 << count

def lastPowerOf2(n):
	count = -1
	if n and (not n & n - 1):
		return n
	while n != 0:
		n >>= 1
		count += 1
	return 1 << count

def cou(n):
	c = 0
	while n > 1:
		c += 1
		n //= 2
	return c

def sortsec(l):
	return sorted(l, key=lambda x: x[1])

def BinarySearch(a, x):
	i = bisect_left(a, x)
	if i:
		return i - 1
	else:
		return -1

def subarray(A):
	r = set()
	p = {0}
	for x in A:
		p = {x | y for y in p} | {x}
		r |= p
	return len(r)

def setBitNumber(n):
	n |= n >> 1
	n |= n >> 2
	n |= n >> 4
	n |= n >> 8
	n |= n >> 16
	n = n + 1
	return n >> 1

def countSetBits(n):
	count = 0
	while n:
		count += n & 1
		n >>= 1
	return count

def allsub(l):
	n = len(l)
	a = []
	for i in range(n - 1):
		a.append([l[:i], l[i:]])
		a.append([l[i:], l[:i]])
	return a

def closestMultiple(n, x):
	if x > n:
		return x
	z = int(x / 2)
	n = n + z
	n = n - n % x
	return n

def block(x):
	v = []
	res = []
	while x > 0:
		v.append(int(x % 2))
		x = int(x / 2)
	for i in range(0, len(v)):
		if v[i] == 1:
			res.append(i)
	return res
for _ in range(ii()):
	(l, r) = mi()
	x = r - l
	y = r // 2
	if r % 2 == 1:
		y += 1
	if x >= y:
		print(max(0, y - 1))
	else:
		print(x)


import sys
for _ in range(int(sys.stdin.readline())):
	(l, r) = [int(x) for x in sys.stdin.readline().split()]
	if l <= r // 2:
		l = r // 2 + 1
	sys.stdout.write(str(r - l) + '\n')


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	m = b // 2 + 1
	if m >= a:
		print(b % m)
	else:
		print(b % a)


t = int(input())
mass = []
for i in range(t):
	(l, r) = map(int, input().split())
	if r // l < 2:
		mass.append(r - l)
	else:
		p = r // 2 + 1
		ans = r % p
		mass.append(ans)
for i in mass:
	print(i)


t = int(input())
for _ in range(t):
	(l, r) = tuple(map(int, input().split()))
	half = r // 2
	g = max(half + 1, l)
	print(r - g)


from collections import deque
import heapq
from math import sqrt
import operator
from operator import itemgetter
import sys
import copy
inf_var = 0
if inf_var == 1:
	inf = open('input.txt', 'r')
else:
	inf = sys.stdin
input = inf.readline

def read_one_int():
	return int(input().rstrip('\n'))

def read_one_float():
	return float(input().rstrip('\n'))

def read_list_of_ints():
	res = [int(val) for val in input().rstrip('\n').split(' ')]
	return res

def read_str():
	return input().rstrip()

def main():
	tests = read_one_int()
	for _ in range(tests):
		(l, r) = read_list_of_ints()
		res_div = r // 2 + 1
		if res_div < l:
			res_div = l
		print(r % res_div)
main()


f = lambda s: map(int, s.split())
for s in [*open(0)][1:]:
	(l, r) = f(s)
	print(l < r // 2 + 1 and r % (r // 2 + 1) or r % l)


import math
debug = False

def solve(l, r):
	if l <= math.ceil(r / 2) <= r:
		print(math.ceil(r / 2) - 1)
		return
	else:
		print(r % l)
if not debug:
	queue = []
	for _ in range(int(input())):
		queue.append(tuple(map(int, input().split())))
	for el in queue:
		solve(*el)


for _ in range(int(input())):
	(l, r) = (int(i) for i in input().split())
	b = r // 2
	d = False
	if l > b:
		b = l
		d = True
	h = 0
	if not d:
		if r % 2 == 0:
			h = b - 1
		else:
			h = b
	else:
		h = r % b
	print(h)


t = int(input())
l = []
for i in range(t):
	(n, m) = input().split()
	n = int(n)
	m = int(m)
	if n <= m / 2:
		if m % 2 == 0:
			c = int(m / 2 - 1)
			l.append(c)
		else:
			c = int(m / 2)
			l.append(c)
	else:
		c = m % n
		l.append(int(c))
for j in l:
	print(int(j))


t = int(input())
for i in range(t):
	(a, b) = (int(n) for n in input().split())
	c = (b >> 1) + 1
	if c <= a:
		print(b % a)
	else:
		print(b % c)


import sys
input = sys.stdin.readline

def main():
	(l, r) = map(int, input().split())
	max_ = (r - 1) // 2
	if r - max_ >= l:
		print(max_)
	else:
		print(r - l)
for _ in range(int(input())):
	main()


import math
loop = int(input())
for i in range(loop):
	(l, r) = map(int, input().split())
	if l > r / 2:
		print(r % l)
	else:
		print(r % (int(r / 2) + 1))


import sys
AAA = '4\n1 1\n999999999 1000000000\n8 26\n1 999999999'
first = True
for line in sys.stdin.readlines():
	if first:
		first = False
		continue
	(l, r) = map(int, line.split())
	max_mod = 0
	(ma, mb) = (0, 0)
	b = max((r + 1) // 2, l)
	g = min(b + b - 1, r)
	print(g % b)


for i in range(int(input())):
	(a, b) = map(int, input().split())
	if a == b:
		print(0)
	elif b // 2 >= a:
		c = b // 2
		print(b - c - 1)
	else:
		print(b - a)


test = int(input())
for i in range(test):
	l1 = list(map(int, input().split()))
	(l, r) = (l1[0], l1[1])
	t = max(l, r // 2 + 1)
	print(r - t)


import math
from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	(l, r) = map(int, input().split())
	k = r
	if r & 1:
		r = r - 1
	if (r >> 1) + 1 >= l:
		print(k - ((r >> 1) + 1))
	else:
		print(k - l)


for _ in range(int(input())):
	(l, r) = list(map(int, input().split()))
	a = r
	b = max(r // 2 + 1, l)
	print(a % b)


mod = 1000000007
from sys import stdin, stdout
for _ in range(int(stdin.readline())):
	m = 0
	(a, b) = map(int, stdin.readline().split())
	(q, w) = (b, max(b // 2 + 1, a))
	print(q % w)


t = int(input())
lan = []
for _ in range(t):
	(l, r) = map(int, input().split())
	b = r // 2 + 1
	if b > l:
		lan.append(r % b)
	else:
		lan.append(r % l)
print(*lan, sep='\n')


import math, sys, itertools
wtf = sys.stdin.read()
wtf = wtf.strip().split('\n')
tmp = [x for x in range(ord('a'), ord('z') + 1)]
for t in wtf[1:]:
	(l, r) = map(int, t.split())
	tmp = r // 2 + 1
	print(r % (r // 2 + 1) if tmp >= l and tmp <= r else r % l)


def get_mod(l, r):
	temp = r // 2 + 1
	if temp > l:
		return r % temp
	else:
		return r % l
testCases = int(input())
res = []
for i in range(testCases):
	test = input()
	test = [int(i) for i in test.split(' ')]
	res.append(get_mod(test[0], test[-1]))
for i in res:
	print(i)


n = int(input())
an = []
for i in range(n):
	(a, b) = input('').split()
	a = int(a)
	b = int(b)
	if a == b:
		an.append(0)
		continue
	x = int(b / 2)
	if a > x:
		an.append(b % a)
	else:
		an.append(b % (x + 1))
for i in range(n):
	print(an[i])


t = int(input())
for _ in range(t):
	(l, r) = map(int, input().split())
	if l == r:
		print(0)
	else:
		k = r // 2 + 1
		if k >= l:
			print(r % k)
		else:
			print(r % l)


import math
t = int(input())
for tc in range(t):
	(l, r) = map(int, input().split())
	if l == r:
		print(0)
		continue
	x = math.ceil(r / 2)
	if x < l:
		print(r % l)
		continue
	if r % 2 == 0:
		r -= 1
	print(r % x)


from __future__ import division, print_function
import os, sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip
from bisect import bisect_left as lower_bound, bisect_right as upper_bound

def so():
	return int(input())

def st():
	return input()

def mj():
	return map(int, input().strip().split(' '))

def msj():
	return map(str, input().strip().split(' '))

def le():
	return list(map(int, input().split()))

def lebe():
	return list(map(int, input()))

def dmain():
	sys.setrecursionlimit(1000000)
	threading.stack_size(1024000)
	thread = threading.Thread(target=main)
	thread.start()

def joro(L):
	return ''.join(map(str, L))

def decimalToBinary(n):
	return bin(n).replace('0b', '')

def isprime(n):
	for i in range(2, int(n ** 0.5) + 1):
		if n % i == 0:
			return False
	return True

def npr(n, r):
	return factorial(n) // factorial(n - r) if n >= r else 0

def ncr(n, r):
	return factorial(n) // (factorial(r) * factorial(n - r)) if n >= r else 0

def lower_bound(li, num):
	answer = -1
	start = 0
	end = len(li) - 1
	while start <= end:
		middle = (end + start) // 2
		if li[middle] >= num:
			answer = middle
			end = middle - 1
		else:
			start = middle + 1
	return answer

def upper_bound(li, num):
	answer = -1
	start = 0
	end = len(li) - 1
	while start <= end:
		middle = (end + start) // 2
		if li[middle] <= num:
			answer = middle
			start = middle + 1
		else:
			end = middle - 1
	return answer

def abs(x):
	return x if x >= 0 else -x

def binary_search(li, val, lb, ub):
	ans = -1
	while lb <= ub:
		mid = (lb + ub) // 2
		if li[mid] > val:
			ub = mid - 1
		elif val > li[mid]:
			lb = mid + 1
		else:
			ans = mid
			break
	return ans

def kadane(x):
	sum_so_far = 0
	current_sum = 0
	for i in x:
		current_sum += i
		if current_sum < 0:
			current_sum = 0
		else:
			sum_so_far = max(sum_so_far, current_sum)
	return sum_so_far

def pref(li):
	pref_sum = [0]
	for i in li:
		pref_sum.append(pref_sum[-1] + i)
	return pref_sum

def SieveOfEratosthenes(n):
	prime = [True for i in range(n + 1)]
	p = 2
	li = []
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	for p in range(2, len(prime)):
		if prime[p]:
			li.append(p)
	return li

def primefactors(n):
	factors = []
	while n % 2 == 0:
		factors.append(2)
		n //= 2
	for i in range(3, int(sqrt(n)) + 1, 2):
		while n % i == 0:
			factors.append(i)
			n //= i
	if n > 2:
		factors.append(n)
	return factors

def read():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

def tr(n):
	return n * (n + 1) // 2

def fb(k, L):
	if k == L[k]:
		return k
	if L[k] == fb(L[k], L):
		return L[k]

def usa(a, b, Y, Z):
	a = fb(a, Y)
	b = fb(b, Y)
	if a != b:
		if Z[a] < Z[b]:
			(a, b) = (b, a)
		Y[b] = a
		Z[a] += Z[b]

def iu():
	import sys
	input = sys.stdin.buffer.readline
	import math as my
	(p, q) = mj()
	a = q
	b = q
	a = q // 2 + 1
	if a < p:
		a = p
	if a > q:
		a = r
	print(b % a)

def main():
	for i in range(so()):
		iu()
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


t = int(input())
op = []
for i in range(t):
	(l, r) = map(int, input().split(' '))
	if l == r:
		op.append(0)
	else:
		if r % 2 == 0:
			mid = int(r / 2) + 1
		else:
			mid = int(r / 2) + 1
		if mid > l:
			ans = r % mid
		else:
			ans = r % l
		op.append(ans)
for i in op:
	print(i)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from itertools import permutations
from collections import *
from collections import deque
from operator import itemgetter, attrgetter
from decimal import *
import bisect
import math
import heapq as hq
MOD = 10 ** 9 + 7

def is_prime(n):
	if n == 2 or n == 3:
		return True
	if n < 2 or n % 2 == 0:
		return False
	if n < 9:
		return True
	if n % 3 == 0:
		return False
	r = int(n ** 0.5)
	f = 5
	while f <= r:
		if n % f == 0:
			return False
		if n % (f + 2) == 0:
			return False
		f += 6
	return True

def pow(a, b, m):
	ans = 1
	while b:
		if b & 1:
			ans = ans * a % m
		b //= 2
		a = a * a % m
	return ans

def ispalindrome(s):
	if s[:] == s[::-1]:
		return 1
	return 0
dp = []
limit = []
v = []

def dpdfs(u, t=-1):
	dp[0][u] = 0
	dp[1][u] = 0
	for i in v[u]:
		if i == t:
			continue
		if dp[1][i] == -1:
			dpdfs(i, u)
		dp[0][u] += max(abs(limit[0][u] - limit[1][i]) + dp[1][i], abs(limit[0][u] - limit[0][i]) + dp[0][i])
		dp[1][u] += max(abs(limit[1][u] - limit[1][i]) + dp[1][i], abs(limit[1][u] - limit[0][i]) + dp[0][i])
vis = []
f = 0

def dfs(i):
	vis[i] = 1
	act[i] = 1
	for j in v[i]:
		if act[j]:
			f = 1
			return -1
		if vis[j] == 0:
			if dfs(j) == -1:
				return -1
	act[i] = 0
	ans.append(i)
	return 0
from queue import PriorityQueue

def z_algorithm(s):
	res = [0] * len(s)
	res[0] = len(s)
	(i, j) = (1, 0)
	while i < len(s):
		while i + j < len(s) and s[j] == s[i + j]:
			j += 1
		res[i] = j
		if j == 0:
			i += 1
			continue
		k = 1
		while i + k < len(s) and k + res[k] < j:
			res[i + k] = res[k]
			k += 1
		(i, j) = (i + k, j - k)
	return res

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def lcm(a, b):
	return a / gcd(a, b) * b

def gcdExtended(a, b):
	if a == 0:
		return (b, 0, 1)
	(gcd, x1, y1) = gcdExtended(b % a, a)
	x = y1 - b // a * x1
	y = x1
	return (gcd, x, y)
import re
for _ in range(int(input())):
	(l, r) = map(int, input().split())
	t = 1 + r // 2
	if t >= l:
		print(r % t)
	else:
		print(r % l)


t = int(input())
for _ in range(t):
	(l, r) = [int(x) for x in input().split()]
	j = max(r // 2, l)
	while r // j != 1:
		j += 1
	print(r % j)


k = []
alphabets = 'abcdefghijklmnopqrstuvwxyz'
for _ in range(int(input())):
	(l, r) = map(int, input().split())
	if r // 2 < l:
		k.append(r % l)
	else:
		k.append(0 - r // -2 - 1)
for i in k:
	print(i)