#include <iostream>
#include <bits/stdc++.h>
#define ll long long
#define ii pair<ll, ll>
#define fi first
#define se second
#define vi vector<int>
#define vvi vector<vector<int>>
#define vvl vector<vector<ll>>
#define vl vector<ll>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define For(i, n) for (int i = 0; i < n; i++)
#define pb(a) push_back(a)
using namespace std;
class A{
public:
    void sovle(){
        int t;
        cin>>t;
        while(t--){
            int n;
            cin>>n;
            if(n>=1) cout<<9;
            for(int i=2,cur=8;i<=n;i++){
                cout<<cur;
                cur=(cur+1)%10;
            }
            cout<<endl;
        }
    }
};
class B{
public:
    void solve(){
        int t;
        cin>>t;
        while(t--){
            int n;
            cin>>n;
            vl a(n+1,0);
            vi l(n+2,0),r(n+2,0);
            for(int i=1;i<=n;i++){
                cin>>a[i];
            }
            for(int i=1;i<=n;i++){
                l[i]=l[i-1];
                if(i>=2&&i<=n-1){
                    if(a[i]>a[i+1]&&a[i]>a[i-1]) l[i]++;
                    if(a[i]<a[i+1]&&a[i]<a[i-1]) l[i]++;
                }
            }
            for(int i=n;i>=1;i--){
                r[i]=r[i+1];
                if(i>=2&&i<=n-1){
                    if(a[i]>a[i+1]&&a[i]>a[i-1]) r[i]++;
                    if(a[i]<a[i+1]&&a[i]<a[i-1]) r[i]++;
                }
            }
            auto check=[&a](int n,int i){
                if(i<=n-1&&i>=2){
                    if(a[i]>a[i+1]&&a[i]>a[i-1]) return 1;
                    if(a[i]<a[i+1]&&a[i]<a[i-1]) return 2;
                    return 0;
                }
                else return 0;
            };
            int res=l[n];
            for(int i=2;i<=n-1;i++){
                int bk=a[i];
                a[i]=a[i-1];
                if(check(n,i+1)!=0) res=min(res,l[i-2]+r[i+2]+1);
                else res=min(res,l[i-2]+r[i+2]);
                a[i]=a[i+1];
                if(check(n,i-1)!=0) res=min(res,l[i-2]+r[i+2]+1);
                else res=min(res,l[i-2]+r[i+2]);
                a[i]=bk;
            }
            cout<<res<<endl;
        }
    }
};
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    B ans;
    ans.solve();
    return 0;
}
