//Dalao厉害,第四题完全没想到用二分;
//我考试时写的第三题O(n),想法可能简单点
#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
using namespace std;
int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        int people;
        cin >> people;
        vector<int> point(people, 0);
        vector<long long> pres(people, 1);
        vector<pair<int, int> > mark(people, make_pair(0, 0));
        for (int j = 0; j < people; ++j) cin >> point[j];
        if (people == 0) {
            cout << 0 << endl;
            continue;
        }
        if (people == 1) {
            cout << 1 << endl;
            continue;
        }
        if (people == 2) {
            if (point[0] == point[1]) cout << 2 << endl;
            else cout << 3 << endl;
            continue;
        }
        for (int j = 0; j < people; ++j) {
                int left = j - 1 < 0 ? people - 1 : j - 1;
                int right = (j + 1) % people;
                if (point[j] > point[left]) mark[j].first = 1;
                if (point[j] > point[right]) mark[j].second = 1;
        }
        for (int j = 0; j < people; ++j) {
            if (mark[j].first == 0 && mark[j].second == 0) {
                int left = j - 1 < 0 ? people - 1 : j - 1;
                while (mark[left].second == 1) {
                    pres[left] = max(pres[left], pres[(left+1)%people] + 1);
                    left = left - 1 < 0 ? people - 1 : left - 1;
                }
                int right= (j + 1) % people;
                while (mark[right].first == 1) {
                    pres[right] = max(pres[right], pres[right-1<0?people-1:right-1] + 1);
                    right = (right + 1) % people;
                }
            }
        }
        long long result = 0;
        for (int j = 0; j < people; ++j) result += pres[j];
        cout << result << endl;
    }
    return 0;
}