import java.io.*;
import java.util.*;

public class B {

	static long solve(int[] a) {
		long ans = 0;
		int n = a.length;
		for (int l = 0; l < n; l++)
			for (int r = l; r < n; r++) {
				boolean ok = false;
				for (int k = l; k <= r; k++)
					if (a[k] == 1)
						ok = true;
				if (ok) {
//					System.err.println(l + " " + r);
					ans++;
				}
			}
		return ans;
	}

	static long sum(long x) {
		return x * (x + 1) / 2;
	}

	static long sum(int l, int r) {
		return sum(r) - sum(l - 1);
	}

	static long solve(int n, int m) {
		if (m == 0)
			return 0;
		if(m==1) {
			if(n%2==1) {
				n=(n+1)/2;
				return n*1L*n;
			}
			n>>=1;
			return n*1L*(n+1);
		}
		long ans = sum(1, m);
		if (n > m) {
			int l = m;
			int r = n - 1;
			ans += sum(l, r);
		}
		return ans;

	}

	static int[] toArray(int x, int n) {
		int[] ans = new int[n];
		for (int i = 0; i < n; i++)
			if ((x & 1 << i) == 0)
				ans[i] = 0;
			else
				ans[i] = 1;
		return ans;
	}

	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner();
		PrintWriter out = new PrintWriter(System.out);
		int tc = sc.nextInt();
//		int m = sc.nextInt();
//		for (int n = m; n <= 15; n++) {
//			long test = 0;
//			for (int msk = 0; msk < 1 << n; msk++) {
//				if (Integer.bitCount(msk) != m)
//					continue;
//				int[] arr = toArray(msk, n);
//				test = Math.max(test, solve(arr));
//
//			}
//			System.out.println(test+" "+solve(n,m));
//		}
		while (tc-- > 0) {
			out.println(solve(sc.nextInt(), sc.nextInt()));
		}

		out.close();

	}

	static class Scanner {
		BufferedReader br;
		StringTokenizer st;

		Scanner() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		Scanner(String fileName) throws FileNotFoundException {
			br = new BufferedReader(new FileReader(fileName));
		}

		String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		String nextLine() throws IOException {
			return br.readLine();
		}

		int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		long nextLong() throws NumberFormatException, IOException {
			return Long.parseLong(next());
		}

		double nextDouble() throws NumberFormatException, IOException {
			return Double.parseDouble(next());
		}

		boolean ready() throws IOException {
			return br.ready();
		}

	}

	static void sort(int[] a) {
		shuffle(a);
		Arrays.sort(a);
	}

	static void shuffle(int[] a) {
		int n = a.length;
		Random rand = new Random();
		for (int i = 0; i < n; i++) {
			int tmpIdx = rand.nextInt(n);
			int tmp = a[i];
			a[i] = a[tmpIdx];
			a[tmpIdx] = tmp;
		}
	}

}