#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t, a, b, n, m, k, i, j, cnt, pp, qq;
  string s;
  cin >> t;
  while (t--) {
    cin >> n >> m;
    a = m / 2 + 1;
    if (a < n) {
      a = n;
    }
    if (a > m) {
      a = m;
    }
    cout << m % a << endl;
  }
}
