#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 20;
long long int ans;
int A[maxn];
void DAQ(int l, int r) {
  if (r - l < 2) {
    if (A[l] == 1) ans++;
    return;
  }
  int mid = (l + r) >> 1;
  vector<int> R;
  R.push_back(A[mid]);
  for (int i = mid + 1; i < r; i++) R.push_back(R.back() + A[i]);
  for (int i = 0; i < R.size(); i++) R[i] -= i + 1;
  sort(R.begin(), R.end());
  int sum = 0;
  for (int i = mid - 1; i >= l; i--) {
    sum += A[i];
    int x = mid - i - sum;
    ans +=
        upper_bound(R.begin(), R.end(), x) - lower_bound(R.begin(), R.end(), x);
  }
  DAQ(l, mid);
  DAQ(mid, r);
}
void solve() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  for (int i = 0; i < n; i++) {
    A[i] = s[i] - 48;
  }
  DAQ(0, n);
  cout << ans << "\n";
  ans = 0;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
