Help me in solving WELLLEFT problem

My issue

import math

cook your dish here

T = int(input())
while T:
N,K,H = map(int,input().split())
if N < H and N == 1:
print(0)
else:
sum = 0
D = math.ceil(H/K)
X = H - D - 1
sum += (X**2 - X)/2 + X
sum += (N - H + 1)*N
if D != 1 :
for i in range(1,D):
sum += (K-1)*i
print(int(sum))
T = T - 1
Thanks for helping!
I considered initially H/K as the minimum height it has to climb in one second. Included them using the triangle which you can observe when you write down all the possibilities and then I also added the pairs in which A >= H by (N - H + 1)*N term. The remaining cases like 17,16 is included by for i in range(1,D):(K-1)*i.

My code

import math
# cook your dish here
T = int(input())
while T:
    N,K,H = map(int,input().split())
    if N < H and N == 1:
        print(0)
    else:
        sumi  = 0
        D = math.ceil(H/K)
        X = H - D - 1
        sum += (X**2 - X)/2 + X
        sum += (N - H + 1)*N
        if D != 1 :
            for i in range(1,D):
                sum += (K-1)*i
        print(int(sum))
    T = T - 1

Problem Link: Amphibian Escape Practice Coding Problem

@nani_p
plzz refer my c++ code

#include <bits/stdc++.h>
using namespace std;

int main() {
	// your code goes here
	int t;
	cin>>t;
	while(t--)
	{
	    long long int n,k,h;
	    cin>>n>>k>>h;
	    long long int ans=0;
	    for(long long int i=1;i<=n;i++)
	    {
	        long long int ch=ceil((h-i)*1.0/(k-1LL)*1.0);
	        if(i>=h)
	        ans+=n;
	        else
	        ans+=max(0LL,i-ch);
	      //  cout<<i<<" "<<ans<<" "<<ch<<endl;
	    }
	    cout<<ans<<endl;
	}
	return 0;
}