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

#define int long long int
#define mod 1000000007
#define f0(n) for(int i=0;i<n;i++)
#define vi vector<int>
#define vs vector<string>
#define mp make_pair
#define pb push_back
#define rt return
#define sumof(arr) accumulate(arr,arr+n,0);
double pi = 3.141592653589793238462;
string numkey="0123456789";
string uni="abcdefghijklmnopqrstuvwxyz";
#define pii pair<int,int>
#define setf cout.setf(ios::fixed);
#define setp setprecision
#define mii map<int,int>
#define F first
#define S second
#define all(x) x.begin(),x.end()

int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};

int INF=(1LL<<62);
int n;
int arr[300000+10];

int calc(int l,int r)
{
  int ans=0;
  for(int i=l+1;i<r-1;i++)
  {
    if(arr[i-1]>arr[i] && arr[i]<arr[i+1])
        ans++;
      else if(arr[i-1]<arr[i] && arr[i]>arr[i+1])
        ans++;
  }
  return ans;
}

signed main()
{
  #ifndef ONLINE_JUDGE
  freopen("input.txt","r",stdin);
  freopen("output.txt","w",stdout);
  #endif
  int t;
  cin>>t;
  while(t--)
  {
    
    cin>>n;
    
    for(int i=0;i<n;i++)
      cin>>arr[i];
    int ans=0;
    for(int i=1;i<n-1;i++)
    {
      if(arr[i-1]>arr[i] && arr[i]<arr[i+1])
        ans++;
      else if(arr[i-1]<arr[i] && arr[i]>arr[i+1])
        ans++;
    }
    //cout<<ans<<" \n";
    int mx=0;
    for(int i=1;i<n-1;i++)
    {
      int val1=calc(max(0LL,i-2),min(n,i+3));
      int temp=arr[i];
      arr[i]=max(arr[i-1],arr[i+1]);
      int val2=calc(max(0LL,i-2),min(n,i+3));
      arr[i]=temp;
      mx=max(mx,val1-val2);
      arr[i]=min(arr[i-1],arr[i+1]);
      int val3=calc(max(0LL,i-2),min(n,i+3));
      arr[i]=temp;
      mx=max(mx,val1-val3);
      
     // cout<<i<<" "<<val1<<" "<<val2<<"\n";

    }

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

  return 0;
  
}