#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define v vector
#define pb push_back
#define pub pull_back
#define be begin
#define p pair<ll,ll>
#define f first
#define se second
#define um unordered_map
#define om map
#define us unordered_set
#define tr pair<ll,pair<long long int,long long int>> 
#define Q  queue
#define cp(v1,x,i,n) for(i=0;i<n;i++){v1[i]=x;}
#define lp(i,a,b) for(i=a;i<b;i++)
#define MAXN 1000000
#define pq priority_queue
void update(ll i,ll j,ll d,ll n,v<v<ll>> &v3)
{
  while(i>0)
  {
     
     ll k=j;
     while(k>0)
     {
       v3[i][k]+=d;
       k-=k&(-k);
     }
     i-=i&(-i);
  }
}
ll getsum(ll i,ll j,ll n,v<v<ll>> &v3)
{
  ll sum=0;
  while(i<=n)
  {
     ll k=j;
     while(k<=n)
     {
       sum+=v3[i][k];
       k+=k&(-k);
     }
     i+=i&(-i);
  }
  return sum;
}
bool cmp(tr a,tr b)
{
  if(a.f==b.f)
  return a.se.se<b.se.se;

   return a.f>b.f;
}
int main()
{
//  ios_base::sync_with_stdio(false);
//     cin.tie(NULL);
 
  ll t=1,pp=0;
cin>>t;
while(t--)
{
ll n,j=0,b=0,sum1=0,sum2=0,mina=INT_MAX,maxa=0,e=0,h=0,g=0,x2=-1,y1,y2=-1,a,l=0,r,l1=0,r1=0,x1=0;
ll sum=0,i=0,y=0,c=0;
ll d=0;
ll k=0,k1=0,k2=0,k3=0,k4=0;   
ll m;
// set<ll> s;       
cin>>n;
string s1;
v<ll> v1(n);
lp(i,0,n)
cin>>v1[i];
sum=2*n-1;
for(i=2;i<n;i++)
{
  if(v1[i-1]<v1[i]&&v1[i-1]<v1[i-2])
  {
    sum++;
    }
  else if(v1[i-1]>v1[i]&&v1[i-1]>v1[i-2])
  sum++;
  if(i!=2)
  {
     if(v1[i-1]<v1[i]&&v1[i-1]<v1[i-2])
    {
        if(v1[i-2]>v1[i-3]&&v1[i-1]<v1[i-3]&&v1[i-2]>v1[i])
        {sum++;
       }
    }
    else if(v1[i-1]>v1[i]&&v1[i-1]>v1[i-2])
    {
      
        if(v1[i-2]<v1[i-3]&&v1[i-1]>v1[i-3]&&v1[i-2]<v1[i])
       { sum++;
       }
    }
  }
 }

cout<<sum<<"\n";
}
return 0;
}