#include<bits/stdc++.h>
// #define int long long
#define pii pair<int,int>
using namespace std;
const int N=5e5+5,M=998244353,INF=1e9;
int n,a[N],p[N],ans;
void sv(){
cin>>n,ans=0;
for(int i=1;i<=n;i++)cin>>a[i],p[a[i]]=i;
for(int i=0,l=INF,r=0,pl,pr,le;i<n;i++){
l=min(l,p[i]),r=max(r,p[i]);
le=i*2+2;
if(r-l+1>le)continue;
pl=max(1,r-le+1),pr=min(l,n-le+1);
ans+=max(0,pr-pl+1);
le--;
if(r-l+1>le)continue;
pl=max(1,r-le+1),pr=min(l,n-le+1);
ans+=max(0,pr-pl+1);
}
printf("%d\n",ans);
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int T=1;cin>>T;while(T--)sv();
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KLy8gI2RlZmluZSBpbnQgbG9uZyBsb25nCiNkZWZpbmUgcGlpIHBhaXI8aW50LGludD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKY29uc3QgaW50IE49NWU1KzUsTT05OTgyNDQzNTMsSU5GPTFlOTsKaW50IG4sYVtOXSxwW05dLGFuczsKdm9pZCBzdigpewogICAgY2luPj5uLGFucz0wOwogICAgZm9yKGludCBpPTE7aTw9bjtpKyspY2luPj5hW2ldLHBbYVtpXV09aTsKICAgIGZvcihpbnQgaT0wLGw9SU5GLHI9MCxwbCxwcixsZTtpPG47aSsrKXsKICAgICAgICBsPW1pbihsLHBbaV0pLHI9bWF4KHIscFtpXSk7CiAgICAgICAgbGU9aSoyKzI7CiAgICAgICAgaWYoci1sKzE+bGUpY29udGludWU7CiAgICAgICAgcGw9bWF4KDEsci1sZSsxKSxwcj1taW4obCxuLWxlKzEpOwogICAgICAgIGFucys9bWF4KDAscHItcGwrMSk7CiAgICAgICAgbGUtLTsKICAgICAgICBpZihyLWwrMT5sZSljb250aW51ZTsKICAgICAgICBwbD1tYXgoMSxyLWxlKzEpLHByPW1pbihsLG4tbGUrMSk7CiAgICAgICAgYW5zKz1tYXgoMCxwci1wbCsxKTsKICAgIH0KICAgIHByaW50ZigiJWRcbiIsYW5zKTsKfQpzaWduZWQgbWFpbigpewogICAgaW9zOjpzeW5jX3dpdGhfc3RkaW8oZmFsc2UpOwogICAgY2luLnRpZSgwKSxjb3V0LnRpZSgwKTsKICAgIGludCBUPTE7Y2luPj5UO3doaWxlKFQtLSlzdigpOwogICAgcmV0dXJuIDA7Cn0=
MQo3MQoxNTczNzMgMTU3MzY2IDE1NzM2NCAxNTczNjEgMTU3MzYwIDE1NzM1OSAxNTczNTggMTU3MzU3IDE1NzM1NiAxNTczNTMgMTU3MzUyIDE1NzM1MSAxNTczNDUgMTU3MzQ0IDE1NzM0MSAxNTczNDAgMTU3MzM1IDE1NzMzNCAxNTczMzMgMTU3MzMyIDE1NzMyOCAxNTczMjMgMTU3MzIyIDE1NzMyMCAxNTczMTUgMTU3MzEyIDE1NzMxMCAxNTczMDkgMTU3MzA4IDE1NzMwNyAxNTczMDMgMTU3MzAyIDE1NzMwMSAxNTczMDAgMTU3Mjk5IDE1NzI5OCAxNTcyOTcgMTU3Mjk1IDE1NzI5NCAxNTcyOTMgMTU3MjkxIDE1NzI5MCAxNTcyODcgMTU3Mjg1IDE1NzI4MSAxNTcyNzcgMTU3Mjc1IDE1NzI3MyAxNTcyNzIgMTU3MjcxIDE1NzI3MCAxNTcyNjggMTU3MjY2IDE1NzI2NSAxNTcyNjQgMTU3MjYyIDE1NzI2MCAxNTcyNTcgMTU3MjU2IDE1NzI1NSAxNTcyNTQgMTU3MjUzIDE1NzI1MCAxNTcyNDkgMTU3MjQ4IDE1NzI0NSAxNTcyNDIgMTU3MjQxIDE1NzIzOSAxNTcyMzMgMTU3MjMx
1
71
157373 157366 157364 157361 157360 157359 157358 157357 157356 157353 157352 157351 157345 157344 157341 157340 157335 157334 157333 157332 157328 157323 157322 157320 157315 157312 157310 157309 157308 157307 157303 157302 157301 157300 157299 157298 157297 157295 157294 157293 157291 157290 157287 157285 157281 157277 157275 157273 157272 157271 157270 157268 157266 157265 157264 157262 157260 157257 157256 157255 157254 157253 157250 157249 157248 157245 157242 157241 157239 157233 157231