#include <bits/stdc++.h>
#define pb push_back
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;

const ll N = 3e5 + 30;
const ll Mod = 1e14 + 99;
const ll B = 727;

ll get(ll x, ll y) {
	return (x * B + y) % Mod;
}

ll a[N];
ll solve(ll l, ll r) {  //  [ )
	if (r <= l || r == l + 1) return 0;
	if (r - l == 2 && a[l] == a[l + 1]) return 1;
	ll mid = (l + r) >> 1;
	ll res = solve(l, mid) + solve(mid, r);
	stack<ll> st, s;
	vector<ll> V;
	ll cur = 0;
	for (int i = mid - 1; i >= l; i --) {
		if (!st.size() || st.top() != a[i])
			st.push(a[i]), cur = get(cur, a[i]), s.push(cur);
		else {
			st.pop();
			if (s.size()) s.pop();
			if (s.size()) cur = s.top();
			else cur = 0;
		}
		V.pb(cur);
	}
	sort(all(V));
	while (st.size()) st.pop();
	while (s.size()) s.pop();
	cur = 0;
	for (int i = mid; i < r; i ++) {
		if (!st.size() || st.top() != a[i])
			st.push(a[i]), cur = get(cur, a[i]), s.push(cur);
		else {
			st.pop();
			if (s.size()) s.pop();
			if (s.size()) cur = s.top();
			else cur = 0;
		}
		res += upper_bound(all(V), cur) - lower_bound(all(V), cur);
	}
	return res;
}
int main () {
    ios::sync_with_stdio(0), cin.tie(0);
	ll q;
	cin >> q;
	while (q --) {
		ll n;
		cin >> n;
		for (int i = 0; i < n; i ++)
			cin >> a[i];
		cout << solve(0, n) << '\n';
	}
    return (0);
}


#include <bits/stdc++.h>
#define pb push_back
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;

const ll N = 3e5 + 30;
const ll Mod = 1111111111111111111;
const ll Base =  7;

ll get(ll x, ll y) {
	return (x * Base + y) % Mod;
}

ll a[N];
ll solve(ll l, ll r) {  //  [ )
	if (r <= l || r == l + 1) return 0;
	if (r - l == 2 && a[l] == a[l + 1]) return 1;
	if (r - l == 2 && a[l] != a[l + 1]) return 0;
	ll mid = (l + r) >> 1;
	ll res = solve(l, mid) + solve(mid, r);
	stack<ll> st, s;
	vector<ll> V;
	ll cur = 0;
	for (int i = mid - 1; i >= l; i --) {
		if (!st.size() || st.top() != a[i])
			st.push(a[i]), cur = get(cur, a[i]), s.push(cur);
		else {
			st.pop();
			if (s.size()) s.pop();
			if (s.size()) cur = s.top();
			else cur = 0;
		}
		V.pb(cur);
	}
	sort(all(V));
	while (st.size()) st.pop();
	while (s.size()) s.pop();
	cur = 0;
	for (int i = mid; i < r; i ++) {
		if (!st.size() || st.top() != a[i])
			st.push(a[i]), cur = get(cur, a[i]), s.push(cur);
		else {
			st.pop();
			if (s.size()) s.pop();
			if (s.size()) cur = s.top();
			else cur = 0;
		}
		res += upper_bound(all(V), cur) - lower_bound(all(V), cur);
	}
	return res;
}
int main () {
    ios::sync_with_stdio(0), cin.tie(0);
	ll q;
	cin >> q;
	while (q --) {
		ll n;
		cin >> n;
		for (int i = 0; i < n; i ++)
			cin >> a[i];
		cout << solve(0, n) << '\n';
	}
    return (0);
}


#include <bits/stdc++.h>
#define pb push_back
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;

const ll N = 3e5 + 30;
const ll Mod = 77777677777;
const ll Base = 7778777;

ll get(ll x, ll y) {
	return (x * Base + y) % Mod;
}

ll a[N];
ll solve(ll l, ll r) {  //  [ )
	if (r <= l || r == l + 1) return 0;
	if (r - l == 2 && a[l] == a[l + 1]) return 1;
	if (r - l == 2 && a[l] != a[l + 1]) return 0;
	ll mid = (l + r) >> 1;
	ll res = solve(l, mid) + solve(mid, r);
	stack<ll> st, s;
	vector<ll> V;
	ll cur = 0;
	for (int i = mid - 1; i >= l; i --) {
		if (!st.size() || st.top() != a[i])
			st.push(a[i]), cur = get(cur, a[i]), s.push(cur);
		else {
			st.pop();
			if (s.size()) s.pop();
			if (s.size()) cur = s.top();
			else cur = 0;
		}
		V.pb(cur);
	}
	sort(all(V));
	while (st.size()) st.pop();
	while (s.size()) s.pop();
	cur = 0;
	for (int i = mid; i < r; i ++) {
		if (!st.size() || st.top() != a[i])
			st.push(a[i]), cur = get(cur, a[i]), s.push(cur);
		else {
			st.pop();
			if (s.size()) s.pop();
			if (s.size()) cur = s.top();
			else cur = 0;
		}
		res += upper_bound(all(V), cur) - lower_bound(all(V), cur);
	}
	return res;
}
int main () {
    ios::sync_with_stdio(0), cin.tie(0);
	ll q;
	cin >> q;
	while (q --) {
		ll n;
		cin >> n;
		for (int i = 0; i < n; i ++)
			cin >> a[i];
		cout << solve(0, n) << '\n';
	}
    return (0);
}


#include <bits/stdc++.h>
#define pb push_back
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;

const ll N = 3e5 + 30;
const ll Mod = (1 << 31) - 1;
const ll B = (1 << 19) - 1;

ll get(ll x, ll y) {
	return (x * B + y) % Mod;
}

ll a[N];
ll solve(ll l, ll r) {  //  [ )
	if (r <= l || r == l + 1) return 0;
	if (r - l == 2 && a[l] == a[l + 1]) return 1;
	if (r - l == 2 && a[l] != a[l + 1]) return 0;
	ll mid = (l + r) >> 1;
	ll res = solve(l, mid) + solve(mid, r);
	stack<ll> st, s;
	vector<ll> V;
	ll cur = 0;
	for (int i = mid - 1; i >= l; i --) {
		if (!st.size() || st.top() != a[i])
			st.push(a[i]), cur = get(cur, a[i]), s.push(cur);
		else {
			st.pop();
			if (s.size()) s.pop();
			if (s.size()) cur = s.top();
			else cur = 0;
		}
		V.pb(cur);
	}
	sort(all(V));
	while (st.size()) st.pop();
	while (s.size()) s.pop();
	cur = 0;
	for (int i = mid; i < r; i ++) {
		if (!st.size() || st.top() != a[i])
			st.push(a[i]), cur = get(cur, a[i]), s.push(cur);
		else {
			st.pop();
			if (s.size()) s.pop();
			if (s.size()) cur = s.top();
			else cur = 0;
		}
		res += upper_bound(all(V), cur) - lower_bound(all(V), cur);
	}
	return res;
}
int main () {
    ios::sync_with_stdio(0), cin.tie(0);
	ll q;
	cin >> q;
	while (q --) {
		ll n;
		cin >> n;
		for (int i = 0; i < n; i ++)
			cin >> a[i];
		cout << solve(0, n) << '\n';
	}
    return (0);
}


#include <bits/stdc++.h>
#define pb push_back
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;

const ll N = 3e5 + 30;
const ll Mod = 1e14 + 31;
const ll B = 7271;

ll get(ll x, ll y) {
	return (x * B + y) % Mod;
}

ll a[N];
ll solve(ll l, ll r) {  //  [ )
	if (r <= l || r == l + 1) return 0;
	if (r - l == 2 && a[l] == a[l + 1]) return 1;
	ll mid = (l + r) >> 1;
	ll res = solve(l, mid) + solve(mid, r);
	stack<ll> st, s;
	vector<ll> V;
	ll cur = 0;
	for (int i = mid - 1; i >= l; i --) {
		if (!st.size() || st.top() != a[i])
			st.push(a[i]), cur = get(cur, a[i]), s.push(cur);
		else {
			st.pop();
			if (s.size()) s.pop();
			if (s.size()) cur = s.top();
			else cur = 0;
		}
		V.pb(cur);
	}
	sort(all(V));
	while (st.size()) st.pop();
	while (s.size()) s.pop();
	cur = 0;
	for (int i = mid; i < r; i ++) {
		if (!st.size() || st.top() != a[i])
			st.push(a[i]), cur = get(cur, a[i]), s.push(cur);
		else {
			st.pop();
			if (s.size()) s.pop();
			if (s.size()) cur = s.top();
			else cur = 0;
		}
		res += upper_bound(all(V), cur) - lower_bound(all(V), cur);
	}
	return res;
}
int main () {
    ios::sync_with_stdio(0), cin.tie(0);
	ll q;
	cin >> q;
	while (q --) {
		ll n;
		cin >> n;
		for (int i = 0; i < n; i ++)
			cin >> a[i];
		cout << solve(0, n) << '\n';
	}
    return (0);
}


#include <bits/stdc++.h>
#define pb push_back
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;

const ll N = 3e5 + 30;
const ll Mod = 77777677777;
const ll Base =  7778777;

ll get(ll x, ll y) {
	return (x * Base + y) % Mod;
}

ll a[N];
ll solve(ll l, ll r) {  //  [ )
	if (r <= l || r == l + 1) return 0;
	if (r - l == 2 && a[l] == a[l + 1]) return 1;
	if (r - l == 2 && a[l] != a[l + 1]) return 0;
	ll mid = (l + r) >> 1;
	ll res = solve(l, mid) + solve(mid, r);
	stack<ll> st, s;
	vector<ll> V;
	ll cur = 0;
	for (int i = mid - 1; i >= l; i --) {
		if (!st.size() || st.top() != a[i])
			st.push(a[i]), cur = get(cur, a[i]), s.push(cur);
		else {
			st.pop();
			if (s.size()) s.pop();
			if (s.size()) cur = s.top();
			else cur = 0;
		}
		V.pb(cur);
	}
	sort(all(V));
	while (st.size()) st.pop();
	while (s.size()) s.pop();
	cur = 0;
	for (int i = mid; i < r; i ++) {
		if (!st.size() || st.top() != a[i])
			st.push(a[i]), cur = get(cur, a[i]), s.push(cur);
		else {
			st.pop();
			if (s.size()) s.pop();
			if (s.size()) cur = s.top();
			else cur = 0;
		}
		res += upper_bound(all(V), cur) - lower_bound(all(V), cur);
	}
	return res;
}
int main () {
    ios::sync_with_stdio(0), cin.tie(0);
	ll q;
	cin >> q;
	while (q --) {
		ll n;
		cin >> n;
		for (int i = 0; i < n; i ++)
			cin >> a[i];
		cout << solve(0, n) << '\n';
	}
    return (0);
}


#include <bits/stdc++.h>
#define pb push_back
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;

const ll N = 3e5 + 30;
const ll Mod = 1e14 + 31;
const ll B = 7271;

ll get(ll x, ll y) {
	return (x * B + y) % Mod;
}

ll a[N];
ll solve(ll l, ll r) {  //  [ )
	if (r <= l || r == l + 1) return 0;
	if (r - l == 2 && a[l] == a[l + 1]) return 1;
	if (r - l == 2 && a[l] != a[l + 1]) return 0;
	ll mid = (l + r) >> 1;
	ll res = solve(l, mid) + solve(mid, r);
	stack<ll> st, s;
	vector<ll> V;
	ll cur = 0;
	for (int i = mid - 1; i >= l; i --) {
		if (!st.size() || st.top() != a[i])
			st.push(a[i]), cur = get(cur, a[i]), s.push(cur);
		else {
			st.pop();
			if (s.size()) s.pop();
			if (s.size()) cur = s.top();
			else cur = 0;
		}
		V.pb(cur);
	}
	sort(all(V));
	while (st.size()) st.pop();
	while (s.size()) s.pop();
	cur = 0;
	for (int i = mid; i < r; i ++) {
		if (!st.size() || st.top() != a[i])
			st.push(a[i]), cur = get(cur, a[i]), s.push(cur);
		else {
			st.pop();
			if (s.size()) s.pop();
			if (s.size()) cur = s.top();
			else cur = 0;
		}
		res += upper_bound(all(V), cur) - lower_bound(all(V), cur);
	}
	return res;
}
int main () {
    ios::sync_with_stdio(0), cin.tie(0);
	ll q;
	cin >> q;
	while (q --) {
		ll n;
		cin >> n;
		for (int i = 0; i < n; i ++)
			cin >> a[i];
		cout << solve(0, n) << '\n';
	}
    return (0);
}


#include <bits/stdc++.h>
#define pb push_back
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;

const ll N = 3e5 + 30;
const ll Mod  = 1111111111111111111;
const ll Base = 1111111111111111111;

ll get(ll x, ll y) {
	return (x * Base + y);
}

ll a[N];
ll solve(ll l, ll r) {  //  [ )
	if (r <= l || r == l + 1) return 0;
	if (r - l == 2 && a[l] == a[l + 1]) return 1;
	if (r - l == 2 && a[l] != a[l + 1]) return 0;
	ll mid = (l + r) >> 1;
	ll res = solve(l, mid) + solve(mid, r);
	stack<ll> st, s;
	vector<ll> V;
	ll cur = 0;
	for (int i = mid - 1; i >= l; i --) {
		if (!st.size() || st.top() != a[i])
			st.push(a[i]), cur = get(cur, a[i]), s.push(cur);
		else {
			st.pop();
			if (s.size()) s.pop();
			if (s.size()) cur = s.top();
			else cur = 0;
		}
		V.pb(cur);
	}
	sort(all(V));
	while (st.size()) st.pop();
	while (s.size()) s.pop();
	cur = 0;
	for (int i = mid; i < r; i ++) {
		if (!st.size() || st.top() != a[i])
			st.push(a[i]), cur = get(cur, a[i]), s.push(cur);
		else {
			st.pop();
			if (s.size()) s.pop();
			if (s.size()) cur = s.top();
			else cur = 0;
		}
		res += upper_bound(all(V), cur) - lower_bound(all(V), cur);
	}
	return res;
}
int main () {
    ios::sync_with_stdio(0), cin.tie(0);
	ll q;
	cin >> q;
	while (q --) {
		ll n;
		cin >> n;
		for (int i = 0; i < n; i ++)
			cin >> a[i];
		cout << solve(0, n) << '\n';
	}
    return (0);
}


