#include <bits/stdc++.h>
using namespace std;
#define int long long
int ans=0;
int canAchieveTarget(int n, int k, int target) {
// To make all elements at least `target`, each element needs to be incremented to `target`
// The total operations needed to achieve this
int requiredOps = target * n; // Total operations needed to set all elements to `target`
return (target*n)*(k-(target*n)); // Check if we have enough operations
}
void solve() {
int n, k;
cin >> n >> k;
ans=min(n, k-n);
ans=max(ans, 0ll);
if(ans==0){
cout<<0<<endl;
return;
}
// Binary search for the maximum value of `target` (Y)
int left = 0, right = n, bestTarget = 0;
while (left <= right) {
int mid = (left + right) / 2;
if (canAchieveTarget(n, k, mid)>ans) {
bestTarget = mid;
left = mid + 1; // Try for a larger target
} else {
right = mid - 1; // Try for a smaller target
}
}
// The final result is X * Y, where X = K - (N * bestTarget)
int X = k - (n * bestTarget);
int Y = bestTarget;
cout << X * Y << endl;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}