#include <cstring>
#include <vector>
#include <stdio.h>
#include <bitset>
#include <string>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include<sstream>
#include <iomanip>
#include <vector>
#include<utility>
#include <iostream>
#define ll long long
using namespace std;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll t; cin >> t;
while (t--) {
ll n, k; cin >> n >> k;
ll ctr = k / 2;
if (n >= (k-1)) {
if(k>2)
cout << ctr << "\n";
else cout << "0\n";
}
else if((k-1)>n) {
ctr -= (k - n);
ctr > 0 ? cout << ctr << "\n" : cout << "0\n";
}
}
}
Copy