#include <bits/stdc++.h>
#define pb push_back
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;

const ll N = 3e5 + 30;
const ll Mod = 99999199999;
const ll Base =  3337333;

ll get(ll x, ll y) {
	return (x * Base + y) % Mod;
}

ll a[N];
ll solve(ll l, ll r) {  //  [ )
	if (r <= l || r == l + 1) return 0;
	if (r - l == 2 && a[l] == a[l + 1]) return 1;
	if (r - l == 2 && a[l] != a[l + 1]) return 0;
	ll mid = (l + r) >> 1;
	ll res = solve(l, mid) + solve(mid, r);
	stack<ll> st, s;
	vector<ll> V;
	ll cur = 0;
	for (int i = mid - 1; i >= l; i --) {
		if (!st.size() || st.top() != a[i])
			st.push(a[i]), cur = get(cur, a[i]), s.push(cur);
		else {
			st.pop();
			if (s.size()) s.pop();
			if (s.size()) cur = s.top();
			else cur = 0;
		}
		V.pb(cur);
	}
	sort(all(V));
	while (st.size()) st.pop();
	while (s.size()) s.pop();
	cur = 0;
	for (int i = mid; i < r; i ++) {
		if (!st.size() || st.top() != a[i])
			st.push(a[i]), cur = get(cur, a[i]), s.push(cur);
		else {
			st.pop();
			if (s.size()) s.pop();
			if (s.size()) cur = s.top();
			else cur = 0;
		}
		res += upper_bound(all(V), cur) - lower_bound(all(V), cur);
	}
	return res;
}
int main () {
    ios::sync_with_stdio(0), cin.tie(0);
	ll q;
	cin >> q;
	while (q --) {
		ll n;
		cin >> n;
		for (int i = 0; i < n; i ++)
			cin >> a[i];
		cout << solve(0, n) << '\n';
	}
    return (0);
}


#include <bits/stdc++.h>
#define pb push_back
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;

const ll N = 3e5 + 30;
const ll Mod = 1e12 + 61;
const ll B = 4000039;

ll get(ll x, ll y) {
	return (x * B + y) % Mod;
}

ll a[N];
ll solve(ll l, ll r) {  //  [ )
	if (r <= l || r == l + 1) return 0;
	if (r - l == 2 && a[l] == a[l + 1]) return 1;
	if (r - l == 2 && a[l] != a[l + 1]) return 0;
	ll mid = (l + r) >> 1;
	ll res = solve(l, mid) + solve(mid, r);
	stack<ll> st, s;
	vector<ll> V;
	ll cur = 0;
	for (int i = mid - 1; i >= l; i --) {
		if (!st.size() || st.top() != a[i])
			st.push(a[i]), cur = get(cur, a[i]), s.push(cur);
		else {
			st.pop();
			if (s.size()) s.pop();
			if (s.size()) cur = s.top();
			else cur = 0;
		}
		V.pb(cur);
	}
	sort(all(V));
	while (st.size()) st.pop();
	while (s.size()) s.pop();
	cur = 0;
	for (int i = mid; i < r; i ++) {
		if (!st.size() || st.top() != a[i])
			st.push(a[i]), cur = get(cur, a[i]), s.push(cur);
		else {
			st.pop();
			if (s.size()) s.pop();
			if (s.size()) cur = s.top();
			else cur = 0;
		}
		res += upper_bound(all(V), cur) - lower_bound(all(V), cur);
	}
	return res;
}
int main () {
    ios::sync_with_stdio(0), cin.tie(0);
	ll q;
	cin >> q;
	while (q --) {
		ll n;
		cin >> n;
		for (int i = 0; i < n; i ++)
			cin >> a[i];
		cout << solve(0, n) << '\n';
	}
    return (0);
}


#include <bits/stdc++.h>
#define pb push_back
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;

const ll N = 3e5 + 30;
const ll Mod = 99999199999;
const ll Base =  3331333;

ll get(ll x, ll y) {
	return (x * Base + y) % Mod;
}

ll a[N];
ll solve(ll l, ll r) {  //  [ )
	if (r <= l || r == l + 1) return 0;
	if (r - l == 2 && a[l] == a[l + 1]) return 1;
	if (r - l == 2 && a[l] != a[l + 1]) return 0;
	ll mid = (l + r) >> 1;
	ll res = solve(l, mid) + solve(mid, r);
	stack<ll> st, s;
	vector<ll> V;
	ll cur = 0;
	for (int i = mid - 1; i >= l; i --) {
		if (!st.size() || st.top() != a[i])
			st.push(a[i]), cur = get(cur, a[i]), s.push(cur);
		else {
			st.pop();
			if (s.size()) s.pop();
			if (s.size()) cur = s.top();
			else cur = 0;
		}
		V.pb(cur);
	}
	sort(all(V));
	while (st.size()) st.pop();
	while (s.size()) s.pop();
	cur = 0;
	for (int i = mid; i < r; i ++) {
		if (!st.size() || st.top() != a[i])
			st.push(a[i]), cur = get(cur, a[i]), s.push(cur);
		else {
			st.pop();
			if (s.size()) s.pop();
			if (s.size()) cur = s.top();
			else cur = 0;
		}
		res += upper_bound(all(V), cur) - lower_bound(all(V), cur);
	}
	return res;
}
int main () {
    ios::sync_with_stdio(0), cin.tie(0);
	ll q;
	cin >> q;
	while (q --) {
		ll n;
		cin >> n;
		for (int i = 0; i < n; i ++)
			cin >> a[i];
		cout << solve(0, n) << '\n';
	}
    return (0);
}


#include <bits/stdc++.h>
#define pb push_back
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;

const ll N = 3e5 + 30;
const ll Mod = 1e14 + 99;
const ll B = 7271;

ll get(ll x, ll y) {
	return (x * B + y) % Mod;
}

ll a[N];
ll solve(ll l, ll r) {  //  [ )
	if (r <= l || r == l + 1) return 0;
	if (r - l == 2 && a[l] == a[l + 1]) return 1;
	if (r - l == 2 && a[l] != a[l + 1]) return 0;
	ll mid = (l + r) >> 1;
	ll res = solve(l, mid) + solve(mid, r);
	stack<ll> st, s;
	vector<ll> V;
	ll cur = 0;
	for (int i = mid - 1; i >= l; i --) {
		if (!st.size() || st.top() != a[i])
			st.push(a[i]), cur = get(cur, a[i]), s.push(cur);
		else {
			st.pop();
			if (s.size()) s.pop();
			if (s.size()) cur = s.top();
			else cur = 0;
		}
		V.pb(cur);
	}
	sort(all(V));
	while (st.size()) st.pop();
	while (s.size()) s.pop();
	cur = 0;
	for (int i = mid; i < r; i ++) {
		if (!st.size() || st.top() != a[i])
			st.push(a[i]), cur = get(cur, a[i]), s.push(cur);
		else {
			st.pop();
			if (s.size()) s.pop();
			if (s.size()) cur = s.top();
			else cur = 0;
		}
		res += upper_bound(all(V), cur) - lower_bound(all(V), cur);
	}
	return res;
}
int main () {
    ios::sync_with_stdio(0), cin.tie(0);
	ll q;
	cin >> q;
	while (q --) {
		ll n;
		cin >> n;
		for (int i = 0; i < n; i ++)
			cin >> a[i];
		cout << solve(0, n) << '\n';
	}
    return (0);
}


#include <bits/stdc++.h>
#define pb push_back
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;

const ll N = 3e5 + 30;
const ll Mod = 1111111111111111111;
const ll Base =  3;

ll get(ll x, ll y) {
	return (x * Base + y) % Mod;
}

ll a[N];
ll solve(ll l, ll r) {  //  [ )
	if (r <= l || r == l + 1) return 0;
	if (r - l == 2 && a[l] == a[l + 1]) return 1;
	if (r - l == 2 && a[l] != a[l + 1]) return 0;
	ll mid = (l + r) >> 1;
	ll res = solve(l, mid) + solve(mid, r);
	stack<ll> st, s;
	vector<ll> V;
	ll cur = 0;
	for (int i = mid - 1; i >= l; i --) {
		if (!st.size() || st.top() != a[i])
			st.push(a[i]), cur = get(cur, a[i]), s.push(cur);
		else {
			st.pop();
			if (s.size()) s.pop();
			if (s.size()) cur = s.top();
			else cur = 0;
		}
		V.pb(cur);
	}
	sort(all(V));
	while (st.size()) st.pop();
	while (s.size()) s.pop();
	cur = 0;
	for (int i = mid; i < r; i ++) {
		if (!st.size() || st.top() != a[i])
			st.push(a[i]), cur = get(cur, a[i]), s.push(cur);
		else {
			st.pop();
			if (s.size()) s.pop();
			if (s.size()) cur = s.top();
			else cur = 0;
		}
		res += upper_bound(all(V), cur) - lower_bound(all(V), cur);
	}
	return res;
}
int main () {
    ios::sync_with_stdio(0), cin.tie(0);
	ll q;
	cin >> q;
	while (q --) {
		ll n;
		cin >> n;
		for (int i = 0; i < n; i ++)
			cin >> a[i];
		cout << solve(0, n) << '\n';
	}
    return (0);
}


#include <bits/stdc++.h>
#define pb push_back
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;

const ll N = 3e5 + 30;
const ll Mod = 1e12 + 39;
const ll B = (1 << 19) - 1;

ll get(ll x, ll y) {
	return (x * B + y) % Mod;
}

ll a[N];
ll solve(ll l, ll r) {  //  [ )
	if (r <= l || r == l + 1) return 0;
	if (r - l == 2 && a[l] == a[l + 1]) return 1;
	if (r - l == 2 && a[l] != a[l + 1]) return 0;
	ll mid = (l + r) >> 1;
	ll res = solve(l, mid) + solve(mid, r);
	stack<ll> st, s;
	vector<ll> V;
	ll cur = 0;
	for (int i = mid - 1; i >= l; i --) {
		if (!st.size() || st.top() != a[i])
			st.push(a[i]), cur = get(cur, a[i]), s.push(cur);
		else {
			st.pop();
			if (s.size()) s.pop();
			if (s.size()) cur = s.top();
			else cur = 0;
		}
		V.pb(cur);
	}
	sort(all(V));
	while (st.size()) st.pop();
	while (s.size()) s.pop();
	cur = 0;
	for (int i = mid; i < r; i ++) {
		if (!st.size() || st.top() != a[i])
			st.push(a[i]), cur = get(cur, a[i]), s.push(cur);
		else {
			st.pop();
			if (s.size()) s.pop();
			if (s.size()) cur = s.top();
			else cur = 0;
		}
		res += upper_bound(all(V), cur) - lower_bound(all(V), cur);
	}
	return res;
}
int main () {
    ios::sync_with_stdio(0), cin.tie(0);
	ll q;
	cin >> q;
	while (q --) {
		ll n;
		cin >> n;
		for (int i = 0; i < n; i ++)
			cin >> a[i];
		cout << solve(0, n) << '\n';
	}
    return (0);
}


#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;

const ll N = 3e5 + 30, Mod = 1e9 + 7;
const ll SQ = 330;

ll a[N];
ll solve(ll l, ll r) {  //  [ )
	if (r <= l || r == l + 1) return 0;
	if (r - l == 2 && a[l] == a[l + 1]) return 1;
	ll mid = (l + r) >> 1;
	ll res = solve(l, mid) + solve(mid, r);
	map<stack<ll>, ll> cnt;
	stack<ll> st;
	for (int i = mid - 1; i >= l; i --) {
		if (!st.size() || st.top() != a[i])
			st.push(a[i]);
		else st.pop();
		cnt[st] ++;
	}
	while (st.size()) st.pop();
	for (int i = mid; i < r; i ++) {
		if (!st.size() || st.top() != a[i])
			st.push(a[i]);
		else st.pop();
		res += cnt[st];
	}
	return res;
}
int main () {
    ios::sync_with_stdio(0), cin.tie(0);
	ll q;
	cin >> q;
	while (q --) {
		ll n;
		cin >> n;
		for (int i = 0; i < n; i ++)
			cin >> a[i];
		cout << solve(0, n) << '\n';
	}
    return (0);
}


#include <bits/stdc++.h>
#define pb push_back
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;

const ll N = 3e5 + 30;
const ll Mod = 99999199999;
const ll Base = 4000039;

ll get(ll x, ll y) {
	return (x * Base + y) % Mod;
}

ll a[N];
ll solve(ll l, ll r) {  //  [ )
	if (r <= l || r == l + 1) return 0;
	if (r - l == 2 && a[l] == a[l + 1]) return 1;
	if (r - l == 2 && a[l] != a[l + 1]) return 0;
	ll mid = (l + r) >> 1;
	ll res = solve(l, mid) + solve(mid, r);
	stack<ll> st, s;
	vector<ll> V;
	ll cur = 0;
	for (int i = mid - 1; i >= l; i --) {
		if (!st.size() || st.top() != a[i])
			st.push(a[i]), cur = get(cur, a[i]), s.push(cur);
		else {
			st.pop();
			if (s.size()) s.pop();
			if (s.size()) cur = s.top();
			else cur = 0;
		}
		V.pb(cur);
	}
	sort(all(V));
	while (st.size()) st.pop();
	while (s.size()) s.pop();
	cur = 0;
	for (int i = mid; i < r; i ++) {
		if (!st.size() || st.top() != a[i])
			st.push(a[i]), cur = get(cur, a[i]), s.push(cur);
		else {
			st.pop();
			if (s.size()) s.pop();
			if (s.size()) cur = s.top();
			else cur = 0;
		}
		res += upper_bound(all(V), cur) - lower_bound(all(V), cur);
	}
	return res;
}
int main () {
    ios::sync_with_stdio(0), cin.tie(0);
	ll q;
	cin >> q;
	while (q --) {
		ll n;
		cin >> n;
		for (int i = 0; i < n; i ++)
			cin >> a[i];
		cout << solve(0, n) << '\n';
	}
    return (0);
}


