#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, m, ans = 0;
    cin >> n >> m;
    if (n == m)
      cout << (n * (n + 1) / 2) << endl;
    else if (2 * m + 1 <= n) {
      int a = (n - m) / (m + 1);
      int b = (n - m) % (m + 1);
      if (a && b) {
        ans = (m + 1 - b) * a * (a + 1) / 2 + (b) * (a + 1) * (a + 2) / 2;
      } else if (a > 0 && b == 0) {
        ans = (m + 1) * (a + 1) * a / 2;
      }
      cout << (n * (n + 1) / 2) - ans << endl;
    } else if (2 * m + 1 > n && m == 1) {
      if (n - m > 1) {
        if ((n - m) % 2 == 0) {
          int a = (n - m) / 2;
          cout << (n * (n + 1) / 2) - a * (a + 1) << endl;
        } else {
          int a = (n - m) / 2;
          cout << (n * (n + 1) / 2) - (a * (a + 1) / 2 + (a + 1) * (a + 2) / 2)
               << endl;
        }
      } else
        cout << (n * (n + 1) / 2) - 1 << endl;
    } else if (2 * m + 1 > n && m > 1) {
      int a = (n - m) / (m);
      int b = (n - m) % (m);
      if (a && b) {
        ans = (m - b) * a * (a + 1) / 2 + (b) * (a + 1) * (a + 2) / 2;
      } else if (a > 0 && b == 0) {
        ans = (m) * (a + 1) * a / 2;
      } else if (b > 0 && a == 0) {
        if (m >= n - m)
          ans = (n - m);
        else
          ans = b * (b + 1) / 2;
      }
      cout << (n * (n + 1) / 2) - ans << endl;
    }
  }
}
