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