#include <bits/stdc++.h>
#define pb push_back
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;

const ll N = 3e5 + 30;
const ll Mod = 1e12 + 39;
const ll B = 4000039;

ll get(ll x, ll y) {
	return (x * B + y) % Mod;
}

ll a[N];
ll solve(ll l, ll r) {  //  [ )
	if (r <= l || r == l + 1) return 0;
	if (r - l == 2 && a[l] == a[l + 1]) return 1;
	if (r - l == 2 && a[l] != a[l + 1]) return 0;
	ll mid = (l + r) >> 1;
	ll res = solve(l, mid) + solve(mid, r);
	stack<ll> st, s;
	vector<ll> V;
	ll cur = 0;
	for (int i = mid - 1; i >= l; i --) {
		if (!st.size() || st.top() != a[i])
			st.push(a[i]), cur = get(cur, a[i]), s.push(cur);
		else {
			st.pop();
			if (s.size()) s.pop();
			if (s.size()) cur = s.top();
			else cur = 0;
		}
		V.pb(cur);
	}
	sort(all(V));
	while (st.size()) st.pop();
	while (s.size()) s.pop();
	cur = 0;
	for (int i = mid; i < r; i ++) {
		if (!st.size() || st.top() != a[i])
			st.push(a[i]), cur = get(cur, a[i]), s.push(cur);
		else {
			st.pop();
			if (s.size()) s.pop();
			if (s.size()) cur = s.top();
			else cur = 0;
		}
		res += upper_bound(all(V), cur) - lower_bound(all(V), cur);
	}
	return res;
}
int main () {
    ios::sync_with_stdio(0), cin.tie(0);
	ll q;
	cin >> q;
	while (q --) {
		ll n;
		cin >> n;
		for (int i = 0; i < n; i ++)
			cin >> a[i];
		cout << solve(0, n) << '\n';
	}
    return (0);
}


#include <bits/stdc++.h>
#define pb push_back
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;

const ll N = 3e5 + 30;
const ll Mod = 77777677777;
const ll Base = 4000039;

ll get(ll x, ll y) {
	return (x * Base + y) % Mod;
}

ll a[N];
ll solve(ll l, ll r) {  //  [ )
	if (r <= l || r == l + 1) return 0;
	if (r - l == 2 && a[l] == a[l + 1]) return 1;
	if (r - l == 2 && a[l] != a[l + 1]) return 0;
	ll mid = (l + r) >> 1;
	ll res = solve(l, mid) + solve(mid, r);
	stack<ll> st, s;
	vector<ll> V;
	ll cur = 0;
	for (int i = mid - 1; i >= l; i --) {
		if (!st.size() || st.top() != a[i])
			st.push(a[i]), cur = get(cur, a[i]), s.push(cur);
		else {
			st.pop();
			if (s.size()) s.pop();
			if (s.size()) cur = s.top();
			else cur = 0;
		}
		V.pb(cur);
	}
	sort(all(V));
	while (st.size()) st.pop();
	while (s.size()) s.pop();
	cur = 0;
	for (int i = mid; i < r; i ++) {
		if (!st.size() || st.top() != a[i])
			st.push(a[i]), cur = get(cur, a[i]), s.push(cur);
		else {
			st.pop();
			if (s.size()) s.pop();
			if (s.size()) cur = s.top();
			else cur = 0;
		}
		res += upper_bound(all(V), cur) - lower_bound(all(V), cur);
	}
	return res;
}
int main () {
    ios::sync_with_stdio(0), cin.tie(0);
	ll q;
	cin >> q;
	while (q --) {
		ll n;
		cin >> n;
		for (int i = 0; i < n; i ++)
			cin >> a[i];
		cout << solve(0, n) << '\n';
	}
    return (0);
}


#include <bits/stdc++.h>
#define pb push_back
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;

const ll N = 3e5 + 30;
const ll Mod = 1e14 + 31;
const ll B = 727;

ll get(ll x, ll y) {
	return (x * B + y) % Mod;
}

ll a[N];
ll solve(ll l, ll r) {  //  [ )
	if (r <= l || r == l + 1) return 0;
	if (r - l == 2 && a[l] == a[l + 1]) return 1;
	ll mid = (l + r) >> 1;
	ll res = solve(l, mid) + solve(mid, r);
	stack<ll> st, s;
	vector<ll> V;
	ll cur = 0;
	for (int i = mid - 1; i >= l; i --) {
		if (!st.size() || st.top() != a[i])
			st.push(a[i]), cur = get(cur, a[i]), s.push(cur);
		else {
			st.pop();
			if (s.size()) s.pop();
			if (s.size()) cur = s.top();
			else cur = 0;
		}
		V.pb(cur);
	}
	sort(all(V));
	while (st.size()) st.pop();
	while (s.size()) s.pop();
	cur = 0;
	for (int i = mid; i < r; i ++) {
		if (!st.size() || st.top() != a[i])
			st.push(a[i]), cur = get(cur, a[i]), s.push(cur);
		else {
			st.pop();
			if (s.size()) s.pop();
			if (s.size()) cur = s.top();
			else cur = 0;
		}
		res += upper_bound(all(V), cur) - lower_bound(all(V), cur);
	}
	return res;
}
int main () {
    ios::sync_with_stdio(0), cin.tie(0);
	ll q;
	cin >> q;
	while (q --) {
		ll n;
		cin >> n;
		for (int i = 0; i < n; i ++)
			cin >> a[i];
		cout << solve(0, n) << '\n';
	}
    return (0);
}


#include <bits/stdc++.h>
#define pb push_back
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;

const ll N = 3e5 + 30;
const ll Mod = 1e15 + 37;
const ll B = 727;

ll get(ll x, ll y) {
	return (x * B + y) % Mod;
}

ll a[N];
ll solve(ll l, ll r) {  //  [ )
	if (r <= l || r == l + 1) return 0;
	if (r - l == 2 && a[l] == a[l + 1]) return 1;
	if (r - l == 2 && a[l] != a[l + 1]) return 0;
	ll mid = (l + r) >> 1;
	ll res = solve(l, mid) + solve(mid, r);
	stack<ll> st, s;
	vector<ll> V;
	ll cur = 0;
	for (int i = mid - 1; i >= l; i --) {
		if (!st.size() || st.top() != a[i])
			st.push(a[i]), cur = get(cur, a[i]), s.push(cur);
		else {
			st.pop();
			if (s.size()) s.pop();
			if (s.size()) cur = s.top();
			else cur = 0;
		}
		V.pb(cur);
	}
	sort(all(V));
	while (st.size()) st.pop();
	while (s.size()) s.pop();
	cur = 0;
	for (int i = mid; i < r; i ++) {
		if (!st.size() || st.top() != a[i])
			st.push(a[i]), cur = get(cur, a[i]), s.push(cur);
		else {
			st.pop();
			if (s.size()) s.pop();
			if (s.size()) cur = s.top();
			else cur = 0;
		}
		res += upper_bound(all(V), cur) - lower_bound(all(V), cur);
	}
	return res;
}
int main () {
    ios::sync_with_stdio(0), cin.tie(0);
	ll q;
	cin >> q;
	while (q --) {
		ll n;
		cin >> n;
		for (int i = 0; i < n; i ++)
			cin >> a[i];
		cout << solve(0, n) << '\n';
	}
    return (0);
}


#include <bits/stdc++.h>
#define pb push_back
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;

const ll N = 3e5 + 30;
const ll Mod = 1e12 + 91;
const ll B = 4000039;

ll get(ll x, ll y) {
	return (x * B + y) % Mod;
}

ll a[N];
ll solve(ll l, ll r) {  //  [ )
	if (r <= l || r == l + 1) return 0;
	if (r - l == 2 && a[l] == a[l + 1]) return 1;
	if (r - l == 2 && a[l] != a[l + 1]) return 0;
	ll mid = (l + r) >> 1;
	ll res = solve(l, mid) + solve(mid, r);
	stack<ll> st, s;
	vector<ll> V;
	ll cur = 0;
	for (int i = mid - 1; i >= l; i --) {
		if (!st.size() || st.top() != a[i])
			st.push(a[i]), cur = get(cur, a[i]), s.push(cur);
		else {
			st.pop();
			if (s.size()) s.pop();
			if (s.size()) cur = s.top();
			else cur = 0;
		}
		V.pb(cur);
	}
	sort(all(V));
	while (st.size()) st.pop();
	while (s.size()) s.pop();
	cur = 0;
	for (int i = mid; i < r; i ++) {
		if (!st.size() || st.top() != a[i])
			st.push(a[i]), cur = get(cur, a[i]), s.push(cur);
		else {
			st.pop();
			if (s.size()) s.pop();
			if (s.size()) cur = s.top();
			else cur = 0;
		}
		res += upper_bound(all(V), cur) - lower_bound(all(V), cur);
	}
	return res;
}
int main () {
    ios::sync_with_stdio(0), cin.tie(0);
	ll q;
	cin >> q;
	while (q --) {
		ll n;
		cin >> n;
		for (int i = 0; i < n; i ++)
			cin >> a[i];
		cout << solve(0, n) << '\n';
	}
    return (0);
}


#include<bits/stdc++.h>

using namespace std;

#define ll  long long
#define N    300001

vector<int> g[N];

int a[N];
int f[N];

ll  dfs(int u,int d)    {
    ll  ans = d;
    a[u] = 1;
    for(int v : g[u])
        ans += dfs(v,d + 1);
    return  ans;
}

ll  work()  {
    int n;  cin >> n;

    for(int i = 1 ; i <= n ; ++i)   {
        cin >> a[i];
        for(f[i] = i - 1 ; 0 < f[i] ; f[i] = f[f[i]] - 1)
            if (a[f[i]] == a[i])    break;
    }
    for(int i = 0 ; i <= n ; ++i)
        a[i] = 0,
        g[i].clear();
    for(int i = 1 ; i <= n ; ++i)   if (f[i])
        g[f[i] - 1].push_back(i);

    ll  ans = 0;

    for(int i = 0 ; i <= n ; ++i)   if(!a[i])
        ans += dfs(i,0);

    return  ans;
}

int main()  {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int q;  cin >> q;

    while (q--)
        cout << work() << "\n";
}


#include<bits/stdc++.h>

using namespace std;

#define ll  long long
#define N    300001

int a[N];
int f[N];
map<int,int>    F[N];

ll  work()  {
    int n;  cin >> n;

    for(int i = 1 ; i <= n ; ++i)   {
        F[i].clear();
        f[i] = 0;
        cin >> a[i];
        if (F[i - 1].count(a[i]))   {
            f[i] = F[i - 1][a[i]];
            F[i].swap(F[f[i] - 1]);
            if (f[i] > 1)
                F[i][a[f[i] - 1]] = f[i] - 1;
        }
        F[i][a[i]] = i;
    }

    ll  ans = 0;

    for(int i = 1 ; i <= n ; ++i)   {
        if (f[i])   a[i] = a[f[i] - 1] + 1;
        if(!f[i])   a[i] = 0;
        ans += a[i];
    }

    return  ans;
}

int main()  {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int q;  cin >> q;

    while (q--)
        cout << work() << "\n";
}


#include<bits/stdc++.h>

using namespace std;

#define ll  long long
#define N    300001

int a[N];
int f[N];

ll  work()  {
    int n;  cin >> n;

    for(int i = 1 ; i <= n ; ++i)   {
        cin >> a[i];
        for(f[i] = i - 1 ; f[i] > 0 ; f[i] = f[f[i]] - 1)
            if (a[f[i]] == a[i])    break;

        if (f[i] < 0)
            f[i] = 0;
        assert(f[i] < i);
    }

    ll  ans = 0;

    for(int i = 1 ; i <= n ; ++i)   {
        if (f[i])   a[i] = a[f[i] - 1] + 1;
        if(!f[i])   a[i] = 0;
        ans += a[i];
    }

    return  ans;
}

int main()  {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int q;  cin >> q;

    while (q--)
        cout << work() << "\n";
}


#include<bits/stdc++.h>

using namespace std;

#define ll  long long
#define N    300001

vector<int> g[N];

int a[N];
int f[N];

ll  dfs(int u,int d)    {
    ll  ans = d;
    a[u] = 1;
    for(int v : g[u])
        ans += dfs(v,d + 1);
    return  ans;
}

ll  work()  {
    int n;  cin >> n;

    for(int i = 1 ; i <= n ; ++i)   {
        cin >> a[i];
        for(f[i] = i - 1 ; f[i] > 0 ; f[i] = f[f[i]] - 1)
            if (a[f[i]] == a[i])    break;
    }
    for(int i = 0 ; i <= n ; ++i)
        a[i] = 0,
        g[i].clear();
    for(int i = 1 ; i <= n ; ++i)   if (f[i] > 0)
        g[f[i] - 1].push_back(i);

    ll  ans = 0;

    for(int i = 0 ; i <= n ; ++i)   if(!a[i])
        ans += dfs(i,0);

    return  ans;
}

