#include <iostream>
#include<bits/stdc++.h>
using namespace std;

int main()
{
    int i,j,k,l,o,p;
    cin>>k;
    while(k--)
    {
        cin>>p;
        l=0;
        o=0;
        vector<int>aa(p,0),bb(p);
        for(i=0;i<p;i++)cin>>bb[i];
        for(i=1;i<p-1;i++){
            if(bb[i]>bb[i-1]&&bb[i]>bb[i+1])aa[i]=1;
            if(bb[i]<bb[i-1]&&bb[i]<bb[i+1])aa[i]=-1;
            l+=abs(aa[i]);
        }
        for(i=1;i<p-1;i++){
            if(aa[i]==1){
                    if(aa[i-1]==-1&&aa[i+1]==-1){
                        o=max(o,3);
                    }
                    else if(aa[i-1]==-1){
                        if(bb[i-1]>=bb[i+1]){
                            o=max(o,2);
                        }
                        else {
                            if((i+1==p-1)||(bb[i+2]==bb[i+1]))
                                o=max(o,2);
                            else 
                                o=max(o,1);
                        }
                    }
                    else if(aa[i+1]==-1){
                        if(bb[i+1]>=bb[i-1]){
                            o=max(o,2);
                        }
                        else {
                            if((i-1==0)||(bb[i-2]==bb[i-1]))
                                o=max(o,2);
                            else 
                                o=max(o,1);
                        }
                    }
                    else o=max(o,1);
            }
            else if(aa[i]==-1){
                    if(aa[i-1]==1&&aa[i+1]==1){
                        o=max(o,3);
                    }
                    else if(aa[i-1]==1){
                        if(bb[i-1]<=bb[i+1]){
                            o=max(o,2);
                        }
                        else {
                            if(i+1==p-1||(bb[i+2]==bb[i+1]))
                                o=max(o,2);
                            else 
                                o=max(o,1);
                        }
                    }
                    else if(aa[i+1]==1){
                        if(bb[i+1]<=bb[i-1]){
                            o=max(o,2);
                        }
                        else {
                            if(i-1==0||(bb[i-1]==bb[i-2]))
                                o=max(o,2);
                            else 
                                o=max(o,1);
                        }
                    }
                    else o=max(o,1);
            }
        }
        cout<<l-o<<"\n";
    }return 0;
}
