Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
for (int i = 0; i < n; i++) {
int amount = scanner.nextInt();
int[] b = new int[amount];
for (int j = 0; j < amount; j++) {
b[j] = scanner.nextInt();
}
int[] present = new int[amount];
Arrays.fill(present, 1);
int current = 0;
int next = 1;
int pre = amount - 1;
while (true) {
boolean flag = false;
for (int z= 0; z< amount; z++) {
if (b[current] > b[pre] && b[current] > b[next]) {
while (present[current] <= present[pre] || present[current] <= present[next]) {
present[current]++;
flag = true;
}
}
else if (b[current] > b[pre] && b[current] <= b[next]) {
while (present[current] <= present[pre]) {
present[current]++;
flag = true;
}
}
else if (b[current] <= b[pre] && b[current] > b[next]) {
while (present[current] <= present[next]) {
present[current]++;
flag = true;
}
}
pre++;
current++;
next++;
pre %= n;
current %= n;
next %= n;
}
if (!flag) {
break;
}
}
int count = 0;
for (int l : present) {
count += l;
}
System.out.println(count);
}