///////////////////////// _LeMur_
#define _CRT_SECURE_NO_WARNINGS
#include <unordered_map>
#include <unordered_set>
#include <functional>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cassert>
#include <chrono>
#include <random>
#include <bitset>
#include <cstdio>
#include <vector>
#include <string>
#include <ctime>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <list>
#include <map>
#include <set>
using namespace std;
const int N = 100005;
const int inf = 1000 * 1000 * 1000;
const int mod = 1000 * 1000 * 1000 + 7;
int t;
int n , k;
long long answ;
int a[N] , b[N];
int mx1[N] , mx2[N];
void solve(int s,int e){
if(s == e){
if(abs(a[s] - b[s]) <= k){
++answ;
}
return ;
}
///
int m = (s + e) / 2;
solve(s , m);
solve(m + 1 , e);
///
for(int step = 0; step < 2; step++){
for(int j=m+1;j<=e;j++){
mx1[j] = (j == m + 1) ? a[j] : max(mx1[j - 1] , a[j]);
mx2[j] = (j == m + 1) ? b[j] : max(mx2[j - 1] , b[j]);
}
int m1 , m2;
for(int i=m;i>=s;i--){
m1 = (i == m) ? a[i] : max(m1 , a[i]);
m2 = (i == m) ? b[i] : max(m2 , b[i]);
int id1 , id2;
if(step == 1)
id1 = lower_bound(mx1 + m + 1 , mx1 + e + 1 , m1) - mx1;
else
id1 = upper_bound(mx1 + m + 1 , mx1 + e + 1 , m1) - mx1;
id2 = upper_bound(mx2 + m + 1 , mx2 + e + 1 , m2) - mx2;
///
if(abs(m1 - m2) <= k)
answ += max(0 , min(id1 , id2) - (m + 1));
///
int id3 = lower_bound(mx2 + id2 , mx2 + e + 1 , m1 - k) - mx2;
int id4 = upper_bound(mx2 + id2 , mx2 + e + 1 , m1 + k) - mx2;
answ += max( min(id1 , id4) - id3 , 0);
}
///
reverse(a + s , a + e + 1);
reverse(b + s , b + e + 1);
m = e - m + s - 1;
}
}
int main() {
cin >> t;
int test = 0;
while(t--){
++test;
answ = 0;
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
}
for(int i=1;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gICAgICAgfQogICAgICAgIHJldHVybiA7CiAgICB9CiAgICAvLy8KICAgIGludCBtID0gKHMgKyBlKSAvIDI7CiAgICBzb2x2ZShzICwgbSk7CiAgICBzb2x2ZShtICsgMSAsIGUpOwogICAgLy8vCiAgICBmb3IoaW50IHN0ZXAgPSAwOyBzdGVwIDwgMjsgc3RlcCsrKXsKICAgICAgICBmb3IoaW50IGo9bSsxO2o8PWU7aisrKXsKICAgICAgICAgICAgbXgxW2pdID0gKGogPT0gbSArIDEpID8gYVtqXSA6IG1heChteDFbaiAtIDFdICwgYVtqXSk7CiAgICAgICAgICAgIG14MltqXSA9IChqID09IG0gKyAxKSA/IGJbal0gOiBtYXgobXgyW2ogLSAxXSAsIGJbal0pOwogICAgICAgIH0KICAgICAgICBpbnQgbTEgLCBtMjsKICAgICAgICBmb3IoaW50IGk9bTtpPj1zO2ktLSl7CiAgICAgICAgICAgIG0xID0gKGkgPT0gbSkgPyBhW2ldIDogbWF4KG0xICwgYVtpXSk7CiAgICAgICAgICAgIG0yID0gKGkgPT0gbSkgPyBiW2ldIDogbWF4KG0yICwgYltpXSk7CiAgICAgICAgICAgIGludCBpZDEgLCBpZDI7CiAgICAgICAgICAgIGlmKHN0ZXAgPT0gMSkKICAgICAgICAgICAgICAgIGlkMSA9IGxvd2VyX2JvdW5kKG14MSArIG0gKyAxICwgbXgxICsgZSArIDEgLCBtMSkgLSBteDE7CiAgICAgICAgICAgIGVsc2UKICAgICAgICAgICAgICAgIGlkMSA9IHVwcGVyX2JvdW5kKG14MSArIG0gKyAxICwgbXgxICsgZSArIDEgLCBtMSkgLSBteDE7CiAgICAgICAgICAgIGlkMiA9IHVwcGVyX2JvdW5kKG14MiArIG0gKyAxICwgbXgyICsgZSArIDEgLCBtMikgLSBteDI7CiAgICAgICAgICAgIC8vLwogICAgICAgICAgICBpZihhYnMobTEgLSBtMikgPD0gaykKICAgICAgICAgICAgICAgIGFuc3cgKz0gbWF4KDAgLCBtaW4oaWQxICwgaWQyKSAtIChtICsgMSkpOwogICAgICAgICAgICAvLy8KICAgICAgICAgICAgaW50IGlkMyA9IGxvd2VyX2JvdW5kKG14MiArIGlkMiAsIG14MiArIGUgKyAxICwgbTEgLSBrKSAtIG14MjsKICAgICAgICAgICAgaW50IGlkNCA9IHVwcGVyX2JvdW5kKG14MiArIGlkMiAsIG14MiArIGUgKyAxICwgbTEgKyBrKSAtIG14MjsKICAgICAgICAgICAgYW5zdyArPSBtYXgoIG1pbihpZDEgLCBpZDQpIC0gaWQzICwgMCk7CiAgICAgICAgfQogICAgICAgIC8vLwogICAgICAgIHJldmVyc2UoYSArIHMgLCBhICsgZSArIDEpOwogICAgICAgIHJldmVyc2UoYiArIHMgLCBiICsgZSArIDEpOwogICAgICAgIG0gPSBlIC0gbSArIHMgLSAxOwogICAgfQp9CgppbnQgbWFpbigpIHsKICAgIGNpbiA+PiB0OwogICAgaW50IHRlc3QgPSAwOwogICAgd2hpbGUodC0tKXsKICAgICAgICArK3Rlc3Q7CiAgICAgICAgYW5zdyA9IDA7CiAgICAgICAgc2NhbmYoIiVkJWQiLCZuLCZrKTsKICAgICAgICBmb3IoaW50IGk9MTtpPD1uO2krKyl7CiAgICAgICAgICAgIHNjYW5mKCIlZCIsJmFbaV0pOwogICAgICAgIH0KICAgICAgICBmb3IoaW50IGk9MTtpPD1uO2krKyl7CiAgICAgICAgICAgIHNjYW5mKCIlZCIsJmJbaV0pOwogICAgICAgIH0KICAgICAgICBzb2x2ZSgxICwgbik7CiAgICAgICAgcHJpbnRmKCJDYXNlICMlZDogJWxsZFxuIix0ZXN0LGFuc3cpOwogICAgfQoJcmV0dXJuIDA7Cn0K