#include<bits/stdc++.h>
#define int long long
using namespace std;
int fa[201001];
bool vis[201001];
int size[201001];
int sum=0;
struct node{
int x,y;
}a[201001];
int find(int x){
if(fa[x]==x)return x;
else return fa[x]=find(fa[x]);
}
bool cmp(node a,node b){
if(a.x==b.x)return a.y<b.y;
else return a.x<b.x;
}
void merge(int x,int y){
int tx=find(x);
int ty=find(y);
fa[ty]=tx;
size[tx]+=size[ty];
sum+=size[ty];
//cout<<"为答案贡献:"<<size[ty]<<endl;
}
int main(){
int t;
cin>>t;
while(t--){
int n;
cin>>n;
sum=0;
memset(vis,0,sizeof(vis));
memset(size,0,sizeof(size));
for(int i=1;i<=n;i++){
fa[i]=i;
}
for(int i=1;i<=n;i++){
cin>>a[i].x;
a[i].y=i;
}
sort(a+1,a+1+n,cmp);
for(int i=1;i<=n;i++){
vis[a[i].y]=1;
size[a[i].y]=1;
//cout<<"i:"<<i<<" "<<a[i].y<<endl;
if(vis[a[i].y+1]){
//cout<<1<<endl;
merge(a[i].y,a[i].y+1);
}
if(vis[a[i].y-1]){
//cout<<2<<endl;
if(a[i-1].x==a[i].x&&a[i-1].y==a[i].y-1){
//cout<<"01"<<endl;
int tx=find(a[i].y-1);
int ty=find(a[i].y);
fa[ty]=tx;
size[tx]++;
}else{
//cout<<"02"<<endl;
merge(a[i].y,a[i].y-1);
}
}
}
cout<<sum<<endl;
}
return 0;
}