int main()  {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int q;  cin >> q;

    while (q--)
        cout << work() << "\n";
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
// typedef __int128 ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define FR first
#define SE second
#define MP make_pair
#define PB push_back
#define vc vector
#define db double
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define bin(x) (1ll<<(x))
#define fo(i,l,r) for(int i=(l),I=(r);i<=I;i++)
#define fd(i,r,l) for(int i=(r),I=(l);i>=I;i--)
#define mem(x,val) memset(x,val,sizeof x)
#define Swap(a,b,n) for(int I=0;I<=n;I++) swap(a[I],b[I])
#define PC __builtin_popcountll
#ifdef DEBUG
	#define debug(A,args...) fprintf(stderr,A,##args)
#else
	#define debug(A,args...) printf("")
#endif
#define deb debug("line %d\n",__LINE__)
namespace mine
{
	ll qread()
	{
		ll ans=0,f=1;char c=getchar();
		while(c<'0' or c>'9') {if(c=='-')f=-1;c=getchar();}
		while('0'<=c and c<='9') ans=ans*10+c-'0',c=getchar();
		return ans*f;
	}
	void write(ll num){if(num<0) putchar('-'),num=-num;if(num>=10) write(num/10);putchar('0'+num%10);}
	void write1(ll num){write(num);putchar(' ');}
	void write2(ll num){write(num);putchar('\n');}
	template<typename T>inline bool chmax(T&a,const T&b){return a<b?a=b,1:0;}
	template<typename T>inline bool chmin(T&a,const T&b){return a>b?a=b,1:0;}
	ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}
	bool IN(ll x,ll l,ll r){return l<=x and x<=r;}
	void GG(){puts("-1");exit(0);}

	const db eps=1e-8;
	const int INF=0x3f3f3f3f;
	const int MOD=998244353;
	int mm(const int x){return x>=MOD?x-MOD:x;}
	template<typename T> void add(T &x,const int &y){x=(x+y>=MOD?x+y-MOD:x+y);}
	ll qpower(ll x,ll e,int mod=MOD){ll ans=1;while(e){if(e&1)ans=ans*x%mod;x=x*x%mod;e>>=1;}return ans;}
	ll invm(ll x){return qpower(x,MOD-2);}
	const int MM=5e6+10;
	ll fac[MM],facinv[MM],Inv[MM];ll Comb(int n,int m){return n<0 or n<m?0:fac[n]*facinv[m]%MOD*facinv[n-m]%MOD;}
	void PRE()
	{
		fac[0]=1;fo(i,1,MM-1) fac[i]=fac[i-1]*i%MOD;
		facinv[MM-1]=invm(fac[MM-1]);fd(i,MM-1,1) facinv[i-1]=facinv[i]*i%MOD;
		Inv[1]=1;fo(i,2,MM-1) Inv[i]=(MOD-MOD/i)*Inv[MOD%i]%MOD;
	}
	const int N=1e6+10;
	//------------------FIXED------------------
	int a[N],len[N],dp[N];
	void main()
	{
		fo(T,1,qread())
		{
			int n=qread();fo(i,1,n) a[i]=qread();
			ll ans=0;dp[n]=len[n]=dp[n+1]=0;
			fd(i,n-1,1)
			{
				int j=i+1;while(len[j]>0 and j+len[j]<=n and a[i]!=a[j+len[j]]) j+=len[j];
				if(j+len[j]<=n and a[i]==a[j+len[j]]) len[i]=j+len[j]-i+1; else len[i]=0;
				if(len[i]) dp[i]=dp[i+len[i]]+1; else dp[i]=0;
				ans+=dp[i];
			}write2(ans);
		}
	}
};//变量重名！
signed main()
{
	#ifdef DEBUG
	//freopen("a.in","r",stdin);
	freopen("z.txt","r",stdin);
	//freopen("a.out","w",stdout);
	#endif
	srand(time(0));
	mine::PRE();
	mine::main();
	debug("\n------------------------------------------\nTime: %.2lf s\n",1.0*clock()/CLOCKS_PER_SEC);
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
// typedef __int128 ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define FR first
#define SE second
#define MP make_pair
#define PB push_back
#define vc vector
#define db double
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define bin(x) (1ll<<(x))
#define fo(i,l,r) for(int i=(l),I=(r);i<=I;i++)
#define fd(i,r,l) for(int i=(r),I=(l);i>=I;i--)
#define mem(x,val) memset(x,val,sizeof x)
#define Swap(a,b,n) for(int I=0;I<=n;I++) swap(a[I],b[I])
#define PC __builtin_popcountll
#ifdef DEBUG
	#define debug(A,args...) fprintf(stderr,A,##args)
#else
	#define debug(A,args...) printf("")
#endif
#define deb debug("line %d\n",__LINE__)
namespace mine
{
	ll qread()
	{
		ll ans=0,f=1;char c=getchar();
		while(c<'0' or c>'9') {if(c=='-')f=-1;c=getchar();}
		while('0'<=c and c<='9') ans=ans*10+c-'0',c=getchar();
		return ans*f;
	}
	void write(ll num){if(num<0) putchar('-'),num=-num;if(num>=10) write(num/10);putchar('0'+num%10);}
	void write1(ll num){write(num);putchar(' ');}
	void write2(ll num){write(num);putchar('\n');}
	template<typename T>inline bool chmax(T&a,const T&b){return a<b?a=b,1:0;}
	template<typename T>inline bool chmin(T&a,const T&b){return a>b?a=b,1:0;}
	ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}
	bool IN(ll x,ll l,ll r){return l<=x and x<=r;}
	void GG(){puts("-1");exit(0);}

	const db eps=1e-8;
	const int INF=0x3f3f3f3f;
	const int MOD=998244353;
	int mm(const int x){return x>=MOD?x-MOD:x;}
	template<typename T> void add(T &x,const int &y){x=(x+y>=MOD?x+y-MOD:x+y);}
	ll qpower(ll x,ll e,int mod=MOD){ll ans=1;while(e){if(e&1)ans=ans*x%mod;x=x*x%mod;e>>=1;}return ans;}
	ll invm(ll x){return qpower(x,MOD-2);}
	const int MM=5e6+10;
	ll fac[MM],facinv[MM],Inv[MM];ll Comb(int n,int m){return n<0 or n<m?0:fac[n]*facinv[m]%MOD*facinv[n-m]%MOD;}
	void PRE()
	{
		fac[0]=1;fo(i,1,MM-1) fac[i]=fac[i-1]*i%MOD;
		facinv[MM-1]=invm(fac[MM-1]);fd(i,MM-1,1) facinv[i-1]=facinv[i]*i%MOD;
		Inv[1]=1;fo(i,2,MM-1) Inv[i]=(MOD-MOD/i)*Inv[MOD%i]%MOD;
	}
	const int N=1e6+10;
	//------------------FIXED------------------
	int a[N],len[N],dp[N];
	void main()
	{
		fo(T,1,qread())
		{
			int n=qread();fo(i,1,n) a[i]=qread();
			ll ans=0;dp[n]=len[n]=dp[n+1]=0;
			fd(i,n-1,1)
			{
				int j=i+1;
				if(a[i]!=a[j])
				{
					while(len[j]>0 and j+len[j]<=n and a[i]!=a[j+len[j]]) j+=len[j];
					if(j+len[j]<=n and a[i]==a[j+len[j]]) len[i]=j+len[j]-i+1; else len[i]=0;
				} else len[i]=2;
				if(len[i]) dp[i]=dp[i+len[i]]+1; else dp[i]=0;
				ans+=dp[i];
				debug("i=%d len=%d dp=%d\n",i,len[i],dp[i]);
			}write2(ans);
		}
	}
};//变量重名！
signed main()
{
	#ifdef DEBUG
	//freopen("a.in","r",stdin);
	freopen("z.txt","r",stdin);
	//freopen("a.out","w",stdout);
	#endif
	srand(time(0));
	mine::PRE();
	mine::main();
	debug("\n------------------------------------------\nTime: %.2lf s\n",1.0*clock()/CLOCKS_PER_SEC);
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
// typedef __int128 ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define FR first
#define SE second
#define MP make_pair
#define PB push_back
#define vc vector
#define db double
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define bin(x) (1ll<<(x))
#define fo(i,l,r) for(int i=(l),I=(r);i<=I;i++)
#define fd(i,r,l) for(int i=(r),I=(l);i>=I;i--)
#define mem(x,val) memset(x,val,sizeof x)
#define Swap(a,b,n) for(int I=0;I<=n;I++) swap(a[I],b[I])
#define PC __builtin_popcountll
#ifdef DEBUG
	#define debug(A,args...) fprintf(stderr,A,##args)
#else
	#define debug(A,args...) printf("")
#endif
#define deb debug("line %d\n",__LINE__)
namespace mine
{
	ll qread()
	{
		ll ans=0,f=1;char c=getchar();
		while(c<'0' or c>'9') {if(c=='-')f=-1;c=getchar();}
		while('0'<=c and c<='9') ans=ans*10+c-'0',c=getchar();
		return ans*f;
	}
	void write(ll num){if(num<0) putchar('-'),num=-num;if(num>=10) write(num/10);putchar('0'+num%10);}
	void write1(ll num){write(num);putchar(' ');}
	void write2(ll num){write(num);putchar('\n');}
	template<typename T>inline bool chmax(T&a,const T&b){return a<b?a=b,1:0;}
	template<typename T>inline bool chmin(T&a,const T&b){return a>b?a=b,1:0;}
	ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}
	bool IN(ll x,ll l,ll r){return l<=x and x<=r;}
	void GG(){puts("-1");exit(0);}

	const db eps=1e-8;
	const int INF=0x3f3f3f3f;
	const int MOD=998244353;
	int mm(const int x){return x>=MOD?x-MOD:x;}
	template<typename T> void add(T &x,const int &y){x=(x+y>=MOD?x+y-MOD:x+y);}
	ll qpower(ll x,ll e,int mod=MOD){ll ans=1;while(e){if(e&1)ans=ans*x%mod;x=x*x%mod;e>>=1;}return ans;}
	ll invm(ll x){return qpower(x,MOD-2);}
	const int MM=5e6+10;
	ll fac[MM],facinv[MM],Inv[MM];ll Comb(int n,int m){return n<0 or n<m?0:fac[n]*facinv[m]%MOD*facinv[n-m]%MOD;}
	void PRE()
	{
		fac[0]=1;fo(i,1,MM-1) fac[i]=fac[i-1]*i%MOD;
		facinv[MM-1]=invm(fac[MM-1]);fd(i,MM-1,1) facinv[i-1]=facinv[i]*i%MOD;
		Inv[1]=1;fo(i,2,MM-1) Inv[i]=(MOD-MOD/i)*Inv[MOD%i]%MOD;
	}
	const int N=1e6+10;
	//------------------FIXED------------------
	int a[N],len[N],dp[N];
	void main()
	{
		fo(T,1,qread())
		{
			int n=qread();fo(i,1,n) a[i]=qread();
			ll ans=0;dp[n]=len[n]=dp[n+1]=0;
			fd(i,n-1,1)
			{
				int j=i+1;
				while(len[j]>0 and j+len[j]<=n and a[i]!=a[j+len[j]]) j+=len[j];
				if(a[i]==a[j+len[j]]) len[i]=j+len[j]-i+1; else len[i]=0;
				debug("i=%d len=%d dp=%d\n",i,len[i],dp[i]);
				if(len[i]) dp[i]=dp[i+len[i]]+1; else dp[i]=0;
				ans+=dp[i];
			}write2(ans);
		}
	}
};//变量重名！
signed main()
{
	#ifdef DEBUG
	//freopen("a.in","r",stdin);
	freopen("z.txt","r",stdin);
	//freopen("a.out","w",stdout);
	#endif
	srand(time(0));
	mine::PRE();
	mine::main();
	debug("\n------------------------------------------\nTime: %.2lf s\n",1.0*clock()/CLOCKS_PER_SEC);
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
// typedef __int128 ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define FR first
#define SE second
#define MP make_pair
#define PB push_back
#define vc vector
#define db double
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define bin(x) (1ll<<(x))
#define fo(i,l,r) for(int i=(l),I=(r);i<=I;i++)
#define fd(i,r,l) for(int i=(r),I=(l);i>=I;i--)
#define mem(x,val) memset(x,val,sizeof x)
#define Swap(a,b,n) for(int I=0;I<=n;I++) swap(a[I],b[I])
#define PC __builtin_popcountll
#ifdef DEBUG
	#define debug(A,args...) fprintf(stderr,A,##args)
#else
	#define debug(A,args...) printf("")
#endif
#define deb debug("line %d\n",__LINE__)
namespace mine
{
	ll qread()
	{
		ll ans=0,f=1;char c=getchar();
		while(c<'0' or c>'9') {if(c=='-')f=-1;c=getchar();}
		while('0'<=c and c<='9') ans=ans*10+c-'0',c=getchar();
		return ans*f;
	}
	void write(ll num){if(num<0) putchar('-'),num=-num;if(num>=10) write(num/10);putchar('0'+num%10);}
	void write1(ll num){write(num);putchar(' ');}
	void write2(ll num){write(num);putchar('\n');}
	template<typename T>inline bool chmax(T&a,const T&b){return a<b?a=b,1:0;}
	template<typename T>inline bool chmin(T&a,const T&b){return a>b?a=b,1:0;}
	ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}
	bool IN(ll x,ll l,ll r){return l<=x and x<=r;}
	void GG(){puts("-1");exit(0);}

	const db eps=1e-8;
	const int INF=0x3f3f3f3f;
	const int MOD=998244353;
	int mm(const int x){return x>=MOD?x-MOD:x;}
	template<typename T> void add(T &x,const int &y){x=(x+y>=MOD?x+y-MOD:x+y);}
	ll qpower(ll x,ll e,int mod=MOD){ll ans=1;while(e){if(e&1)ans=ans*x%mod;x=x*x%mod;e>>=1;}return ans;}
	ll invm(ll x){return qpower(x,MOD-2);}
	const int MM=5e6+10;
	ll fac[MM],facinv[MM],Inv[MM];ll Comb(int n,int m){return n<0 or n<m?0:fac[n]*facinv[m]%MOD*facinv[n-m]%MOD;}
	void PRE()
	{
		fac[0]=1;fo(i,1,MM-1) fac[i]=fac[i-1]*i%MOD;
		facinv[MM-1]=invm(fac[MM-1]);fd(i,MM-1,1) facinv[i-1]=facinv[i]*i%MOD;
		Inv[1]=1;fo(i,2,MM-1) Inv[i]=(MOD-MOD/i)*Inv[MOD%i]%MOD;
	}
	const int N=1e6+10;
	//------------------FIXED------------------
	int a[N],nxt[N],dp[N];map<int,int> go[N];
	void main()
	{
		fo(T,1,qread())
		{
			int n=qread();fo(i,1,n) a[i]=qread(),go[i].clear();
			ll ans=0;dp[n]=dp[n+1]=0,nxt[n]=n,go[n][a[n]]=n;
			fd(i,n-1,1)
			{
				if(a[i]!=a[i+1])
				{
					int j=(go[i+1].count(a[i])?go[i+1][a[i]]:n+1);
					if(j<=n) assert(a[i]==a[j]),nxt[i]=j+1; else nxt[i]=i;
				} else nxt[i]=i+2;
				if(nxt[i]!=i) dp[i]=dp[nxt[i]]+1,go[i].swap(go[nxt[i]]); else dp[i]=0;
				ans+=dp[i];go[i][a[i]]=i;
			}write2(ans);
		}
	}
};//变量重名！
signed main()
{
	#ifdef DEBUG
	//freopen("a.in","r",stdin);
	freopen("z.txt","r",stdin);
	//freopen("a.out","w",stdout);
	#endif
	srand(time(0));
	mine::PRE();
	mine::main();
	debug("\n------------------------------------------\nTime: %.2lf s\n",1.0*clock()/CLOCKS_PER_SEC);
}


