#include <bits/stdc++.h>
#define endl '\n'
#define F first
#define S second
using namespace std;
#define ll long long
#define pb push_back
#define mod 1000000007
#define int long long
#define all(x) x.begin(),x.end()
#define allr(x) x.rbegin(),x.rend()
#define CheckBit(x,k) (x & (1LL << k))
#define SetBit(x,k) (x |= (1LL << k))
#define ClearBit(x,k) (x &= ~(1LL << k))
#define LSB(mask) __builtin_ctzll(mask)
#define MSB(mask) 63-__builtin_clzll(mask)
#define print(x) cout << #x << " : " << x << endl
#define error1(x) cerr << #x << " = " << (x) <<endl
#define coutall(v) for(auto &it: v) cout<<it<<' '; cout<<endl
#define Abid_52 ios::sync_with_stdio(false);cin.tie(0),cin.tie(0)
#define error2(a,b) cerr<<"( "<<#a<<" , "<<#b<<" ) = ( "<<(a)<<" , "<<(b)<<" )\n"
#define UNIQUE(x) sort(all(x)), x.erase(unique(all(x)), x.end()), x.shrink_to_fit()
template <typename T, typename U> T ceil(T x, U y) {return (x > 0 ? (x + y - 1) / y : x / y);}
template <typename T, typename U> T floor(T x, U y) {return (x > 0 ? x / y : (x - y + 1) / y);}
const int N = 2e5 + 10;
void solve()
{
int n, m;
cin >> n >> m;
int l = 0, r = 1e10, ans = 0;
int minDiff = LLONG_MAX;
int bestAns = 0;
while (l <= r) {
int mid = (l + r) / 2;
int lagbe = mid * n;
if (lagbe > m) {
r = mid - 1;
}
else {
int baki = m - lagbe;
int tot = lagbe / n;
int diff = abs(lagbe - baki);
if (diff < minDiff) {
minDiff = diff;
bestAns = baki;
}
if (baki <= tot) {
l = mid + 1;
ans = baki;
}
else {
r = mid - 1;
}
}
}
ll fans = bestAns * ((m - bestAns) / n);
cout << fans << endl;
}
int32_t main()
{
Abid_52;
int t = 1;
cin >> t;
for (int tc = 1; tc <= t; ++tc)
{
// cout << "Case " << tc << ": ";
solve();
}
}