#include <bits/stdc++.h>
using namespace std;
#define int long long
// Function to check if we can achieve a target value for all elements with at most `k` operations
bool canAchieveTarget(int n, int k, int target) {
// Total operations needed to set all elements to at least `target`
int requiredOps = target * n;
return requiredOps <= k;
}
void solve() {
int n, k;
cin >> n >> k;
// Binary search for the maximum possible value of `target` (Y)
int left = 0, right = k / n, bestTarget = 0;
while (left <= right) {
int mid = (left + right) / 2;
if (canAchieveTarget(n, k, mid)) {
bestTarget = mid; // This target is achievable, try for a larger one
left = mid + 1;
} else {
right = mid - 1;
}
}
// The final result is X * Y, where X = K - (N * bestTarget)
int Y = bestTarget;
int X = k - (n * Y);
// Print the result
cout << X * Y << endl;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}