#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 2e5 + 999;
int stk[N], l[N], r[N], top;
int a[N];
void solve()
{
int n;cin>>n;
for(int i = 1; i <= n; i ++)cin>>a[i];
stk[top = 0] = 0;
for(int i = 1; i <= n; i ++)
{
while(top && a[stk[top]] < a[i])top --;
l[i] = stk[top] + 1;
stk[++ top] = i;
}
stk[top = 0] = n + 1;
a[n + 1] = 0;
for(int i = n; i >= 1; i --)
{
while(top && a[stk[top]] < a[i])top --;
r[i] = stk[top] - 1;
if(a[i] == a[r[i] + 1])r[i] = i;
stk[++ top] = i;
}
int res = 0;
for(int i = 1; i <= n; i ++)res += r[i] - l[i];
cout<<res<<endl;
}
int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t;cin>>t;
while(t --)
solve();
return 0;
}