#include <bits/stdc++.h>
using namespace std;
int main() {
  int t, it;
  cin >> t;
  for (it = 1; it <= t; it++) {
    long long l, r, i, j, d;
    cin >> l >> r;
    if (l == r) {
      cout << "0\n";
    } else {
      d = r / 2;
      d++;
      if (d < l) {
        cout << r % l << endl;
      } else {
        cout << r % d << endl;
      }
    }
  }
  return 0;
}
