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