#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define all(c) c.begin(),c.end()
#define pb push_back
#define fs first
#define sc second
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
using namespace std;
template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){
	return o<<"("<<p.fs<<","<<p.sc<<")";
}
template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){
	o<<"{";
	for(const T& v:vc) o<<v<<",";
	o<<"}";
	return o;
}
using ll = long long;
template<class T> using V = vector<T>;
template<class T> using VV = vector<vector<T>>;
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); }

#ifdef LOCAL
#define show(x) cerr << "LINE" << __LINE__ << " : " << #x << " = " << (x) << endl
#else
#define show(x) true
#endif
struct Aho{
	using NP = Aho*;
	map<int,NP> nx;
	NP p = nullptr;
	ll cnt = 0;

	NP add(int c){
		if(nx[c] == nullptr){
			nx[c] = new Aho();
			nx[c] -> p = this;
		}
		return nx[c];
	}
};
ll solve(){
	int N;
	cin >> N;
	V<int> a(N); rep(i,N) cin >> a[i];

	function<ll(int,int)> f = [&](int l,int r){
		if(r-l == 1) return 0LL;
		int m = (l+r)/2;
		ll res = f(l,m) + f(m,r);

		auto T = new Aho();
		auto now = T;
		stack<int> s; s.push(-1);
		for(int i=m-1;i>=l;i--){
			if(s.top() == a[i]){
				s.pop();
				now = now -> p;
			}else{
				s.push(a[i]);
				now = now->add(a[i]);
			}
			now -> cnt ++;
		}

		now = T;
		while(!s.empty()) s.pop();
		s.push(-1);
		for(int i=m;i<r;i++){
			if(s.top() == a[i]){
				s.pop();
				now = now -> p;
			}else{
				s.push(a[i]);
				now = now->add(a[i]);
			}
			res += now->cnt;
		}
//		show("--------------------");show(l);show(r);show(res);
		return res;
	};

	return f(0,N);
}
int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);		//DON'T USE scanf/printf/puts !!
	cout << fixed << setprecision(20);

	int T;
	cin >> T;
	rep(_,T) cout << solve() << '\n';
}


#include<bits/stdc++.h>
using namespace std;
#define HOME
#define f first
#define s second
#define int long long
const int N = 3e5 + 7, MOD = 1000 * 1000 * 1000 + 7;
int n, a[N];
int solve(int l, int r) {
    if (l == r) return 0;
    int m = (l + r) >> 1;
    int ans = solve(l, m) + solve(m + 1, r);
    vector <int> cur, h = {0};
    map <int, int> cnt;
    for (int i = m; i >= l; --i) {
        if (cur.size() && cur.back() == a[i]) {
            cur.pop_back(); h.pop_back();
        }
        else {
            cur.push_back(a[i]); h.push_back((h.back() * N + a[i]) % MOD);
        }
        ++cnt[h.back()];
    }
    cur.clear(); h = {0};
    for (int i = m + 1; i <= r; ++i) {
        if (cur.size() && cur.back() == a[i]) {
            cur.pop_back(); h.pop_back();
        }
        else {
            cur.push_back(a[i]); h.push_back((h.back() * N + a[i]) % MOD);
        }
        ans += cnt[h.back()];
    }
    return ans;
}
void solve() {
    cin >> n;
    for (int i = 0; i < n; ++i) cin >> a[i];
    cout << solve(0, n - 1) << '\n';
}
signed main() {
    #ifdef HOME
    freopen("input.txt", "r", stdin);
    #else
    ios_base::sync_with_stdio(0); cin.tie(0);
    #endif // HOME
    int t; cin >> t; while (t--) solve();
}


#include<bits/stdc++.h>
using namespace std;
//#define HOME
#define f first
#define s second
#define int long long
const int N = 3e5 + 7, MOD = 1000 * 1000 * 1000 + 7;
int n, a[N];
int solve(int l, int r) {
    if (l == r) return 0;
    int m = (l + r) >> 1;
    int ans = solve(l, m) + solve(m + 1, r);
    vector <int> cur, h = {0};
    map <int, int> cnt;
    for (int i = m; i >= l; --i) {
        if (cur.size() && cur.back() == a[i]) {
            cur.pop_back(); h.pop_back();
        }
        else {
            cur.push_back(a[i]); h.push_back((h.back() * N + a[i]) % MOD);
        }
        ++cnt[h.back()];
    }
    cur.clear(); h = {0};
    for (int i = m + 1; i <= r; ++i) {
        if (cur.size() && cur.back() == a[i]) {
            cur.pop_back(); h.pop_back();
        }
        else {
            cur.push_back(a[i]); h.push_back((h.back() * N + a[i]) % MOD);
        }
        ans += cnt[h.back()];
    }
    return ans;
}
void solve() {
    cin >> n;
    for (int i = 0; i < n; ++i) cin >> a[i];
    cout << solve(0, n - 1) << '\n';
}
signed main() {
    #ifdef HOME
    freopen("input.txt", "r", stdin);
    #else
    ios_base::sync_with_stdio(0); cin.tie(0);
    #endif // HOME
    int t; cin >> t; while (t--) solve();
}


    #include<bits/stdc++.h>
    using namespace std;
    //#define HOME
    #define f first
    #define s second
    #define int long long
    const int N = 3e5 + 7;
    mt19937 rnd(2007);
    int n, a[N], f[N];
    int solve(int l, int r) {
        if (l == r) return 0;
        int m = (l + r) >> 1;
        int ans = solve(l, m) + solve(m + 1, r);
        vector <int> cur, h = {0};
        map <int, int> cnt;
        for (int i = m; i >= l; --i) {
            if (cur.size() && cur.back() == a[i]) {
                cur.pop_back(); h.pop_back();
            }
            else {
                cur.push_back(a[i]); h.push_back(h.back() + a[i] * f[h.size()]);
            }
            ++cnt[h.back()];
        }
        cur.clear(); h = {0};
        for (int i = m + 1; i <= r; ++i) {
            if (cur.size() && cur.back() == a[i]) {
                cur.pop_back(); h.pop_back();
            }
            else {
                cur.push_back(a[i]); h.push_back(h.back() + a[i] * f[h.size()]);
            }
            ans += cnt[h.back()];
        }
        return ans;
    }
    void solve() {
        cin >> n;
        for (int i = 0; i < n; ++i) cin >> a[i];
        cout << solve(0, n - 1) << '\n';
    }
    signed main() {
        #ifdef HOME
        freopen("input.txt", "r", stdin);
        #else
        ios_base::sync_with_stdio(0); cin.tie(0);
        #endif // HOME
        for (int i = 0; i < N; ++i) f[i] = rnd();
        int t; cin >> t; while (t--) solve();
    }


    #include<bits/stdc++.h>
    using namespace std;
    #define HOME
    #define f first
    #define s second
    #define int long long
    const int N = 3e5 + 7;
    mt19937 rnd(2007);
    int n, a[N], f[N];
    int solve(int l, int r) {
        if (l == r) return 0;
        int m = (l + r) >> 1;
        int ans = solve(l, m) + solve(m + 1, r);
        vector <int> cur, h = {0};
        map <int, int> cnt;
        for (int i = m; i >= l; --i) {
            if (cur.size() && cur.back() == a[i]) {
                cur.pop_back(); h.pop_back();
            }
            else {
                cur.push_back(a[i]); h.push_back(h.back() + a[i] * f[h.size()]);
            }
            ++cnt[h.back()];
        }
        cur.clear(); h = {0};
        for (int i = m + 1; i <= r; ++i) {
            if (cur.size() && cur.back() == a[i]) {
                cur.pop_back(); h.pop_back();
            }
            else {
                cur.push_back(a[i]); h.push_back(h.back() + a[i] * f[h.size()]);
            }
            ans += cnt[h.back()];
        }
        return ans;
    }
    void solve() {
        cin >> n;
        for (int i = 0; i < n; ++i) cin >> a[i];
        cout << solve(0, n - 1) << '\n';
    }
    signed main() {
        #ifdef HOME
        freopen("input.txt", "r", stdin);
        #else
        ios_base::sync_with_stdio(0); cin.tie(0);
        #endif // HOME
        for (int i = 0; i < N; ++i) f[i] = rnd();
        int t; cin >> t; while (t--) solve();
    }


#include<bits/stdc++.h>
using namespace std;
//#define HOME
#define f first
#define s second
#define int long long
const int N = 3e5 + 7, MOD = 1000 * 1000 * 1000 + 9;
int n, a[N];
int solve(int l, int r) {
    if (l == r) return 0;
    int m = (l + r) >> 1;
    int ans = solve(l, m) + solve(m + 1, r);
    vector <int> cur, h = {0};
    map <int, int> cnt;
    for (int i = m; i >= l; --i) {
        if (cur.size() && cur.back() == a[i]) {
            cur.pop_back(); h.pop_back();
        }
        else {
            cur.push_back(a[i]); h.push_back((h.back() * N + a[i]) % MOD);
        }
        ++cnt[h.back()];
    }
    cur.clear(); h = {0};
    for (int i = m + 1; i <= r; ++i) {
        if (cur.size() && cur.back() == a[i]) {
            cur.pop_back(); h.pop_back();
        }
        else {
            cur.push_back(a[i]); h.push_back((h.back() * N + a[i]) % MOD);
        }
        ans += cnt[h.back()];
    }
    return ans;
}
void solve() {
    cin >> n;
    for (int i = 0; i < n; ++i) cin >> a[i];
    cout << solve(0, n - 1) << '\n';
}
signed main() {
    #ifdef HOME
    freopen("input.txt", "r", stdin);
    #else
    ios_base::sync_with_stdio(0); cin.tie(0);
    #endif // HOME
    int t; cin >> t; while (t--) solve();
}


// while (clock()<=69*CLOCKS_PER_SEC)
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define pb push_back
#define SZ(x) ((int)(x).size())
#define ALL(x) x.begin(),x.end()
#define all(x) x.begin(),x.end()
#define fi first
#define se second
#define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define erase_duplicates(x) sort(all(x)); (x).resize(distance((x).begin(), unique(all(x))));

using namespace std;
using namespace __gnu_pbds;
template<typename T>
using ordered_set = tree<
T,
null_type,
less<T>,
rb_tree_tag,
tree_order_statistics_node_update>;

//X.find_by_order(k); - zwraca iterator na k-ty element (numeracja od zerowego)
//X.order_of_key(k); - zwraca liczbę elementów ostro mniejszych niż k

typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
typedef vector<PII> VPII;
typedef vector<PLL> VPLL;
typedef vector<LL> VLL;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<char> VC;
typedef long double LD;
typedef pair<LD,LD> PLD;
typedef vector<LD> VLD;
typedef vector<PLD> VPLD;

template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<" = "<<h<<endl; }
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;
  cerr<<" = "<<h<<", "; _dbg(sdbg+1, a...);
}

#ifdef LOCAL
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define dbg(...)
#define cerr if(0)cout
#endif

const int maxn = (3e5)+7;
const int maxk = 20;
const int inf = (1e9)+7;
const LL LLinf = ((LL)1e18)+7LL;
const LD eps = 1e-9;
const LL mod = 1e9+7;

// ***************************** CODE ***************************** //

struct node
{
    int val;
    int left = -1, right = -1;
    node(int VAL = 0){val = VAL;}
    node(node * xd)
    {
      val = xd->val;
      left = xd->left;
      right = xd->right;
    }
};

int num;

LL dp[maxn];
int tab[maxn];
int gdzie[maxn];
int ja[maxn];
node wek[maxn * 50];

int add(int cur, int x, int val, int l, int p)
{
  if(l == p)
  {
    wek[num].val = val;
    return num++;
  }
  int sr = (l + p) / 2;
  int tmp = num++;
  wek[tmp] = wek[cur];
  if(x <= sr)
  {
    if(wek[tmp].left == -1)
      wek[tmp].left = num++;
    wek[tmp].left = add(wek[tmp].left, x, val, l, sr);
  }
  else
  {
    if(wek[tmp].right == -1)
      wek[tmp].right = num++;
    wek[tmp].right = add(wek[tmp].right, x, val, sr + 1, p);
  }
  return tmp;
}

int znajdz(int cur, int x, int l, int p)
{
  if(cur == -1) return 0;
  if(l == p) return wek[cur].val;
  int sr = (l + p) / 2;
  if(x <= sr)
    return znajdz(wek[cur].left, x, l, sr);
  else
    return znajdz(wek[cur].right, x, sr + 1, p);
}

void wypisz(node * cur)
{
  if(!cur) return ;
  // wypisz(cur->left);
  // wypisz(cur->right);
  // if(cur->l == cur->p)
    // dbg(cur->l, cur->val);
}

void solve()
{
  int n;
  cin>>n;
  // dbg(n);
  for(int i = 0;i <= (n + 2) * 20;i++)
  {
    if(i <= n + 2)
      dp[i] = tab[i] = gdzie[i] = 0;
    wek[i] = node(0);
  }
  num = 0;
  for(int i = 1;i <= n;i++)
    cin>>tab[i];
  int pot = 1;
  while(pot < n)
    pot *= 2;
  LL res = 0LL;
  // dbg("ELO");
  for(int i = n;i > 0;i--)
  {
    // dbg(i);
    gdzie[i] = znajdz(ja[i + 1], tab[i], 1, pot) + 1;
    if(gdzie[i] == 1)
      gdzie[i] = n + 2;
    // dbg(gdzie[i]);
    ja[i] = add(ja[gdzie[i]], tab[i], i, 1, pot);
    // wypisz(wek[i]);
    // dbg("");
    dp[i] = (dp[gdzie[i]] + (gdzie[i] < n + 2));
    res += dp[i];
  }
  cout<<res<<"\n";
}

