#include <stdio.h>
#include <math.h>
#define ll long long
int solve(int n, ll k){
int sum = 0;
for(int i = 1; i <= n; i ++){
sum += i % k;
}
return sum;
}
int main(){
int t;
scanf("%d", &t);
while(t--){
int n;
long long k;
scanf("%d%lld", &n, &k);
printf("%d\n", solve(n, k));
}
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxtYXRoLmg+CiNkZWZpbmUgbGwgbG9uZyBsb25nCgppbnQgc29sdmUoaW50IG4sIGxsIGspewoJaW50IHN1bSA9IDA7Cglmb3IoaW50IGkgPSAxOyBpIDw9IG47IGkgKyspewoJCXN1bSArPSBpICUgazsKCX0KCXJldHVybiBzdW07Cn0KCmludCBtYWluKCl7CglpbnQgdDsKCXNjYW5mKCIlZCIsICZ0KTsKCXdoaWxlKHQtLSl7CgkJaW50IG47CgkJbG9uZyBsb25nIGs7CgkJc2NhbmYoIiVkJWxsZCIsICZuLCAmayk7CgkJcHJpbnRmKCIlZFxuIiwgc29sdmUobiwgaykpOwoJfQp9