#include <bits/stdc++.h>

using namespace std;

#define loop(start, end, var) for (int var = start; var <= end; var++)
#define loopDown(start, end, var) for (int var = start; var >= end; var--)
#define foreach(el, arr) for (auto el : arr)
#define F first
#define S second
#define all(arr) arr.begin(), arr.end()
typedef unsigned unt;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;

int check(int cur, int left, int right)
{
    if (left == -1 || right == -1)
        return 0;
    int left_r = cur - left,
        right_r = cur - right;

    if (left_r > 0 && right_r > 0)
        return 1;
    else if (left_r < 0 && right_r < 0)
        return -1;

    return 0;
}

int main()
{
    ios_base::sync_with_stdio(false);

    int t;

    cin >> t;

    loop(1, t, i)
    {
        int n;

        cin >> n;

        vi a(n + 2);
        a[0] = -1;
        a[n + 1] = -1;

        loop(1, n, j)
        {
            cin >> a[j];
        }

        int ans_k = 0;

        vi height(n + 1);

        loop(2, n - 1, j)
        {
            int res = check(a[j], a[j - 1], a[j + 1]);
            height[j] = res;

            if (abs(res))
                ans_k++;
        }

        int ans = 0;

        if (n == 3)
        {
            cout << 0 << '\n';
            continue;
        }

        loop(2, n - 1, j)
        {
            int left = a[j - 1],
                right = a[j + 1];

            int local_ans = abs(check(left, a[j - 2], a[j])) + abs(check(a[j], left, right)) + abs(check(right, a[j], a[j + 2]));
            int current_state = local_ans;

            loop(left - 5, left + 5, cur)
            {
                if (cur < 0)
                    continue;

                int k = abs(check(left, a[j - 2], cur)) + abs(check(cur, left, right)) + abs(check(right, cur, a[j + 2]));

                local_ans = min(local_ans, k);
            }

            loop(right - 5, right + 5, cur)
            {
                if (cur < 0)
                    continue;

                int k = abs(check(left, a[j - 2], cur)) + abs(check(cur, left, right)) + abs(check(right, cur, a[j + 2]));

                local_ans = min(local_ans, k);
            }

            ans = max(ans, current_state - local_ans);
        }

        cout << ans_k - ans << '\n';
    }

    return 0;
}