int main()
{
	_upgrade
  int t;
  cin>>t;
  while(t--)
    solve();

	return 0;
}


// while (clock()<=69*CLOCKS_PER_SEC)
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define pb push_back
#define SZ(x) ((int)(x).size())
#define ALL(x) x.begin(),x.end()
#define all(x) x.begin(),x.end()
#define fi first
#define se second
#define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define erase_duplicates(x) sort(all(x)); (x).resize(distance((x).begin(), unique(all(x))));

using namespace std;
using namespace __gnu_pbds;
template<typename T>
using ordered_set = tree<
T,
null_type,
less<T>,
rb_tree_tag,
tree_order_statistics_node_update>;

//X.find_by_order(k); - zwraca iterator na k-ty element (numeracja od zerowego)
//X.order_of_key(k); - zwraca liczbę elementów ostro mniejszych niż k

typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
typedef vector<PII> VPII;
typedef vector<PLL> VPLL;
typedef vector<LL> VLL;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<char> VC;
typedef long double LD;
typedef pair<LD,LD> PLD;
typedef vector<LD> VLD;
typedef vector<PLD> VPLD;

template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<" = "<<h<<endl; }
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;
  cerr<<" = "<<h<<", "; _dbg(sdbg+1, a...);
}

#ifdef LOCAL
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define dbg(...)
#define cerr if(0)cout
#endif

const int maxn = (1e6)+7;
const int maxk = 20;
const int inf = (1e9)+7;
const LL LLinf = ((LL)1e18)+7LL;
const LD eps = 1e-9;
const LL mod = 1e9+7;

// ***************************** CODE ***************************** //

struct node
{
    int l, p;
    int val;
    node * left = 0, * right = 0;
    node(){}
    node(int L, int P, int VAL = 0){l = L; p = P; val = VAL;}
    node(node * xd)
    {
      l = xd->l;
      p = xd->p;
      left = xd->left;
      right = xd->right;
    }
};

LL dp[maxn];
int tab[maxn];
int gdzie[maxn];
node * wek[maxn];

node* add(node * cur, int x, int val)
{
  if(cur->l == cur->p) return new node(x, x, val);
  int sr = (cur->l + cur->p) / 2;
  node * tmp = new node(cur);
  if(x <= sr)
  {
    if(tmp->left == NULL)
      tmp->left = new node(cur->l, sr);
    tmp->left = add(tmp->left, x, val);
  }
  else
  {
    if(tmp->right == NULL)
      tmp->right = new node(sr + 1, cur->p);
    tmp->right = add(tmp->right, x, val);
  }
  return tmp;
}

int znajdz(node * cur, int x)
{
  if(!cur) return 0;
  if(cur->l == cur->p) return cur->val;
  int sr = (cur->l + cur->p) / 2;
  if(x <= sr)
    return znajdz(cur->left, x);
  else
    return znajdz(cur->right, x);
}

void wypisz(node * cur)
{
  if(!cur) return ;
  wypisz(cur->left);
  wypisz(cur->right);
  if(cur->l == cur->p)
    dbg(cur->l, cur->val);
}

void solve()
{
  int n;
  cin>>n;
  // dbg(n);
  for(int i = 0;i <= n + 2;i++)
    dp[i] = tab[i] = gdzie[i] = 0;
  for(int i = 1;i <= n;i++)
    cin>>tab[i];
  int pot = 1;
  while(pot < n)
    pot *= 2;
  wek[n + 2] = wek[n + 1] = new node(1, pot);
  LL res = 0LL;
  // dbg("ELO");
  for(int i = n;i > 0;i--)
  {
    // dbg(i);
    gdzie[i] = znajdz(wek[i + 1], tab[i]) + 1;
    if(gdzie[i] == 1)
      gdzie[i] = n + 2;
    // dbg(gdzie[i]);
    wek[i] = add(wek[gdzie[i]], tab[i], i);
    // wypisz(wek[i]);
    // dbg("");
    dp[i] = (dp[gdzie[i]] + (gdzie[i] < n + 2));
    res += dp[i];
  }
  cout<<res<<"\n";
}

int main()
{
	_upgrade
  int t;
  cin>>t;
  while(t--)
    solve();

	return 0;
}


// while (clock()<=69*CLOCKS_PER_SEC)
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define pb push_back
#define SZ(x) ((int)(x).size())
#define ALL(x) x.begin(),x.end()
#define all(x) x.begin(),x.end()
#define fi first
#define se second
#define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define erase_duplicates(x) sort(all(x)); (x).resize(distance((x).begin(), unique(all(x))));

using namespace std;
using namespace __gnu_pbds;
template<typename T>
using ordered_set = tree<
T,
null_type,
less<T>,
rb_tree_tag,
tree_order_statistics_node_update>;

//X.find_by_order(k); - zwraca iterator na k-ty element (numeracja od zerowego)
//X.order_of_key(k); - zwraca liczbę elementów ostro mniejszych niż k

typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
typedef vector<PII> VPII;
typedef vector<PLL> VPLL;
typedef vector<LL> VLL;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<char> VC;
typedef long double LD;
typedef pair<LD,LD> PLD;
typedef vector<LD> VLD;
typedef vector<PLD> VPLD;

template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<" = "<<h<<endl; }
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;
  cerr<<" = "<<h<<", "; _dbg(sdbg+1, a...);
}

#ifdef LOCAL
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define dbg(...)
#define cerr if(0)cout
#endif

const int maxn = (3e5)+7;
const int maxk = 20;
const int inf = (1e9)+7;
const LL LLinf = ((LL)1e18)+7LL;
const LD eps = 1e-9;
const LL mod = 1e9+7;

// ***************************** CODE ***************************** //

struct node
{
    int val;
    int left = -1, right = -1;
    node(int VAL = 0){val = VAL;}
    node(node * xd)
    {
      val = xd->val;
      left = xd->left;
      right = xd->right;
    }
};

int num;

LL dp[maxn];
int tab[maxn];
int gdzie[maxn];
int ja[maxn];
node wek[maxn * 60];

int add(int cur, int x, int val, int l, int p)
{
  if(l == p)
  {
    wek[num].val = val;
    return num++;
  }
  int sr = (l + p) / 2;
  int tmp = num++;
  wek[tmp] = wek[cur];
  if(x <= sr)
  {
    if(wek[tmp].left == -1)
      wek[tmp].left = num++;
    wek[tmp].left = add(wek[tmp].left, x, val, l, sr);
  }
  else
  {
    if(wek[tmp].right == -1)
      wek[tmp].right = num++;
    wek[tmp].right = add(wek[tmp].right, x, val, sr + 1, p);
  }
  return tmp;
}

int znajdz(int cur, int x, int l, int p)
{
  if(cur == -1) return 0;
  if(l == p) return wek[cur].val;
  int sr = (l + p) / 2;
  if(x <= sr)
    return znajdz(wek[cur].left, x, l, sr);
  else
    return znajdz(wek[cur].right, x, sr + 1, p);
}

void wypisz(node * cur)
{
  if(!cur) return ;
  // wypisz(cur->left);
  // wypisz(cur->right);
  // if(cur->l == cur->p)
    // dbg(cur->l, cur->val);
}

void solve()
{
  int n;
  cin>>n;
  // dbg(n);
  for(int i = 0;i <= (n + 2) * 20;i++)
  {
    if(i <= n + 2)
      dp[i] = tab[i] = gdzie[i] = 0;
    wek[i] = node(0);
  }
  num = 1;
  for(int i = 1;i <= n;i++)
    cin>>tab[i];
  int pot = 1;
  while(pot < n)
    pot *= 2;
  LL res = 0LL;
  // dbg("ELO");
  for(int i = n;i > 0;i--)
  {
    // dbg(i);
    gdzie[i] = znajdz(ja[i + 1], tab[i], 1, pot) + 1;
    if(gdzie[i] == 1)
      gdzie[i] = n + 2;
    // dbg(gdzie[i]);
    ja[i] = add(ja[gdzie[i]], tab[i], i, 1, pot);
    // wypisz(wek[i]);
    // dbg("");
    dp[i] = (dp[gdzie[i]] + (gdzie[i] < n + 2));
    res += dp[i];
  }
  cout<<res<<"\n";
}

int main()
{
	_upgrade
  int t;
  cin>>t;
  while(t--)
    solve();

	return 0;
}


// while (clock()<=69*CLOCKS_PER_SEC)
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define pb push_back
#define SZ(x) ((int)(x).size())
#define ALL(x) x.begin(),x.end()
#define all(x) x.begin(),x.end()
#define fi first
#define se second
#define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define erase_duplicates(x) sort(all(x)); (x).resize(distance((x).begin(), unique(all(x))));

using namespace std;
using namespace __gnu_pbds;
template<typename T>
using ordered_set = tree<
T,
null_type,
less<T>,
rb_tree_tag,
tree_order_statistics_node_update>;

//X.find_by_order(k); - zwraca iterator na k-ty element (numeracja od zerowego)
//X.order_of_key(k); - zwraca liczbę elementów ostro mniejszych niż k

typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
typedef vector<PII> VPII;
typedef vector<PLL> VPLL;
typedef vector<LL> VLL;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<char> VC;
typedef long double LD;
typedef pair<LD,LD> PLD;
typedef vector<LD> VLD;
typedef vector<PLD> VPLD;

template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<" = "<<h<<endl; }
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;
  cerr<<" = "<<h<<", "; _dbg(sdbg+1, a...);
}

#ifdef LOCAL
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define dbg(...)
#define cerr if(0)cout
#endif

const int maxn = (3e5)+7;
const int maxk = 20;
const int inf = (1e9)+7;
const LL LLinf = ((LL)1e18)+7LL;
const LD eps = 1e-9;
const LL mod = 1e9+7;

// ***************************** CODE ***************************** //

struct node
{
    int val = 0;
    int left = -1, right = -1;
    node(int VAL = 0){val = VAL;}
    node(node * xd)
    {
      val = xd->val;
      left = xd->left;
      right = xd->right;
    }
};

int num;

LL dp[maxn];
int tab[maxn];
int gdzie[maxn];
int ja[maxn];
node wek[maxn * 60];

int add(int cur, int x, int val, int l, int p)
{
  if(l == p)
  {
    wek[num].val = val;
    return num++;
  }
  int sr = (l + p) / 2;
  int tmp = num++;
  wek[tmp] = wek[cur];
  if(x <= sr)
  {
    if(wek[tmp].left == -1)
      wek[tmp].left = num++;
    wek[tmp].left = add(wek[tmp].left, x, val, l, sr);
  }
  else
  {
    if(wek[tmp].right == -1)
      wek[tmp].right = num++;
    wek[tmp].right = add(wek[tmp].right, x, val, sr + 1, p);
  }
  return tmp;
}

int znajdz(int cur, int x, int l, int p)
{
  if(cur == -1) return 0;
  if(l == p) return wek[cur].val;
  int sr = (l + p) / 2;
  if(x <= sr)
    return znajdz(wek[cur].left, x, l, sr);
  else
    return znajdz(wek[cur].right, x, sr + 1, p);
}

void wypisz(node * cur)
{
  if(!cur) return ;
  // wypisz(cur->left);
  // wypisz(cur->right);
  // if(cur->l == cur->p)
    // dbg(cur->l, cur->val);
}

void solve()
{
  int n;
  cin>>n;
  // dbg(n);
  for(int i = 0;i <= (n + 2) * 20;i++)
  {
    if(i <= n + 2)
      dp[i] = tab[i] = gdzie[i] = 0;
    wek[i] = node(0);
  }
  num = 1;
  for(int i = 1;i <= n;i++)
    cin>>tab[i];
  int pot = 1;
  while(pot < n)
    pot *= 2;
  LL res = 0LL;
  // dbg("ELO");
  ja[n + 1] = ja[n + 2] = 0;
  for(int i = n;i > 0;i--)
  {
    // dbg(i);
    gdzie[i] = znajdz(ja[i + 1], tab[i], 1, pot) + 1;
    if(gdzie[i] == 1)
      gdzie[i] = n + 2;
    // dbg(gdzie[i]);
    ja[i] = add(ja[gdzie[i]], tab[i], i, 1, pot);
    // wypisz(wek[i]);
    // dbg("");
    dp[i] = (dp[gdzie[i]] + (gdzie[i] < n + 2));
    res += dp[i];
  }
  cout<<res<<"\n";
}

int main()
{
	_upgrade
  int t;
  cin>>t;
  while(t--)
    solve();

	return 0;
}


// while (clock()<=69*CLOCKS_PER_SEC)
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define pb push_back
#define SZ(x) ((int)(x).size())
#define ALL(x) x.begin(),x.end()
#define all(x) x.begin(),x.end()
#define fi first
#define se second
#define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define erase_duplicates(x) sort(all(x)); (x).resize(distance((x).begin(), unique(all(x))));

using namespace std;
using namespace __gnu_pbds;
template<typename T>
using ordered_set = tree<
T,
null_type,
less<T>,
rb_tree_tag,
tree_order_statistics_node_update>;

//X.find_by_order(k); - zwraca iterator na k-ty element (numeracja od zerowego)
//X.order_of_key(k); - zwraca liczbę elementów ostro mniejszych niż k

typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
typedef vector<PII> VPII;
typedef vector<PLL> VPLL;
typedef vector<LL> VLL;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<char> VC;
typedef long double LD;
typedef pair<LD,LD> PLD;
typedef vector<LD> VLD;
typedef vector<PLD> VPLD;

