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