import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.StringTokenizer;

import javax.sound.sampled.SourceDataLine;

public class div {
	static ArrayList<Integer> primes;
	static int[] isComposite;

	static void sieve(int N) // O(N log log N)
	{
		isComposite = new int[N + 1];
		isComposite[0] = isComposite[1] = 1; // 0 indicates a prime number
		primes = new ArrayList<Integer>();

		for (int i = 2; i <= N; ++i) // can loop till i*i <= N if primes array is not needed O(N log log sqrt(N))
			if (isComposite[i] == 0) // can loop in 2 and odd integers for slightly better performance
			{
				primes.add(i);
				if (1l * i * i <= N)
					for (int j = i * i; j <= N; j += i) // j = i * 2 will not affect performance too much, may alter in
														// modified sieve
						isComposite[j] = 1;
			}
	}

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		PrintWriter pw = new PrintWriter(System.out);
		int t = Integer.parseInt(br.readLine());
		while (t-- > 0) {
			StringTokenizer st = new StringTokenizer(br.readLine());
			int n = Integer.parseInt(st.nextToken());
			int m = Integer.parseInt(st.nextToken());
			int x = n / 2;
			long sol = 0;
			if (m > x)
				sol = m - x;
//			if (n % 2 == 1)
//				n--;
			int q = n / 2;
			if (m != 0) {
				if(n%2==1)
				sol += q * (q + 1) * 2;
				else
					sol+=q *(q+1);
			}
		//	System.out.println(q);
			if (m < x && m != 0) {
				sol -= (m - x) * (m - x + 1) * 2;
			}
			pw.println(sol);
		}

		pw.flush();
	}

	static class tri {
		long d;
		int v;
		int t;

		public tri(long q, int w, int r) {
			d = q;
			v = w;
			t = r;
		}

		static float power(float x, int y) {
			float temp;
			if (y == 0)
				return 1;
			temp = power(x, y / 2);

			if (y % 2 == 0)
				return temp * temp;
			else {
				if (y > 0)
					return x * temp * temp;
				else
					return (temp * temp) / x;
			}
		}
	}
}