template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<" = "<<h<<endl; }
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;
  cerr<<" = "<<h<<", "; _dbg(sdbg+1, a...);
}

#ifdef LOCAL
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define dbg(...)
#define cerr if(0)cout
#endif

const int maxn = (3e5)+7;
const int maxk = 20;
const int inf = (1e9)+7;
const LL LLinf = ((LL)1e18)+7LL;
const LD eps = 1e-9;
const LL mod = 1e9+7;

// ***************************** CODE ***************************** //

struct node
{
    int val = 0;
    int left = -1, right = -1;
    node(int VAL = 0){val = VAL;}
    node(node * xd)
    {
      val = xd->val;
      left = xd->left;
      right = xd->right;
    }
};

int num;

LL dp[maxn];
int tab[maxn];
int gdzie[maxn];
int ja[maxn];
node wek[maxn * 60];

int add(int cur, int x, int val, int l, int p)
{
  if(l == p)
  {
    wek[num].val = val;
    return num++;
  }
  int sr = (l + p) / 2;
  int tmp = num++;
  wek[tmp] = wek[cur];
  if(x <= sr)
  {
    if(wek[tmp].left == -1)
      wek[tmp].left = num++;
    wek[tmp].left = add(wek[tmp].left, x, val, l, sr);
  }
  else
  {
    if(wek[tmp].right == -1)
      wek[tmp].right = num++;
    wek[tmp].right = add(wek[tmp].right, x, val, sr + 1, p);
  }
  return tmp;
}

int znajdz(int cur, int x, int l, int p)
{
  if(cur == -1) return 0;
  if(l == p) return wek[cur].val;
  int sr = (l + p) / 2;
  if(x <= sr)
    return znajdz(wek[cur].left, x, l, sr);
  else
    return znajdz(wek[cur].right, x, sr + 1, p);
}

void wypisz(node * cur)
{
  if(!cur) return ;
  // wypisz(cur->left);
  // wypisz(cur->right);
  // if(cur->l == cur->p)
    // dbg(cur->l, cur->val);
}

void solve()
{
  int n;
  cin>>n;
  // dbg(n);
  for(int i = 0;i <= (n + 2) * 60;i++)
  {
    if(i <= n + 2)
      ja[i] = dp[i] = tab[i] = gdzie[i] = 0;
    wek[i] = node(0);
  }
  num = 1;
  for(int i = 1;i <= n;i++)
    cin>>tab[i];
  int pot = 1;
  while(pot < n)
    pot *= 2;
  LL res = 0LL;
  // dbg("ELO");
  ja[n + 1] = ja[n + 2] = 0;
  for(int i = n;i > 0;i--)
  {
    // dbg(i);
    gdzie[i] = znajdz(ja[i + 1], tab[i], 1, pot) + 1;
    if(gdzie[i] == 1)
      gdzie[i] = n + 2;
    // dbg(gdzie[i]);
    ja[i] = add(ja[gdzie[i]], tab[i], i, 1, pot);
    // wypisz(wek[i]);
    // dbg("");
    dp[i] = (dp[gdzie[i]] + (gdzie[i] < n + 2));
    res += dp[i];
  }
  cout<<res<<"\n";
}

int main()
{
	_upgrade
  int t;
  cin>>t;
  while(t--)
    solve();

	return 0;
}


// while (clock()<=69*CLOCKS_PER_SEC)
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define pb push_back
#define SZ(x) ((int)(x).size())
#define ALL(x) x.begin(),x.end()
#define all(x) x.begin(),x.end()
#define fi first
#define se second
#define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define erase_duplicates(x) sort(all(x)); (x).resize(distance((x).begin(), unique(all(x))));

using namespace std;
using namespace __gnu_pbds;
template<typename T>
using ordered_set = tree<
T,
null_type,
less<T>,
rb_tree_tag,
tree_order_statistics_node_update>;

//X.find_by_order(k); - zwraca iterator na k-ty element (numeracja od zerowego)
//X.order_of_key(k); - zwraca liczbę elementów ostro mniejszych niż k

typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
typedef vector<PII> VPII;
typedef vector<PLL> VPLL;
typedef vector<LL> VLL;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<char> VC;
typedef long double LD;
typedef pair<LD,LD> PLD;
typedef vector<LD> VLD;
typedef vector<PLD> VPLD;

template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<" = "<<h<<endl; }
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;
  cerr<<" = "<<h<<", "; _dbg(sdbg+1, a...);
}

#ifdef LOCAL
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define dbg(...)
#define cerr if(0)cout
#endif

const int maxn = (3e5)+7;
const int maxk = 20;
const int inf = (1e9)+7;
const LL LLinf = ((LL)1e18)+7LL;
const LD eps = 1e-9;
const LL mod = 1e9+7;

// ***************************** CODE ***************************** //

struct node
{
    int l, p;
    int val;
    node * left = 0, * right = 0;
    node(){}
    node(int L, int P, int VAL = 0){l = L; p = P; val = VAL;}
    node(node * xd)
    {
      l = xd->l;
      p = xd->p;
      left = xd->left;
      right = xd->right;
    }
};

LL dp[maxn];
int tab[maxn];
int gdzie[maxn];
node * wek[maxn];

node* add(node * cur, int x, int val)
{
  if(cur->l == cur->p) return new node(x, x, val);
  int sr = (cur->l + cur->p) / 2;
  node * tmp = new node(cur);
  if(x <= sr)
  {
    if(tmp->left == NULL)
      tmp->left = new node(cur->l, sr);
    tmp->left = add(tmp->left, x, val);
  }
  else
  {
    if(tmp->right == NULL)
      tmp->right = new node(sr + 1, cur->p);
    tmp->right = add(tmp->right, x, val);
  }
  return tmp;
}

int znajdz(node * cur, int x)
{
  if(!cur) return 0;
  if(cur->l == cur->p) return cur->val;
  int sr = (cur->l + cur->p) / 2;
  if(x <= sr)
    return znajdz(cur->left, x);
  else
    return znajdz(cur->right, x);
}

void wypisz(node * cur)
{
  if(!cur) return ;
  wypisz(cur->left);
  wypisz(cur->right);
  if(cur->l == cur->p)
    dbg(cur->l, cur->val);
}

void usun(node * cur)
{
  if(!cur) return;
  usun(cur->left);
  usun(cur->right);
  delete cur;
}

void solve()
{
  int n;
  cin>>n;
  // dbg(n);
  for(int i = 0;i <= n + 2;i++)
    dp[i] = tab[i] = gdzie[i] = 0;
  for(int i = 1;i <= n;i++)
    cin>>tab[i];
  int pot = 1;
  while(pot < n)
    pot *= 2;
  wek[n + 2] = wek[n + 1] = new node(1, pot);
  LL res = 0LL;
  // dbg("ELO");
  for(int i = n;i > 0;i--)
  {
    // dbg(i);
    gdzie[i] = znajdz(wek[i + 1], tab[i]) + 1;
    if(gdzie[i] == 1)
      gdzie[i] = n + 2;
    // dbg(gdzie[i]);
    wek[i] = add(wek[gdzie[i]], tab[i], i);
    // wypisz(wek[i]);
    // dbg("");
    dp[i] = (dp[gdzie[i]] + (gdzie[i] < n + 2));
    res += dp[i];
  }
  for(int i = 1;i <= n + 2;i++)
    usun(wek[i]);
  cout<<res<<"\n";
}

int main()
{
	_upgrade
  int t;
  cin>>t;
  while(t--)
    solve();

	return 0;
}


// while (clock()<=69*CLOCKS_PER_SEC)
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define pb push_back
#define SZ(x) ((int)(x).size())
#define ALL(x) x.begin(),x.end()
#define all(x) x.begin(),x.end()
#define fi first
#define se second
#define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define erase_duplicates(x) sort(all(x)); (x).resize(distance((x).begin(), unique(all(x))));

using namespace std;
using namespace __gnu_pbds;
template<typename T>
using ordered_set = tree<
T,
null_type,
less<T>,
rb_tree_tag,
tree_order_statistics_node_update>;

//X.find_by_order(k); - zwraca iterator na k-ty element (numeracja od zerowego)
//X.order_of_key(k); - zwraca liczbę elementów ostro mniejszych niż k

typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
typedef vector<PII> VPII;
typedef vector<PLL> VPLL;
typedef vector<LL> VLL;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<char> VC;
typedef long double LD;
typedef pair<LD,LD> PLD;
typedef vector<LD> VLD;
typedef vector<PLD> VPLD;

template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<" = "<<h<<endl; }
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;
  cerr<<" = "<<h<<", "; _dbg(sdbg+1, a...);
}

#ifdef LOCAL
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define dbg(...)
#define cerr if(0)cout
#endif

const int maxn = (3e5)+7;
const int maxk = 20;
const int inf = (1e9)+7;
const LL LLinf = ((LL)1e18)+7LL;
const LD eps = 1e-9;
const LL mod = 1e9+7;

// ***************************** CODE ***************************** //

struct node
{
    int val;
    node * left = 0, * right = 0;
    node(int VAL = 0){val = VAL;}
    node(node * xd)
    {
      val = xd->val;
      left = xd->left;
      right = xd->right;
    }
};

LL dp[maxn];
int tab[maxn];
int gdzie[maxn];
node * wek[maxn];

node* add(node * cur, int x, int val, int l, int p)
{
  if(l == p) return new node(val);
  int sr = (l + p) / 2;
  node * tmp = new node(cur);
  if(x <= sr)
  {
    if(tmp->left == NULL)
      tmp->left = new node();
    tmp->left = add(tmp->left, x, val, l, sr);
  }
  else
  {
    if(tmp->right == NULL)
      tmp->right = new node();
    tmp->right = add(tmp->right, x, val, sr + 1, p);
  }
  return tmp;
}

int znajdz(node * cur, int x, int l, int p)
{
  if(!cur) return 0;
  if(l == p) return cur->val;
  int sr = (l + p) / 2;
  if(x <= sr)
    return znajdz(cur->left, x, l, sr);
  else
    return znajdz(cur->right, x, sr + 1, p);
}

void wypisz(node * cur)
{
  if(!cur) return ;
  wypisz(cur->left);
  wypisz(cur->right);
  // if(cur->l == cur->p)
    // dbg(cur->l, cur->val);
}

void usun(node * cur)
{
  if(!cur) return;
  dbg(cur->val);
  usun(cur->left);
  usun(cur->right);
  delete cur;
}

void solve()
{
  int n;
  cin>>n;
  // dbg(n);
  for(int i = 0;i <= n + 2;i++)
    dp[i] = tab[i] = gdzie[i] = 0;
  for(int i = 1;i <= n;i++)
    cin>>tab[i];
  int pot = 1;
  while(pot < n)
    pot *= 2;
  wek[n + 2] = wek[n + 1] = new node();
  LL res = 0LL;
  // dbg("ELO");
  for(int i = n;i > 0;i--)
  {
    // dbg(i);
    gdzie[i] = znajdz(wek[i + 1], tab[i], 1, pot) + 1;
    if(gdzie[i] == 1)
      gdzie[i] = n + 2;
    // dbg(gdzie[i]);
    wek[i] = add(wek[gdzie[i]], tab[i], i, 1, pot);
    // wypisz(wek[i]);
    // dbg("");
    dp[i] = (dp[gdzie[i]] + (gdzie[i] < n + 2));
    res += dp[i];
  }
  // for(int i = 1;i <= n + 2;i++)
    // usun(wek[i]);
  cout<<res<<"\n";
}

int main()
{
	_upgrade
  int t;
  cin>>t;
  while(t--)
    solve();

	return 0;
}


// while (clock()<=69*CLOCKS_PER_SEC)
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define pb push_back
#define SZ(x) ((int)(x).size())
#define ALL(x) x.begin(),x.end()
#define all(x) x.begin(),x.end()
#define fi first
#define se second
#define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define erase_duplicates(x) sort(all(x)); (x).resize(distance((x).begin(), unique(all(x))));

using namespace std;
using namespace __gnu_pbds;
template<typename T>
using ordered_set = tree<
T,
null_type,
less<T>,
rb_tree_tag,
tree_order_statistics_node_update>;

//X.find_by_order(k); - zwraca iterator na k-ty element (numeracja od zerowego)
//X.order_of_key(k); - zwraca liczbę elementów ostro mniejszych niż k

typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
typedef vector<PII> VPII;
typedef vector<PLL> VPLL;
typedef vector<LL> VLL;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<char> VC;
typedef long double LD;
typedef pair<LD,LD> PLD;
typedef vector<LD> VLD;
typedef vector<PLD> VPLD;

template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<" = "<<h<<endl; }
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;
  cerr<<" = "<<h<<", "; _dbg(sdbg+1, a...);
}

#ifdef LOCAL
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define dbg(...)
#define cerr if(0)cout
#endif

const int maxn = (3e5)+7;
const int maxk = 20;
const int inf = (1e9)+7;
const LL LLinf = ((LL)1e18)+7LL;
const LD eps = 1e-9;
const LL mod = 1e9+7;

// ***************************** CODE ***************************** //

struct node
{
    int val;
    int left = -1, right = -1;
    node(int VAL = 0){val = VAL;}
    node(node * xd)
    {
      val = xd->val;
      left = xd->left;
      right = xd->right;
    }
};

int num;

LL dp[maxn];
int tab[maxn];
int gdzie[maxn];
int ja[maxn];
node wek[maxn * 20];

