//⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⣀⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣠⣾⢿⡏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣾⣛⣫⣿⡿⣧⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣤⣴⡶⠞⠛⠛⠛⠛⠛⠛⠓⠶⣶⣤⣀⣼⣿⣿⣿⣷⣽⣧⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣸⡟⠉⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢹⡇⢩⡿⢹⡿⠿⠋⠀⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⡇⠀⠀⢠⣤⣤⣶⣶⣶⣶⣶⣶⣶⣶⣾⣧⣼⢣⣿⣀⣴⡾⠟⠛⠿⢶⣦⣄⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠸⣿⣤⡀⠈⠀⠙⠻⢶⣤⣄⣀⣀⠀⢠⣶⣿⡟⣼⡟⠛⠛⠛⠷⣦⣄⡀⠀⠉⠛⠿⣦⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠙⠛⢷⣦⣤⣤⣤⣬⣭⣽⣟⣛⣛⣿⡟⣿⣾⣿⣀⠀⠀⠀⠈⠙⠻⢦⣄⡀⠀⠈⠙⢷⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣤⡶⠞⠛⠋⠉⠉⠉⠀⠀⠉⠉⠉⠙⣿⣶⠿⠻⠿⢿⣿⣶⣶⣤⣤⣤⣤⣽⣿⣦⡀⠀⠀⠙⣷⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣴⡿⠋⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠁⠀⠀⠀⠀⠀⠉⠉⠛⠷⣦⣌⡉⠁⠈⠻⠀⠀⠀⠈⣷⠀⠀⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⢀⡀⠀⠀⠀⠀⠀⠀⠀⣀⣠⡾⠟⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢿⣷⡶⠶⢶⡶⠶⠾⠏⠀⠀⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⢻⡛⠛⠿⠿⠿⠿⠛⠛⠛⠉⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠻⣦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⠸⣷⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣤⣶⣾⣶⣶⡶⠶⢶⣿⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⢿⣤⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⠀⠻⣷⣄⠀⠀⠀⠀⠀⠀⢀⣴⡿⣋⣭⣷⠶⠶⠶⢶⣤⣸⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣴⠟⠋⠀⠀⠀⠀⠀⠀⠀⡀⠀⠀⠀⠹⣷⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⠀⠀⠈⢙⣿⠆⠀⠀⠀⢰⣿⢯⣾⣿⣧⣄⠀⠀⠀⠀⠈⠻⣧⡀⠀⠀⠀⠀⠀⠀⠀⣠⣾⢿⣿⠀⠀⠀⠀⠀⠀⠀⠀⢀⣿⡀⠀⠀⠀⠘⣷⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⠀⠀⣰⡿⠃⠀⠀⠀⣴⡟⢁⣿⣿⣿⣿⣿⡇⠀⠀⠀⠀⠀⠸⣷⠀⠀⠀⠀⠀⢀⣴⠟⠁⠈⣿⣀⣀⠀⠀⠀⠀⠀⢀⣼⣿⣷⣀⠀⠀⠀⠘⣷⡄⠀⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⠀⣴⡟⠀⠀⠀⢠⣾⠋⠀⢸⣿⣿⣿⣿⡿⠃⠀⠀⠀⠀⠀⠀⢹⡆⠀⠀⠀⣠⡿⠁⣠⣶⠿⠛⠛⠛⠛⠷⣦⡀⣠⣿⣿⠅⢙⣿⣦⣤⣀⡀⠹⣷⣄⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⣸⡟⠀⠀⠀⢠⡿⠃⠀⠀⠘⣷⡈⠉⠉⠀⠀⠀⠀⠀⠀⠀⢠⣿⡆⠀⠀⢰⡿⠁⣼⣯⣥⣤⣄⠀⠀⠀⠀⠈⢿⣿⣿⣅⡀⠀⢀⣿⡿⠛⠁⠀⠀⠙⢷⣦⣀⠀⠀⠀⠀
//⠀⢠⡿⠀⠀⢀⣠⡾⠁⠀⠀⠀⠀⠘⢿⣦⡀⠀⠀⠀⠀⠀⣀⣴⠟⠉⣿⡄⠀⣿⠀⢸⣿⣿⣿⣿⣿⡧⠀⠀⠀⠀⠘⣿⠉⠻⣿⣿⣿⡟⠀⠀⠀⠀⠀⠀⠀⠊⢻⣷⠀⠀⠀
//⠀⣿⠇⠀⠀⠏⣀⡀⠀⠀⠀⠀⠀⠀⠀⠈⠛⠷⠶⠶⠶⠟⠋⠁⠀⠀⠈⠻⣶⣏⠀⢸⡟⢿⣿⡿⠟⠁⠀⠀⠀⠀⠀⣿⣆⠀⠈⢿⡟⠀⠀⠀⠀⠀⢠⣀⣠⣶⠟⠁⠀⠀⠀
//⢀⣿⠀⠀⠀⢰⡟⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢾⣆⠀⠀⠀⠀⠀⠀⠈⠛⠂⠘⢿⣄⠀⠀⠀⠀⠀⠀⠀⠀⣼⡟⣿⡆⠀⠘⠃⠀⠀⢠⡄⠀⠘⣿⡉⠀⠀⠀⠀⠀⠀
//⠸⣿⠀⠀⠀⢸⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣼⣿⣷⣶⣶⣾⣷⣄⣀⠀⣀⣤⠈⠛⢷⣤⣄⣀⣀⣠⣴⠾⢿⣄⣹⡇⠀⠀⠀⠀⠀⠸⣿⠀⠀⠙⢷⣤⡀⠀⠀⠀⠀
//⠀⣿⡀⠀⠀⠘⣷⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⣿⠟⠛⠋⠉⠉⠉⠉⠙⠛⠻⣿⠉⠀⠀⠀⠈⠉⠉⠉⠉⠁⠀⠈⠻⣿⡧⠀⠀⠀⠀⠀⠀⣿⠀⠀⠀⠀⠉⠛⠷⣶⣤⡀
//⠀⠹⣷⡀⢀⡀⠙⢷⣄⠀⠀⠀⠀⠀⠀⠀⠀⣰⡿⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢼⡟⠀⠀⠀⠀⠀⠀⠀⢹⠀⠀⠀⠀⠀⠀⠀⣠⡾⠁
//⠀⠀⠙⢷⣿⣷⡀⠀⠙⢿⣦⣄⡀⠀⠀⠀⠀⣿⣁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⠇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⡿⠇⠀⠀⠀⠀⠀⠀⢸⡀⠀⠀⠀⠀⠀⣾⠛⠀⠀
//⠀⠀⠀⠀⠙⠛⠿⣦⣄⣸⣧⣿⣿⣷⣤⣄⡀⠀⠈⠛⠦⢤⣀⡀⠀⠀⠀⠀⠀⣰⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣼⡟⠀⠀⠀⠀⠀⠀⠀⠀⢸⠀⣀⠀⠀⠀⠀⣿⠀⠀⠀
//⠀⠀⠀⠀⠀⠀⠀⠈⠙⠛⠿⢿⣿⣯⣋⢻⣿⢷⣶⣤⣄⣀⠈⠉⠙⠒⠒⠒⠺⠟⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣾⢏⡄⠀⠀⠀⠀⠀⠀⠀⠀⣿⢀⣿⣦⡀⠀⢠⣿⠀⠀⠀
//⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⡾⣿⠟⠛⠿⢷⣶⣿⣿⡟⣿⣿⣷⣶⣦⣤⣤⣤⣀⡀⠀⠀⠀⠀⠀⠀⠀⠀⢠⣾⣯⡿⠃⠀⠀⠀⠀⠀⠀⠀⢸⣿⣾⡟⣿⡇⢀⣼⠇⠀⠀⠀
//⠀⠀⠀⠀⠀⠀⠀⠀⢀⣾⠏⢰⡟⢺⣶⣾⠿⠿⢛⢽⣵⣿⢿⣿⠀⢩⣿⣿⣿⣿⣿⠿⢷⣶⣶⣶⣶⣶⣶⣿⠟⠋⠀⠀⠀⠀⠀⠀⠀⠀⢠⣿⣿⣿⣾⣿⣷⠿⠃⠀⠀⠀⠀
//⠀⠀⠀⠀⢀⣀⡀⣠⡿⠁⠀⣿⠁⠈⠻⢿⣿⣧⣾⠿⠟⣁⣾⡏⠀⢈⣿⡇⠉⠛⠿⣿⡿⠿⠿⠻⣿⣟⠉⠀⠀⠀⠀⠀⣀⡀⠀⠀⠀⣰⡿⠿⠛⣋⣀⣽⣵⣾⣿⣿⡇⠀⠀
//⠀⠀⠀⠀⣿⠙⢿⣿⣤⣤⣼⡏⠀⠀⠀⠀⠀⠀⢀⠀⣼⣿⣿⠀⢠⣿⣟⠻⣾⣛⣛⠻⢿⣷⣤⣄⣀⡙⠻⠷⠶⠶⠿⣻⡟⠁⠀⣠⣾⣿⣿⣿⣿⣿⣟⣋⣡⣠⣄⣿⡇⠀⠀
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define fi first
#define se second
#define pb push_back
#define endl '\n'
#define taskname "temp"
using namespace std;
const ll INF = 1e18;
const ll MOD = 1e9 + 7;
void solve() {
ll n;
cin >> n;
vector<ll> arr(n);
for (ll &x : arr) cin >> x;
vector<ll>dp(n + 1, 0);
unordered_map<ll, vector<ll>>pos;
for (ll i = 1; i <= n; i++) {
dp[i] = dp[i - 1];
ll val = arr[i - 1];
pos[val].pb(i);
if ((ll)pos[val].size() >= val) {
ll first = pos[val][pos[val].size() - val];
dp[i] = max(dp[i], dp[first - 1] + val);
}
}
cout << dp[n] << endl;
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
//freopen(taskname".INP", "r", stdin);
//freopen(taskname".OUT", "w", stdout);
ll testcase = 1;
cin >> testcase;
for (ll test = 1; test <= testcase; ++test){
solve();
}
return 0;
}
/*
roi em se gap mot chang trai khac
nam tay va buoc di bau troi xanh khac
/\\ /\\ //\
(TwT) (-.-) (-.-)
/= =\ /> \> <3 </ <\
*/