#include <bits/stdc++.h>
using namespace std;
long long t, l, r;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> t;
  while (t--) {
    cin >> l >> r;
    if (l <= (r / 2) + 1)
      cout << r % (r / 2 + 1) << '\n';
    else
      cout << r % l << '\n';
  }
}