int add(int cur, int x, int val, int l, int p)
{
  if(l == p)
  {
    wek[num].val = val;
    return num++;
  }
  int sr = (l + p) / 2;
  int tmp = num++;
  wek[tmp] = wek[cur];
  if(x <= sr)
  {
    if(wek[tmp].left == -1)
      wek[tmp].left = num++;
    wek[tmp].left = add(wek[tmp].left, x, val, l, sr);
  }
  else
  {
    if(wek[tmp].right == -1)
      wek[tmp].right = num++;
    wek[tmp].right = add(wek[tmp].right, x, val, sr + 1, p);
  }
  return tmp;
}

int znajdz(int cur, int x, int l, int p)
{
  if(cur == -1) return 0;
  if(l == p) return wek[cur].val;
  int sr = (l + p) / 2;
  if(x <= sr)
    return znajdz(wek[cur].left, x, l, sr);
  else
    return znajdz(wek[cur].right, x, sr + 1, p);
}

void wypisz(node * cur)
{
  if(!cur) return ;
  // wypisz(cur->left);
  // wypisz(cur->right);
  // if(cur->l == cur->p)
    // dbg(cur->l, cur->val);
}

void solve()
{
  int n;
  cin>>n;
  // dbg(n);
  for(int i = 0;i <= (n + 2) * 20;i++)
  {
    if(i <= n + 2)
      dp[i] = tab[i] = gdzie[i] = 0;
    wek[i] = node(0);
  }
  num = n + 1;
  for(int i = 1;i <= n;i++)
    cin>>tab[i];
  int pot = 1;
  while(pot < n)
    pot *= 2;
  LL res = 0LL;
  // dbg("ELO");
  for(int i = n;i > 0;i--)
  {
    // dbg(i);
    gdzie[i] = znajdz(ja[i + 1], tab[i], 1, pot) + 1;
    if(gdzie[i] == 1)
      gdzie[i] = n + 2;
    // dbg(gdzie[i]);
    ja[i] = add(ja[gdzie[i]], tab[i], i, 1, pot);
    // wypisz(wek[i]);
    // dbg("");
    dp[i] = (dp[gdzie[i]] + (gdzie[i] < n + 2));
    res += dp[i];
  }
  cout<<res<<"\n";
}

int main()
{
	_upgrade
  int t;
  cin>>t;
  while(t--)
    solve();

	return 0;
}


// while (clock()<=69*CLOCKS_PER_SEC)
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define pb push_back
#define SZ(x) ((int)(x).size())
#define ALL(x) x.begin(),x.end()
#define all(x) x.begin(),x.end()
#define fi first
#define se second
#define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define erase_duplicates(x) sort(all(x)); (x).resize(distance((x).begin(), unique(all(x))));

using namespace std;
using namespace __gnu_pbds;
template<typename T>
using ordered_set = tree<
T,
null_type,
less<T>,
rb_tree_tag,
tree_order_statistics_node_update>;

//X.find_by_order(k); - zwraca iterator na k-ty element (numeracja od zerowego)
//X.order_of_key(k); - zwraca liczbę elementów ostro mniejszych niż k

typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
typedef vector<PII> VPII;
typedef vector<PLL> VPLL;
typedef vector<LL> VLL;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<char> VC;
typedef long double LD;
typedef pair<LD,LD> PLD;
typedef vector<LD> VLD;
typedef vector<PLD> VPLD;

template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<" = "<<h<<endl; }
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;
  cerr<<" = "<<h<<", "; _dbg(sdbg+1, a...);
}

#ifdef LOCAL
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define dbg(...)
#define cerr if(0)cout
#endif

const int maxn = (1e6)+7;
const int maxk = 20;
const int inf = (1e9)+7;
const LL LLinf = ((LL)1e18)+7LL;
const LD eps = 1e-9;
const LL mod = 1e9+7;

// ***************************** CODE ***************************** //

LL dp[maxn];
int tab[maxn];
int gdzie[maxn];

void solve()
{
  int n;
  cin>>n;
  dbg(n);
  for(int i = 0;i <= n + 2;i++)
    dp[i] = tab[i] = gdzie[i] = 0;
  for(int i = 1;i <= n;i++)
    cin>>tab[i];
  int wsk = n + 1;
  deque<int> stos;
  int kand = n + 1;
  LL res = 0LL;
  tab[n + 1] = 0;
  gdzie[n + 1] = n + 2;
  tab[n + 2] = 0;
  gdzie[n + 2] = n + 2;
  for(int i = n;i > 0;i--)
  {
    gdzie[i] = i + 1;
    while(gdzie[i] <= n and tab[gdzie[i]] != tab[i])
      gdzie[i] = gdzie[gdzie[i]];
    gdzie[i]++;
    // if(tab[i] == tab[kand])
    // {
    //   wsk--;
    //   kand++;
    //   gdzie[i] = kand;
    // }
    // else
    // {
    //   kand = i;
    //   stos.push_front(tab[i]);
    //   while(SZ(stos) and wsk <= n)
    //   {
    //     if(stos.back() == tab[wsk])
    //       stos.pop_back();
    //     else
    //       stos.pb(tab[wsk]);
    //     wsk++;
    //   }
    //   if(SZ(stos) == 0)
    //     gdzie[i] = wsk;
    //   else
    //     gdzie[i] = n + 2;
    // }
    dp[i] = dp[gdzie[i]] + (gdzie[i] <= n + 1);
    res += dp[i];
    dbg(i, dp[i], gdzie[i], wsk);
  }
  cout<<res<<"\n";
}

int main()
{
	_upgrade
  int t;
  cin>>t;
  while(t--)
    solve();

	return 0;
}


// while (clock()<=69*CLOCKS_PER_SEC)
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define pb push_back
#define SZ(x) ((int)(x).size())
#define ALL(x) x.begin(),x.end()
#define all(x) x.begin(),x.end()
#define fi first
#define se second
#define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define erase_duplicates(x) sort(all(x)); (x).resize(distance((x).begin(), unique(all(x))));

using namespace std;
using namespace __gnu_pbds;
template<typename T>
using ordered_set = tree<
T,
null_type,
less<T>,
rb_tree_tag,
tree_order_statistics_node_update>;

//X.find_by_order(k); - zwraca iterator na k-ty element (numeracja od zerowego)
//X.order_of_key(k); - zwraca liczbę elementów ostro mniejszych niż k

typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
typedef vector<PII> VPII;
typedef vector<PLL> VPLL;
typedef vector<LL> VLL;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<char> VC;
typedef long double LD;
typedef pair<LD,LD> PLD;
typedef vector<LD> VLD;
typedef vector<PLD> VPLD;

template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<" = "<<h<<endl; }
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;
  cerr<<" = "<<h<<", "; _dbg(sdbg+1, a...);
}

#ifdef LOCAL
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define dbg(...)
#define cerr if(0)cout
#endif

const int maxn = (3e5)+7;
const int maxk = 20;
const int inf = (1e9)+7;
const LL LLinf = ((LL)1e18)+7LL;
const LD eps = 1e-9;
const LL mod = 1e9+7;

// ***************************** CODE ***************************** //

struct node
{
    int val = 0;
    int left = -1, right = -1;
    node(int VAL = 0){val = VAL;}
    node(node * xd)
    {
      val = xd->val;
      left = xd->left;
      right = xd->right;
    }
};

int num;

LL dp[maxn];
int tab[maxn];
int gdzie[maxn];
int ja[maxn];
node wek[maxn * 60];

int add(int cur, int x, int val, int l, int p)
{
  if(l == p)
  {
    wek[num].val = val;
    return num++;
  }
  int sr = (l + p) / 2;
  int tmp = num++;
  wek[tmp] = wek[cur];
  if(x <= sr)
  {
    if(wek[tmp].left == -1)
      wek[tmp].left = num++;
    wek[tmp].left = add(wek[tmp].left, x, val, l, sr);
  }
  else
  {
    if(wek[tmp].right == -1)
      wek[tmp].right = num++;
    wek[tmp].right = add(wek[tmp].right, x, val, sr + 1, p);
  }
  return tmp;
}

int znajdz(int cur, int x, int l, int p)
{
  if(cur == -1) return 0;
  if(l == p) return wek[cur].val;
  int sr = (l + p) / 2;
  if(x <= sr)
    return znajdz(wek[cur].left, x, l, sr);
  else
    return znajdz(wek[cur].right, x, sr + 1, p);
}

void wypisz(node * cur)
{
  if(!cur) return ;
  // wypisz(cur->left);
  // wypisz(cur->right);
  // if(cur->l == cur->p)
    // dbg(cur->l, cur->val);
}

void solve()
{
  int n;
  cin>>n;
  // dbg(n);
  for(int i = 0;i <= (n + 2) * 20;i++)
  {
    if(i <= n + 2)
      ja[i] = dp[i] = tab[i] = gdzie[i] = 0;
    wek[i] = node(0);
  }
  num = 1;
  for(int i = 1;i <= n;i++)
    cin>>tab[i];
  int pot = 1;
  while(pot < n)
    pot *= 2;
  LL res = 0LL;
  // dbg("ELO");
  ja[n + 1] = ja[n + 2] = 0;
  for(int i = n;i > 0;i--)
  {
    // dbg(i);
    gdzie[i] = znajdz(ja[i + 1], tab[i], 1, pot) + 1;
    if(gdzie[i] == 1)
      gdzie[i] = n + 2;
    // dbg(gdzie[i]);
    ja[i] = add(ja[gdzie[i]], tab[i], i, 1, pot);
    // wypisz(wek[i]);
    // dbg("");
    dp[i] = (dp[gdzie[i]] + (gdzie[i] < n + 2));
    res += dp[i];
  }
  cout<<res<<"\n";
}

int main()
{
	_upgrade
  int t;
  cin>>t;
  while(t--)
    solve();

	return 0;
}


#include <iostream>
#include <cstdio>
#include <cassert>
#include <algorithm>
#include <map>
#include <vector>
#define all(x) begin(x), end(x)
using namespace std;
typedef long long ll;
typedef long double ld;
#define int ll
#define all(x) begin(x), end(x)

const int N = 4e5;
const ll  P = 1e6 + 3;
const ll M1 = 1e7 + 21;
const ll M2 = 1e7 + 33;
ll pw1[N], pw2[N];

struct st {
	vector<int> a;
	ll h1, h2;
	void clear() {
		h1 = 0;
		h2 = 0;
		a.clear();
	}
	st() {
		clear();
	}
	int back() {
		if (a.empty()) {
			return -1;
		}
		return a.back();
	}
	void push(int v) {
		h1 = (h1 + v * pw1[a.size()]) % M1;
		h2 = (h2 + v * pw2[a.size()]) % M2;
		a.push_back(v);
	}
	void pop() {
		int v = a.back();
		a.pop_back();
		h1 = (h1 - v * pw1[a.size()] + M1 * M1) % M1;
		h2 = (h2 - v * pw2[a.size()] + M2 * M2) % M2;
	}
	ll value() {
		return (h1 << 32) + h2;
	}
};

int n, a[N];
st sta;

ll dc(int l, int r) {
	if (r - l <= 1) {
		return 0;
	}
	int m = (l + r) / 2;
	ll answer = dc(l, m) + dc(m, r);
	map<ll, int> cnt;
	sta.clear();
	for (int i = m; i < r; ++i) {
		if (sta.back() == a[i]) {
			sta.pop();
		} else {
			sta.push(a[i]);
		}
		++cnt[sta.value()];
	}
	sta.clear();
	for (int i = m - 1; i >= l; --i) {
		if (sta.back() == a[i]) {
			sta.pop();
		} else {
			sta.push(a[i]);
		}
		answer += cnt[sta.value()];
	}
	return answer;
}

ll solve() {
	cin >> n;
	for (int i = 0; i < n; ++i) {
		cin >> a[i];
	}
	return dc(0, n);
}

