#include <bits/stdc++.h>
using namespace std;
long long t, l, r;
int main() {
  cin >> t;
  while (t--) {
    cin >> l >> r;
    if (r / 2 + 1 >= l)
      cout << r % (r / 2 + 1) << endl;
    else
      cout << r % l << endl;
  }
  return 0;
}
