#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define faster ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define read freopen("in.txt", "r", stdin)
#define write freopen("out.txt", "w", stdout)
#define mem(x, n) memset(x, n, sizeof(x))
#define all(x) x.begin(), x.end()
#define endl "\n"

bool canCreate(vector<int> &a, int currInd, int changedInd, int val)
{
	if (currInd - 1 < 0 || currInd + 1 >= a.size())
		return 0;

	vector<int> curr = {a[currInd - 1], a[currInd], a[currInd + 1]};

	if (currInd - 1 == changedInd)
		curr[0] = val;
	else if (currInd == changedInd)
		curr[1] = val;
	else
		curr[2] = val;

	if (curr[1] > curr[0] && curr[1] > curr[2])
		return 1;
	if (curr[1] < curr[0] && curr[1] < curr[2])
		return 1;
	return 0;
}

int main()
{
	faster;
	const int hill = 1, valley = 2;

	int t;
	cin >> t;

	while (t--)
	{
		int n;
		cin >> n;

		vector<int> a(n);
		for (int i = 0; i < n; i++)
			cin >> a[i];

		int cnt = 0;

		vector<int> sit(n, 0);

		for (int i = 1; i < n - 1; i++)
		{
			if (a[i] > a[i - 1] && a[i] > a[i + 1])
				cnt++, sit[i] = hill;
			else if (a[i] < a[i - 1] && a[i] < a[i + 1])
				cnt++, sit[i] = valley;
		}

		int mnn = cnt;

		for (int i = 1; i < n - 1; i++)
		{
			int curr = cnt - (sit[i] != 0);
			vector<int> candidates;

			if (i - 1 >= 0)
				candidates.push_back(a[i - 1]), curr -= (sit[i - 1] != 0);
			if (i + 1 < n)
				candidates.push_back(a[i + 1]), curr -= (sit[i + 1] != 0);

			for (auto x : candidates)
			{
				int currCnt = 0;
				for (int ind = i - 1; ind <= i + 1; ind++)
				{
					currCnt += canCreate(a, ind, i, x);
				}
				mnn = min(mnn, currCnt + curr);
			}
		}

		cout << mnn << endl;
	}

	return 0;
}