#include <bits/stdc++.h>
using namespace std;
const int maxn = int(3e5) + 100;
struct node {
  node *son[2];
  int value;
  node() {
    son[0] = son[1] = NULL;
    value = 0;
  };
  void init() {
    son[0] = son[1] = NULL;
    value = 0;
  }
};
int n;
int a[maxn];
int f[maxn][22];
node memory[maxn * 22];
node *mem = memory, *tree[maxn];
void read() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
}
node *updata(node *cur, int L, int R, int x, int v) {
  if (L > x || R < x) return cur;
  node *ret = mem++;
  if (cur) *ret = *cur;
  if (L == R) {
    ret->value = v;
    return ret;
  }
  int mid = (L + R) >> 1;
  ret->son[0] = updata(ret->son[0], L, mid, x, v);
  ret->son[1] = updata(ret->son[1], mid + 1, R, x, v);
  return ret;
}
int ask(node *cur, int L, int R, int x) {
  if (!cur) return 0;
  if (L == R) return cur->value;
  int mid = (L + R) >> 1;
  if (x <= mid)
    return ask(cur->son[0], L, mid, x);
  else
    return ask(cur->son[1], mid + 1, R, x);
}
void solve() {
  for (int i = 0; i <= n; ++i) tree[i] = NULL;
  for (node *cur = memory; cur != mem; ++cur) cur->init();
  mem = memory;
  int LG = 0;
  while ((1 << LG) <= n) ++LG;
  f[1][0] = 0;
  for (int i = 2; i <= n; ++i) {
    if (a[i] == a[i - 1])
      f[i][0] = i - 1;
    else
      f[i][0] = ask(tree[i - 1], 1, n, a[i]);
    if (f[i][0])
      tree[i] = updata(tree[f[i][0] - 1], 1, n, a[f[i][0] - 1], f[i][0] - 1);
  }
  for (int j = 1; j < LG; ++j)
    for (int i = 1; i <= n; ++i)
      if (f[i][j - 1])
        f[i][j] = f[f[i][j - 1] - 1][j - 1];
      else
        f[i][j] = 0;
  long long ans = 0;
  for (int i = 1; i <= n; ++i)
    for (int j = LG - 1, x = i; j >= 0; --j)
      if (f[x][j]) ans += (1LL << j), x = f[x][j] - 1;
  printf("%lld\n", ans);
}
int main() {
  int casesum;
  scanf("%d", &casesum);
  while (casesum--) {
    read();
    solve();
  }
  return 0;
}