//This file was created by Zory at 2020.03.30 17:58:25
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
// typedef __int128 ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define FR first
#define SE second
#define MP make_pair
#define PB push_back
#define vc vector
#define db double
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define bin(x) (1ll<<(x))
#define fo(i,l,r) for(int i=(l),I=(r);i<=I;i++)
#define fd(i,r,l) for(int i=(r),I=(l);i>=I;i--)
#define mem(x,val) memset(x,val,sizeof x)
#define Swap(a,b,n) for(int I=0;I<=n;I++) swap(a[I],b[I])
#define PC __builtin_popcountll
#ifdef DEBUG
	#define debug(A,args...) fprintf(stderr,A,##args)
#else
	#define debug(A,args...) printf("")
#endif
#define deb debug("line %d\n",__LINE__)
namespace mine
{
	ll qread()
	{
		ll ans=0,f=1;char c=getchar();
		while(c<'0' or c>'9') {if(c=='-')f=-1;c=getchar();}
		while('0'<=c and c<='9') ans=ans*10+c-'0',c=getchar();
		return ans*f;
	}
	void write(ll num){if(num<0) putchar('-'),num=-num;if(num>=10) write(num/10);putchar('0'+num%10);}
	void write1(ll num){write(num);putchar(' ');}
	void write2(ll num){write(num);putchar('\n');}
	template<typename T>inline bool chmax(T&a,const T&b){return a<b?a=b,1:0;}
	template<typename T>inline bool chmin(T&a,const T&b){return a>b?a=b,1:0;}
	ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}
	bool IN(ll x,ll l,ll r){return l<=x and x<=r;}
	void GG(){puts("-1");exit(0);}

	const db eps=1e-8;
	const int INF=0x3f3f3f3f;
	const int MOD=998244353;
	int mm(const int x){return x>=MOD?x-MOD:x;}
	template<typename T> void add(T &x,const int &y){x=(x+y>=MOD?x+y-MOD:x+y);}
	ll qpower(ll x,ll e,int mod=MOD){ll ans=1;while(e){if(e&1)ans=ans*x%mod;x=x*x%mod;e>>=1;}return ans;}
	ll invm(ll x){return qpower(x,MOD-2);}
	const int MM=5e6+10;
	ll fac[MM],facinv[MM],Inv[MM];ll Comb(int n,int m){return n<0 or n<m?0:fac[n]*facinv[m]%MOD*facinv[n-m]%MOD;}
	void PRE()
	{
		fac[0]=1;fo(i,1,MM-1) fac[i]=fac[i-1]*i%MOD;
		facinv[MM-1]=invm(fac[MM-1]);fd(i,MM-1,1) facinv[i-1]=facinv[i]*i%MOD;
		Inv[1]=1;fo(i,2,MM-1) Inv[i]=(MOD-MOD/i)*Inv[MOD%i]%MOD;
	}
	const int N=1e6+10;
	//随机数
	mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
	ll rd(ll l,ll r){return l+rng()%(r-l+1);}
	//------------------FIXED------------------

	int a[N],nxt[N],dp[N];map<int,int> go[N];
	void main()
	{
		fo(T,1,qread())
		{
			int n=qread();fo(i,1,n) a[i]=qread(),go[i].clear();
			ll ans=0;dp[n]=dp[n+1]=0,nxt[n]=n,go[n][a[n]]=n;
			fd(i,n-1,1)
			{
				if(a[i]!=a[i+1])
				{
					int j=(go[i+1].count(a[i])?go[i+1][a[i]]:n+1);
					if(j<=n) assert(a[i]==a[j]),nxt[i]=j+1; else nxt[i]=i;
				} else nxt[i]=i+2;
				if(nxt[i]!=i) dp[i]=dp[nxt[i]]+1,go[i].swap(go[nxt[i]]); else dp[i]=0;
				ans+=dp[i];go[i][a[i]]=i;
			}write2(ans);
		}
	}
};//变量重名！
signed main()
{
	#ifdef DEBUG
	//freopen("a.in","r",stdin);
	//freopen("z.txt","r",stdin);
	//freopen("a.out","w",stdout);
	#endif
	srand(time(0));
	mine::main();
	debug("\n------------------------------------------\nTime: %.2lf s\n",1.0*clock()/CLOCKS_PER_SEC);
}


// Problem : D. Stack Exterminable Arrays
// Contest : Codeforces Round #591 (Div. 1, based on Technocup 2020 Elimination Round 1)
// URL : https://codeforces.com/contest/1240/problem/D
// Memory Limit : 256 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

//Zory-2020
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
// typedef __int128 ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define FR first
#define SE second
#define MP make_pair
#define PB push_back
#define vc vector
#define db double
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define bin(x) (1ll<<(x))
#define fo(i,l,r) for(int i=(l),I=(r);i<=I;i++)
#define fd(i,r,l) for(int i=(r),I=(l);i>=I;i--)
#define mem(x,val) memset(x,val,sizeof x)
#define Swap(a,b,n) for(int I=0;I<=n;I++) swap(a[I],b[I])
#define PC __builtin_popcountll
#ifdef DEBUG
	#define debug(A,args...) fprintf(stderr,A,##args)
#else
	#define debug(A,args...) printf("")
#endif
#define deb debug("line %d\n",__LINE__)
namespace mine
{
	ll qread()
	{
		ll ans=0,f=1;char c=getchar();
		while(c<'0' or c>'9') {if(c=='-')f=-1;c=getchar();}
		while('0'<=c and c<='9') ans=ans*10+c-'0',c=getchar();
		return ans*f;
	}
	void write(ll num){if(num<0) putchar('-'),num=-num;if(num>=10) write(num/10);putchar('0'+num%10);}
	void write1(ll num){write(num);putchar(' ');}
	void write2(ll num){write(num);putchar('\n');}
	template<typename T>inline bool chmax(T&a,const T&b){return a<b?a=b,1:0;}
	template<typename T>inline bool chmin(T&a,const T&b){return a>b?a=b,1:0;}
	ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}
	bool IN(ll x,ll l,ll r){return l<=x and x<=r;}
	void GG(){puts("-1");exit(0);}

	const db eps=1e-8;
	const int INF=0x3f3f3f3f;
	const int MOD=998244353;
	int mm(const int x){return x>=MOD?x-MOD:x;}
	template<typename T> void add(T &x,const int &y){x=(x+y>=MOD?x+y-MOD:x+y);}
	ll qpower(ll x,ll e,int mod=MOD){ll ans=1;while(e){if(e&1)ans=ans*x%mod;x=x*x%mod;e>>=1;}return ans;}
	ll invm(ll x){return qpower(x,MOD-2);}
	const int MM=5e6+10;
	ll fac[MM],facinv[MM],Inv[MM];ll Comb(int n,int m){return n<0 or n<m?0:fac[n]*facinv[m]%MOD*facinv[n-m]%MOD;}
	void PRE()
	{
		fac[0]=1;fo(i,1,MM-1) fac[i]=fac[i-1]*i%MOD;
		facinv[MM-1]=invm(fac[MM-1]);fd(i,MM-1,1) facinv[i-1]=facinv[i]*i%MOD;
		Inv[1]=1;fo(i,2,MM-1) Inv[i]=(MOD-MOD/i)*Inv[MOD%i]%MOD;
	}
	const int N=1e6+10;
	//随机数
	mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
	ll rd(ll l,ll r){return l+rng()%(r-l+1);}
	//------------------FIXED------------------

	int a[N],nxt[N],dp[N];map<int,int> go[N];
	void main()
	{
		fo(T,1,qread())
		{
			int n=qread();fo(i,1,n) a[i]=qread(),go[i].clear();
			ll ans=0;dp[n]=dp[n+1]=0,nxt[n]=n,go[n][a[n]]=n;
			fd(i,n-1,1)
			{
				if(a[i]!=a[i+1])
				{
					int j=(go[i+1].count(a[i])?go[i+1][a[i]]:n+1);
					if(j<=n) assert(a[i]==a[j]),nxt[i]=j+1; else nxt[i]=i;
				} else nxt[i]=i+2;
				if(nxt[i]!=i) dp[i]=dp[nxt[i]]+1,go[i].swap(go[nxt[i]]); else dp[i]=0;
				ans+=dp[i];go[i][a[i]]=i;
			}write2(ans);
		}
	}
};//变量重名！
signed main()
{
	#ifdef DEBUG
	//freopen("a.in","r",stdin);
	//freopen("z.txt","r",stdin);
	//freopen("a.out","w",stdout);
	#endif
	srand(time(0));
	mine::main();
	//debug("\n------------------------------------------\nTime: %.2lf s\n",1.0*clock()/CLOCKS_PER_SEC);
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define sz(v) ((ll)v.size())
#define mp make_pair
#define all(a) (a).begin(),(a).end()
#define F first
#define S second
#define pii pair<ll,ll>
const ll mod = 1000000007;
const ll mod1= 1000000009;
const ll base= 10529;
const ll base1=11699 ;
#define MAXN 2000004
#define trace1(x)                cerr << #x << ": " << x << "\n";
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << "\n";
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << "\n";
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << "\n";
ll a[300002];
ll h[300002];
ll ans=0;
void f(ll l,ll r){
  if(l==r) return;
  ll m=(l+r)>>1;
  f(l,m);f(m+1,r);
  map<ll,ll> my;
  ll i;
  stack<ll> st;
  h[m+1]=0;
  for(i=m;i>=l;i--){
   h[i]=(h[i+1]*base1+a[i])%mod;
   if(!st.empty() and a[st.top()]==a[i]) st.pop();
   else st.push(i);
   ll id=0;
   if(!st.empty()) id=h[st.top()];
   my[id]++;
   h[i]=id;
   //cout<<i<<" "<<m<<" "<<id<<endl;
  }
  while(!st.empty()) st.pop();
  h[m]=0;
  for(i=m+1;i<=r;i++){
   h[i]=(h[i-1]*base1+a[i])%mod;
   if(!st.empty() and a[st.top()]==a[i]) st.pop();
   else st.push(i);
   ll id=0;
   if(!st.empty()) id=h[st.top()];
   ans+=my[id];
   h[i]=id;
   //cout<<m+1<<" "<<i<<" "<<id<<endl;
  }
 // cout<<endl;
}
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll q,i;
  cin>>q;
  while(q--){
    ll n;
    cin>>n;
    ans=0;
    for(i=1;i<=n;i++) cin>>a[i];
    f(1,n);
    cout<<ans<<'\n';
  }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define sz(v) ((ll)v.size())
#define mp make_pair
#define all(a) (a).begin(),(a).end()
#define F first
#define S second
#define pii pair<ll,ll>
const ll mod = 1000000007;
const ll mod1= 1000000009;
const ll base= 10529;
const ll base1=11699 ;
#define MAXN 2000004
#define trace1(x)                cerr << #x << ": " << x << "\n";
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << "\n";
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << "\n";
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << "\n";
ll a[300002];pii h[300002];
ll ans=0;
void f(ll l,ll r){
  if(l==r) return;
  ll m=(l+r)>>1;
  f(l,m);f(m+1,r);
  map<pii,ll> my;
  ll i;
  stack<ll> st;
  h[m+1]={0,0};
  for(i=m;i>=l;i--){
   h[i].F=(h[i+1].F*base1+a[i])%mod;
   h[i].S=(h[i+1].S*base+a[i])%mod1;
   if(!st.empty() and a[st.top()]==a[i]) st.pop();
   else st.push(i);
   pii id={0,0};
   if(!st.empty()) id=h[st.top()];
   my[id]++;
   h[i]=id;
   //cout<<i<<" "<<m<<" "<<id<<endl;
  }
  while(!st.empty()) st.pop();
  h[m]={0,0};
  for(i=m+1;i<=r;i++){
  h[i].F=(h[i-1].F*base1+a[i])%mod;
   h[i].S=(h[i-1].S*base+a[i])%mod1;
   if(!st.empty() and a[st.top()]==a[i]) st.pop();
   else st.push(i);
   pii id={0,0};
   if(!st.empty()) id=h[st.top()];
   ans+=my[id];
   h[i]=id;   //cout<<m+1<<" "<<i<<" "<<id<<endl;
  }
 // cout<<endl;
}
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll q,i;
  cin>>q;
  while(q--){
    ll n;
    cin>>n;
    ans=0;
    for(i=1;i<=n;i++) cin>>a[i];
    f(1,n);
    cout<<ans<<'\n';
  }
}


#include <utility>
#include <iostream>
#include <cmath>
#include <deque>
#include <string>
#include <time.h>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <sstream>
#include <algorithm>
#include <bitset>

#define pii pair<ll,ll>
#define X first
#define Y second
#define mp make_pair

typedef long long ll;
using namespace std;
#define MAXN (1<<20)
#define MOD 1000000007
#define N 1048576
ll n, x, k, z, m, pos, q, ans_v;
ll x1, p1, x2, p2;
map<ll, ll> g[600500];
ll f[500500], a[600500], sb[600500], b[600500], p[600500];

