#include <bits/stdc++.h>
using namespace std;
int T;
int l, r;
int maxn = 0;
int main() {
  scanf("%d", &T);
  for (int n = 1; n <= T; n++) {
    l = 0, r = 0;
    maxn = 0;
    scanf("%d %d", &l, &r);
    if (l == r)
      cout << "0" << endl;
    else {
      cout << r % max(r / 2 + 1, l) << endl;
    }
  }
  return 0;
}
