#include<bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<int,int> ii;
void fastio(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
int checkindex(int i,int j,int array[]){
if(i==-1){
return j;
}
if(j==-1){
return i;
}
if(array[i]>=array[j]){
return i;
}
else{
return j;
}
}
int query1(int segtree[],int low,int high,int qlow,int qhigh,int pos,int array[]){
if(qhigh<low || qlow>high)
return -1;
else if(low>=qlow && high<=qhigh)
return segtree[pos];
int mid=(low+high)/2;
return checkindex(query1(segtree,low,mid,qlow,qhigh,2*pos+1,array),query1(segtree,mid+1,high,qlow,qhigh,2*pos+2,array),array);
}
void constructTree(int segtree[],int array[],int low,int high,int pos){
if(low==high){
segtree[pos]=low;
return;
}
int mid=(low+high)/2;
constructTree(segtree,array,low,mid,2*pos+1);
constructTree(segtree,array,mid+1,high,2*pos+2);
segtree[pos]=checkindex(segtree[2*pos+1],segtree[2*pos+2],array);
return;
}
int main(){
fastio();
int t;
cin>>t;
for(int caseno=1;caseno<=t;caseno++){
int n;
cin>>n;
int k;
cin>>k;
int c[n];
int d[n];
for(int i=0;i<n;i++){
cin>>c[i];
}
for(int i=0;i<n;i++){
cin>>d[i];
}
int size=2*pow(2,((int)ceil(log2(n))))-1;
int segtree1[size];
int segtree2[size];
constructTree(segtree1,c,0,n-1,0);
constructTree(segtree2,d,0,n-1,0);
long long ans=0;
for(int i=0;i<n;i++){
long long l1=i+1,l2=i+1,r1=i-1,r2=i-1;
int sl=0,el=i;
while(sl<=el){
int mid=(sl+el)/2;
int index2=query1(segtree1,0,n-1,mid,i,0,c);
int index1=query1(segtree2,0,n-1,mid,i,0,d);
if(index2==i && d[index1]<=c[i]+k){
l1=mid;
el=mid-1;
}
else{
sl=mid+1;
}
}
int sr=i,er=n-1;
while(sr<=er){
int mid=(sr+er)/2;
int index2=query1(segtree1,0,n-1,i,mid,0,c);
int index1=query1(segtree2,0,n-1,i,mid,0,d);
if(index2==i && d[index1]<=c[i]+k){
r1=mid;
sr=mid+1;
}
else{
er=mid-1;
}
}
sl=0,el=i;
while(sl<=el){
int mid=(sl+el)/2;
int index2=query1(segtree1,0,n-1,mid,i,0,c);
int index1=query1(segtree2,0,n-1,mid,i,0,d);
if(index2==i && d[index1]<c[i]-k){
l2=mid;
el=mid-1;
}
else{
sl=mid+1;
}
}
sr=i,er=n-1;
while(sr<=er){
int mid=(sr+er)/2;
int index2=query1(segtree1,0,n-1,i,mid,0,c);
int index1=query1(segtree2,0,n-1,i,mid,0,d);
if(index2==i && d[index1]<c[i]-k){
r2=mid;
sr=mid+1;
}
else{
er=mid-1;
}
}
ans=ans+(i-l1+1)*(r1-i+1)-(i-l2+1)*(r2-i+1);
}
cout<<"Case #"<<caseno<<": "<<ans<<endl;
}
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKI2RlZmluZSBtb2QgMTAwMDAwMDAwNwojZGVmaW5lIHBiIHB1c2hfYmFjawojZGVmaW5lIG1wIG1ha2VfcGFpcgp0eXBlZGVmIGxvbmcgbG9uZyBsbDsKdHlwZWRlZiBwYWlyPGludCxpbnQ+IGlpOwp2b2lkIGZhc3Rpbygpewppb3NfYmFzZTo6c3luY193aXRoX3N0ZGlvKGZhbHNlKTsKY2luLnRpZShOVUxMKTsKfQppbnQgY2hlY2tpbmRleChpbnQgaSxpbnQgaixpbnQgYXJyYXlbXSl7CglpZihpPT0tMSl7CgkJcmV0dXJuIGo7Cgl9CglpZihqPT0tMSl7CgkJcmV0dXJuIGk7Cgl9CglpZihhcnJheVtpXT49YXJyYXlbal0pewoJCXJldHVybiBpOwoJfQoJZWxzZXsKCQlyZXR1cm4gajsKCX0KfQppbnQgcXVlcnkxKGludCBzZWd0cmVlW10saW50IGxvdyxpbnQgaGlnaCxpbnQgcWxvdyxpbnQgcWhpZ2gsaW50IHBvcyxpbnQgYXJyYXlbXSl7CglpZihxaGlnaDxsb3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