/// Author : Nguyễn Thái Sơn - K18 - KHMT - UIT
/// Training ICPC 2023
#include<bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/trie_policy.hpp>
//#include <ext/rope>
//#pragma GCC optimize("Ofast")
//#pragma GCC optimization("unroll-loops, no-stack-protector")
//#pragma GCC target("avx,avx2,fma")
//using namespace std;
//using namespace __gnu_pbds;
//using namespace __gnu_cxx;
#define fi first
#define se second
#define TASK "test"
#define pb push_back
#define EL cout << endl
#define Ti20_ntson int main()
#define in(x) cout << x << endl
#define all(x) (x).begin(),(x).end()
#define getbit(x, i) (((x) >> (i)) & 1)
#define cntbit(x) __builtin_popcount(x)
#define FOR(i,l,r) for (int i = l; i <= r; i++)
#define FORD(i,l,r) for (int i = l; i >= r; i--)
#define Debug(a,n) for (int i = 1; i <= n; i++) cout << a[i] << " "; cout << endl
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> vii;
typedef unsigned long long ull;
typedef vector<vector<int>> vvi;
int fastMax(int x, int y) { return (((y-x)>>(32-1))&(x^y))^y; }
const int N = 5005;
const int oo = INT_MAX;
const int mod = 1e9 + 7;
const int d4x[4] = {-1, 0, 1, 0} , d4y[4] = {0, 1, 0, -1};
const int d8x[8] = {-1, -1, 0, 1, 1, 1, 0, -1}, d8y[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int n, dp[N][2], cnt[N], Val[N][N], a[N];
vector<int> Store[N];
inline void Read_Input() {
cin >> n;
FOR(i, 1, n) {
int u; cin >> u;
a[i] = u;
if (Store[u].size() == 0) Store[u].push_back(0);
Store[u].push_back(i);
}
}
inline void Solve() {
FOR(i, 1, n) {
set<int, greater<int>> S; S.clear();
for (int j = i; j <= n; j++) {
S.erase(cnt[a[j]]);
++cnt[a[j]];
S.insert(cnt[a[j]]);
int u = *S.begin();
Val[i][j] = max(0, 2 * u - (j - i + 1));
}
for (int j = i; j <= n; j++)
cnt[a[j]]--;
}
int Ans = 0;
FOR(i, 1, n) {
int cnt = Store[i].size();
if (cnt == 0) continue;
Store[i].push_back(n + 1);
FOR(j, 0, cnt)
dp[j][0] = dp[j][1] = -mod;
dp[0][0] = 1;
FOR(l, 0, cnt - 1)
FOR(j, 0, 1) {
FOR(r, l + 1, cnt) {
/// ko lay l + 1
int dis = Store[i][r] - Store[i][l];
if (r != cnt)
dp[r][(j + dis) & 1] = max(dp[r][(j + dis) & 1], dp[l][j] - Val[Store[i][l] + 1][Store[i][r]]);
if ((j + dis) & 1) {
dp[r][0] = max(dp[r][0], dp[l][j] + 1 - Val[Store[i][l] + 1][Store[i][r] - 1]);
}
}
}
Ans = max(Ans, dp[cnt][0]);
}
cout << max(Ans - 2, 0) << '\n';
FOR(i, 1, n)
Store[i].clear();
}
Ti20_ntson {
// freopen(TASK".INP","r",stdin);
// freopen(TASK".OUT","w",stdout);
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int T = 1;
cin >> T;
while (T -- ) {
Read_Input();
Solve();
}
}
Ly8vIEF1dGhvciA6IE5ndXnhu4VuIFRow6FpIFPGoW4gLSBLMTggLSBLSE1UIC0gVUlUCi8vLyBUcmFpbmluZyBJQ1BDIDIwMjMKCiNpbmNsdWRlPGJpdHMvc3RkYysrLmg+Ci8vI2luY2x1ZGUgPGV4dC9wYl9kcy9hc3NvY19jb250YWluZXIuaHBwPgovLyNpbmNsdWRlIDxleHQvcGJfZHMvdHJpZV9wb2xpY3kuaHBwPgovLyNpbmNsdWRlIDxleHQvcm9wZT4KCi8vI3ByYWdtYSBHQ0Mgb3B0aW1pemUoIk9mYXN0IikKLy8jcHJhZ21hIEdDQyBvcHRpbWl6YXRpb24oInVucm9sbC1sb29wcywgbm8tc3RhY2stcHJvdGVjdG9yIikKLy8jcHJhZ21hIEdDQyB0YXJnZXQoImF2eCxhdngyLGZtYSIpCgovL3VzaW5nIG5hbWVzcGFjZSBzdGQ7Ci8vdXNpbmcgbmFtZXNwYWNlIF9fZ251X3BiZHM7Ci8vdXNpbmcg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ICwgZDR5WzRdID0gezAsIDEsIDAsIC0xfTsKY29uc3QgaW50IGQ4eFs4XSA9IHstMSwgLTEsIDAsIDEsIDEsIDEsIDAsIC0xfSwgZDh5WzhdID0gezAsIDEsIDEsIDEsIDAsIC0xLCAtMSwgLTF9OwoKaW50IG4sIGRwW05dWzJdLCBjbnRbTl0sIFZhbFtOXVtOXSwgYVtOXTsKdmVjdG9yPGludD4gU3RvcmVbTl07CgppbmxpbmUgdm9pZCBSZWFkX0lucHV0KCkgewogICAgY2luID4+IG47CiAgICBGT1IoaSwgMSwgbikgewogICAgICAgIGludCB1OyBjaW4gPj4gdTsKICAgICAgICBhW2ldID0gdTsKICAgICAgICBpZiAoU3RvcmVbdV0uc2l6ZSgpID09IDApIFN0b3JlW3VdLnB1c2hfYmFjaygwKTsKICAgICAgICBTdG9yZVt1XS5wdXNoX2JhY2soaSk7CiAgICB9Cn0KCmlubGluZSB2b2lkIFNvbHZlKCkgewogICAgRk9SKGksIDEsIG4pIHsKICAgICAgICBzZXQ8aW50LCBncmVhdGVyPGludD4+IFM7IFMuY2xlYXIoKTsKICAgICAgICBmb3IgKGludCBqID0gaTsgaiA8PSBuOyBqKyspIHsKICAgICAgICAgICAgUy5lcmFzZShjbnRbYVtqXV0pOwogICAgICAgICAgICArK2NudFthW2pdXTsKICAgICAgICAgICAgUy5pbnNlcnQoY250W2Fbal1dKTsKICAgICAgICAgICAgaW50IHUgPSAqUy5iZWdpbigpOwogICAgICAgICAgICBWYWxbaV1bal0gPSBtYXgoMCwgMiAqIHUgLSAoaiAtIGkgKyAxKSk7CiAgICAgICAgfQogICAgICAgIGZvciAoaW50IGogPSBpOyBqIDw9IG47IGorKykKICAgICAgICAgICAgY250W2Fbal1dLS07CiAgICB9CiAgICBpbnQgQW5zID0gMDsKICAgIEZPUihpLCAxLCBuKSB7CiAgICAgICAgaW50IGNudCA9IFN0b3JlW2ldLnNpemUoKTsKICAgICAgICBpZiAoY250ID09IDApIGNvbnRpbnVlOwogICAgICAgIFN0b3JlW2ldLnB1c2hfYmFjayhuICsgMSk7CiAgICAgICAgRk9SKGosIDAsIGNudCkKICAgICAgICAgICAgZHBbal1bMF0gPSBkcFtqXVsxXSA9IC1tb2Q7CiAgICAgICAgZHBbMF1bMF0gPSAxOwogICAgICAgIEZPUihsLCAwLCBjbnQgLSAxKQogICAgICAgICAgICBGT1IoaiwgMCwgMSkgewogICAgICAgICAgICAgICAgRk9SKHIsIGwgKyAxLCBjbnQpIHsKICAgICAgICAgICAgICAgICAgICAvLy8ga28gbGF5IGwgKyAxCiAgICAgICAgICAgICAgICAgICAgaW50IGRpcyA9IFN0b3JlW2ldW3JdIC0gU3RvcmVbaV1bbF07CiAgICAgICAgICAgICAgICAgICAgaWYgKHIgIT0gY250KQogICAgICAgICAgICAgICAgICAgIGRwW3JdWyhqICsgZGlzKSAmIDFdID0gbWF4KGRwW3JdWyhqICsgZGlzKSAmIDFdLCBkcFtsXVtqXSAtIFZhbFtTdG9yZVtpXVtsXSArIDFdW1N0b3JlW2ldW3JdXSk7CiAgICAgICAgICAgICAgICAgICAgaWYgKChqICsgZGlzKSAmIDEpIHsKICAgICAgICAgICAgICAgICAgICAgICAgZHBbcl1bMF0gPSBtYXgoZHBbcl1bMF0sIGRwW2xdW2pdICsgMSAtIFZhbFtTdG9yZVtpXVtsXSArIDFdW1N0b3JlW2ldW3JdIC0gMV0pOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgIEFucyA9IG1heChBbnMsIGRwW2NudF1bMF0pOwogICAgfQogICAgY291dCA8PCBtYXgoQW5zIC0gMiwgMCkgPDwgJ1xuJzsKICAgIEZPUihpLCAxLCBuKQogICAgICAgIFN0b3JlW2ldLmNsZWFyKCk7Cn0KClRpMjBfbnRzb24gewovLyAgICBmcmVvcGVuKFRBU0siLklOUCIsInIiLHN0ZGluKTsKLy8gICAgZnJlb3BlbihUQVNLIi5PVVQiLCJ3IixzdGRvdXQpOwogICAgaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbyhmYWxzZSk7IGNpbi50aWUoMCk7IGNvdXQudGllKDApOwogICAgaW50IFQgPSAxOwogICAgY2luID4+IFQ7CiAgICB3aGlsZSAoVCAtLSApIHsKICAgICAgICBSZWFkX0lucHV0KCk7CiAgICAgICAgU29sdmUoKTsKICAgIH0KfQoKCg==