/**
* Author: AhSaN (JUST-22)
* Created: 06-09-2024 07:44:28
**/
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
void Sol(int Cs) {
long long n, k;
cin >> n >> k;
long long hi = k / n, lo = 0, res = 0;
while (hi >= lo) {
long long mid = (hi + lo) >> 1;
long long L = mid * n;
long long R = k - (mid * n);
if (R >= L) {
res = max(res, mid * R);
lo = mid + 1;
} else {
res = max(res, mid * R);
hi = mid - 1;
}
}
cout << res << "\n";
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int Tc = 1;
cin >> Tc;
for (int Cs = 1; Cs <= Tc; Cs++) {
Sol(Cs);
}
return 0;
}