void solve() {
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    ll cur = 0;
    ll prv = -1;
    for (int i = 1; i <= 2 * n; i++) {
        g[i].clear();
        p[i] = sb[i] = b[i] = 0;
    }
    ll ans = 0;
    ll sz = 0;
    b[0] = 0;
    for (int i = 0; i < n; i++) {
        if (sb[cur] == a[i]) {
            b[cur]++;
            prv = p[cur];
            cur = prv;
            ans += b[cur];
        } else {
            if (g[cur].find(a[i]) == g[cur].end()) {
                b[cur]++;
                sb[++sz] = a[i];
                p[sz] = cur;
                g[cur][a[i]] = sz;
                cur = sz;
            } else {
                b[cur]++;
                cur = g[cur][a[i]];
                ans += b[cur];
            }
        }
        //cout << ans << endl;
    }
    //cout << "HYU" << endl;
    cout << ans << "\n";
}

/*
3
5
2 1 1 2 2
6
1 2 1 1 3 2
9
3 1 2 2 1 6 6 3 3

 */

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    ll t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}


#include <utility>
#include <iostream>
#include <cmath>
#include <deque>
#include <string>
#include <time.h>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <sstream>
#include <algorithm>
#include <bitset>

#define pii pair<ll,ll>
#define X first
#define Y second
#define mp make_pair

typedef long long ll;
using namespace std;
#define MAXN (1<<20)
#define MOD 1000000007
#define N 1048576
ll n, x, k, z, m, pos, q, ans_v;
ll x1, p1, x2, p2;
ll dp[500500][2];
vector<pii> g[500500];
ll f[500500], a[300500], sb[300500], b[300500], lst[300500];

void solve() {
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }

    for (int i = 0; i <= n+2; i++) {
        sb[i] = 0;
        b[i] = 0;
        lst[i] = -1;
    }
    a[n] = -1;
    ll best = 0, cur = -1, pos = -1, k = 0;
    for (int i = n-1; i >= 0; i--) {
        if (best == 0) {
            if (a[i] != cur) {
                cur = a[i];
                b[i] = 0;
            } else {
                b[i] = 1;
                best = 2;
                pos = i;
                sb[i] = 1;
            }
            continue;
        }
        if (cur == a[i]) {
            if (pos == i+1) {
                best++;
                pos = i;
            } else {
                best = 2;
                pos = i;
                k = 0;
            }
        } else {
            if (pos == i + k + 1 && a[i] == a[pos + best + k]) {
                k++;
            }
        }

        if (best % 2) {
            if (pos == i) {
                b[i] = (best - 1) / 2 + (b[pos + best - 1]);
                lst[i] = pos + best - 1;
            }
        } else {
            if (pos == i + k) {
                if (k == 0) {
                    b[i] = (best / 2) + b[pos + best];
                    lst[i] = pos + best;
                } else {
                    b[i] = 1 + b[pos + best + k];
                    lst[i] = pos + best + k;
                }
            } else {
                ll cur = i + 1;
                for (int j = 0; j < 500; j++) {
                    cur = lst[cur];
                    if (cur == -1) {
                        break;
                    }
                    if (a[cur] == a[i]) {
                        lst[i] = cur + 1;
                        b[i] = 1 + b[cur + 1];
                    }
                }
            }
        }
        sb[i] = sb[i+1] + b[i];
        cur = a[i];
    }
    ll sum = 0;
    for (int i = 0; i < n; i++) {
        sum += b[i];
        //cout << b[i] << " ";
    }
    //cout << endl;
    cout << sum << "\n";
}

/*
3
5
2 1 1 2 2
6
1 2 1 1 3 2
9
3 1 2 2 1 6 6 3 3

 */

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    ll t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}


#include <utility>
#include <iostream>
#include <cmath>
#include <deque>
#include <string>
#include <time.h>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <sstream>
#include <algorithm>
#include <bitset>

#define pii pair<ll,ll>
#define X first
#define Y second
#define mp make_pair

typedef long long ll;
using namespace std;
#define MAXN (1<<20)
#define MOD 1000000007
#define N 1048576
ll n, x, k, z, m, pos, q, ans_v;
ll x1, p1, x2, p2;
map<ll, ll> g[600500];
ll f[500500], a[600500], sb[600500], b[600500], p[600500];

void solve() {
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    ll cur = 0;
    ll prv = -1;
    for (int i = 0; i <= 2 * n; i++) {
        g[i].clear();
        p[i] = sb[i] = b[i] = 0;
    }
    ll ans = 0;
    ll sz = 0;
    b[0] = 0;
    for (int i = 0; i < n; i++) {
        if (sb[cur] == a[i]) {
            b[cur]++;
            prv = p[cur];
            cur = prv;
            ans += b[cur];
        } else {
            if (g[cur].find(a[i]) == g[cur].end()) {
                b[cur]++;
                sb[++sz] = a[i];
                p[sz] = cur;
                g[cur][a[i]] = sz;
                cur = sz;
            } else {
                b[cur]++;
                cur = g[cur][a[i]];
                ans += b[cur];
            }
        }
        //cout << ans << endl;
    }
    //cout << "HYU" << endl;
    cout << ans << "\n";
}

/*
3
5
2 1 1 2 2
6
1 2 1 1 3 2
9
3 1 2 2 1 6 6 3 3

 */

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    ll t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}


#include <utility>
#include <iostream>
#include <cmath>
#include <deque>
#include <string>
#include <time.h>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <sstream>
#include <algorithm>
#include <bitset>

#define pii pair<ll,ll>
#define X first
#define Y second
#define mp make_pair

typedef long long ll;
using namespace std;
#define MAXN (1<<20)
#define MOD 1000000007
#define N 1048576
ll n, x, k, z, m, pos, q, ans_v;
ll x1, p1, x2, p2;
ll dp[500500][2];
vector<pii> g[500500];
ll f[500500], a[300500], sb[300500], b[300500], lst[300500];

void solve() {
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }

    for (int i = 0; i <= n+2; i++) {
        sb[i] = 0;
        b[i] = 0;
        lst[i] = -1;
    }
    a[n] = -1;
    ll best = 0, cur = -1, pos = -1, k = 0;
    for (int i = n-1; i >= 0; i--) {
        if (best == 0) {
            if (a[i] != cur) {
                cur = a[i];
                b[i] = 0;
            } else {
                b[i] = 1;
                best = 2;
                pos = i;
                sb[i] = 1;
                lst[i] = i + 2;
            }
            continue;
        }
        if (cur == a[i]) {
            if (pos == i+1) {
                best++;
                pos = i;
            } else {
                best = 2;
                pos = i;
                k = 0;
            }
        } else {
            if (pos == i + k + 1 && a[i] == a[pos + best + k]) {
                k++;
            }
        }

        if (best % 2) {
            if (pos == i) {
                b[i] = (best - 1) / 2 + (b[pos + best - 1]);
                lst[i] = pos + best - 1;
            }
        } else {
            if (pos == i + k) {
                if (k == 0) {
                    b[i] = (best / 2) + b[pos + best];
                    lst[i] = pos + best;
                } else {
                    b[i] = 1 + b[pos + best + k];
                    lst[i] = pos + best + k;
                }
            } else {
                ll cur = i + 1;
                for (int j = 0; j < 500; j++) {
                    cur = lst[cur];
                    if (cur == -1) {
                        break;
                    }
                    if (a[cur] == a[i]) {
                        lst[i] = cur + 1;
                        b[i] = 1 + b[cur + 1];
                    }
                }
            }
        }
        sb[i] = sb[i+1] + b[i];
        cur = a[i];
    }
    ll sum = 0;
    for (int i = 0; i < n; i++) {
        sum += b[i];
        //cout << b[i] << " " << lst[i] << endl;
    }
    /*cout << endl;*/
    cout << sum << "\n";
}

/*
3
5
2 1 1 2 2
6
1 2 1 1 3 2
9
3 1 2 2 1 6 6 3 3

 */

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    ll t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}


#include <utility>
#include <iostream>
#include <cmath>
#include <deque>
#include <string>
#include <time.h>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <sstream>
#include <algorithm>
#include <bitset>

#define pii pair<ll,ll>
#define X first
#define Y second
#define mp make_pair

typedef long long ll;
using namespace std;
#define MAXN (1<<20)
#define MOD 1000000007
#define N 1048576
ll n, x, k, z, m, pos, q, ans_v;
ll x1, p1, x2, p2;
ll dp[500500][2];
map<ll, ll> g[500500];
ll f[500500], a[300500], sb[300500], b[300500], p[300500];

void solve() {
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    ll cur = 0;
    ll prv = -1;
    for (int i = 1; i <= 2 * n; i++) {
        g[i].clear();
        p[i] = sb[i] = b[i] = 0;
    }
    ll ans = 0;
    ll sz = 0;
    b[0] = 0;
    for (int i = 0; i < n; i++) {
        if (sb[cur] == a[i]) {
            b[cur]++;
            prv = p[cur];
            cur = prv;
            ans += b[cur];
        } else {
            if (g[cur].find(a[i]) == g[cur].end()) {
                b[cur]++;
                sb[++sz] = a[i];
                p[sz] = cur;
                g[cur][a[i]] = sz;
                cur = sz;
            } else {
                b[cur]++;
                cur = g[cur][a[i]];
                ans += b[cur];
            }
        }
        //cout << ans << endl;
    }
    //cout << "HYU" << endl;
    cout << ans << "\n";
}

/*
3
5
2 1 1 2 2
6
1 2 1 1 3 2
9
3 1 2 2 1 6 6 3 3

 */

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    ll t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int nxt[300005];
// m[i][x] = r s.t. a[i:r] is exterminable and a[r+1] == x;
map<int, int> m[300005];
int dp[300005], n;
int a[300005];
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int t;
    cin >> t;
    while(t--)
    {
        cin >> n;
        for(int i=1;i<=n+3;i++)
            m[i].clear();
        for(int i=1;i<=n;i++)
            cin >> a[i];
        // [a] <b c c b> (a) {d c ...}
        ll ans = 0;
        for(int i=n;i;i--)
        {
            if(a[i] == a[i+1])
                m[i+1][a[i]] = i;
            // asking the pos >  where the seq. is [] <> () {}
            int r = m[i+1][a[i]]; // [a] -> pos just before of of () ?
            if(r)
            {
                nxt[i] = r+1; // position of ()
                swap(m[i], m[nxt[i]+1]); // we won't access old map {}
                m[i][a[nxt[i]+1]] = nxt[i]; // definition
            }
            else nxt[i] = 0;
        }
        for(int i=n;i;i--)
        {
            dp[i] = 0;
            if(nxt[i])
            {
                dp[i]++;
                if(nxt[i] <= n) dp[i] += dp[nxt[i]+1];
            }
            ans += dp[i];
        }
        cout << ans << '\n';
    }
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int nxt[300005];
// m[i][x] = r s.t. a[i:r] is exterminable and a[r+1] == x;
map<int, int> m[300005];
int dp[300005], n;
int a[300005];
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    memset(nxt, -1, sizeof(nxt));
    int t;
    cin >> t;
    while(t--)
    {
        cin >> n;
        for(int i=1;i<=n+3;i++)
            m[i].clear();
        for(int i=1;i<=n;i++)
            cin >> a[i];
        // [a] <b c c b> (a) {d c ...}
        ll ans = 0;
        for(int i=n;i;i--)
        {
            if(a[i] == a[i+1])
                m[i+1][a[i]] = i;
            // asking the pos >  where the seq. is [] <> () {}
            int r = m[i+1][a[i]]; // [a] -> pos just before of of () ?
            if(r)
            {
                nxt[i] = r+1; // position of ()
                if(nxt[i] > n) nxt[i] = -1;
                if(~nxt[i]) swap(m[i], m[nxt[i]+1]); // we won't access old map {}
                m[i][a[nxt[i]+1]] = nxt[i]; // definition
            }
            else nxt[i] = -1;
        }
        for(int i=n;i;i--)
        {
            if(~nxt[i])
                dp[i] += 1 + dp[nxt[i]+1];
            ans += dp[i];
        }
        cout << ans << '\n';
        for(int i=1;i<=n;i++)
            dp[i] = 0, nxt[i] = -1;
    }
}


#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
#define uint unsigned int

#if 0
const int MOD = (int) ;

inline int lgput(int a, int b) {
    int ans = 1;
    while(b > 0) {
        if(b & 1) ans = (1LL * ans * a) % MOD;
        b >>= 1;
        a = (1LL * a * a) % MOD;
    }
    return ans;
}

inline void mod(int &x) {
    if(x >= MOD)
        x -= MOD;
}

inline void add(int &x, int y) {
    x += y;
    mod(x);
}

inline void sub(int &x, int y) {
    x += MOD - y;
    mod(x);
}

inline void mul(int &x, int y) {
    x = (1LL * x * y) % MOD;
}

inline int inv(int x) {
    return lgput(x, MOD - 2);
}
#endif

#if 0
int fact[], invfact[];

inline void prec(int n) {
    fact[0] = 1;
    for(int i = 1; i <= n; i++) {
        fact[i] = (1LL * fact[i - 1] * i) % MOD;
    }
    invfact[n] = lgput(fact[n], MOD - 2);
    for(int i = n - 1; i >= 0; i--) {
        invfact[i] = (1LL * invfact[i + 1] * (i + 1)) % MOD;
    }
}

inline int comb(int n, int k) {
    if(n < k) return 0;
    return (1LL * fact[n] * (1LL * invfact[k] * invfact[n - k] % MOD)) % MOD;
}
#endif

using namespace std;

const int MAXN = (int) 3e5 + 5;

map <int, int> nxtX[MAXN + 1];
int nxt[MAXN + 1], a[MAXN + 1];

