#include <bits/stdc++.h>
#define ll long long
#define endll '\n';
#define pb push_back
#define all(v) v.begin(), v.end()
using namespace std;
const int mod = 1e9 + 7, N = 1e6;
void solve()
{
int n, k;
cin >> n >> k;
int ans = 0;
if (n <= k)
{
ans = k - n;
}
int vis[n + 3] = {0};
for (int i = 2; i <= n; i++)
{
if (vis[i] == 0)
{
for (int j = i; j <= n; j += i)
{
ll x = j * n;
if (x <= k)
{
ans = max(ans, (int)(j * (k - x)));
}
else
{
break;
}
vis[j] = 1;
}
}
}
cout << ans << endll;
}
int32_t main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int t = 1;
cin >> t;
while (t--)
{
solve();
}
return 0;
}