#include <bits/stdc++.h>
using namespace std;
// Macros
#define REP(i, j, n) for(int i = j; i < n; i++)
#define trav(i, a) for(auto i: a)
#define all(x) x.begin(), x.end()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define F first
#define S second
#define GCD __gcd
// Type Names
typedef unsigned long long ull;
typedef long long ll;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef pair<int, int> pii;
// Global Variables
const int MOD = 1e9 + 7;
// custom hash
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
// http://xorshift.di.unimi.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
// print vector X)
template <typename T>
void print_vector(vector<T> a){
int n = a.size();
for (int i = 0; i < n; i++){
cout << a[i] << (i == n - 1 ? "\n" : " ");
}
}
template <typename T>
void print_2d_vector(vector<vector<T>> a){
int row = a.size();
int col = a[0].size();
for (int i = 0; i < row; i++){
for (int j = 0; j < col; j++){
cout << a[i][j] << " ";
}
cout << "\n";
}
}
template <typename T>
void print_stack(stack<T> st){
while (!st.empty()){
cout << st.top() << " ";
st.pop();
}
cout << "\n";
}
template <typename T>
void print_set(set<T> s){
for (auto itr = s.begin(); itr != s.end(); itr++){
cout << *itr << " ";
}
cout << "\n";
}
template <typename T>
void print_set(multiset<T> s){
for (auto itr = s.begin(); itr != s.end(); itr++){
cout << *itr << " ";
}
cout << "\n";
}
string int_to_bin(ll n){
string ans = "";
while (n) {
ans = to_string(n % 2) + ans;
n /= 2;
}
return ans;
}
ull bin_to_int(string s){
bitset<20> bin(s);
return bin.to_ulong();
}
ll res (ll n, ll k, ll y){
return y * (k - (n * y));
}
void solve()
{
ll n, k;
cin >> n >> k;
ll y1 = ceil((k / (2.0 * n)));
ll y2 = floor((k / (2.0 * n)));
cout << max(0ll, max(res(n, k, y1), res(n, k, y2))) << "\n";
}
int main()
{
cin.tie(NULL);
ios_base::sync_with_stdio(false);
// freopen("breedflip.in", "r", stdin);
// freopen("breedflip.out", "w", stdout);
int t;
cin >> t;
for(int i = 1; i <= t; i++)
solve();
return 0;
}