/* For you,
                who has never forgotten 
                the magic.
                It wants you to know 
                it remembers you too.*/
/*J. M. D.  */
#include <bits/stdc++.h>
using namespace std;
#define int             long long 
#define pll             pair<int,int>
#define ppll            pair<int,pll>
#define vvll            vector<vector<int>>
#define l_b             lower_bound
#define u_b             upper_bound
#define vll             vector<int>
#define vpll            vector<pll>
#define vc              vector <char>
#define vs              vector <string>
#define pb              emplace_back
#define p_b             pop_back()
#define prq             priority_queue
#define setbits(x)      __builtin_popcountll(x)
#define sps(x,y)        fixed<<setprecision(y)<<x
#define hell            1000000007
#define rep(i,s,n)      for(int i=s;i<n;i++)
#define ff              first
#define mem(arr,x)      memset(arr,x,sizeof(arr))
#define ss              second
#define pi              3.14159265358979323846
#define all(v)          v.begin(),v.end()
#define allr(x)         x.rbegin(),x.rend()
#define mp              make_pair
#define display(x)      for(auto dsp:x)cout<<dsp<<" "; cout<<"\n";
#define INF             1000000000000000000//15
void fast() 
{
  ios_base::sync_with_stdio(0);
  cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}
const int N = 2e6+500;
//---------------------------------------------
int dr[4] = {0, 0, 1,-1};
int dc[4] = {1,-1, 0, 0};
 
int ddr[8]= {0, 0, 1, -1, 1, -1, 1, -1};
int ddc[8]= {1, -1, 0, 0, 1, -1, -1, 1};
 
int ddr1[8]= {1, 1, -1, -1, 2, 2, -2, -2};
int ddc1[8]= {2, -2, 2, -2, 1, -1, 1, -1};
//--------------------------------------------


int a[N], n, ans, sub;
bool Hill(int x, int y, int z ){
         if(y>x && y>z) return 1;
         if(y<x && y<z) return 1;
         return 0;
}




int32_t main() {
     fast();
     int t,zz;
     cin>>t;
     rep(zz,1,t+1){
     // cout<<"Case #"<<zz<<": ";
     cin>>n;
     ans= 0;
     sub= 0;
     for(int i=1; i<=n; i++) cin>>a[i];
     a[0]= a[1]; a[n+1]= a[n];

     for(int i=1; i<=n; i++) 
                                  ans+= Hill(a[i-1], a[i], a[i+1]);

     for(int i=2; i<n; i++){
                                  int t1= a[i];
                                  int past=0, pre1=0, pre2=0;
                                  for( int j=0; j<3; j++)
                                       past+= Hill(a[i-2+j], a[i-1+j], a[i+j]);
                                  a[i]= a[i-1];
                                  for(int j=2; j<3; j++)
                                       pre1+= Hill(a[i-2+j], a[i-1+j], a[i+j]);
                                  a[i]= a[i+1];
                                  for(int j=0; j<1; j++)
                                       pre2+= Hill(a[i-2+j], a[i-1+j], a[i+j]);
                                  
                                  past-= min(pre1, pre2);
                                  sub= max(sub, past);
                                  a[i]= t1;
     }

     cout<<ans-sub<<"\n";
 }}