#pragma GCC optimize("Ofast") #include #define LL long long int #define ULL unsigned LL #define LD long double #define FI first #define SE second #define PB push_back #define PF push_front #define V vector #define PQ priority_queue #define ALL(x) x.begin(), x.end() #define SZ(x) (int)(x).size() #define FORI(i, a, b) for(int i = a; i < b ; ++i) #define FORD(i, a, b) for(int i = a; i > b ; --i) using namespace std; using pii = pair; const int INF = 1e9; int stairway_lis(const V &a, map> &pos){ int n = a.size(), j; V d(n+1, INF); d[0] = -INF; PQ pq; FORI(i,0,n){ while(!pq.empty() && -pq.top().first <= i){ j = -pq.top().second; d[j] = min(d[j], a[-pq.top().first]); pq.pop(); } j = upper_bound(ALL(d), a[i]) - d.begin(); auto it = upper_bound(ALL(pos[a[i]]), i); if (it != pos[a[i]].end() && d[j-1] < a[i] && a[i] < d[j]){ pq.push({-*it, -j}); } // j = 0; // while(d[j] < INF){ // cout << d[j++] << ' '; // } // cout << endl; } int ans = 0; for (int i = 0; i <= n; i++) { if (d[i] < INF) ans = i; } return 2 * ans; } int main(){ ifstream cin("stairway.in"); ofstream cout("stairway.out"); // ios_base::sync_with_stdio(false); // cin.tie(NULL); // cout.tie(NULL); mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); clock_t start_t = clock(); int t, n; cin >> t; FORI(ti,0,t){ cin >> n; V a(n); map> pos; FORI(i,0,n){ cin >> a[i]; pos[a[i]].push_back(i); } cout << stairway_lis(a, pos) << '\n'; } return 0; }