signed main() {
#ifdef LC
	assert(freopen("input.txt", "r", stdin));
#endif
    ios::sync_with_stdio(0);
    cin.tie(0);
    pw1[0] = 1;
    pw2[0] = 1;
    for (int i = 1; i < N; ++i) {
		pw1[i] = pw1[i - 1] * P % M1;
		pw2[i] = pw2[i - 1] * P % M2;
	}
    int q;
    cin >> q;
    while (q--) {
		cout << solve() << "\n";
	}
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cassert>
#include <algorithm>
#include <map>
#include <vector>

#define all(x) begin(x), end(x)
using namespace std;
typedef long long ll;
typedef long double ld;
#define int ll
#define all(x) begin(x), end(x)

const int N = 4e5;
const ll  P = 1e6 + 3;
const ll M1 = 1e7 + 21;
const ll M2 = 1e7 + 33;
ll pw1[N], pw2[N];

struct st {
	vector<int> a;
	ll h1, h2;
	void clear() {
		h1 = 0;
		h2 = 0;
		a.clear();
	}
	st() {
		clear();
	}
	int back() {
		if (a.empty()) {
			return -1;
		}
		return a.back();
	}
	void push(int v) {
		h1 = (h1 + v * pw1[a.size()]) % M1;
		h2 = (h2 + v * pw2[a.size()]) % M2;
		a.push_back(v);
	}
	void pop() {
		int v = a.back();
		a.pop_back();
		h1 = (h1 - v * pw1[a.size()] + M1 * M1) % M1;
		h2 = (h2 - v * pw2[a.size()] + M2 * M2) % M2;
	}
	ll value() {
		return (h1 << 32) + h2;
	}
};

int n, a[N];
st sta;

ll dc(int l, int r) {
	if (r - l <= 1) {
		return 0;
	}
	int m = (l + r) / 2;
	ll answer = dc(l, m) + dc(m, r);
	map<ll, int> cnt;
	sta.clear();
	for (int i = m; i < r; ++i) {
		if (sta.back() == a[i]) {
			sta.pop();
		} else {
			sta.push(a[i]);
		}
		++cnt[sta.value()];
	}
	sta.clear();
	for (int i = m - 1; i >= l; --i) {
		if (sta.back() == a[i]) {
			sta.pop();
		} else {
			sta.push(a[i]);
		}
		answer += cnt[sta.value()];
	}
	return answer;
}

ll solve() {
	cin >> n;
	for (int i = 0; i < n; ++i) {
		cin >> a[i];
	}
	return dc(0, n);
}

signed main() {
#ifdef LC
	assert(freopen("input.txt", "r", stdin));
#endif
    ios::sync_with_stdio(0);
    cin.tie(0);
    pw1[0] = 1;
    pw2[0] = 1;
    for (int i = 1; i < N; ++i) {
		pw1[i] = pw1[i - 1] * P % M1;
		pw2[i] = pw2[i - 1] * P % M2;
	}
    int q;
    cin >> q;
    while (q--) {
		cout << solve() << "\n";
	}
    return 0;
}


#pragma GCC target("sse,sse2,ssse3,sse4.1,avx,avx2")
#include <iostream>
#include <cstdio>
#include <cassert>
#include <algorithm>
#include <map>
#include <vector>

#define all(x) begin(x), end(x)
using namespace std;
typedef long long ll;
typedef long double ld;
#define int ll
#define all(x) begin(x), end(x)

const int N = 4e5;
const ll  P = 1e6 + 3;
const ll M1 = 1e7 + 21;
const ll M2 = 1e7 + 33;
ll pw1[N], pw2[N];

struct st {
	vector<int> a;
	ll h1, h2;
	void clear() {
		h1 = 0;
		h2 = 0;
		a.clear();
	}
	st() {
		clear();
	}
	int back() {
		if (a.empty()) {
			return -1;
		}
		return a.back();
	}
	void push(int v) {
		h1 = (h1 + v * pw1[a.size()]) % M1;
		h2 = (h2 + v * pw2[a.size()]) % M2;
		a.push_back(v);
	}
	void pop() {
		int v = a.back();
		a.pop_back();
		h1 = (h1 - v * pw1[a.size()] + M1 * M1) % M1;
		h2 = (h2 - v * pw2[a.size()] + M2 * M2) % M2;
	}
	ll value() {
		return (h1 << 32) + h2;
	}
};

int n, a[N];
st sta;

ll dc(int l, int r) {
	if (r - l <= 1) {
		return 0;
	}
	int m = (l + r) / 2;
	ll answer = dc(l, m) + dc(m, r);
	map<ll, int> cnt;
	sta.clear();
	for (int i = m; i < r; ++i) {
		if (sta.back() == a[i]) {
			sta.pop();
		} else {
			sta.push(a[i]);
		}
		++cnt[sta.value()];
	}
	sta.clear();
	for (int i = m - 1; i >= l; --i) {
		if (sta.back() == a[i]) {
			sta.pop();
		} else {
			sta.push(a[i]);
		}
		answer += cnt[sta.value()];
	}
	return answer;
}

ll solve() {
	cin >> n;
	for (int i = 0; i < n; ++i) {
		cin >> a[i];
	}
	return dc(0, n);
}

signed main() {
#ifdef LC
	assert(freopen("input.txt", "r", stdin));
#endif
    ios::sync_with_stdio(0);
    cin.tie(0);
    pw1[0] = 1;
    pw2[0] = 1;
    for (int i = 1; i < N; ++i) {
		pw1[i] = pw1[i - 1] * P % M1;
		pw2[i] = pw2[i - 1] * P % M2;
	}
    int q;
    cin >> q;
    while (q--) {
		cout << solve() << "\n";
	}
    return 0;
}


#pragma GCC optimize("O3")
#include <iostream>
#include <cstdio>
#include <cassert>
#include <algorithm>
#include <map>
#include <vector>

#define all(x) begin(x), end(x)
using namespace std;
typedef long long ll;
typedef long double ld;
#define int ll
#define all(x) begin(x), end(x)

const int N = 4e5;
const ll  P = 1e6 + 3;
const ll M1 = 1e7 + 21;
const ll M2 = 1e7 + 33;
ll pw1[N], pw2[N];

struct st {
	vector<int> a;
	ll h1, h2;
	void clear() {
		h1 = 0;
		h2 = 0;
		a.clear();
	}
	st() {
		clear();
	}
	int back() {
		if (a.empty()) {
			return -1;
		}
		return a.back();
	}
	void push(int v) {
		h1 = (h1 + v * pw1[a.size()]) % M1;
		h2 = (h2 + v * pw2[a.size()]) % M2;
		a.push_back(v);
	}
	void pop() {
		int v = a.back();
		a.pop_back();
		h1 = (h1 - v * pw1[a.size()] + M1 * M1) % M1;
		h2 = (h2 - v * pw2[a.size()] + M2 * M2) % M2;
	}
	ll value() {
		return (h1 << 32) + h2;
	}
};

int n, a[N];
st sta;

ll dc(int l, int r) {
	if (r - l <= 1) {
		return 0;
	}
	int m = (l + r) / 2;
	ll answer = dc(l, m) + dc(m, r);
	map<ll, int> cnt;
	sta.clear();
	for (int i = m; i < r; ++i) {
		if (sta.back() == a[i]) {
			sta.pop();
		} else {
			sta.push(a[i]);
		}
		++cnt[sta.value()];
	}
	sta.clear();
	for (int i = m - 1; i >= l; --i) {
		if (sta.back() == a[i]) {
			sta.pop();
		} else {
			sta.push(a[i]);
		}
		answer += cnt[sta.value()];
	}
	return answer;
}

ll solve() {
	cin >> n;
	for (int i = 0; i < n; ++i) {
		cin >> a[i];
	}
	return dc(0, n);
}

signed main() {
#ifdef LC
	assert(freopen("input.txt", "r", stdin));
#endif
    ios::sync_with_stdio(0);
    cin.tie(0);
    pw1[0] = 1;
    pw2[0] = 1;
    for (int i = 1; i < N; ++i) {
		pw1[i] = pw1[i - 1] * P % M1;
		pw2[i] = pw2[i - 1] * P % M2;
	}
    int q;
    cin >> q;
    while (q--) {
		cout << solve() << "\n";
	}
    return 0;
}


/*
  Warn - Don't change next line else you will get WA verdict. Online Judge is configured to give WA if next line is not present.
  "An ideal problem has no test data."
  Author - Aryan Choudhary (@aryanc403)
*/

// #pragma warning(disable:4996)
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize ("Ofast")
// #pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC optimize ("-ffloat-store")

#include<iostream>
#include<bits/stdc++.h>
#include<stdio.h>
using namespace std;
#define fo(i,n)   for(i=0;i<(n);++i)
#define repA(i,j,n)   for(i=(j);i<=(n);++i)
#define repD(i,j,n)   for(i=(j);i>=(n);--i)
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define endl "\n"
typedef long long int lli;
typedef long double mytype;
typedef pair<lli,lli> ii;
typedef vector<ii> vii;
typedef vector<lli> vi;

clock_t time_p=clock();
void aryanc403()
{
    time_p=clock()-time_p;
    cerr<<"Time Taken : "<<(float)(time_p)/CLOCKS_PER_SEC<<"\n";
}

#ifdef ARYANC403
#define dbg(...) { cerr<<"[ "; __aryanc403__(#__VA_ARGS__, __VA_ARGS__);}
#undef endl
template <typename Arg1,typename Arg2>
ostream& operator << (ostream& out, const pair<Arg1,Arg2> &x) {
    return out<<"("<<x.X<<","<<x.Y<<")";
}

template <typename Arg1>
ostream& operator << (ostream& out, const vector<Arg1> &a) {
    out<<"[";for(const auto &x:a)out<<x<<",";return out<<"]";
}

template <typename Arg1>
ostream& operator << (ostream& out, const set<Arg1> &a) {
    out<<"[";for(const auto &x:a)out<<x<<",";return out<<"]";
}

template <typename Arg1,typename Arg2>
ostream& operator << (ostream& out, const map<Arg1,Arg2> &a) {
    out<<"[";for(const auto &x:a)out<<x<<",";return out<<"]";
}

template <typename Arg1>
void __aryanc403__(const string name, Arg1&& arg1){
	cerr << name << " : " << arg1 << " ] " << endl;
}

template <typename Arg1, typename... Args>
void __aryanc403__(const string names, Arg1&& arg1, Args&&... args){
	const string name = names.substr(0,names.find(','));
	cerr<<name<<" : "<<arg1<<" | ";
	__aryanc403__(names.substr(1+(int)name.size()), args...);
}

#else
    #define dbg(args...)
#endif

const lli INF = 0xFFFFFFFFFFFFFFFL;

lli seed;
mt19937 rng(seed=chrono::steady_clock::now().time_since_epoch().count());
inline lli rnd(lli l=0,lli r=INF)
{return uniform_int_distribution<lli>(l,r)(rng);}

class CMP
{public:
bool operator()(ii a , ii b) //For min priority_queue .
{    return ! ( a.X < b.X || a.X==b.X && a.Y <= b.Y);   }};

void add( map<lli,lli> &m, lli x,lli cnt=1)
{
    auto jt=m.find(x);
    if(jt==m.end())         m.insert({x,cnt});
    else                    jt->Y+=cnt;
}

void del( map<lli,lli> &m, lli x,lli cnt=1)
{
    auto jt=m.find(x);
    if(jt->Y<=cnt)            m.erase(jt);
    else                      jt->Y-=cnt;
}

bool cmp(const ii &a,const ii &b)
{
    return a.X<b.X||(a.X==b.X&&a.Y<b.Y);
}

const lli mod = 1000000007L;

struct node{
    lli v;
    map<lli,node*> m;
    node(){v=0;m.clear();};
};

    lli T,n,i,j,k,in,cnt,l,r,u,v,x,y;
    lli m,ans;
    string s;
    vi a;
    //priority_queue < ii , vector < ii > , CMP > pq;// min priority_queue .

void solve(lli l,lli r)
{
    if(r-l+1<2)
        return;
    lli m=(l+r)/2;
    solve(l,m);
    solve(m+1,r);

    node *root=new node();
    vector<pair<node*,lli>> b;
    lli i,j;
    b.clear();
    b.pb(mp(root,-1));

    repA(i,m+1,r)
    {
        if(!b.empty()&&b.back().Y==a[i])
            b.pop_back();
        else
        {
            auto rt=b.back().X;
            auto it=rt->m.find(a[i]);

            if(it==rt->m.end())
            {
                rt->m[a[i]]=new node();
                it=rt->m.find(a[i]);
            }
            b.pb(mp(it->Y,a[i]));
        }
        b.back().X->v++;
    }

    // dbg(l,r,m,mm);
    b.clear();
    b.pb(mp(root,-1));

    repD(i,m,l)
    {
        if(!b.empty()&&b.back().Y==a[i])
            b.pop_back();
        else
        {
            auto rt=b.back().X;
            auto it=rt->m.find(a[i]);

            if(it==rt->m.end())
            {
                rt->m[a[i]]=new node();
                it=rt->m.find(a[i]);
            }
            b.pb(mp(it->Y,a[i]));
        }
        ans+=b.back().X->v;
        // dbg(b,mm[b]);
    }
}

int main(void) {
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    // freopen("txt.in", "r", stdin);
    // freopen("txt.out", "w", stdout);
// cout<<std::fixed<<std::setprecision(35);
cin>>T;while(T--)
{

    cin>>n;
    a.clear();a.reserve(n);

    fo(i,n)
    {
        cin>>in;
        a.pb(in);
    }

    ans=0;

    solve(0,n-1);

    cout<<ans<<endl;
}   aryanc403();
    return 0;
}


/*
  Warn - Don't change next line else you will get WA verdict. Online Judge is configured to give WA if next line is not present.
  "An ideal problem has no test data."
  Author - Aryan Choudhary (@aryanc403)
*/

// #pragma warning(disable:4996)
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize ("Ofast")
// #pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC optimize ("-ffloat-store")

#include<iostream>
#include<bits/stdc++.h>
#include<stdio.h>
using namespace std;
#define fo(i,n)   for(i=0;i<(n);++i)
#define repA(i,j,n)   for(i=(j);i<=(n);++i)
#define repD(i,j,n)   for(i=(j);i>=(n);--i)
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define endl "\n"
typedef long long int lli;
typedef long double mytype;
typedef pair<lli,lli> ii;
typedef vector<ii> vii;
typedef vector<lli> vi;

clock_t time_p=clock();
void aryanc403()
{
    time_p=clock()-time_p;
    cerr<<"Time Taken : "<<(float)(time_p)/CLOCKS_PER_SEC<<"\n";
}

#ifdef ARYANC403
#define dbg(...) { cerr<<"[ "; __aryanc403__(#__VA_ARGS__, __VA_ARGS__);}
#undef endl
template <typename Arg1,typename Arg2>
ostream& operator << (ostream& out, const pair<Arg1,Arg2> &x) {
    return out<<"("<<x.X<<","<<x.Y<<")";
}

template <typename Arg1>
ostream& operator << (ostream& out, const vector<Arg1> &a) {
    out<<"[";for(const auto &x:a)out<<x<<",";return out<<"]";
}

template <typename Arg1>
ostream& operator << (ostream& out, const set<Arg1> &a) {
    out<<"[";for(const auto &x:a)out<<x<<",";return out<<"]";
}

template <typename Arg1,typename Arg2>
ostream& operator << (ostream& out, const map<Arg1,Arg2> &a) {
    out<<"[";for(const auto &x:a)out<<x<<",";return out<<"]";
}

template <typename Arg1>
void __aryanc403__(const string name, Arg1&& arg1){
	cerr << name << " : " << arg1 << " ] " << endl;
}

template <typename Arg1, typename... Args>
void __aryanc403__(const string names, Arg1&& arg1, Args&&... args){
	const string name = names.substr(0,names.find(','));
	cerr<<name<<" : "<<arg1<<" | ";
	__aryanc403__(names.substr(1+(int)name.size()), args...);
}

#else
    #define dbg(args...)
#endif

const lli INF = 0xFFFFFFFFFFFFFFFL;

lli seed;
mt19937 rng(seed=chrono::steady_clock::now().time_since_epoch().count());
inline lli rnd(lli l=0,lli r=INF)
{return uniform_int_distribution<lli>(l,r)(rng);}

class CMP
{public:
bool operator()(ii a , ii b) //For min priority_queue .
{    return ! ( a.X < b.X || a.X==b.X && a.Y <= b.Y);   }};

void add( map<lli,lli> &m, lli x,lli cnt=1)
{
    auto jt=m.find(x);
    if(jt==m.end())         m.insert({x,cnt});
    else                    jt->Y+=cnt;
}

void del( map<lli,lli> &m, lli x,lli cnt=1)
{
    auto jt=m.find(x);
    if(jt->Y<=cnt)            m.erase(jt);
    else                      jt->Y-=cnt;
}

bool cmp(const ii &a,const ii &b)
{
    return a.X<b.X||(a.X==b.X&&a.Y<b.Y);
}

const lli mod = 1000000007L;

struct node{
    lli v;
    map<lli,node*> m;
    node(){v=0;m.clear();};
};

    lli T,n,i,j,k,in,cnt,l,r,u,v,x,y;
    lli m,ans;
    string s;
    vi a;
    //priority_queue < ii , vector < ii > , CMP > pq;// min priority_queue .

void del(node *rt)
{
    for(auto x:rt->m)
        del(x.Y);
    delete(rt);
}

void solve(lli l,lli r)
{
    if(r-l+1<2)
        return;
    lli m=(l+r)/2;
    solve(l,m);
    solve(m+1,r);

    node *root=new node();
    vector<pair<node*,lli>> b;
    lli i;
    b.clear();
    b.pb(mp(root,-1));

    repA(i,m+1,r)
    {
        if(!b.empty()&&b.back().Y==a[i])
            b.pop_back();
        else
        {
            auto rt=b.back().X;
            auto it=rt->m.find(a[i]);

            if(it==rt->m.end())
            {
                rt->m[a[i]]=new node();
                it=rt->m.find(a[i]);
            }
            b.pb(mp(it->Y,a[i]));
        }
        b.back().X->v++;
    }

    // dbg(l,r,m,mm);
    b.clear();
    b.pb(mp(root,-1));

    repD(i,m,l)
    {
        if(!b.empty()&&b.back().Y==a[i])
            b.pop_back();
        else
        {
            auto rt=b.back().X;
            auto it=rt->m.find(a[i]);

            if(it==rt->m.end())
            {
                rt->m[a[i]]=new node();
                it=rt->m.find(a[i]);
            }
            b.pb(mp(it->Y,a[i]));
        }
        ans+=b.back().X->v;
        // dbg(b,mm[b]);
    }

    del(root);
}

int main(void) {
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    // freopen("txt.in", "r", stdin);
    // freopen("txt.out", "w", stdout);
// cout<<std::fixed<<std::setprecision(35);
cin>>T;while(T--)
{

    cin>>n;
    a.clear();a.reserve(n);

    fo(i,n)
    {
        cin>>in;
        a.pb(in);
    }

    ans=0;

    solve(0,n-1);

    cout<<ans<<endl;
}   aryanc403();
    return 0;
}


/*
  Warn - Don't change next line else you will get WA verdict. Online Judge is configured to give WA if next line is not present.
  "An ideal problem has no test data."
  Author - Aryan Choudhary (@aryanc403)

*/

// #pragma warning(disable:4996)
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize ("Ofast")
// #pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC optimize ("-ffloat-store")

#include<iostream>
#include<bits/stdc++.h>
#include<stdio.h>
using namespace std;
#define fo(i,n)   for(i=0;i<(n);++i)
#define repA(i,j,n)   for(i=(j);i<=(n);++i)
#define repD(i,j,n)   for(i=(j);i>=(n);--i)
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define endl "\n"
typedef long long int lli;
typedef long double mytype;
typedef pair<lli,lli> ii;
typedef vector<ii> vii;
typedef vector<lli> vi;

clock_t time_p=clock();
void aryanc403()
{
    time_p=clock()-time_p;
    cerr<<"Time Taken : "<<(float)(time_p)/CLOCKS_PER_SEC<<"\n";
}

#ifdef ARYANC403
#define dbg(...) { cerr<<"[ "; __aryanc403__(#__VA_ARGS__, __VA_ARGS__);}
#undef endl
template <typename Arg1,typename Arg2>
ostream& operator << (ostream& out, const pair<Arg1,Arg2> &x) {
    return out<<"("<<x.X<<","<<x.Y<<")";
}

template <typename Arg1>
ostream& operator << (ostream& out, const vector<Arg1> &a) {
    out<<"[";for(const auto &x:a)out<<x<<",";return out<<"]";
}

template <typename Arg1>
ostream& operator << (ostream& out, const set<Arg1> &a) {
    out<<"[";for(const auto &x:a)out<<x<<",";return out<<"]";
}

template <typename Arg1,typename Arg2>
ostream& operator << (ostream& out, const map<Arg1,Arg2> &a) {
    out<<"[";for(const auto &x:a)out<<x<<",";return out<<"]";
}

template <typename Arg1>
void __aryanc403__(const string name, Arg1&& arg1){
	cerr << name << " : " << arg1 << " ] " << endl;
}

template <typename Arg1, typename... Args>
void __aryanc403__(const string names, Arg1&& arg1, Args&&... args){
	const string name = names.substr(0,names.find(','));
	cerr<<name<<" : "<<arg1<<" | ";
	__aryanc403__(names.substr(1+(int)name.size()), args...);
}

#else
    #define dbg(args...)
#endif

const lli INF = 0xFFFFFFFFFFFFFFFL;

lli seed;
mt19937 rng(seed=chrono::steady_clock::now().time_since_epoch().count());
inline lli rnd(lli l=0,lli r=INF)
{return uniform_int_distribution<lli>(l,r)(rng);}

class CMP
{public:
bool operator()(ii a , ii b) //For min priority_queue .
{    return ! ( a.X < b.X || a.X==b.X && a.Y <= b.Y);   }};

void add( map<lli,lli> &m, lli x,lli cnt=1)
{
    auto jt=m.find(x);
    if(jt==m.end())         m.insert({x,cnt});
    else                    jt->Y+=cnt;
}

void del( map<lli,lli> &m, lli x,lli cnt=1)
{
    auto jt=m.find(x);
    if(jt->Y<=cnt)            m.erase(jt);
    else                      jt->Y-=cnt;
}

bool cmp(const ii &a,const ii &b)
{
    return a.X<b.X||(a.X==b.X&&a.Y<b.Y);
}

const lli mod = 1000000007L;

struct node{
    lli v;
    map<lli,node*> m;
    node(){v=0;m.clear();};
};

    lli T,n,i,j,k,in,cnt,l,r,u,v,x,y;
    lli m,ans;
    string s;
    vi a;
    //priority_queue < ii , vector < ii > , CMP > pq;// min priority_queue .

void solve(lli l,lli r)
{
    if(r-l+1<2)
        return;
    lli m=(l+r)/2;
    solve(l,m);
    solve(m+1,r);

    node *root=new node();
    vector<pair<node*,lli>> b;
    lli i,j;
    b.clear();
    b.pb(mp(root,-1));

    repA(i,m+1,r)
    {
        if(!b.empty()&&b.back().Y==a[i])
            b.pop_back();
        else
        {
            auto rt=b.back().X;
            auto it=rt->m.find(a[i]);

            if(it==rt->m.end())
            {
                rt->m[a[i]]=new node();
                it=rt->m.find(a[i]);
            }
            b.pb(mp(it->Y,a[i]));
        }
        b.back().X->v++;
    }

    // dbg(l,r,m,mm);
    b.clear();
    b.pb(mp(root,-1));

    repD(i,m,l)
    {
        if(!b.empty()&&b.back().Y==a[i])
            b.pop_back();
        else
        {
            auto rt=b.back().X;
            auto it=rt->m.find(a[i]);

            if(it==rt->m.end())
            {
                rt->m[a[i]]=new node();
                it=rt->m.find(a[i]);
            }
            b.pb(mp(it->Y,a[i]));
        }
        ans+=b.back().X->v;
        // dbg(b,mm[b]);
    }
}

int main(void) {
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    // freopen("txt.in", "r", stdin);
    // freopen("txt.out", "w", stdout);
// cout<<std::fixed<<std::setprecision(35);
cin>>T;while(T--)
{

    cin>>n;
    a.clear();a.reserve(n);

    fo(i,n)
    {
        cin>>in;
        a.pb(in);
    }

    ans=0;

    solve(0,n-1);

    cout<<ans<<endl;
}   aryanc403();
    return 0;
}


#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#define _USE_MATH_DEFINES
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()

const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007; // 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
/*-------------------------------------------------*/
int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);
  // freopen("input.txt", "r", stdin);

  int q; cin >> q;
  while (q--) {
    int n; cin >> n;
    vector<int> a(n); REP(i, n) cin >> a[i];
    vector<int> nxt(n, INF);
    vector<map<int, int> > nxtX(n);
    for (int i = n - 2; i >= 0; --i) {
      if (a[i] == a[i + 1]) {
        nxt[i] = i + 1;
      } else if (nxtX[i + 1].count(a[i]) == 1) {
        nxt[i] = nxtX[i + 1][a[i]];
      }
      if (nxt[i] + 1 < n) {
        swap(nxtX[i], nxtX[nxt[i] + 1]);
        nxtX[i][a[nxt[i] + 1]] = nxt[i] + 1;
      }
      nxtX[i][a[i]] = i;
    }
    vector<long long> dp(n + 1, 0);
    for (int i = n - 2; i >= 0; --i) {
      if (nxt[i] != INF) dp[i] = dp[nxt[i] + 1] + 1;
    }
    cout << accumulate(ALL(dp), 0) << '\n';
  }
  return 0;
}


