/*
*Copyright (c) Swadheen Islam Robi (SIR01)
*Created on Thu Sep 05 2024 10:19:08 PM
*/
#include <iostream>
#include <vector>
using namespace std;
int solve(int N, int K) {
vector<int> A(N, 0);
int mon = min(K, N);
for (int i = 0; i < mon; i++) {
A[i] = 1;
}
int X = K - mon;
for (int i = mon; i < N && X > 0; i++) {
A[i] = 1;
X--;
}
int Y = 1;
for (int i = 0; i < N; i++) {
Y &= A[i];
}
return X * Y;
}
int main() {
int T;
cin >> T;
while (T--) {
int N, K;
cin >> N >> K;
int ans = solve(N, K);
cout << ans << endl;
}
return 0;
}