#include <bits/stdc++.h>
using namespace std;
struct _ {
  ios_base::Init i;
  _() {
    cin.sync_with_stdio(0);
    cin.tie(0);
    cout << fixed << setprecision(10);
  }
} ___;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << "  ";
  __f(comma + 1, args...);
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
long long int my_rand(long long int l, long long int r) {
  return uniform_int_distribution<long long int>(l, r)(rng);
}
signed main() {
  long long int __, _ = 0;
  cin >> __;
  for (; _++ < __;) {
    long long int l, r;
    cin >> l >> r;
    long long int lo = l, hi = r;
    while (lo < hi) {
      long long int mid = (lo + hi + 1) >> 1;
      if (mid * 2 - 1 >= l and mid * 2 - 1 <= r)
        lo = mid;
      else
        hi = mid - 1;
    }
    if (lo * 2 - 1 >= l and lo * 2 - 1 <= r) {
      cout << lo - 1 << '\n';
    } else
      cout << r % l << '\n';
  }
}