#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#define _USE_MATH_DEFINES
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()

const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007; // 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
/*-------------------------------------------------*/
int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);
  // freopen("input.txt", "r", stdin);

  int q; cin >> q;
  while (q--) {
    int n; cin >> n;
    vector<int> a(n); REP(i, n) cin >> a[i];
    vector<int> nxt(n, INF);
    vector<map<int, int> > nxtX(n);
    for (int i = n - 2; i >= 0; --i) {
      if (a[i] == a[i + 1]) {
        nxt[i] = i + 1;
      } else if (nxtX[i + 1].count(a[i]) == 1) {
        nxt[i] = nxtX[i + 1][a[i]];
      }
      if (nxt[i] + 1 < n) {
        swap(nxtX[i], nxtX[nxt[i] + 1]);
        nxtX[i][a[nxt[i] + 1]] = nxt[i] + 1;
      }
    }
    vector<long long> dp(n + 1, 0);
    for (int i = n - 2; i >= 0; --i) {
      if (nxt[i] != INF) dp[i] = dp[nxt[i] + 1] + 1;
    }
    cout << accumulate(ALL(dp), 0) << '\n';
  }
  return 0;
}


#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#define _USE_MATH_DEFINES
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()

const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007; // 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
/*-------------------------------------------------*/
int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);
  // freopen("input.txt", "r", stdin);

  int q; cin >> q;
  while (q--) {
    int n; cin >> n;
    vector<int> a(n); REP(i, n) cin >> a[i];
    vector<int> nxt(n, INF);
    vector<map<int, int> > nxtX(n);
    for (int i = n - 1; i >= 0; --i) {
      if (i + 1 < n) {
        if (a[i] == a[i + 1]) {
          nxt[i] = i + 1;
        } else if (nxtX[i + 1].count(a[i]) == 1) {
          nxt[i] = nxtX[i + 1][a[i]];
        }
      }
      if (nxt[i] + 1 < n) {
        swap(nxtX[i], nxtX[nxt[i] + 1]);
        nxtX[i][a[nxt[i] + 1]] = nxt[i] + 1;
      }
      nxtX[i][a[i]] = i;
    }
    vector<long long> dp(n + 1, 0);
    for (int i = n - 2; i >= 0; --i) {
      if (nxt[i] != INF) dp[i] = dp[nxt[i] + 1] + 1;
    }
    cout << accumulate(ALL(dp), 0LL) << '\n';
  }
  return 0;
}


#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#define _USE_MATH_DEFINES
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()

