#include <bits/stdc++.h>

using namespace std;

int n, a[500001], stk[500001], l[500001], r[500001];
int t;
pair<int, int> e[500001];

int main( ) {
	scanf("%d", &t);
	while (t --) {
		scanf("%d", &n);
		int top = 0, ans = 0;
		stk[0] = 0;
		for (int i = 1; i <= n; i ++) {
			scanf("%d", &a[i]);
		}
		for (int i = 1; i <= n; i ++) {
			int idx = n;
			int mn2 = 0x3f3f3f3f, mn1 = 0x3f3f3f3f;
			int mx2 = -0x3f3f3f3f, mx1 = -0x3f3f3f3f;
			for (int j = i; j <= n; j ++) {
				if (a[j] >= mn2) {
					idx = j - 1;
					break;
				}
				if (a[j] < mn2 && a[j] >= mn1) mn2 = a[j];
				mn1 = min(mn1, a[j]);
				if (a[j] <= mx2) {
					idx = j - 1;
					break;
				}
				if (a[j] > mx2 && a[j] <= mx1) mx2 = a[j];
				mx1 = max(mx1, a[j]);
			}
			ans += idx - i + 1;
		}
		cout << ans << endl;
	}

	return 0;
}
			 