#include <bits/stdc++.h>
using namespace std;
using ui = unsigned int;
const double pi = acos(-1.0);
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
const int limit = 5e5;
int read() {
  int s = 0;
  char c = getchar(), lc = '+';
  while (c < '0' || '9' < c) lc = c, c = getchar();
  while ('0' <= c && c <= '9') s = s * 10 + c - '0', c = getchar();
  return lc == '-' ? -s : s;
}
void write(long long x) {
  if (x < 0) putchar('-'), x = -x;
  if (x < 10)
    putchar(x + '0');
  else
    write(x / 10), putchar(x % 10 + '0');
}
void print(long long x, char c = '\n') {
  write(x);
  putchar(c);
}
long long mod = 998244353;
const int N = 5e5 + 5;
long long n, m, _;
int i, j, k;
signed main() {
  int T;
  while (cin >> T)
    while (T--) {
      int l, r;
      scanf("%d%d", &l, &r);
      if (r / 2 >= l) {
        int ans = r / 2;
        if (r % 2 == 0) ans--;
        printf("%d\n", (ans));
      } else {
        printf("%d\n", (r % l));
      }
    }
  return 0;
}
