#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define F first
#define S second 
#define N1 100005
#define N2 200005
#define endl "\n"
#define mod 1000000007
#define all(v) v.begin(),v.end()
#define vll vector<ll>
#define pb push_back
#define for0(i,n) for(ll i=0;i<n;i++)
#define for1(i,n) for(ll i=1;i<=n;i++)
#define cy cout<<"YES\n"
#define cn cout<<"NO\n"
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)

void __print(int x) {cout << x;}
void __print(long x) {cout << x;}
void __print(long long x) {cout << x;}
void __print(unsigned x) {cout << x;}
void __print(unsigned long x) {cout << x;}
void __print(unsigned long long x) {cout << x;}
void __print(float x) {cout << x;}
void __print(double x) {cout << x;}
void __print(long double x) {cout << x;}
void __print(char x) {cout << '\'' << x << '\'';}
void __print(const char *x) {cout << '\"' << x << '\"';}
void __print(const string &x) {cout << '\"' << x << '\"';}
void __print(bool x) {cout << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cout << '{'; __print(x.first); cout << ','; __print(x.second); cout << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cout << '{'; for (auto &i: x) cout << (f++ ? "," : ""), __print(i); cout << "}";}
void _print() {cout << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cout << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cout << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif


int check(vector<ll> &ar){
    ll n=ar.size();
    ll ma=1;
    vector<ll> dp(n);
    dp[0]=1;
    for(ll i=1;i<n;i++){
        dp[i]=1;
        for(ll j=0;j<i;j++){
            if(ar[i]>=ar[j]) dp[i]=max(dp[i],1+dp[j]);
        }
        ma=max(ma,dp[i]);
    }
    dp.clear();
    dp.assign(n,1);
    for(ll i=1;i<n;i++){
        dp[i]=1;
        for(ll j=0;j<i;j++){
            if(ar[i]<=ar[j]) dp[i]=max(dp[i],1+dp[j]);
        }
        ma=max(ma,dp[i]);
    }
    return ma>=3;
}




int main()
{
    fastio;
    #ifndef ONLINE_JUDGE
    freopen("./input.txt", "r", stdin);
    freopen("./output.txt", "w", stdout);
    #endif

    ll t=1;
    cin>>t;
    while(t--)
    {
        ll n;
        cin>>n;
        vector<ll> ar(n);
        for0(i,n){
            cin>>ar[i];
        }
        int cnt=n+n-1;

        for(int i=0;i<n;i++){
            for(int j=i+2;j<n&&j<i+7;j++){
                vector<ll> temp(ar.begin()+i,ar.begin()+j+1);
                if(check(temp)==0) cnt++;
            }
        }

        cout<<cnt<<"\n";

    }
    return 0;
}
