/** ____ ____ ____ ____ ____ ____ ||l |||e |||i |||n |||a |||d || ||__|||__|||__|||__|||__|||__|| |/__\|/__\|/__\|/__\|/__\|/__\| **/ #include #define endl '\n' using namespace std; typedef long long ll; const int maxn = 1e5 + 10; void speed() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } int n, a[maxn], nxt[maxn], dp[maxn]; vector < int > upd[maxn]; void solve() { cin >> n; for (int i = 1; i <= n; i ++) { cin >> a[i]; nxt[i] = 0; dp[i] = 0; upd[i].clear(); } unordered_map < int, int > last; for (int i = n; i > 0; i --) { if (last[a[i]] == 0) nxt[i] = -1; else nxt[i] = last[a[i]]; last[a[i]] = i; } int to = 0; for (int i = 1; i <= n; i ++) { if (nxt[i] != -1) { int lf = 0, rf = to; while(lf <= rf) { int mf = (lf + rf) / 2; if (dp[mf] >= a[i]) rf = mf - 1; else lf = mf + 1; } upd[nxt[i]].push_back(lf); } for (int j = 0; j < upd[i].size(); j ++) { int idx = upd[i][j]; if (idx > to) { to ++; dp[to] = a[i]; } else { if (dp[idx] > a[i]) dp[idx] = a[i]; } } } cout << to * 2 << endl; } int main() { freopen("stairway.in", "r", stdin); freopen("stairway.out", "w", stdout); int t; cin >> t; while(t --) solve(); return 0; } /** 3 7 1 1 1 2 3 3 4 6 1 2 3 4 5 6 10 3 3 2 2 1 1 2 2 3 3 */