#include <iostream>
#include <algorithm>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin >> T;
while(T--){
// Read N and K for the test case.
long long N, K;
cin >> N >> K;
// If we can't even give each element one increment,
// the AND remains 0, so answer is 0.
if(K < N){
cout << 0 << "\n";
continue;
}
// Let v be the common value we want for each A[i].
// To get every element to at least v, we need N*v operations.
// The remaining operations: X = K - N*v.
// And the AND becomes v.
// So product = f(v) = v * (K - N*v).
// We need to choose 1 <= v <= floor(K/N).
long long max_v = K / N;
// The function f(v) = K*v - N*v*v is concave and
// its continuous maximum is at v = K / (2*N).
long long candidate1 = K / (2 * N);
if(candidate1 < 1) candidate1 = 1; // ensure at least 1.
long long candidate2 = candidate1 + 1;
auto f = [&](long long v) -> long long {
return v * (K - N * v);
};
long long ans = f(candidate1);
if(candidate2 <= max_v){
ans = max(ans, f(candidate2));
}
cout << ans << "\n";
}
return 0;
}