#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> pii;
#define mp make_pair
#define pb push_back
#define f first
#define s second

//var 

LL t,n,a[300001],b[300001];
bool v[300001];

int main(){
    ios_base::sync_with_stdio(0); 
    cin.tie(0);

    cin >> t;
    while(t--){
      LL ans = 0;
      cin >> n;
      for(int i = 0; i < n; i++){
        cin >> a[i];
        b[i] = 0;
        v[i] = false;
      }
      for(int i = 1; i < n-1; i++){
        if(a[i] > a[i-1] && a[i] > a[i+1]){
          b[i] = 1;
          ans++;
        }else if(a[i] < a[i-1] && a[i] < a[i+1]){
          b[i] = 1;
          ans++;
        }
      }
      LL maxchain = 0;
      for(int i = 1; i < n-1; i++){
        if(v[i]){
          continue;
        }
        if(b[i] == 1){
          LL chain = 1;
          for(int j = i+1; j < n-1; j++){
            if(b[j] == 1){
              chain++;
              v[j] = true;
            }else{
              break;
            }
          }
          maxchain = max(chain, maxchain);
        }
      }
      if(maxchain == 0){
        cout << 0 << endl;
      }else if(maxchain == 1){
        cout << ans-1 << endl;
      }else if(maxchain == 2){
        bool found = false;
        for(int i = 1; i < n-2; i++){
          if(b[i] == 1 && b[i+1] == 1){
            if(i-2 >= 0 && i+3 <= n-1){
              if(a[i-2] < a[i-1] && a[i-1] < a[i] && a[i+1] < a[i-1] && a[i] > a[i+2] && a[i+2] < a[i+3]){
                continue;
              }else if(a[i-2] > a[i-1] && a[i-1] > a[i] && a[i+1] > a[i-1] && a[i] < a[i+2] && a[i+2] > a[i+3]){
                continue;
              }else{
                found = true;
              }
            }else{
              found = true;
            }
          }
        }
        if(found){
          cout << ans-2 << endl;
        }else{
          cout << ans-1 << endl;
        }
      }else{
        cout << ans-3 << endl;
      }
    }

    cout.flush();
    return 0;
}