int main() {
#ifdef HOME
    ifstream cin("A.in");
    ofstream cout("A.out");
#endif
    int i, n, q;
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    cin >> q;

    while(q--) {
        cin >> n;

        for(i = 1; i <= n; i++) {
            cin >> a[i];
            nxtX[i].clear();
        }

        for(i = n; i >= 1; i--) {
            if(a[i] == a[i + 1]) {
                nxt[i] = i + 1;
            }
            else {
                nxt[i] = nxtX[i + 1][a[i]];
            }

            if(nxt[i] != 0) {
                swap(nxtX[i], nxtX[nxt[i] + 1]);
                nxtX[i][a[nxt[i] + 1]] = nxt[i] + 1;
            }
        }

        vector <int> dp(n + 2);
        ll ans = 0;
        for(i = n; i >= 1; i--) {
            if(nxt[i] != 0) {
                dp[i] = 1 + dp[nxt[i] + 1];
            }
            ans += dp[i];
        }

        /*for(i = 1; i <= n; i++) {
            cerr << nxt[i] << " ";
        }
        cerr << "\n";*/

        cout << ans << "\n";
    }

    return 0;
}


#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
#define uint unsigned int

#if 0
const int MOD = (int) ;

inline int lgput(int a, int b) {
    int ans = 1;
    while(b > 0) {
        if(b & 1) ans = (1LL * ans * a) % MOD;
        b >>= 1;
        a = (1LL * a * a) % MOD;
    }
    return ans;
}

inline void mod(int &x) {
    if(x >= MOD)
        x -= MOD;
}

inline void add(int &x, int y) {
    x += y;
    mod(x);
}

inline void sub(int &x, int y) {
    x += MOD - y;
    mod(x);
}

inline void mul(int &x, int y) {
    x = (1LL * x * y) % MOD;
}

inline int inv(int x) {
    return lgput(x, MOD - 2);
}
#endif

#if 0
int fact[], invfact[];

inline void prec(int n) {
    fact[0] = 1;
    for(int i = 1; i <= n; i++) {
        fact[i] = (1LL * fact[i - 1] * i) % MOD;
    }
    invfact[n] = lgput(fact[n], MOD - 2);
    for(int i = n - 1; i >= 0; i--) {
        invfact[i] = (1LL * invfact[i + 1] * (i + 1)) % MOD;
    }
}

inline int comb(int n, int k) {
    if(n < k) return 0;
    return (1LL * fact[n] * (1LL * invfact[k] * invfact[n - k] % MOD)) % MOD;
}
#endif

using namespace std;

const int MAXN = (int) 3e5 + 5;

map <int, int> nxtX[MAXN + 1];
int nxt[MAXN + 1], a[MAXN + 1];

int main() {
#ifdef HOME
    ifstream cin("A.in");
    ofstream cout("A.out");
#endif
    int i, n, q;
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    cin >> q;

    while(q--) {
        cin >> n;

        for(i = 1; i <= n; i++) {
            cin >> a[i];
            nxtX[i].clear();
        }
        nxtX[n + 1].clear();

        for(i = n; i >= 1; i--) {
            if(i < n && a[i] == a[i + 1]) {
                nxt[i] = i + 1;
            }
            else {
                nxt[i] = nxtX[i + 1][a[i]];
            }

            if(nxt[i] != 0) {
                swap(nxtX[i], nxtX[nxt[i] + 1]);
                nxtX[i][a[nxt[i] + 1]] = nxt[i] + 1;
            }
        }

        vector <int> dp(n + 2);
        ll ans = 0;
        for(i = n; i >= 1; i--) {
            if(nxt[i] != 0) {
                dp[i] = 1 + dp[nxt[i] + 1];
            }
            ans += dp[i];
        }

        cout << ans << "\n";
    }

    return 0;
}


#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
#define uint unsigned int

#if 0
const int MOD = (int) ;

inline int lgput(int a, int b) {
    int ans = 1;
    while(b > 0) {
        if(b & 1) ans = (1LL * ans * a) % MOD;
        b >>= 1;
        a = (1LL * a * a) % MOD;
    }
    return ans;
}

inline void mod(int &x) {
    if(x >= MOD)
        x -= MOD;
}

inline void add(int &x, int y) {
    x += y;
    mod(x);
}

inline void sub(int &x, int y) {
    x += MOD - y;
    mod(x);
}

inline void mul(int &x, int y) {
    x = (1LL * x * y) % MOD;
}

inline int inv(int x) {
    return lgput(x, MOD - 2);
}
#endif

#if 0
int fact[], invfact[];

inline void prec(int n) {
    fact[0] = 1;
    for(int i = 1; i <= n; i++) {
        fact[i] = (1LL * fact[i - 1] * i) % MOD;
    }
    invfact[n] = lgput(fact[n], MOD - 2);
    for(int i = n - 1; i >= 0; i--) {
        invfact[i] = (1LL * invfact[i + 1] * (i + 1)) % MOD;
    }
}

inline int comb(int n, int k) {
    if(n < k) return 0;
    return (1LL * fact[n] * (1LL * invfact[k] * invfact[n - k] % MOD)) % MOD;
}
#endif

using namespace std;

const int MAXN = (int) 3e5 + 5;

map <int, int> nxtX[MAXN + 1];
int nxt[MAXN + 1], a[MAXN + 1];

int main() {
#ifdef HOME
    ifstream cin("A.in");
    ofstream cout("A.out");
#endif
    int i, n, q;
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    cin >> q;

    while(q--) {
        cin >> n;

        for(i = 1; i <= n; i++) {
            cin >> a[i];
            nxtX[i].clear();
        }
        nxtX[n + 1].clear();

        for(i = n; i >= 1; i--) {
            if(a[i] == a[i + 1]) {
                nxt[i] = i + 1;
            }
            else {
                nxt[i] = nxtX[i + 1][a[i]];
            }

            if(nxt[i] != 0) {
                swap(nxtX[i], nxtX[nxt[i] + 1]);
                nxtX[i][a[nxt[i] + 1]] = nxt[i] + 1;
            }
        }

        vector <int> dp(n + 2);
        ll ans = 0;
        for(i = n; i >= 1; i--) {
            if(nxt[i] != 0) {
                dp[i] = 1 + dp[nxt[i] + 1];
            }
            ans += dp[i];
        }

        /*for(i = 1; i <= n; i++) {
            cerr << nxt[i] << " ";
        }
        cerr << "\n";*/

        cout << ans << "\n";
    }

    return 0;
}


#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
#define uint unsigned int

#if 0
const int MOD = (int) ;

inline int lgput(int a, int b) {
    int ans = 1;
    while(b > 0) {
        if(b & 1) ans = (1LL * ans * a) % MOD;
        b >>= 1;
        a = (1LL * a * a) % MOD;
    }
    return ans;
}

inline void mod(int &x) {
    if(x >= MOD)
        x -= MOD;
}

inline void add(int &x, int y) {
    x += y;
    mod(x);
}

inline void sub(int &x, int y) {
    x += MOD - y;
    mod(x);
}

inline void mul(int &x, int y) {
    x = (1LL * x * y) % MOD;
}

inline int inv(int x) {
    return lgput(x, MOD - 2);
}
#endif

#if 0
int fact[], invfact[];

inline void prec(int n) {
    fact[0] = 1;
    for(int i = 1; i <= n; i++) {
        fact[i] = (1LL * fact[i - 1] * i) % MOD;
    }
    invfact[n] = lgput(fact[n], MOD - 2);
    for(int i = n - 1; i >= 0; i--) {
        invfact[i] = (1LL * invfact[i + 1] * (i + 1)) % MOD;
    }
}

inline int comb(int n, int k) {
    if(n < k) return 0;
    return (1LL * fact[n] * (1LL * invfact[k] * invfact[n - k] % MOD)) % MOD;
}
#endif

using namespace std;

const int MAXN = (int) 3e5 + 5;

map <int, int> nxtX[MAXN + 1];
int nxt[MAXN + 1], a[MAXN + 1];

int main() {
#ifdef HOME
    ifstream cin("A.in");
    ofstream cout("A.out");
#endif
    int i, n, q;
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    cin >> q;

    while(q--) {
        cin >> n;

        for(i = 1; i <= n; i++) {
            cin >> a[i];
            nxtX[i].clear();
        }
        nxtX[n + 1].clear();

        for(i = n; i >= 1; i--) {
            if(i < n && a[i] == a[i + 1]) {
                nxt[i] = i + 1;
            }
            else {
                nxt[i] = nxtX[i + 1][a[i]];
            }

            if(nxt[i] != 0 && nxt[i] < n) {
                swap(nxtX[i], nxtX[nxt[i] + 1]);
                nxtX[i][a[nxt[i] + 1]] = nxt[i] + 1;
            }
        }

        vector <int> dp(n + 2);
        ll ans = 0;
        for(i = n; i >= 1; i--) {
            if(nxt[i] != 0) {
                dp[i] = 1 + dp[nxt[i] + 1];
            }
            ans += dp[i];
        }

        cout << ans << "\n";
    }

    return 0;
}


#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
#define uint unsigned int

#if 0
const int MOD = (int) ;

inline int lgput(int a, int b) {
    int ans = 1;
    while(b > 0) {
        if(b & 1) ans = (1LL * ans * a) % MOD;
        b >>= 1;
        a = (1LL * a * a) % MOD;
    }
    return ans;
}

inline void mod(int &x) {
    if(x >= MOD)
        x -= MOD;
}

inline void add(int &x, int y) {
    x += y;
    mod(x);
}

inline void sub(int &x, int y) {
    x += MOD - y;
    mod(x);
}

inline void mul(int &x, int y) {
    x = (1LL * x * y) % MOD;
}

inline int inv(int x) {
    return lgput(x, MOD - 2);
}
#endif

#if 0
int fact[], invfact[];

inline void prec(int n) {
    fact[0] = 1;
    for(int i = 1; i <= n; i++) {
        fact[i] = (1LL * fact[i - 1] * i) % MOD;
    }
    invfact[n] = lgput(fact[n], MOD - 2);
    for(int i = n - 1; i >= 0; i--) {
        invfact[i] = (1LL * invfact[i + 1] * (i + 1)) % MOD;
    }
}

inline int comb(int n, int k) {
    if(n < k) return 0;
    return (1LL * fact[n] * (1LL * invfact[k] * invfact[n - k] % MOD)) % MOD;
}
#endif

using namespace std;

const int MAXN = (int) 3e5;

map <int, int> nxtX[MAXN + 1];
int nxt[MAXN + 1], a[MAXN + 1];

int main() {
#ifdef HOME
    ifstream cin("A.in");
    ofstream cout("A.out");
#endif
    int i, n, q;
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    cin >> q;

    while(q--) {
        cin >> n;

        for(i = 1; i <= n; i++) {
            cin >> a[i];
            nxtX[i].clear();
        }

        for(i = n; i >= 1; i--) {
            if(a[i] == a[i + 1]) {
                nxt[i] = i + 1;
            }
            else {
                nxt[i] = nxtX[i + 1][a[i]];
            }

            if(nxt[i] != 0) {
                swap(nxtX[i], nxtX[nxt[i] + 1]);
                nxtX[i][a[nxt[i] + 1]] = nxt[i] + 1;
            }
        }

        vector <int> dp(n + 2);
        ll ans = 0;
        for(i = n; i >= 1; i--) {
            if(nxt[i] != 0) {
                dp[i] = 1 + dp[nxt[i] + 1];
            }
            ans += dp[i];
        }

        /*for(i = 1; i <= n; i++) {
            cerr << nxt[i] << " ";
        }
        cerr << "\n";*/

        cout << ans << "\n";
    }

    return 0;
}


#include "bits/stdc++.h"
#define FI first.first
#define SE first.second
#define TH second
#define fi first
#define se second
#define th second

using namespace std;

class DebugStream {}LOG;
template <typename T>DebugStream &operator<<(DebugStream &s, const T&) { return s; }
#ifdef DEBUG
#define LOG clog
#endif

typedef long long ll;
typedef pair<ll, ll> ii;
typedef long double ld;
mt19937 rnd(time(NULL));

const int maxN = 3e5 + 9, maxV = 1e6 + 9, MOD = 1e9 + 7, SQ = 335, lg = 20, bs = 20;

struct Node {
	Node* le;
	Node* ri;
	int vlue;
	Node(){}
	Node(int vlue, Node* le, Node* ri): vlue(vlue), le(le), ri(ri) {}
} bode[maxN * 80], *trs[maxN];
int cuenta[maxN];
Node* null;
int bodc = 0, n, arr[maxN], dp[maxN];

Node* newNode(int vlue, Node* le, Node* ri) {
	return &(bode[bodc++] = Node(vlue, le, ri));
}

Node* change(Node* nd, int nmb, int x, int lev) {
	if(lev < 0) return newNode(x, null, null);
	if((nmb >> lev) & 1) return newNode(666, nd->le, change(nd->ri, nmb, x, lev - 1));
	else return newNode(666, change(nd->le, nmb, x, lev - 1), nd->ri);
}

int get(Node* nd, int nmb, int lev) {
	if(lev < 0) return nd-> vlue;
	if((nmb >> lev) & 1) return get(nd->ri, nmb, lev - 1);
	else return get(nd->le, nmb, lev - 1);
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	#ifdef DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif

	null = new Node(0, NULL, NULL);
	null->le = null->ri = null;

	auto other = change(null, 2, 20, 2);
	int t;
	cin >> t;
	while(t--) {
		cin >> n;
		bodc = 0;
		for(int i = 0; i < n; i++) cin >> arr[i];
		for(int i = 0; i <= n; i++) trs[i] = null, cuenta[i] = 0;

		for(int i = n - 1; i >= 0; i--) {
			dp[i] = get(trs[i + 1], arr[i], bs);
			trs[i] = change(trs[dp[i]], arr[i], i + 1, bs);
		}

		ll ans = 0;
		for(int i = n - 1; i >= 0; i--) {
			if(dp[i])
				cuenta[i] = cuenta[dp[i]] + 1;
			ans += cuenta[i];
		}
		cout << ans << '\n';
	}
}


#include "bits/stdc++.h"
#define FI first.first
#define SE first.second
#define TH second
#define fi first
#define se second
#define th second

using namespace std;

class DebugStream {}LOG;
template <typename T>DebugStream &operator<<(DebugStream &s, const T&) { return s; }
#ifdef DEBUG
#define LOG clog
#endif

typedef long long ll;
typedef pair<ll, ll> ii;
typedef long double ld;
mt19937 rnd(time(NULL));

