https://codeforces.com/contest/1974/problem/C
#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
#define oz 998244353
#define N 200010
#define INF 0x3f3f3f3f
typedef long long ll;
#define endl '\n'
const int mod = 1e9 + 7;
void solve() {
int n;
cin >> n;
vector<int> ar(n + 1);
map<PII, int> a, b, c;
map<vector<int>, int> d;
for (int i = 1; i <= n; i++) {
cin >> ar[i];
}
int res = 0;
for (int i = 1; i <= n - 2; i++) {
res += a[ {ar[i], ar[i + 1]}] ++;
res += b[ {ar[i + 1], ar[i + 2]}] ++;
res += c[ {ar[i], ar[i + 2]}] ++;
vector<int> v = {ar[i], ar[i + 1], ar[i + 2]};
res -= d[v] * 3;
d[v] ++;
}
cout << res << endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int T = 1;
cin >> T;
while (T --)
solve();
return 0;
}