const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007; // 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
/*-------------------------------------------------*/
int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);
  // freopen("input.txt", "r", stdin);

  int q; cin >> q;
  while (q--) {
    int n; cin >> n;
    vector<int> a(n); REP(i, n) cin >> a[i];
    vector<int> nxt(n, INF);
    vector<map<int, int> > nxtX(n);
    for (int i = n - 1; i >= 0; --i) {
      if (i + 1 < n) {
        if (a[i] == a[i + 1]) {
          nxt[i] = i + 1;
        } else if (nxtX[i + 1].count(a[i]) == 1) {
          nxt[i] = nxtX[i + 1][a[i]];
        }
      }
      if (nxt[i] + 1 < n) {
        swap(nxtX[i], nxtX[nxt[i] + 1]);
        nxtX[i][a[nxt[i] + 1]] = nxt[i] + 1;
      }
      nxtX[i][a[i]] = i;
    }
    vector<long long> dp(n + 1, 0);
    for (int i = n - 2; i >= 0; --i) {
      if (nxt[i] != INF) dp[i] = dp[nxt[i] + 1] + 1;
    }
    cout << accumulate(ALL(dp), 0) << '\n';
  }
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define all(v) (v).begin(), (v).end()
#define sz(v) (int)(v).size()

typedef long long ll;
typedef pair<int, int> pii;

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

int a[N], link[N];
int dp[N];

int main()
{
	int t;
	scanf("%d", &t);
	while(t--) {
		int n;
		scanf("%d", &n);
		a[0] = -1;
		dp[0] = 0;
		ll ans = 0;
		for (int i = 1; i <= n; ++i) {
			scanf("%d", &a[i]);
			int x = i;
			while(x && a[x - 1] != a[i])
				x = link[x - 1];
			link[i] = max(0, x - 1);
			dp[i] = (link[i] ? dp[link[i] - 1] + 1 : 0);
			//printf("%d %d %d\n", i, link[i], dp[i]);
			ans += dp[i];
		}
		printf("%lld\n", ans);
	}
}


#include <bits/stdc++.h>

using namespace std;

#define all(v) (v).begin(), (v).end()
#define sz(v) (int)(v).size()

typedef long long ll;
typedef pair<int, int> pii;

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

int a[N];
map<int, int> link[N];
int dp[N];

int main()
{
	int t;
	scanf("%d", &t);
	while(t--) {
		int n;
		scanf("%d", &n);
		a[0] = -1;
		dp[0] = 0;
		ll ans = 0;
		for (int i = 1; i <= n; ++i) {
			int x;
			scanf("%d", &x);
			link[i].clear();
			dp[i] = 0;
			if (link[i - 1].count(x)) {
				int j = link[i - 1][x];
				dp[i] = dp[j - 1] + 1;
				--j;
				link[i].swap(link[j]);
				link[j][a[j]] = j;
			}
			link[i][x] = i;
			a[i] = x;
			//printf("%d %d %d\n", i, link[i][x], dp[i]);
			ans += dp[i];
		}
		printf("%lld\n", ans);
	}
}


/* In the name of Allah */
#include<bits/stdc++.h>
using namespace std;

const int N = 3e5 + 5;
int q, n, a[N], nxt[N], dp[N];

int main() {
	scanf("%d", &q);
	while (q--) {
		scanf("%d", &n);
		for (int i = 0; i < n; i++)
			scanf("%d", a + i);
		long long ans = dp[n] = 0;
		for (int i = n - 1; ~i; i--) {
			nxt[i] = i + 1;
			while (nxt[i] < n && a[nxt[i]] ^ a[i])
				nxt[i] = nxt[nxt[i]] + 1;
			ans += (dp[i] = nxt[i] < n? dp[nxt[i] + 1] + 1: 0);
		}
		printf("%lld\n", ans);
	}
}


/* In the name of Allah */
#include<bits/stdc++.h>
using namespace std;

const int N = 3e5 + 5;
unordered_map<int, int> mp[N];
int q, n, a[N], nxt[N], dp[N];

int main() {
	scanf("%d", &q);
	while (q--) {
		scanf("%d", &n); mp[n].clear();
		for (int i = dp[n] = 0; i < n; i++)
			scanf("%d", a + i);
		long long ans = 0;
		for (int i = n - 1; ~i; i--) {
			dp[i] = 0, nxt[i] = n, mp[i].clear();
			if (mp[i + 1].count(a[i])) {
				nxt[i] = mp[i + 1][a[i]];
				dp[i] = dp[++nxt[i]] + 1;
				swap(mp[i], mp[nxt[i]--]);
			}
			ans += dp[mp[i][a[i]] = i];
		}
		printf("%lld\n", ans);
	}
}


/* In the name of Allah */
#include<bits/stdc++.h>
using namespace std;

const int N = 3e5 + 5;
int q, n, a[N], nxt[N], dp[N];

int main() {
	scanf("%d", &q);
	while (q--) {
		scanf("%d", &n);
		for (int i = 0; i < n; i++)
			scanf("%d", a + i);
		long long ans = dp[n] = 0;
		for (int i = n - 1; ~i; i--) {
			nxt[i] = i + 1;
			while (nxt[i] < n && a[nxt[i]] ^ a[i])
				nxt[i] = min(nxt[nxt[i]] + 1, n);
			ans += max(dp[i] = nxt[i] < n? dp[nxt[i] + 1] + 1: 0, 0);
		}
		printf("%lld\n", ans);
	}
}


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

typedef long long ll;
const int maxn=3e5+7;
const int inf=INT_MAX;
const ll inff=1e18;
const ll mod=1e9+7;
#define pii pair<int,int>
#define mkp make_pair
#define F first
#define S second
#define pb push_back
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(),(v).end()
#define int ll

#ifdef HNO2
#define IOS
#else
#define endl '\n'
#define IOS ios::sync_with_stdio(0); cin.tie(0);
#endif // HNO2

int q,n;
int a[maxn];
int dp[maxn],nex[maxn],pp[maxn],depth[maxn];
int p[maxn][20];

int32_t main()
{
    IOS
    cin>>q;
    while (q--)
    {
        //clear
        cin>>n;
        for (int i=0;i<=n+2;i++)
        {
            dp[i]=0,nex[i]=0,depth[i]=0;
            for (int j=0;j<=19;j++) p[i][j]=0;
        }

        p[0][0]=-1;
        for (int i=1;i<=n;i++) cin>>a[i];
        for (int i=n;i>=1;i--)
        {
            nex[i]=pp[a[i]];
            pp[a[i]]=i;
        }
        int ans=0;
        for (int i=n;i>=1;i--)
        {
            int ok=0;
            if (nex[i])
            {
                if (nex[i]==i+1) ok=1;
                else
                {
                    int d=depth[i+1]-depth[nex[i]],now=i+1;
                    if (d>0)
                        for (int j=19;j>=0;j--)
                        {
                            if ((1<<j)&d) now=p[now][j];
                            if (now==-1) break;
                        }
                    if (now==nex[i]) ok=1;
                }
            }
            if (ok) dp[i]=dp[nex[i]+1]+1;
            if (ok&&nex[i]+1<=n) p[i][0]=nex[i]+1,depth[i]=depth[nex[i]+1]+1;
            else p[i][0]=-1;
            for (int j=1;j<=19;j++)
                if (p[i][j-1]==-1) p[i][j]=-1;
                else p[i][j]=p[p[i][j-1]][j-1];
            ans+=dp[i];
        }
        cout<<ans<<endl;
        for (int i=1;i<=n;i++) pp[a[i]]=0;
    }
}


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

typedef long long ll;
const int maxn=3e5+7;
const int inf=INT_MAX;
const ll inff=1e18;
const ll mod=1e9+7;
#define pii pair<int,int>
#define mkp make_pair
#define F first
#define S second
#define pb push_back
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(),(v).end()
#define int ll

#ifdef HNO2
#define IOS
#else
#define endl '\n'
#define IOS ios::sync_with_stdio(0); cin.tie(0);
#endif // HNO2

int q,n;
int a[maxn];
int dp[maxn],nex[maxn],pp[maxn],depth[maxn];
int p[maxn][20];

int32_t main()
{
    IOS
    cin>>q;
    while (q--)
    {
        //clear
        cin>>n;
        for (int i=0;i<=n+2;i++)
        {
            dp[i]=0,nex[i]=0,pp[i]=0,depth[i]=0;
            for (int j=0;j<=19;j++) p[i][j]=0;
        }

        p[0][0]=-1;
        for (int i=1;i<=n;i++) cin>>a[i];
        for (int i=n;i>=1;i--)
        {
            if (pp[a[i]]&&pp[a[i]]!=n) p[i][0]=pp[a[i]]+1;
            else p[i][0]=0;
            //cout<<":"<<i<<' '<<p[i][0]<<endl;
            depth[i]=depth[p[i][0]]+1;
            nex[i]=pp[a[i]];
            pp[a[i]]=i;
        }
        for (int i=0;i<=n;i++)
            for (int j=1;j<=19;j++)
                if (p[i][j-1]==-1) p[i][j]=-1;
                else p[i][j]=p[p[i][j-1]][j-1];
        int ans=0;
        for (int i=n;i>=1;i--)
        {
            if (nex[i])
            {
                int ok=0;
                if (nex[i]==i+1) ok=1;
                else
                {
                    int d=depth[i+1]-depth[nex[i]],now=i+1;
                    //cout<<d<<endl;
                    if (d>0)
                        for (int j=19;j>=0;j--)
                        {
                            if ((1<<j)&d) now=p[now][j];
                            if (now==-1) break;
                        }
                    if (now==nex[i]) ok=1;
                }
                if (ok) dp[i]=dp[nex[i]+1]+1;
            }
            //cout<<i<<' '<<dp[i]<<endl;
            ans+=dp[i];
        }
        cout<<ans<<endl;
    }
}


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

typedef long long ll;
const int maxn=3e5+7;
const int inf=INT_MAX;
const ll inff=1e18;
const ll mod=1e9+7;
#define pii pair<int,int>
#define mkp make_pair
#define F first
#define S second
#define pb push_back
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(),(v).end()
#define int ll

#ifdef HNO2
#define IOS
#else
#define endl '\n'
#define IOS ios::sync_with_stdio(0); cin.tie(0);
#endif // HNO2

int q,n;
int a[maxn];
int dp[maxn],nex[maxn],pp[maxn],depth[maxn];
int p[maxn][20];

int32_t main()
{
    IOS
    cin>>q;
    while (q--)
    {
        //clear
        cin>>n;
        for (int i=0;i<=n+2;i++)
        {
            dp[i]=0,nex[i]=0,depth[i]=0;
            for (int j=0;j<=19;j++) p[i][j]=0;
        }

        p[0][0]=-1;
        for (int i=1;i<=n;i++) cin>>a[i];
        for (int i=n;i>=1;i--)
        {
            nex[i]=pp[a[i]];
            pp[a[i]]=i;
        }
        int ans=0;
        for (int i=n;i>=1;i--)
        {
            if (nex[i])
            {
                int ok=0;
                if (nex[i]==i+1) ok=1;
                else
                {
                    int d=depth[i+1]-depth[nex[i]],now=i+1;
                    //cout<<d<<endl;
                    if (d>0)
                        for (int j=19;j>=0;j--)
                        {
                            if ((1<<j)&d) now=p[now][j];
                            if (now==-1) break;
                        }
                    if (now==nex[i]) ok=1;
                }
                if (ok)
                {
                    dp[i]=dp[nex[i]+1]+1;
                    if (nex[i]+1<=n) p[i][0]=nex[i]+1,depth[i]=depth[nex[i]+1]+1;
                    else p[i][0]=-1;
                    for (int j=1;j<=19;j++)
                        if (p[i][j-1]==-1) p[i][j]=-1;
                        else p[i][j]=p[p[i][j-1]][j-1];
                }
            }
            //cout<<i<<' '<<dp[i]<<endl;
            ans+=dp[i];
        }
        cout<<ans<<endl;
        for (int i=1;i<=n;i++) pp[a[i]]=0;
    }
}


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

typedef long long ll;
const int maxn=3e5+7;
const int inf=INT_MAX;
const ll inff=1e18;
const ll mod=1e9+7;
#define pii pair<int,int>
#define mkp make_pair
#define F first
#define S second
#define pb push_back
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(),(v).end()
#define int ll

#ifdef HNO2
#define IOS
#else
#define endl '\n'
#define IOS ios::sync_with_stdio(0); cin.tie(0);
#endif // HNO2

int q,n;
int a[maxn];
int dp[maxn],nex[maxn],pp[maxn],depth[maxn];
int p[maxn][20];

int32_t main()
{
    IOS
    cin>>q;
    while (q--)
    {
        //clear
        cin>>n;
        for (int i=0;i<=n+2;i++)
        {
            dp[i]=0,nex[i]=0,depth[i]=0;
            for (int j=0;j<=19;j++) p[i][j]=0;
        }

        p[0][0]=-1;
        for (int i=1;i<=n;i++) cin>>a[i];
        for (int i=n;i>=1;i--)
        {
            nex[i]=pp[a[i]];
            pp[a[i]]=i;
        }
        int ans=0;
        for (int i=n;i>=1;i--)
        {
            if (nex[i])
            {
                int ok=0;
                if (nex[i]==i+1) ok=1;
                else
                {
                    int d=depth[i+1]-depth[nex[i]],now=i+1;
                    //cout<<d<<endl;
                    if (d>0)
                        for (int j=19;j>=0;j--)
                        {
                            if ((1<<j)&d) now=p[now][j];
                            if (now==-1) break;
                        }
                    if (now==nex[i]) ok=1;
                }
                if (ok) dp[i]=dp[nex[i]+1]+1;
                if (ok&&nex[i]+1<=n) p[i][0]=nex[i]+1,depth[i]=depth[nex[i]+1]+1;
                else p[i][0]=-1;
                for (int j=1;j<=19;j++)
                    if (p[i][j-1]==-1) p[i][j]=-1;
                    else p[i][j]=p[p[i][j-1]][j-1];
            }
            //cout<<i<<' '<<dp[i]<<endl;
            ans+=dp[i];
        }
        cout<<ans<<endl;
        for (int i=1;i<=n;i++) pp[a[i]]=0;
    }
}


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