const int maxN = 3e5 + 9, maxV = 1e6 + 9, MOD = 1e9 + 7, SQ = 335, lg = 20, bs = 20;

struct Node {
	Node* le;
	Node* ri;
	int vlue;
	Node(){}
	Node(int vlue, Node* le, Node* ri): vlue(vlue), le(le), ri(ri) {}
} bode[maxN * 30], *trs[maxN];
int cuenta[maxN];
Node* null;
int bodc = 0, n, arr[maxN], dp[maxN];

Node* newNode(int vlue, Node* le, Node* ri) {
	return &(bode[bodc++] = Node(vlue, le, ri));
}

Node* change(Node* nd, int nmb, int x, int lev) {
	if(lev < 0) return newNode(x, null, null);
	if((nmb >> lev) & 1) return newNode(666, nd->le, change(nd->ri, nmb, x, lev - 1));
	else return newNode(666, change(nd->le, nmb, x, lev - 1), nd->ri);
}

int get(Node* nd, int nmb, int lev) {
	if(lev < 0) return nd-> vlue;
	if((nmb >> lev) & 1) return get(nd->ri, nmb, lev - 1);
	else return get(nd->le, nmb, lev - 1);
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	#ifdef DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif

	null = new Node(0, NULL, NULL);
	null->le = null->ri = null;

	auto other = change(null, 2, 20, 2);
	int t;
	cin >> t;
	while(t--) {
		cin >> n;
		bodc = 0;
		for(int i = 0; i < n; i++) cin >> arr[i];
		for(int i = 0; i <= n; i++) trs[i] = null, cuenta[i] = 0;

		for(int i = n - 1; i >= 0; i--) {
			dp[i] = get(trs[i + 1], arr[i], bs);
			trs[i] = change(trs[dp[i]], arr[i], i + 1, bs);
		}

		ll ans = 0;
		for(int i = n - 1; i >= 0; i--) {
			if(dp[i])
				cuenta[i] = cuenta[dp[i]] + 1;
			ans += cuenta[i];
		}
		cout << ans << '\n';
	}
}


fun main() {
    fun readInt() = readLine()!!.toInt()
    fun readInts() = readLine()!!.split(" ").map(String::toInt)

    val numQueries = readInt()
    val sols = IntArray(numQueries)
    for (query in 0 until numQueries) {
        val (_, numPlatforms) = readInts()
        val out = readInts()
        var sol = 0
        for (pos in 1 until numPlatforms)
            if (out[pos] > 2 && (pos == numPlatforms - 1 || out[pos] - 1 != out[pos + 1]))
                sol++
        sols[query] = sol
    }
    print(sols.joinToString(System.lineSeparator()))
}


fun main() {
    fun readInt() = readLine()!!.toInt()
    fun readInts() = readLine()!!.split(" ").map(String::toInt)

    val numQueries = readInt()
    val sols = IntArray(numQueries)
    for (query in 0 until numQueries) {
        val (_, numPlatforms) = readInts()
        val out = readInts().toMutableList()
        out.add(-1)
        var sol = 0
        var pos = 0
        while (++pos < numPlatforms) {
            if (out[pos] > 1 && out[pos] - 1 != out[pos + 1])
                sol++
            else if (out[pos] - 1 == out[pos + 1]) pos++
        }
        sols[query] = sol
    }
    print(sols.joinToString(System.lineSeparator()))
}


fun main() {
    fun readInt() = readLine()!!.toInt()
    fun readInts() = readLine()!!.split(" ").map(String::toInt)

    val numQueries = readInt()
    val sols = IntArray(numQueries)
    for (query in 0 until numQueries) {
        val (_, numPlatforms) = readInts()
        val out = readInts()
        var sol = 0
        var pos = 0
        while (++pos < numPlatforms) {
            if (out[pos] > 1 && (pos == numPlatforms - 1 || out[pos] - 1 != out[pos + 1]))
                sol++
            else if (pos < numPlatforms - 1 && out[pos] - 1 == out[pos + 1]) pos++
        }
        sols[query] = sol
    }
    print(sols.joinToString(System.lineSeparator()))
}


fun main() {
    fun readInt() = readLine()!!.toInt()
    fun readInts() = readLine()!!.split(" ").map(String::toInt)

    val numQueries = readInt()
    val sols = IntArray(numQueries)
    for (query in 0 until numQueries) {
        val (_, numPlatforms) = readInts()
        val out = readInts().toMutableList()
        out.add(0)
        var sol = 0
        var pos = 0
        while (++pos < numPlatforms)
            if (out[pos] - 1 != out[pos + 1]) sol++ else pos++
        sols[query] = sol
    }
    print(sols.joinToString(System.lineSeparator()))
}


fun main() {
    fun readInt() = readLine()!!.toInt()
    fun readInts() = readLine()!!.split(" ").map(String::toInt)

    val numQueries = readInt()
    val sols = IntArray(numQueries)
    for (query in 0 until numQueries) {
        val (_, numPlatforms) = readInts()
        val out = readInts()
        var sol = 0
        for (pos in 1 until numPlatforms)
            if (out[pos] > 1 && (pos == numPlatforms - 1 || out[pos] - 1 != out[pos + 1]))
                sol++
        sols[query] = sol
    }
    print(sols.joinToString(System.lineSeparator()))
}


fun main() {
    fun readInt() = readLine()!!.toInt()
    fun readInts() = readLine()!!.split(" ").map(String::toInt)

    val numQueries = readInt()
    val sols = IntArray(numQueries)
    for (query in 0 until numQueries) {
        val (_, numPlatforms) = readInts()
        val out = readInts()
        var sol = 0
        for (pos in 1 until numPlatforms - 1)
            if (out[pos] - 1 != out[pos + 1])
                sol++
        sols[query] = sol
    }
    print(sols.joinToString(System.lineSeparator()))
}


fun main() {
    fun readInt() = readLine()!!.toInt()
    fun readInts() = readLine()!!.split(" ").map(String::toInt)

    val numQueries = readInt()
    val sols = IntArray(numQueries)
    for (query in 0 until numQueries) {
        val (height, _) = readInts()
        val out = readInts().toMutableSet()
        var current = height
        var sol = 0
        while (current > 2) {
            when {
                current - 1 !in out -> current--
                current - 2 in out -> current -= 2
                else -> {
                    sol++
                    if (current - 2 > 2 && current - 3 in out && current - 4 !in out)
                        current--
                    else
                        current -= 2
                }
            }
        }
        sols[query] = sol
    }
    print(sols.joinToString(System.lineSeparator()))
}


fun main(args: Array<String> ) {
    val t = readLine()!!.toInt()
    (1..t).forEach {
        val (h, n) = readLine()!!.split(" ").map { it.toInt() }
        val x = readLine()!!.split(" ").map { it.toInt() }
        var res = 0
        var cnt = 0
        x.zipWithNext { a, b ->
            if ( a-b > 1 ) {
                if ( cnt % 2 > 0 ) res += 1
                cnt = 1
            }
            else {
                cnt += 1
            }
            if ( b == 1 ) {
                cnt = 0
            }
        }
        if ( cnt % 2 > 0 ) res += 1
        println(res)
    }
}


import java.util.*;
import java.io.*;
public class Solution{

	public static void main(String[] argv)throws Exception {
		new Solver().solve1();
	}
}

class Solver{

	public void solve1()throws Exception {
		//Scanner sc = new Scanner(new File("input.txt"));
		Scanner sc = new Scanner(System.in);
		int t=sc.nextInt();
		while(t-->0) {
			int h=sc.nextInt();
			int n=sc.nextInt();
			int[] b = new int[n];
			HashSet a = new HashSet();
			for(int i=0;i<n;i++) {
				b[i]=sc.nextInt();
				a.add(b[i]);
			}
			a.add(0);
			int cost=0;
			int k=1;
			int next = (k>=n)? 0 : b[k] ;
			h=next+1;
			while(h>0){
				if(h==1)break;
				if(!a.contains(h-2)) {
					cost++;
					k++;
				}
				else
					k=k+2;
				next = (k>=n)?0:b[k];
				h=next+1;
			}
			System.out.println(cost);
		}
	}
}


import java.util.*;
import java.io.*;
public class Solution{

	public static void main(String[] argv)throws Exception {
		new Solver().solve1();
	}
}

class Solver{

	public void solve1()throws Exception {
		//Scanner sc = new Scanner(new File("input.txt"));
		Scanner sc = new Scanner(System.in);
		int t=sc.nextInt();
		while(t-->0) {
			//System.out.println("..."+t);
			int h=sc.nextInt();
			int n=sc.nextInt();
			HashSet a = new HashSet();
			for(int i=0;i<n;i++)
				a.add(sc.nextInt());
			a.add(0);
			int cost=0;
			while(h>0){
				if(h==1)break;
				if(a.contains(h-1)) {
					if(!a.contains(h-2))
						cost++;
					h=h-2;
					//System.out.print(h+" ");
				}
				else{
					h=h-1;
					//System.out.print(h+" ");
				}
			}
			//System.out.println();
			System.out.println(cost);
		}

	}
}


//package com.company;

import java.util.Scanner;

public class CF_1238_C {
    public static void main(String args[]) {
        Scanner sc=new Scanner(System.in);
        int q=sc.nextInt();
        while(q-->0) {
            int h=sc.nextInt();
            int n=sc.nextInt();
            int a[]=new int[n];
            for(int i=0;i<a.length;++i)
                a[i]=sc.nextInt();
            int ans=0;
            for(int i=0;i<a.length-1;++i) {
//                if(i+1<a.length&&a[i+1]!=a[i]-1) {
//                    ans+=a[i-1]-a[i]-1;
//                }
//                if(a[i+1]==1)
//                    break;
//                System.out.print(i+" ");
                if(i+2<a.length&&a[i+2]==a[i+1]-1)
                    i++;
                else if(i+2<a.length||i==a.length-2&&a[i+1]+1>2) {
                    ans++;
                }
            }
//            System.out.println();
            System.out.println(ans);

        }
    }
}


//package com.company;

import java.util.Scanner;

public class CF_1238_C {
    public static void main(String args[]) {
        Scanner sc=new Scanner(System.in);
        int q=sc.nextInt();
        while(q-->0) {
            int h=sc.nextInt();
            int n=sc.nextInt();
            int a[]=new int[n];
            for(int i=0;i<a.length;++i)
                a[i]=sc.nextInt();
            int ans=0;
            for(int i=0;i<a.length-1;++i) {
//                if(i+1<a.length&&a[i+1]!=a[i]-1) {
//                    ans+=a[i-1]-a[i]-1;
//                }
//                if(a[i+1]==1)
//                    break;
//                System.out.print(i+" ");
                if(i+2<a.length&&a[i+2]==a[i+1]-1)
                    i++;
                else if(i+2<a.length) {
                    ans++;
                }
            }
//            System.out.println();
            System.out.println(ans);

        }
    }
}


import java.util.*;
import java.io.*;

public class Main {

    public static int[] moved = new int[200005];

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public char nextChar() {
            return next().charAt(0);
        }

        public float nextFloat() {
            return Float.parseFloat(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }

        public int[] nextIntArray(int n) {
            int[] intArray = new int[n];
            for (int i = 0; i < n; i++) {
                intArray[i] = nextInt();
            }
            return intArray;
        }
    }

    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        Task solver = new Task();
        int q = in.nextInt();
        for (int i = 0; i < q; i++) {
            solver.solve(i + 1, in, out);
        }
        out.close();
    }

    static class Task {

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int h = in.nextInt();
            int n = in.nextInt();
            int count = 0;
            for (int i = 0; i < n; i++) {
                moved[i] = in.nextInt();
            }
            for (int i = 0; i < n - 2; i++) {
                moved[i] = moved[i + 1] + 1;
                if (moved[i] - moved[i + 2] > 2) {
                    count++;
                    moved[i + 1]--;
                } else {
                    i++;
                }
            }
            out.println(count);
        }
    }
}


import java.util.*;
import java.io.*;

public class Main {

    public static int[] moved = new int[200005];

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public char nextChar() {
            return next().charAt(0);
        }

        public float nextFloat() {
            return Float.parseFloat(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }

        public int[] nextIntArray(int n) {
            int[] intArray = new int[n];
            for (int i = 0; i < n; i++) {
                intArray[i] = nextInt();
            }
            return intArray;
        }
    }

    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        Task solver = new Task();
        int q = in.nextInt();
        for (int i = 0; i < q; i++) {
            solver.solve(i + 1, in, out);
        }
        out.close();
    }

    static class Task {

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int h = in.nextInt();
            int n = in.nextInt();
            int count = 0;
            for (int i = 0; i < n; i++) {
                moved[i] = in.nextInt();
            }
            int i = 0;
            while (true) {
                if (i == n - 2) {
                    if (moved[i + 1] > 1) {
                        count++;
                    }
                    break;
                } else if (i == n - 1) {
                    break;
                }
                moved[i] = moved[i + 1] + 1;
                if (moved[i] - moved[i + 2] > 2) {
                    count++;
                    moved[i + 1]--;
                    i++;
                    continue;
                }
                i += 2;
            }
            out.println(count);
        }
    }
}


import java.util.*;
import java.io.*;

public class Main {

    public static int[] moved = new int[200005];

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public char nextChar() {
            return next().charAt(0);
        }

        public float nextFloat() {
            return Float.parseFloat(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }

        public int[] nextIntArray(int n) {
            int[] intArray = new int[n];
            for (int i = 0; i < n; i++) {
                intArray[i] = nextInt();
            }
            return intArray;
        }
    }

    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        Task solver = new Task();
        int q = in.nextInt();
        for (int i = 0; i < q; i++) {
            solver.solve(i + 1, in, out);
        }
        out.close();
    }

    static class Task {

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int h = in.nextInt();
            int n = in.nextInt();
            int count = 0;
            for (int i = 0; i < n; i++) {
                moved[i] = in.nextInt();
            }
            for (int i = 0; i < n - 2; i++) {
                moved[i] = moved[i + 1] + 1;
                if (moved[i] - moved[i + 2] != 2) {
                    count++;
                    i++;
                }
            }
            out.println(count);
        }
    }
}