//by Top1
#include <bits/stdc++.h>
      
#define f first
#define s second
#define int long long
#define ld long double
#define ull unsigned long long
#define pii pair <int, int>
#define pll pair <long, long>
#define mp make_pair
#define pb push_back
#define pf push_front
#define pp pop_back
#define rev reverse
#define all(s) s.begin(), s.end()
#define sz(s) (int)s.size()
#define forn(i, a, b) for (int i = a; i <= b; i++)
#define boost ios_base::sync_with_stdio(false); cin.tie(0),cout.tie(0);
#define nxtp next_permutation
#define pvtp prev_permutation
#define KZ return 
#define x1 x21212121
#define y1 y21212121
#define sqr(x) x * x 
  
const int inf = (1e9) + 1;
const int mod = 1e9 + 7;
const int maxn = 4e5 + 111;
      
using namespace std;
 
int n, a, b, c;

void solve(){                 
	cin >> a >> b;

	if (a / 2 >= b){
		int x = a - 1;
		int ans;
		if (b == 1){
			cout << x * 2 << endl;
			return;
		}
		if (x % 2 == 0)
			ans = (x + (x - (b - 1) * 2)) * b;
		else
			ans = (x + (x - (b - 1) * 2)) * b;
		cout << ans << endl;
	}
	else{
		int x = a - 1;
		int h = a / 2;
		int ans;
		if (x % 2 == 0)
			ans = (x + 2) * h;
		else
			ans = (x + 1) * h;
		h = b - h;
		ans += h;
		cout << ans << endl;
	}
}         
  
main(){
    boost;
  
    int q = 1;
  
    cin >> q;

    while (q--){
        solve();
    }
}