typedef long long ll;
const int maxn=3e5+7;
const int inf=INT_MAX;
const ll inff=1e18;
const ll mod=1e9+7;
#define pii pair<int,int>
#define mkp make_pair
#define F first
#define S second
#define pb push_back
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(),(v).end()
#define int ll

#ifdef HNO2
#define IOS
#else
#define endl '\n'
#define IOS ios::sync_with_stdio(0); cin.tie(0);
#endif // HNO2

int q;
int n;
int a[maxn],dp[maxn];
map<int,int> mm[maxn];

int32_t main()
{
    IOS
    cin>>q;
    while (q--)
    {
        cin>>n;
        for (int i=0;i<=n+3;i++) dp[i]=0,a[i]=0,mm[i].clear();
        for (int i=1;i<=n;i++) cin>>a[i];
        int ans=0;
        for (int i=n;i>=1;i--)
        {
            int nex;
            if (a[i+1]==a[i]) nex=i+1;
            else nex=mm[i+1][a[i]];
            if (nex!=0)
            {
                dp[i]=dp[nex+1]+1;
                ans+=dp[i];
                swap(mm[i],mm[nex+1]);
                if (a[nex+1]!=0) mm[i][a[nex+1]]=nex+1;
            }
        }
        cout<<ans<<endl;
    }
}


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

typedef long long ll;
const int maxn=3e5+7;
const int inf=INT_MAX;
const ll inff=1e18;
const ll mod=1e9+7;
#define pii pair<int,int>
#define mkp make_pair
#define F first
#define S second
#define pb push_back
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(),(v).end()
#define int ll

#ifdef HNO2
#define IOS
#else
#define endl '\n'
#define IOS ios::sync_with_stdio(0); cin.tie(0);
#endif // HNO2

int q,n;
int a[maxn];
int dp[maxn],nex[maxn],pp[maxn],depth[maxn];
int p[maxn][20];

int32_t main()
{
    IOS
    cin>>q;
    while (q--)
    {
        //clear
        cin>>n;
        for (int i=0;i<=n+2;i++)
        {
            dp[i]=0,nex[i]=0,depth[i]=0;
            for (int j=0;j<=19;j++) p[i][j]=0;
        }

        p[0][0]=-1;
        for (int i=1;i<=n;i++) cin>>a[i];
        for (int i=n;i>=1;i--)
        {
            if (pp[a[i]]&&pp[a[i]]!=n) p[i][0]=pp[a[i]]+1;
            else p[i][0]=0;
            //cout<<":"<<i<<' '<<p[i][0]<<endl;
            depth[i]=depth[p[i][0]]+1;
            nex[i]=pp[a[i]];
            pp[a[i]]=i;
        }
        for (int i=0;i<=n;i++)
            for (int j=1;j<=19;j++)
                if (p[i][j-1]==-1) p[i][j]=-1;
                else p[i][j]=p[p[i][j-1]][j-1];
        int ans=0;
        for (int i=n;i>=1;i--)
        {
            if (nex[i])
            {
                int ok=0;
                if (nex[i]==i+1) ok=1;
                else
                {
                    int d=depth[i+1]-depth[nex[i]],now=i+1;
                    //cout<<d<<endl;
                    if (d>0)
                        for (int j=19;j>=0;j--)
                        {
                            if ((1<<j)&d) now=p[now][j];
                            if (now==-1) break;
                        }
                    if (now==nex[i]) ok=1;
                }
                if (ok) dp[i]=dp[nex[i]+1]+1;
            }
            //cout<<i<<' '<<dp[i]<<endl;
            ans+=dp[i];
        }
        cout<<ans<<endl;
        for (int i=1;i<=n;i++) pp[a[i]]=0;
    }
}


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

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

  int test;
  cin >> test;
  while (test--)
  {
    int n, a[300300], l[300300], f[300300];
    map<int, int> *lMap[300300];
    cin >> n;
    long long ans = 0;
    for (int i = 0; i < n; i++)
    {
      cin >> a[i];
      f[i] = 0;
      l[i] = -1;
      lMap[i] = new map<int, int>();
      if (!i)
        continue;

      if (a[i] == a[i - 1]) l[i] = i - 1;
      else if (l[i - 1] >= 0 && lMap[i - 1]->count(a[i])) l[i] = (*lMap[i - 1])[a[i]];

      if (l[i] >= 0)
      {
        f[i] = 1;
        if (l[i])
        {
          lMap[i] = lMap[l[i] - 1];
          (*lMap[i])[a[l[i] - 1]] = l[i] - 1;
          f[i] += f[l[i] - 1];
        }
        ans += f[i];
      }
    }

    cout << ans << '\n';
  }
}


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

vector<int> id[300300];

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

  int test;
  cin >> test;
  while (test--)
  {
    int n, a[300300];
    cin >> n;
    vector<int> f(n + 1, 0), leftmost(n + 1);
    long long ans = 0;
    leftmost[0] = 1;
    for (int i = 1; i <= n; i++)
    {
      leftmost[i] = i + 1;
      cin >> a[i];
      if (!id[a[i]].empty())
      {
        int j = id[a[i]].back();
        if (j == i - 1 || leftmost[i - 1] == j + 1)
        {
          leftmost[i] = min(j, leftmost[j - 1]);
          f[i] = f[j - 1] + 1;
          ans += f[i];
        }
      }
      id[a[i]].push_back(i);
    }

    cout << ans << '\n';

    for (int i = 1; i <= n; i++)
      id[i].clear();
  }
}


//#define DEBUG
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

#define BSET(a, p) ((a) | (1ll << (p)))
#define BCHK(a, p) ((a) & (1ll << (p)))
#define BXOR(a, p) ((a) ^ (1ll << (p)));
#define BREM(a, p) (BCHK(a, p)?(BXOR(a, p)):(a))
#define BSHO(a, N) (bitset<N>(a))

#define fi first
#define sc second
#define pb push_back

#ifdef DEBUG
    #define dbg(s) {s;}
    #define PRELUDE
#endif

#ifndef DEBUG
    #define PRELUDE { ios :: sync_with_stdio(false); cin.tie(0); cout.tie(0); }
    #define dbg(s)
    #define endl "\n"
#endif

using namespace std;

#define int ll
#define i32 int32_t

#define RBTTYPE int
#define ordered_set tree<RBTTYPE, null_type, less<RBTTYPE>, rb_tree_tag,tree_order_statistics_node_update>

#define all(v) (v).begin(),(v).end()

typedef long long ll;
typedef long double ld;
typedef pair< int, int > pii;
typedef pair< ll, ll > pll;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
inline int ri(int x) { // from [0, n-1]
    return uniform_int_distribution<int>(0, x - 1)(rng);
}

inline ld rf() { // from [0, 1]
    return uniform_real_distribution<ld>(0, 1)(rng);
}

ll gcd(ll x, ll y) {
    if (x < y) return gcd(y, x);
    if (y == 0) return x;
    return gcd(y, x % y);
}

const ll mod = 1e9 + 7;

ll modexp(ll x, ll ex) {
    ll ans = 1ll;
    while (ex > 0) {
        if (ex & 1ll) ans = (ans * x) % mod;
        ex >>= 1ll;
        x = (x * x) % mod;
    }
    return ans;
}

const int maxn = 1e6 + 7;

const ll inf = 1e9 + 7;

inline void setmp(map < int, int > &mp, int x, int v) {
    if (mp.find(x) == mp.end()) mp[x] = v;
    else mp[x] = min(mp[x], v);
}

int a[maxn];
int dp[maxn];
int chn[maxn];
int minf[maxn];
map < int, int > fld[maxn];

void solve() {
    int n; cin >> n;
    for (int j = 1; j <= n; j++) {
        cin >> a[j];
        fld[j].clear();
        chn[j]  = -1;
        minf[j] = -1;
    }

    int ci = 0, ans = 0;

    for (int j = n - 1; j >= 1; j--) {

        dbg(cout << j << " : " << endl; );

        if (a[j + 1] == a[j]) {
            minf[j] = j + 1;
            if (j + 2 <= n) {
                if (chn[j + 2] >= 0) {
                    chn[j] = chn[j + 2];
                    setmp(fld[chn[j]], a[j + 2], j + 2);
                } else {
                    chn[j] = ++ci;
                    fld[ci][a[j + 2]] = j + 2;
                }
            } else {
                chn[j] = ++ci;
            }
        } else {
            if (chn[j + 1] < 0) continue;
            auto& mp = fld[chn[j + 1]];
            if (mp.find(a[j]) == mp.end()) continue;
            dbg(cout << a[j] << " >>: " << mp[a[j]] << endl;);
            int y = mp[a[j]]; assert(a[j] == a[y]);
            minf[j] = y;
            if (y + 1 <= n && chn[y + 1] >= 0) chn[j] = chn[y + 1];
            else chn[j] = ++ci;
            if (y + 1 <= n) setmp(fld[chn[j]], a[y + 1], y + 1);
        }

        dp[j] = 0;
        dbg(cout << chn[j] << " : " << minf[j] << endl;);

        if (minf[j] >= 0) {
            dp[j] = 1;
            if (minf[j] < n) dp[j] += dp[minf[j] + 1];
        }
        ans += dp[j];
    }

    cout << ans << endl;
}

i32 main() {
    //freopen("<file>.in", "r", stdin);
    //freopen("<file>.out", "w", stdout);

    PRELUDE;

    int q; cin >> q;
    while (q--) solve();

}


////#define DEBUG
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

#define BSET(a, p) ((a) | (1ll << (p)))
#define BCHK(a, p) ((a) & (1ll << (p)))
#define BXOR(a, p) ((a) ^ (1ll << (p)));
#define BREM(a, p) (BCHK(a, p)?(BXOR(a, p)):(a))
#define BSHO(a, N) (bitset<N>(a))

#define fi first
#define sc second
#define pb push_back

#ifdef DEBUG
    #define dbg(s) {s;}
    #define PRELUDE
#endif

#ifndef DEBUG
    #define PRELUDE { ios :: sync_with_stdio(false); cin.tie(0); cout.tie(0); }
    #define dbg(s)
    #define endl "\n"
#endif

using namespace std;

#define int ll
#define i32 int32_t

#define RBTTYPE int
#define ordered_set tree<RBTTYPE, null_type, less<RBTTYPE>, rb_tree_tag,tree_order_statistics_node_update>

#define all(v) (v).begin(),(v).end()

typedef long long ll;
typedef long double ld;
typedef pair< int, int > pii;
typedef pair< ll, ll > pll;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
inline int ri(int x) { // from [0, n-1]
    return uniform_int_distribution<int>(0, x - 1)(rng);
}

inline ld rf() { // from [0, 1]
    return uniform_real_distribution<ld>(0, 1)(rng);
}

ll gcd(ll x, ll y) {
    if (x < y) return gcd(y, x);
    if (y == 0) return x;
    return gcd(y, x % y);
}

const ll mod = 1e9 + 7;

ll modexp(ll x, ll ex) {
    ll ans = 1ll;
    while (ex > 0) {
        if (ex & 1ll) ans = (ans * x) % mod;
        ex >>= 1ll;
        x = (x * x) % mod;
    }
    return ans;
}

const int maxn = 1e6 + 7;

const ll inf = 1e9 + 7;

inline void setmp(map < int, int > &mp, int x, int v) {
    if (mp.find(x) == mp.end()) mp[x] = v;
    else mp[x] = min(mp[x], v);
}

int a[maxn];
int dp[maxn];
int chn[maxn];
int minf[maxn];
map < int, int > fld[maxn];

void solve() {
    int n; cin >> n;
    for (int j = 1; j <= n; j++) {
        cin >> a[j];
        fld[j].clear();
        chn[j]  = -1;
        minf[j] = -1;
        dp[j] = 0;
    }

    int ci = 0, ans = 0;

    for (int j = n - 1; j >= 1; j--) {

        dbg(cout << j << " : " << endl; );

        if (a[j + 1] == a[j]) {
            minf[j] = j + 1;
            if (j + 2 <= n) {
                if (chn[j + 2] >= 0) {
                    chn[j] = chn[j + 2];
                    setmp(fld[chn[j]], a[j + 2], j + 2);
                } else {
                    chn[j] = ++ci;
                    fld[ci][a[j + 2]] = j + 2;
                }
            } else {
                chn[j] = ++ci;
            }
        } else {
            if (chn[j + 1] < 0) continue;
            auto& mp = fld[chn[j + 1]];
            if (mp.find(a[j]) == mp.end()) continue;
            dbg(cout << a[j] << " >>: " << mp[a[j]] << endl;);
            int y = mp[a[j]]; assert(a[j] == a[y]);
            minf[j] = y;
            if (y + 1 <= n && chn[y + 1] >= 0) chn[j] = chn[y + 1];
            else chn[j] = ++ci;
            if (y + 1 <= n) setmp(fld[chn[j]], a[y + 1], y + 1);
        }

        dbg(cout << chn[j] << " : " << minf[j] << endl;);

        if (minf[j] >= 0) {
            dp[j] = 1;
            if (minf[j] < n) dp[j] += dp[minf[j] + 1];
        }
        ans += dp[j];
    }

    cout << ans << endl;
}

i32 main() {
    //freopen("<file>.in", "r", stdin);
    //freopen("<file>.out", "w", stdout);

    PRELUDE;

    int q; cin >> q;
    while (q--) solve();

}


#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 + 67;
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 = 1e9 + 9;
const ll B = 1e9 + 7;

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);
}