//generated by ChatGPT #include using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int Q; cin >> Q; while (Q--) { int N; cin >> N; if (N == -1) { return 0; } vector P(N); int pos1 = -1; for (int i = 0; i < N; ++i) { cin >> P[i]; if (P[i] == 1) { pos1 = i; } } // 値 1 の位置から d だけ進んだ場所の値 auto value_at = [&](int d) -> int { return P[(pos1 + d) % N]; }; int offset; if (N == 6) { bool u = value_at(2) < value_at(4); bool v = value_at(1) < value_at(5); offset = (u ^ v) ? 2 : 5; } else { int M = N / 2; const array position = { M + 1, 2, M - 1, N - 2 }; const array answer_offset = { 0, 1, M + 1, M }; int best = 0; for (int c = 1; c < 4; ++c) { if (value_at(position[c]) < value_at(position[best])) { best = c; } } offset = answer_offset[best]; } int answer = (pos1 + offset) % N; // endl によって flush する cout << answer << endl; } return 0; }