You are given an array $a_1, a_2, \dots , a_n$ consisting of integers from $0$ to $9$. A subarray $a_l, a_{l+1}, a_{l+2}, \dots , a_{r-1}, a_r$ is good if the sum of elements of this subarray is equal to the length of this subarray ($\sum\limits_{i=l}^{r} a_i = r - l + 1$).

For example, if $a = [1, 2, 0]$, then there are $3$ good subarrays: $a_{1 \dots 1} = [1], a_{2 \dots 3} = [2, 0]$ and $a_{1 \dots 3} = [1, 2, 0]$.

Calculate the number of good subarrays of the array $a$.


-----Input-----

The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases.

The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$) — the length of the array $a$.

The second line of each test case contains a string consisting of $n$ decimal digits, where the $i$-th digit is equal to the value of $a_i$.

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.


-----Output-----

For each test case print one integer — the number of good subarrays of the array $a$.


-----Example-----
Input
3
3
120
5
11011
6
600005

Output
3
6
1



-----Note-----

The first test case is considered in the statement.

In the second test case, there are $6$ good subarrays: $a_{1 \dots 1}$, $a_{2 \dots 2}$, $a_{1 \dots 2}$, $a_{4 \dots 4}$, $a_{5 \dots 5}$ and $a_{4 \dots 5}$. 

In the third test case there is only one good subarray: $a_{2 \dots 6}$.

Source: codeforces | Difficulty: MEDIUM_HARD
Tags: Dynamic programming

Data structures

Mathematics


Solutions:
res1 = [0, 1, 3]
for i in range(3, 10 ** 5 + 1):
	res1.append(res1[-1] + i)

def main(case):
	n = int(input())
	nums = input()
	res = [0 for _ in range(2 * 10 * n + 1)]
	cur = 0
	for i in nums:
		a = int(i)
		cur += a - 1
		res[cur + n] += 1
	sm = 0
	for i in res:
		if i > 0:
			sm += res1[i - 1]
	print(sm + res[n])
t = int(input())
for i in range(t):
	main(i + 1)


from collections import Counter
import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	lis = list(map(int, input().strip()))
	c = [0] * (n + 1)
	for i in range(1, n + 1):
		c[i] = c[i - 1] + lis[i - 1]
	p = [c[i] - i for i in range(n + 1)]
	p = Counter(p)
	ans = 0
	for i in p.values():
		ans += i * (i - 1) // 2
	print(ans)


from collections import defaultdict

def solve():
	N = int(input())
	A = list(map(int, input()))
	P = [A[0]]
	for i in range(1, N):
		P.append(P[-1] + A[i])
	ans = 0
	cnt = defaultdict(int)
	for i in range(N - 1, -1, -1):
		q = P[i] - i
		cnt[q] += 1
		ans += cnt[q - A[i] + 1]
	return ans
TC = int(input())
for _ in range(TC):
	print(solve())


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from collections import Counter
for j in range(int(input())):
	n = int(input())
	vals = [int(k) - 1 for k in input()]
	sum1 = [0]
	for s in range(len(vals)):
		sum1.append(sum1[-1] + vals[s])
	a = Counter(sum1)
	ans = 0
	for s in a:
		ans += a[s] * (a[s] - 1) // 2
	print(ans)


for _ in range(int(input())):
	n = int(input())
	s = input()
	ps = []
	summ = 0
	for i in s:
		summ += int(i) - 1
		ps.append(summ)
	ps = [0] + ps
	map1 = {}
	for i in ps:
		if map1.get(i) == None:
			map1[i] = 1
		else:
			map1[i] += 1
	cnt = 0
	for i in map1.keys():
		if map1[i] >= 2:
			cnt += map1[i] / 2 * (map1[i] - 1)
	print(int(cnt))


(z, zz) = (input, lambda : list(map(int, z().split())))
zzz = lambda : [int(i) for i in stdin.readline().split()]
(szz, graph, mod, szzz) = (lambda : sorted(zz()), {}, 10 ** 9 + 7, lambda : sorted(zzz()))
from string import *
from re import *
from collections import *
from queue import *
from sys import *
from collections import *
from math import *
from heapq import *
from itertools import *
from bisect import *
from collections import Counter as cc
from math import factorial as f
from bisect import bisect as bs
from bisect import bisect_left as bsl
from itertools import accumulate as ac

def lcd(xnum1, xnum2):
	return xnum1 * xnum2 // gcd(xnum1, xnum2)

def prime(x):
	p = ceil(x ** 0.5) + 1
	for i in range(2, p):
		if x % i == 0 and x != 2 or x == 0:
			return 0
	return 1

def dfs(u, visit, graph):
	visit[u] = True
	for i in graph[u]:
		if not visit[i]:
			dfs(i, visit, graph)
num = int(z())
res = []
for _ in range(num):
	n = int(z())
	lst = [0] * (n + 1)
	j = 0
	for i in stdin.readline():
		lst[j] = int(i) + lst[j - 1]
		j += 1
		if j >= n:
			break
	ans = 0
	li = [1] + [0] * (9 * n)
	for i in range(n):
		t = lst[i] - i - 1
		ans += li[t]
		li[t] += 1
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	s = list(map(int, list(input())))
	A = [0] * (n * 9 + 10)
	ind = 0
	ans = 0
	for i in range(n):
		A[ind] += 1
		ind += int(s[i]) - 1
		ans += A[ind]
	print(ans)


import sys
input = lambda : sys.stdin.readline().strip()
char = [chr(i) for i in range(97, 123)]
CHAR = [chr(i) for i in range(65, 91)]
mp = lambda : list(map(int, input().split()))
INT = lambda : int(input())
rn = lambda : range(INT())
from math import ceil, sqrt, factorial, gcd
for _ in rn():
	n = INT()
	l = list(map(lambda x: int(x) - 1, list(input())))
	sm_dict = {}
	sm = 0
	ans = 0
	for i in range(n):
		sm += l[i]
		if sm in sm_dict:
			sm_dict[sm] += 1
			if sm == 0:
				ans += sm_dict[sm]
			else:
				ans += sm_dict[sm] - 1
		else:
			sm_dict[sm] = 1
			if sm == 0:
				ans += 1
	print(ans)


from collections import defaultdict
T = int(input())
for t in range(T):
	n = int(input())
	xs = [int(x) for x in input()]
	seen = defaultdict(int)
	ans = 0
	cum = 0
	seen[cum] += 1
	for (i, x) in enumerate(xs):
		x -= 1
		cum += x
		ans += seen[cum]
		seen[cum] += 1
	print(ans)


for _ in range(int(input())):
	am = int(input())
	arr = list(map(int, list(input())))
	p = [0]
	for i in range(am):
		p.append(p[-1] + arr[i] - 1)
	obj = {}
	for i in range(am + 1):
		if p[i] in obj:
			obj[p[i]] += 1
		else:
			obj[p[i]] = 1
	c = 0
	for (i, d) in obj.items():
		c += d * (d - 1) // 2
	print(c)


from itertools import accumulate
from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	A = [0] + list(map(int, input()))
	B = list(accumulate(A))
	for i in range(len(B)):
		B[i] = B[i] - i
	Map = Counter(B)
	ans = 0
	for i in Map.values():
		ans += i * (i - 1) // 2
	print(ans)


import math
import heapq, bisect
import sys
from collections import deque, defaultdict
from fractions import Fraction
import sys
mod = 10 ** 9 + 7
mod1 = 998244353
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192
sys.setrecursionlimit(300000)

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

class SegmentTree:

	def __init__(self, data, default=0, func=lambda a, b: a + b):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class Factorial:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorials = [1, 1]
		self.invModulos = [0, 1]
		self.invFactorial_ = [1, 1]

	def calc(self, n):
		if n <= -1:
			print('Invalid argument to calculate n!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.factorials):
			return self.factorials[n]
		nextArr = [0] * (n + 1 - len(self.factorials))
		initialI = len(self.factorials)
		prev = self.factorials[-1]
		m = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * i % m
		self.factorials += nextArr
		return self.factorials[n]

	def inv(self, n):
		if n <= -1:
			print('Invalid argument to calculate n^(-1)')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		p = self.MOD
		pi = n % p
		if pi < len(self.invModulos):
			return self.invModulos[pi]
		nextArr = [0] * (n + 1 - len(self.invModulos))
		initialI = len(self.invModulos)
		for i in range(initialI, min(p, n + 1)):
			next = -self.invModulos[p % i] * (p // i) % p
			self.invModulos.append(next)
		return self.invModulos[pi]

	def invFactorial(self, n):
		if n <= -1:
			print('Invalid argument to calculate (n^(-1))!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.invFactorial_):
			return self.invFactorial_[n]
		self.inv(n)
		nextArr = [0] * (n + 1 - len(self.invFactorial_))
		initialI = len(self.invFactorial_)
		prev = self.invFactorial_[-1]
		p = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * self.invModulos[i % p] % p
		self.invFactorial_ += nextArr
		return self.invFactorial_[n]

class Combination:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorial = Factorial(MOD)

	def ncr(self, n, k):
		if k < 0 or n < k:
			return 0
		k = min(k, n - k)
		f = self.factorial
		return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD

def powm(a, n, m):
	if a == 1 or n == 0:
		return 1
	if n % 2 == 0:
		s = powm(a, n // 2, m)
		return s * s % m
	else:
		return a * powm(a, n - 1, m) % m

def sort_list(list1, list2):
	zipped_pairs = zip(list2, list1)
	z = [x for (_, x) in sorted(zipped_pairs)]
	return z

def product(l):
	por = 1
	for i in range(len(l)):
		por *= l[i]
	return por

def binarySearchCount(arr, n, key):
	left = 0
	right = n - 1
	count = 0
	while left <= right:
		mid = int((right + left) / 2)
		if arr[mid] < key:
			count = mid + 1
			left = mid + 1
		else:
			right = mid - 1
	return count

def countdig(n):
	c = 0
	while n > 0:
		n //= 10
		c += 1
	return c

def binary(x, length):
	y = bin(x)[2:]
	return y if len(y) >= length else '0' * (length - len(y)) + y

def countGreater(arr, n, k):
	l = 0
	r = n - 1
	leftGreater = n
	while l <= r:
		m = int(l + (r - l) / 2)
		if arr[m] >= k:
			leftGreater = m
			r = m - 1
		else:
			l = m + 1
	return n - leftGreater

class BinaryTrie:

	class Node:

		def __init__(self, bit: bool=False):
			self.bit = bit
			self.children = []
			self.count = 0
			self.counter = 1

	def __init__(self, size):
		self.root = BinaryTrie.Node()
		self.size = size

	def convert(self, key):
		bits = []
		if isinstance(key, int):
			key = bin(key)[2:]
		if isinstance(key, str):
			for i in range(self.size - len(key)):
				bits += [False]
			for i in key:
				if i == '0':
					bits += [False]
				else:
					bits += [True]
		else:
			return list(key)
		return bits

	def add(self, key):
		node = self.root
		bits = self.convert(key)
		for bit in bits:
			found_in_child = False
			for child in node.children:
				if child.bit == bit:
					child.counter += 1
					node = child
					found_in_child = True
					break
			if not found_in_child:
				new_node = BinaryTrie.Node(bit)
				node.children.append(new_node)
				node = new_node
		node.count += 1

	def remove(self, key):
		node = self.root
		bits = self.convert(key)
		nodelist = [node]
		for bit in bits:
			for child in node.children:
				if child.bit == bit:
					node = child
					node.counter -= 1
					nodelist.append(node)
					break
		node.count -= 1
		if not node.children and (not node.count):
			for i in range(len(nodelist) - 2, -1, -1):
				nodelist[i].children.remove(nodelist[i + 1])
				if nodelist[i].children or nodelist[i].count:
					break

	def query(self, prefix, root=None):
		if not root:
			root = self.root
		node = root
		if not root.children:
			return 0
		for bit in prefix:
			bit_not_found = True
			for child in node.children:
				if child.bit == bit:
					bit_not_found = False
					node = child
					break
			if bit_not_found:
				return 0
		return node

class TrieNode:

	def __init__(self):
		self.children = [None] * 26
		self.data = 0
		self.isEndOfWord = False

class Trie:

	def __init__(self):
		self.root = self.getNode()

	def getNode(self):
		return TrieNode()

	def _charToIndex(self, ch):
		return ord(ch) - ord('a')

	def insert(self, key, val):
		pCrawl = self.root
		length = len(key)
		for level in range(length):
			index = self._charToIndex(key[level])
			if not pCrawl.children[index]:
				pCrawl.children[index] = self.getNode()
			pCrawl = pCrawl.children[index]
			pCrawl.data += val
		pCrawl.isEndOfWord = True

	def search(self, key):
		pCrawl = self.root
		length = len(key)
		c = 0
		for level in range(length):
			index = self._charToIndex(key[level])
			if not pCrawl.children[index]:
				return False
			pCrawl = pCrawl.children[index]
		return pCrawl != None

	def present(self, key):
		ans = 0
		pCrawl = self.root
		length = len(key)
		c = 0
		for level in range(length):
			index = self._charToIndex(key[level])
			if not pCrawl.children[index]:
				return False
			pCrawl = pCrawl.children[index]
			ans += pCrawl.data
		if pCrawl != None:
			return ans
		return -1
for ik in range(int(input())):
	n = int(input())
	l = list(input())
	l = [int(l[i]) for i in range(n)]
	dp = [0 for i in range(n)]
	last = defaultdict(int)
	last1 = defaultdict(int)
	if l[0] == 1:
		dp[0] = 1
	tot = 1 - l[0]
	last[tot] = 1
	for i in range(1, n):
		c = 1 - l[i]
		tot += 1 - l[i]
		if last[tot] != 0:
			dp[i] += dp[last[tot] - 1] + 1
		if c == 0 and last[tot] != i:
			dp[i] += 1
		if tot == 0 and last[tot] == 0:
			dp[i] += 1
		last[tot] = i + 1
	print(sum(dp))


from collections import defaultdict

def solve(arr):
	d = defaultdict(int)
	ans = 0
	cur = 0
	d[0] += 1
	for i in range(len(arr)):
		cur += arr[i] - 1
		ans += d[cur]
		d[cur] += 1
	print(ans)
t = int(input())
for i in range(t):
	input()
	solve(list(map(int, list(input()))))


for _ in range(int(input())):
	n = int(input())
	s = input()
	cnt = [0] * (n * 9 + 2)
	ind = 0
	ans = 0
	for i in range(n):
		cnt[ind] += 1
		ind += int(s[i]) - 1
		ans += cnt[ind]
	print(ans)


import sys, math, cmath, time
start_time = time.time()
from collections import Counter

def solve():
	n = inp()
	s = insr()
	a = [int(s[i]) - 1 for i in range(len(s))]
	b = [0]
	for i in range(len(a)):
		b.append(b[-1] + a[i])
	q = counter_elements(b)
	w = 0
	for i in q:
		w = w + q[i] * (q[i] - 1) // 2
	print(w)

def main():
	global tt
	if not ONLINE_JUDGE:
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	t = 1
	t = inp()
	for tt in range(t):
		solve()
	if not ONLINE_JUDGE:
		print('Time Elapsed :', time.time() - start_time, 'seconds')
		sys.stdout.close()

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	return input().strip()

def invr():
	return map(int, input().split())

def counter(a):
	q = [0] * 100
	for i in range(len(a)):
		q[a[i] - 1] = q[a[i] - 1] + 1
	return q

def counter_elements(a):
	q = dict()
	for i in range(len(a)):
		if a[i] not in q:
			q[a[i]] = 0
		q[a[i]] = q[a[i]] + 1
	return q

def string_counter(a):
	q = [0] * 26
	for i in range(len(a)):
		q[ord(a[i]) - 97] = q[ord(a[i]) - 97] + 1
	return q

def factors(n):
	q = []
	for i in range(1, int(n ** 0.5) + 1):
		if n % i == 0:
			q.append(i)
			q.append(n // i)
	return list(sorted(list(set(q))))

def prime_factors(n):
	q = []
	while n % 2 == 0:
		q.append(2)
		n = n // 2
	for i in range(3, int(n ** 0.5) + 1, 2):
		while n % i == 0:
			q.append(i)
			n = n // i
	if n > 2:
		q.append(n)
	return list(sorted(q))

def transpose(a):
	(n, m) = (len(a), len(a[0]))
	b = [[0] * n for i in range(m)]
	for i in range(m):
		for j in range(n):
			b[i][j] = a[j][i]
	return b

def factorial(n, m=1000000007):
	q = 1
	for i in range(n):
		q = q * (i + 1) % m
	return q
ONLINE_JUDGE = __debug__
if ONLINE_JUDGE:
	input = sys.stdin.readline
main()


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	s = input()
	ans = 0
	d = defaultdict(int)
	t = 0
	for i in s:
		d[t] += 1
		t += int(i) - 1
		ans += d[t]
	print(ans)


cases = int(input())
for i in range(cases):
	x = int(input())
	a = input()
	b = {0: 1}
	(res, s) = (0, 0)
	for i in range(x):
		s += int(a[i])
		x = s - i - 1
		if x not in b:
			b[x] = 0
		b[x] += 1
		res += b[x] - 1
	print(res)


from collections import defaultdict as dc
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input()))
	p = [0] * n
	p[0] = l[0]
	c = 0
	if p[0] == 1:
		c += 1
	for i in range(1, n):
		p[i] = p[i - 1] + l[i]
		if p[i] == i + 1:
			c += 1
	q = [0] * n
	x = dc(int)
	for i in range(n):
		k = p[i] - i
		if k in x:
			c += x[k]
			x[k] += 1
		else:
			x[k] += 1
	print(c)


def solve():
	n = int(input())
	a = list(map(int, input()))
	s = [0]
	d = {}
	for i in range(n):
		s.append(s[-1] + a[i])
	for i in range(n + 1):
		val = s[i] - i
		if val not in d:
			d[val] = 0
		d[val] += 1
	ans = 0
	for value in d.values():
		ans += value * (value - 1) // 2
	print(ans)
for _ in range(int(input())):
	solve()


import collections
for _ in range(int(input())):
	n = int(input())
	a = input()
	pref = collections.defaultdict(int)
	pref[0] = 1
	pref_sum = 0
	ans = 0
	for (i, num) in enumerate([int(i) for i in a], 1):
		pref_sum += num
		target = pref_sum - i
		ans += pref[target]
		pref[target] += 1
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = input()
	f = {0: 1}
	current = 0
	res = 0
	for i in a:
		current += int(i) - 1
		if current in f:
			res += f[current]
		else:
			f[current] = 0
		f[current] += 1
	print(res)


tests = int(input())
n = []
s = []
for _ in range(tests):
	n.append(int(input()))
	s.append(list(map(int, input())))
for t in range(tests):
	good = 0
	hash = {}
	hash[0] = 1
	prefix_sum = 0
	sub_len = 0
	for i in range(n[t]):
		prefix_sum += s[t][i]
		sub_len = i + 1
		if prefix_sum - sub_len in hash:
			good += hash[prefix_sum - sub_len]
			hash[prefix_sum - sub_len] += 1
		else:
			hash[prefix_sum - sub_len] = 1
	print(good)


T = int(input())
for _ in range(T):
	n = int(input())
	s = input()
	arr = []
	for i in s:
		arr.append(int(i) - 1)
	h = dict()
	ans = 0
	sum1 = 0
	h[0] = 1
	for i in range(n):
		sum1 += arr[i]
		if sum1 in h:
			ans += h[sum1]
			h[sum1] += 1
		else:
			h[sum1] = 1
	print(ans)


from sys import stdin, stdout
t = int(stdin.readline().strip())
for _ in range(t):
	n = int(stdin.readline().strip())
	arr = list(map(int, list(stdin.readline().strip())))
	for i in range(len(arr)):
		arr[i] -= 1
	tarr = [0]
	d = {0: 1}
	for i in range(len(arr)):
		tarr.append(tarr[-1] + arr[i])
		if tarr[-1] in d:
			d[tarr[-1]] += 1
		else:
			d[tarr[-1]] = 1
	ans = 0
	for i in d:
		ans += d[i] * (d[i] - 1) // 2
	stdout.write(str(ans) + '\n')


from collections import *
from itertools import accumulate
for _ in range(int(input())):
	n = int(input())
	l = [0] + list(accumulate(map(lambda x: int(x) - 1, input())))
	d = defaultdict(int)
	ans = 0
	for i in l:
		d[i] += 1
	for i in d.values():
		ans += i * (i - 1) // 2
	print(ans)


t = int(input())
while t != 0:
	t = t - 1
	n = int(input())
	s = input()
	arr = [0] * n
	for i in range(0, n):
		arr[i] = int(s[i]) - 1
	sum = 0
	count = 0
	map = {}
	k = 0
	for i in range(0, n):
		sum = sum + arr[i]
		if sum == 0:
			count = count + 1
		if map.__contains__(sum) == True:
			count = count + map[sum]
		if map.__contains__(sum) == True:
			k = map[sum]
			k = k + 1
			map[sum] = k
		else:
			map[sum] = 1
	print(count)


T = int(input().strip())
for i in range(T):
	N = int(input().strip())
	a = [i for i in input().strip()]
	d = {0: 1}
	a.insert(0, 'f')
	(answer, s) = (0, 0)
	for j in range(1, N + 1):
		s += int(a[j])
		x = s - j
		if x not in d:
			d[x] = 0
		answer += d[x]
		d[x] += 1
	print(answer)


def process(n, c):
	di = {0: 1}
	ans = 0
	temp = 0
	for ele in c:
		temp += ele
		if temp not in di:
			di[temp] = 1
		else:
			di[temp] += 1
		ans += di[temp] - 1
	print(ans)
for t in range(int(input())):
	n = int(input())
	c = [int(k) - 1 for k in input()]
	process(n, c)


import sys
input = sys.stdin.readline
from math import ceil, log2
for _ in range(int(input())):
	n = int(input())
	s = input()
	arr = []
	for i in range(n):
		arr.append(int(s[i]))
	sumarr = [arr[0]]
	for i in range(1, n):
		sumarr.append(sumarr[i - 1] + arr[i])
	for i in range(len(sumarr)):
		sumarr[i] = sumarr[i] - i - 1
	count = 0
	sumarr.insert(0, 0)
	dic = dict()
	dic[0] = 0
	for i in range(len(sumarr)):
		if sumarr[i] not in dic:
			dic[sumarr[i]] = 0
		temp = dic[sumarr[i]]
		dic[sumarr[i]] += 1
		count += temp
	print(count)


from sys import stdin, stdout
t = int(stdin.readline())
for _ in range(t):
	n = int(stdin.readline())
	ar = stdin.readline()
	di = {1: 1}
	psum = 0
	countps = 0
	for i in range(0, n):
		psum += int(ar[i])
		if psum - i in di:
			countps += di[psum - i]
		if psum - i in di:
			di[psum - i] += 1
		else:
			di[psum - i] = 1
	print(countps)


import sys
import math

def int_arr():
	return list(map(int, sys.stdin.readline().split()))

def str_arr():
	return list(map(str, sys.stdin.readline().split()))

def input():
	return sys.stdin.readline().strip()
for _ in range(int(input())):
	n = int(input())
	st = input()
	prev = 0
	ans = 0
	dict = {}
	for i in range(n):
		prev += int(st[i]) - 1
		if dict.get(prev, None) != None:
			if prev == 0:
				ans += 1
			ans += dict[prev]
			dict[prev] += 1
		else:
			if prev == 0:
				ans += 1
			dict[prev] = 1
	print(ans)


T = int(input())
for _ in range(T):
	N = int(input())
	A = input()
	cums = [0]
	for c in A:
		cums.append(cums[-1] + int(c) - 1)
	from collections import Counter
	ctr = Counter(cums)
	ans = 0
	for v in ctr.values():
		ans += v * (v - 1) // 2
	print(ans)


for _ in range(int(input())):
	n = int(input())
	nums = input()
	d = {0: 1}
	(s, ans) = (0, 0)
	for i in range(n):
		s += int(nums[i]) - 1
		ans += d.get(s, 0)
		d[s] = d.get(s, 0) + 1
	print(ans)


from itertools import accumulate
from collections import Counter
t = int(input())
for i in range(t):
	n = int(input())
	s = [int(i) for i in input()]
	a = [0] + list(accumulate(s))
	b = [a[i] - i for i in range(n + 1)]
	cnt = Counter(b)
	ans = 0
	for each in cnt.values():
		ans += each * (each - 1) // 2
	print(ans)


from sys import stdin

def inp():
	return stdin.buffer.readline().rstrip().decode('utf8')

def itg():
	return int(stdin.buffer.readline())

def mpint():
	return map(int, stdin.buffer.readline().split())
from collections import defaultdict

def subarray_sum_equal_length(iterable):
	sum_ = result = 0
	d = defaultdict(int)
	d[0] += 1
	index = 1
	for item in iterable:
		sum_ += item
		result += d[sum_ - index]
		d[sum_ - index] += 1
		index += 1
	return result
for case in range(1, itg() + 1):
	n = itg()
	aa = map(int, inp())
	print(subarray_sum_equal_length(aa))


import collections
T = int(input())
while T > 0:
	n = int(input())
	arr = input()
	dp = [0] * (n + 1)
	dic = collections.defaultdict(int)
	dic[0] = 1
	res = 0
	for i in range(n):
		dp[i + 1] = int(arr[i]) + dp[i]
		res += dic[dp[i + 1] - (i + 1)]
		dic[dp[i + 1] - (i + 1)] += 1
	print(res)
	T -= 1


import collections
t = int(input())
for _ in range(t):
	n = int(input())
	l = input()
	l = list(l)
	(a, b, c) = (0, collections.Counter(), 0)
	b[0] = 1
	for i in range(n):
		c = c + int(l[i])
		x = c - i - 1
		b[x] += 1
		a += b[x] - 1
	print(a)


import array as arr
offset = int(100000.0)
p = arr.array('i')
br = arr.array('i')
t = int(input())
ans = []
cnt = 0
for i in range(2 * offset + 10):
	br.append(0)
	p.append(0)
	ans.append(0)
	cnt += 1
for j in range(t):
	a = input()
	n = input()
	for i in range(2 * offset):
		br[i] = 0
	br[offset] = 1
	for i in range(len(n)):
		if n[i] == '\n':
			continue
		curr = p[i] + int(n[i])
		while cnt <= curr - i - 1 + offset:
			br.append(0)
			cnt += 1
		p[i + 1] = curr
		ans[j] += br[curr - i - 1 + offset]
		br[curr - i - 1 + offset] += 1
for j in range(t):
	print(ans[j])


for _ in range(int(input())):
	n = int(input())
	S = input()
	D = {0: 1}
	s = 0
	res = 0
	for _ in S:
		s += int(_) - 1
		if s in D:
			D[s] += 1
		else:
			D[s] = 1
		res += D[s] - 1
	print(res)


import sys, bisect, string, math, time, functools, random, fractions
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
from itertools import permutations, combinations, groupby
rep = range
R = range

def Golf():
	(n, *t) = map(int, open(0).read().split())

def I():
	return int(input())

def S_():
	return input()

def IS():
	return input().split()

def LS():
	return [i for i in input().split()]

def MI():
	return map(int, input().split())

def LI():
	return [int(i) for i in input().split()]

def LI_():
	return [int(i) - 1 for i in input().split()]

def NI(n):
	return [int(input()) for i in range(n)]

def NI_(n):
	return [int(input()) - 1 for i in range(n)]

def StoLI():
	return [ord(i) - 97 for i in input()]

def ItoS(n):
	return chr(n + 97)

def LtoS(ls):
	return ''.join([chr(i + 97) for i in ls])

def RA():
	return map(int, open(0).read().split())

def RLI(n=8, a=1, b=10):
	return [random.randint(a, b) for i in range(n)]

def RI(a=1, b=10):
	return random.randint(a, b)

def Rtest(T):
	(case, err) = (0, 0)
	for i in range(T):
		inp = INP()
		(a1, ls) = naive(*inp)
		a2 = solve(*inp)
		if a1 != a2:
			print((a1, a2), inp)
			err += 1
		case += 1
	print('Tested', case, 'case with', err, 'errors')

def GI(V, E, ls=None, Directed=False, index=1):
	org_inp = []
	g = [[] for i in range(V)]
	FromStdin = True if ls == None else False
	for i in range(E):
		if FromStdin:
			inp = LI()
			org_inp.append(inp)
		else:
			inp = ls[i]
		if len(inp) == 2:
			(a, b) = inp
			c = 1
		else:
			(a, b, c) = inp
		if index == 1:
			a -= 1
			b -= 1
		aa = (a, c)
		bb = (b, c)
		g[a].append(bb)
		if not Directed:
			g[b].append(aa)
	return (g, org_inp)

def GGI(h, w, search=None, replacement_of_found='.', mp_def={'#': 1, '.': 0}, boundary=1):
	mp = [boundary] * (w + 2)
	found = {}
	for i in R(h):
		s = input()
		for char in search:
			if char in s:
				found[char] = (i + 1) * (w + 2) + s.index(char) + 1
				mp_def[char] = mp_def[replacement_of_found]
		mp += [boundary] + [mp_def[j] for j in s] + [boundary]
	mp += [boundary] * (w + 2)
	return (h + 2, w + 2, mp, found)

def TI(n):
	return GI(n, n - 1)

def accum(ls):
	rt = [0]
	for i in ls:
		rt += [rt[-1] + i]
	return rt

def bit_combination(n, base=2):
	rt = []
	for tb in R(base ** n):
		s = [tb // base ** bt % base for bt in R(n)]
		rt += [s]
	return rt

def gcd(x, y):
	if y == 0:
		return x
	if x % y == 0:
		return y
	while x % y != 0:
		(x, y) = (y, x % y)
	return y

def YN(x):
	print(['NO', 'YES'][x])

def Yn(x):
	print(['No', 'Yes'][x])

def show(*inp, end='\n'):
	if show_flg:
		print(*inp, end=end)
mo = 10 ** 9 + 7
inf = float('inf')
FourNb = [(-1, 0), (1, 0), (0, 1), (0, -1)]
EightNb = [(-1, 0), (1, 0), (0, 1), (0, -1), (1, 1), (-1, -1), (1, -1), (-1, 1)]
compas = dict(zip('WENS', FourNb))
cursol = dict(zip('LRUD', FourNb))
l_alp = string.ascii_lowercase
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
input = lambda : sys.stdin.readline().rstrip()
show_flg = False
show_flg = True
ans = 0
for _ in range(I()):
	n = I()
	a = [int(i) - 1 for i in S_()]
	ac = accum(a)
	c = Counter(ac)
	ans = 0
	for (i, m) in c.items():
		ans += m * ~-m // 2
	print(ans)


def checkArray():
	lengthArray = int(input())
	array = str(input())
	val = 0
	_dict = {0: 1}
	res = 0
	for x in range(lengthArray):
		val += int(array[x])
		x = val - x - 1
		if x not in _dict:
			_dict[x] = 0
		_dict[x] += 1
		res += _dict[x] - 1
	print(res)
case = int(input())
while case > 0:
	checkArray()
	case -= 1


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	arr = [int(w) for w in input()]
	ans = 0
	pre = [0]
	for i in range(n):
		pre.append(pre[-1] + arr[i])
	d = defaultdict(int)
	for i in range(n + 1):
		d[pre[i] - i] = d[pre[i] - i] + 1
	ans = 0
	for item in d.values():
		ans = ans + item * (item - 1) // 2
	print(ans)


from collections import Counter
for _ in range(int(input())):
	N = int(input())
	List = [int(x) - 1 for x in input()]
	dp = [0] * (N + 1)
	for i in range(N):
		dp[i + 1] = dp[i] + List[i]
	Ans = 0
	Dict = Counter(dp)
	for i in Dict.keys():
		Ans += Dict[i] * (Dict[i] - 1) >> 1
	print(Ans)


from collections import defaultdict

def solve():
	n = int(input())
	a = list(map(int, input()))
	s = [0] * (n + 1)
	for i in range(n):
		s[i + 1] = s[i] + a[i] - 1
	t = defaultdict(int)
	t[s[0]] += 1
	ans = 0
	for i in range(1, n + 1):
		ans += t[s[i]]
		t[s[i]] += 1
	print(ans)
t = int(input())
for _ in range(t):
	solve()


import os, sys
from io import IOBase, BytesIO
py2 = round(0.5)
if py2:
	from future_builtins import ascii, filter, hex, map, oct, zip
	range = xrange
BUFSIZE = 8192

class FastIO(BytesIO):
	newlines = 0

	def __init__(self, file):
		self._file = file
		self._fd = file.fileno()
		self.writable = 'x' in file.mode or 'w' in file.mode
		self.write = super(FastIO, self).write if self.writable else None

	def _fill(self):
		s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
		self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
		return s

	def read(self):
		while self._fill():
			pass
		return super(FastIO, self).read()

	def readline(self):
		while self.newlines == 0:
			s = self._fill()
			self.newlines = s.count(b'\n') + (not s)
		self.newlines -= 1
		return super(FastIO, self).readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.getvalue())
			(self.truncate(0), self.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		if py2:
			self.write = self.buffer.write
			self.read = self.buffer.read
			self.readline = self.buffer.readline
		else:
			self.write = lambda s: self.buffer.write(s.encode('ascii'))
			self.read = lambda : self.buffer.read().decode('ascii')
			self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import sys

class ostream:

	def __lshift__(self, a):
		sys.stdout.write(str(a))
		return self
cout = ostream()
endl = '\n'

def get_input(a=str):
	return a(input())

def get_int_input():
	return get_input(int)

def get_input_arr(a):
	return list(map(a, input().split()))

def get_int_input_arr():
	return get_input_arr(int)
from collections import defaultdict

def solve():
	t = get_int_input()
	for _ in range(t):
		n = get_int_input()
		st = get_input()
		arr = [0]
		for i in range(len(st)):
			arr.append(ord(st[i]) - ord('0'))
		prefx_arr = [0]
		for i in range(1, len(arr)):
			prefx_arr.append(arr[i] + prefx_arr[i - 1])
		sm_found = defaultdict(lambda : 0)
		res = 0
		for i in range(len(arr)):
			res += sm_found[prefx_arr[i] - i]
			sm_found[prefx_arr[i] - i] += 1
		cout << res << endl

def main():
	solve()
main()


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, list(input())))
	psum = 0
	for i in range(n):
		psum += l[i]
		l[i] = psum - i - 1
	c = Counter(l)
	if 0 in c:
		c[0] += 1
	ans = 0
	for (i, j) in c.items():
		ans += j * (j - 1) // 2
	print(ans)


from sys import stdin, stdout
from math import gcd, log2, log10, floor
import math
from collections import defaultdict, OrderedDict
for i in range(int(input())):
	n = int(input())
	l = list(input())
	prefixsum = [0] * (n + 1)
	prefixsum[0] = 0
	for i in range(n):
		prefixsum[i + 1] = prefixsum[i] + int(l[i]) - 1
	dict = defaultdict(int)
	for i in prefixsum:
		dict[i] += 1
	suma = 0
	for i in dict:
		suma += dict[i] * (dict[i] - 1) / 2
	print(int(suma))


t = int(input())
for _ in range(0, t):
	n = int(input())
	v = input()
	prefix = [0 for i in range(0, n + 1)]
	for i in range(0, n):
		prefix[i + 1] = int(v[i]) - int('0') + prefix[i]
	d = {1: 1}
	ans = 0
	for i in range(0, n):
		if prefix[i + 1] - i in d.keys():
			ans += d[prefix[i + 1] - i]
		if prefix[i + 1] - i in d.keys():
			d[prefix[i + 1] - i] += 1
		else:
			d[prefix[i + 1] - i] = 1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, list(input())))
	pre = [0]
	su = 0
	for i in a:
		su += i
		pre.append(su)
	ans1 = []
	ans2 = []
	for i in range(n + 1):
		ans1.append(pre[i] - i)
	d = dict()
	for i in ans1:
		if i not in d.keys():
			d[i] = 1
		else:
			d[i] += 1
	fin_ans = 0
	for i in d.values():
		fin_ans += i * (i - 1) // 2
	print(fin_ans)


from collections import Counter
from itertools import accumulate

def cmb(n):
	return n * (n - 1) // 2

def solve(arr):
	length = len(arr)
	arr = list(accumulate([x - 1 for x in arr]))
	zero = arr.count(0)
	c = Counter(arr)
	c = list(c.values())
	ans = sum([cmb(x) for x in c if x >= 2]) + zero
	return ans

def main():
	t = int(input())
	for _ in range(t):
		_ = int(input())
		arr = list(input())
		arr = [int(x) for x in arr]
		print(solve(arr))
main()


for _ in range(int(input())):
	n = int(input())
	a = input()
	d = {0: 1}
	s = 0
	res = 0
	for i in range(n):
		s += int(a[i])
		x = s - i - 1
		if x not in d:
			d[x] = 0
		d[x] += 1
		res += d[x] - 1
	print(res)


t = int(input())
for i in range(t):
	n = int(input())
	a1 = input()
	a = [int(x) for x in a1]
	s1 = [0]
	c = 0
	for j in range(n):
		c = c + a[j]
		s1.append(c - j - 1)
	d = {}
	for k in s1:
		if k in d:
			d[k] += 1
		else:
			d[k] = 1
	ans = 0
	for y in list(d.values()):
		ans += y * (y - 1) / 2
	print(int(ans))


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	str1 = input()
	summ = 0
	res = 0
	d1 = defaultdict(int)
	d1[0] = 1
	for i in range(n):
		x = int(str1[i]) - 1
		summ += x
		if summ in d1:
			res += d1[summ]
			d1[summ] += 1
		else:
			d1[summ] = 1
	print(res)


from collections import defaultdict
T = int(input())
for t in range(T):
	n = int(input())
	str = input()
	cum = 0
	ans = 0
	prevsums = defaultdict(lambda : 0)
	for i in range(len(str)):
		val = int(str[i]) - 1
		cum += val
		if cum == 0:
			ans += 1
		if cum in prevsums:
			ans += prevsums[cum]
		prevsums[cum] += 1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list((int(x) for x in input()))
	a.insert(0, 0)
	ans = 0
	d = {}
	d[0] = 1
	for i in range(1, n + 1):
		a[i] += a[i - 1] - 1
		if a[i] in d:
			ans += d[a[i]]
			d[a[i]] += 1
		else:
			d[a[i]] = 1
	print(ans)


from collections import defaultdict
import itertools
for t in range(int(input())):
	n = int(input())
	a = [int(v) for v in list(input())]
	a = list(itertools.accumulate(a))
	c = defaultdict(int)
	c[1] = 1
	ans = 0
	for i in range(n):
		ai = a[i] - i
		ans += c[ai]
		c[ai] += 1
	print(ans)


import sys
LI = lambda : list(map(int, sys.stdin.readline().strip('\n').split()))
MI = lambda : map(int, sys.stdin.readline().strip('\n').split())
SI = lambda : sys.stdin.readline().strip('\n')
II = lambda : int(sys.stdin.readline().strip('\n'))
for _ in range(II()):
	n = II()
	s = [0] + [ord(i) - ord('0') for i in SI()]
	cnt = {}
	for i in range(1, n + 1):
		s[i] += s[i - 1]
	for (i, v) in enumerate(s):
		cnt[v - i] = cnt.get(v - i, 0) + 1
	ans = 0
	for v in cnt.keys():
		ans += cnt[v] * (cnt[v] - 1) // 2
	print(ans)


import os
import sys
from io import BytesIO, IOBase
from collections import Counter
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from math import gcd, ceil

def pre(s):
	n = len(s)
	pi = [0] * n
	for i in range(1, n):
		j = pi[j - 1]
		while j and s[i] != s[j]:
			j = pi[j - 1]
		if s[i] == s[j]:
			j += 1
		pi[i] = j
	return pi

def prod(a):
	ans = 1
	for each in a:
		ans = ans * each
	return ans

def lcm(a, b):
	return a * b // gcd(a, b)

def binary(x, length=16):
	y = bin(x)[2:]
	return y if len(y) >= length else '0' * (length - len(y)) + y
for _ in range(int(input()) if True else 1):
	n = int(input())
	a = input()
	s = [int(k) for k in a]
	for i in range(n):
		s[i] -= 1
	psum = [0] * (n + 1)
	a = Counter([0])
	for i in range(n):
		psum[i + 1] = s[i] + psum[i]
		a[psum[i + 1]] += 1
	ans = 0
	for val in a.values():
		ans += val * (val - 1) // 2
	print(ans)


N = int(input())
for i in range(N):
	num = int(input())
	lst = list(map(int, input().strip()))
	(res, dic, temp) = (0, {0: 1}, 0)
	for j in lst:
		temp += j - 1
		if temp in dic:
			res += dic[temp]
			dic[temp] += 1
		else:
			dic[temp] = 1
	print(res)


def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		a = list(map(int, list(input())))
		ar_sum = [0] * (n + 1)
		for i in range(n):
			ar_sum[i + 1] = a[i] + ar_sum[i]
		m = {}
		ans = 0
		for i in range(1, n + 1):
			m[ar_sum[i - 1] - i + 1] = m.get(ar_sum[i - 1] - i + 1, 0) + 1
			if m.get(ar_sum[i] - i, -1) != -1:
				ans += m[ar_sum[i] - i]
		print(ans)
main()


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
import copy
import time
starttime = time.time()
mod = int(pow(10, 9) + 7)
mod2 = 998244353

def data():
	return sys.stdin.readline().strip()

def out(*var, end='\n'):
	sys.stdout.write(' '.join(map(str, var)) + end)

def L():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]
try:
	sys.stdin = open('input.txt', 'r')
except:
	pass
for _ in range(L()[0]):
	n = L()[0]
	s = input()
	pref = [0 for i in range(len(s) + 1)]
	for i in range(len(s)):
		pref[i + 1] = pref[i] + int(s[i])
	ans = 0
	d = {}
	d[0] = 1
	for i in range(1, len(s) + 1):
		d[pref[i] - i] = d.get(pref[i] - i, 0) + 1
	for k in d:
		ans += d[k] * (d[k] - 1) // 2
	print(ans)
endtime = time.time()


from collections import defaultdict
T = int(input())
for _ in range(T):
	(n, dg) = (int(input()), list(map(int, list(input()))))
	(dc, sm, cc) = (defaultdict(int, {0: 1}), 0, 0)
	for u in dg:
		sm += u - 1
		cc += dc[sm]
		dc[sm] += 1
	print(cc)


import sys
buf = sys.stdin.read()
nowbuf = 0
endbuf = len(buf)

def getint():
	global nowbuf
	valnow = 0
	while buf[nowbuf] < '0' or buf[nowbuf] > '9':
		nowbuf += 1
	while nowbuf < endbuf and buf[nowbuf] >= '0' and (buf[nowbuf] <= '9'):
		valnow = valnow * 10 + int(buf[nowbuf])
		nowbuf += 1
	return valnow

def getstr():
	global nowbuf
	strnow = ''
	while buf[nowbuf] == ' ' or buf[nowbuf] == '\n':
		nowbuf += 1
	while nowbuf < endbuf and buf[nowbuf] != ' ' and (buf[nowbuf] != '\n'):
		strnow += buf[nowbuf]
		nowbuf += 1
	return strnow

def mycmp1(cur):
	return cur[0]

def mycmp2(cur):
	return cur[0]

def solve(array):
	n = len(array)
	ans = 0
	num = 0
	now = 0
	o = []
	vis = [0 for i in range(n + n)]
	pos = [0 for i in range(n + n)]
	for i in range(0, n):
		num += int(array[i])
		o.append((i + 1 - num, i))
		if num == i + 1:
			ans += 1
	o.sort(key=mycmp1)
	for i in range(0, n):
		if i != 0 and o[i][0] != o[i - 1][0]:
			now += 1
		pos[o[i][1]] = now
	for i in range(0, n):
		ans += vis[pos[i]]
		vis[pos[i]] += 1
	print(ans)
	return
t = getint()
for i in range(0, t):
	getint()
	solve(getstr())


t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	a = list(s)
	a = [ord(a[i]) - ord('0') for i in range(n)]
	dp = [0] * (9 * n)
	dp[0] = 1
	(sm, ans) = (0, 0)
	for i in range(n):
		sm += a[i]
		ans += dp[sm - i - 1]
		dp[sm - i - 1] += 1
	print(ans)


import sys

def answer(n, a):
	p = [0 for _ in range(n + 1)]
	for i in range(1, n + 1):
		p[i] = p[i - 1] + a[i - 1]
	pimi = [0 for _ in range(n + 1)]
	for i in range(n + 1):
		pimi[i] = p[i] - i
	d = dict()
	res = 0
	for i in range(n + 1):
		if pimi[i] not in d:
			d[pimi[i]] = 0
		d[pimi[i]] += 1
		res += d[pimi[i]] - 1
	return res

def main():
	t = int(sys.stdin.readline())
	while t:
		n = int(sys.stdin.readline())
		a = [int(c) for c in sys.stdin.readline().rstrip()]
		print(answer(n, a))
		t -= 1
	return
main()


import sys
input = sys.stdin.readline
from bisect import bisect_left
from bisect import bisect_right
from math import ceil, factorial

def ceil(x):
	if x != int(x):
		x = int(x) + 1
	return x

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def nCr(n, k):
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return int(res)

def primefs(n):
	primes = {}
	while n % 2 == 0 and n > 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0 and n > 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	p = x
	while p != link[p]:
		p = link[p]
	while x != p:
		nex = link[x]
		link[x] = p
		x = nex
	return p

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime
MAXN = int(100000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i

def factoriazation(x):
	ret = {}
	while x != 1:
		ret[spf[x]] = ret.get(spf[x], 0) + 1
		x = x // spf[x]
	return ret

def int_array():
	return list(map(int, input().strip().split()))

def float_array():
	return list(map(float, input().strip().split()))

def str_array():
	return input().strip().split()
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, list(input().strip())))
	vals = []
	for i in range(n):
		if a[i] == 0:
			vals.append(1)
		else:
			vals.append(1 - a[i])
	for i in range(1, n):
		vals[i] += vals[i - 1]
	dick = {}
	for i in vals:
		dick[i] = dick.get(i, 0) + 1
	ans = 0
	for i in range(n):
		x = vals[i] + a[i] - 1
		x = dick.get(x, 0)
		dick[vals[i]] -= 1
		ans += x
	print(ans)


import sys
input = sys.stdin.readline
import heapq
import copy
import math
import decimal

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def insr2():
	s = input()
	return s[:len(s) - 1]

def invr():
	return map(int, input().split())

def pr_list(a):
	print(*a, sep=' ')

def main():
	tests = inp()
	mod = 1000000007
	limit = 10 ** 18
	ans = 0
	stack = []
	hashm = {}
	arr = []
	heapq.heapify(arr)
	for test in range(tests):
		n = inp()
		s = insr()
		for i in range(len(s)):
			s[i] = int(s[i])
		hashm = {}
		hashm[0] = 1
		ans = 0
		pre = 0
		for i in range(0, n):
			pre = pre + s[i]
			if pre - (i + 1) in hashm:
				ans = ans + hashm[pre - (i + 1)]
				hashm[pre - (i + 1)] = hashm[pre - (i + 1)] + 1
			else:
				hashm[pre - (i + 1)] = 1
		print(ans)
main()


t = int(input())
for i in range(t):
	n = int(input())
	l = input()
	i_l = list(l.strip())
	i_li = [int(x) for x in i_l]
	cur_sum = 0
	exist = dict()
	exist[0] = 0
	count = [0 for j in range(n)]
	for j in range(n):
		cur_sum += i_li[j] - 1
		if cur_sum in exist:
			last_j = exist[cur_sum]
			exist[cur_sum] = j
			count[j] = count[last_j] + 1
		else:
			exist[cur_sum] = j
	print(sum(count))


for _ in range(int(input())):
	n = int(input())
	s = input()
	l = [int(i) - 1 for i in s]
	d = {}
	d[0] = 1
	(s, ans) = (0, 0)
	for i in l:
		s += i
		if s in d:
			ans += d[s]
			d[s] += 1
		else:
			d[s] = 1
	print(ans)


def countsumzero(lst, pdt, zr):
	prefixsums = [zr]
	for x in lst:
		prefixsums.append(prefixsums[-1 * pdt] + x + zr)
	freq = {}
	freq2 = {}
	for y in prefixsums:
		if y in freq:
			freq[y + zr] = (pdt + freq[y]) * pdt * pdt
			freq2[y + zr] = (pdt + freq[y]) * pdt * pdt
		else:
			freq[y + zr] = pdt * pdt + zr
	return sum(((zr + v) * (v - pdt + zr) // (2 + zr) for v in freq.values()))
for _ in range(int(input())):
	n = int(input())
	lst = (int(i) + -1 for i in list(input()))
	print(countsumzero(lst, 1, 0))


try:
	for _ in range(int(input())):
		a = int(input())
		b = [int(i) for i in input()]
		ans = 0
		c = [] + b
		for i in range(1, a):
			c[i] += c[i - 1]
		f = [] + c
		for i in range(a):
			f[i] -= i
		there = dict()
		for j in range(a):
			if f[j] == 1:
				ans += 1
			if f[j] in there:
				ans += there[f[j]]
			else:
				there[f[j]] = 0
			there[f[j]] += 1
		print(ans)
except Exception as e:
	print(e)


import sys, math, heapq, collections, itertools, bisect
sys.setrecursionlimit(101000)

def solve(n, a):
	diffs = {0: 1}
	curr = sol = 0
	for i in range(n):
		curr += int(a[i])
		diff = curr - (i + 1)
		sol += diffs.get(diff, 0)
		diffs[diff] = diffs.get(diff, 0) + 1
	return sol
t = int(input())
for _ in range(t):
	n = int(input())
	a = input()
	print(solve(n, a))


t = int(input())
cnt = 0
while cnt < t:
	cnt += 1
	n = int(input())
	a = [int(i) for i in input()]
	ans = 0
	s = 0
	res = 0
	d = dict()
	d[0] = 1
	for i in range(n):
		s += a[i]
		if d.get(s - i - 1, 0) == 0:
			d[s - i - 1] = 1
		else:
			res += d[s - i - 1]
			d[s - i - 1] += 1
	print(res)


t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	a = [0] * (10 * n)
	ans = 0
	pre = 0
	a[pre] = 1
	for i in range(n):
		pre += int(s[i]) - 1
		ans += a[pre]
		a[pre] += 1
	print(ans)


t = int(input())
for i in range(t):
	n = input()
	s = input()
	dp = {0: 1}
	prv = 0
	for i in range(0, len(s)):
		y = prv + int(s[i])
		x = i + 1
		c = y - x
		if c in dp:
			dp[c] += 1
		else:
			dp[c] = 1
		prv = y
	ans = 0
	for val in dp.values():
		ans += int(val * (val - 1) / 2)
	print(ans)


from collections import defaultdict

def solution(arr, n):
	prevSum = defaultdict(lambda : 0)
	res = 0
	currSum = 0
	for i in range(n):
		currSum += arr[i]
		if currSum == 0:
			res += 1
		if currSum in prevSum:
			res += prevSum[currSum]
		prevSum[currSum] += 1
	return res
t = int(input())
for i in range(t):
	n = int(input())
	s = str(input())
	arr = []
	for j in s:
		arr.append(int(j) - 1)
	print(solution(arr, n))


import math
from math import gcd, sqrt, floor, factorial, ceil
from bisect import bisect_left, bisect_right
import bisect
import sys
from sys import stdin, stdout
import os
sys.setrecursionlimit(pow(10, 7))
import collections
from collections import defaultdict, Counter
from statistics import median
from queue import Queue
inf = float('inf')
from operator import neg
t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input()))
	m = [0]
	for i in range(len(l)):
		m.append(m[-1] + l[i] - 1)
	dict = Counter(m)
	ansList = [i * (i - 1) // 2 for i in dict.values()]
	print(sum(ansList))


from collections import defaultdict
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(input())
	lst = [int(ele) for ele in a]
	s = 0
	ans = 0
	mp = defaultdict(int)
	mp[0] += 1
	for i in range(n):
		s += int(a[i])
		ans += mp[s - (i + 1)]
		mp[s - (i + 1)] += 1
	print(ans)


from collections import defaultdict
n = int(input())
for i in range(n):
	k = int(input())
	s = input()
	l = defaultdict(int)
	l[0] = 1
	su = 0
	ans = 0
	for i in s:
		su += int(i) - 1
		ans += l[su]
		l[su] += 1
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(i) for i in list(input())]
	s = 0
	dp = [0] * (9 * n)
	cnt = 0
	dp[0] = 1
	for i in range(n):
		s += a[i]
		cnt += dp[s - i - 1]
		dp[s - i - 1] += 1
	print(cnt)


for _ in range(int(input())):
	N = int(input())
	array = [int(x) for x in list(input())]
	partialsum = [0]
	total = 0
	for i in array:
		total += i
		partialsum.append(total)
	goodarrays = []
	for i in range(len(partialsum)):
		goodarrays.append(partialsum[i] - i)
	dictionary = dict()
	counter = 0
	for i in goodarrays:
		if i in dictionary:
			counter += dictionary[i]
			dictionary[i] += 1
		else:
			dictionary[i] = 1
	print(counter)


import sys
from collections import defaultdict as dd
from collections import deque
from functools import *
from fractions import Fraction as f
from copy import *
from bisect import *
from heapq import *
from math import *
from itertools import permutations

def eprint(*args):
	print(*args, file=sys.stderr)
zz = 1
if zz:
	input = sys.stdin.readline
else:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('all.txt', 'w')

def inc(d, c):
	d[c] = d[c] + 1 if c in d else 1

def li():
	return [int(xx) for xx in input().split()]

def fli():
	return [float(x) for x in input().split()]

def comp(a, b):
	if a > b:
		return 2
	return 2 if a == b else 0

def gi():
	return [xx for xx in input().split()]

def fi():
	return int(input())

def pro(a):
	return reduce(lambda a, b: a * b, a)

def swap(a, i, j):
	(a[i], a[j]) = (a[j], a[i])

def si():
	return list(input().rstrip())

def mi():
	return map(int, input().split())

def gh():
	sys.stdout.flush()

def isvalid(i, j):
	return 0 <= i < n and 0 <= j < n

def graph(n, m):
	for i in range(m):
		(x, y) = mi()
		a[x].append(y)
		a[y].append(x)
t = fi()
while t > 0:
	t -= 1
	n = fi()
	s = si()
	a = []
	d = [0] * (n + 1)
	for i in range(n):
		a.append(int(s[i]))
	ans = 0
	c = [0] * (10 * n + 1)
	c[0] = 1
	for i in range(n):
		if a[i] == 0:
			d[i] = d[i - 1] - 1
		else:
			d[i] = d[i - 1] + (a[i] - 1)
		ans += c[d[i]]
		c[d[i]] += 1
	print(ans)


for s in [*open(0)][2::2]:
	a = [1] + len(s) * 8 * [0]
	t = 0
	for x in s[:-1]:
		t += int(x) - 1
		a[t] += 1
	print(sum((x * (x - 1) // 2 for x in a)))


q = int(input())
for _ in range(q):
	n = int(input())
	s = list(input())
	s = list(map(int, s))
	l = [s[i] - 1 for i in range(n)]
	pref = [0] * (n + 1)
	for i in range(1, n + 1):
		pref[i] = pref[i - 1] + l[i - 1]
	d = {}
	for elt in pref:
		try:
			d[elt] += 1
		except Exception:
			d[elt] = 1
	wyn = 0
	for elt in d:
		wyn += d[elt] * (d[elt] - 1) // 2
	print(wyn)


t = int(input())
for i in range(t):
	n = int(input())
	ls = [0] * (n + 1)
	s = input()
	for j in range(1, n + 1):
		ls[j] = ls[j - 1] + int(s[j - 1])
	count = [0] * (n + 1)
	for j in range(len(ls)):
		count[j] = ls[j] - j
	dict = {}
	for j in range(len(count)):
		key = count[j]
		if key not in dict:
			dict[key] = 1
		else:
			dict[key] += 1
	sum = 0
	for x in dict.values():
		sum += (x - 1) * x / 2
	print(int(sum))


for _ in range(int(input())):
	n = int(input())
	w = input()
	dic = {0: 1}
	(s, ans) = (0, 0)
	for i in range(n):
		s += int(w[i])
		x = s - i - 1
		dic[x] = dic.get(x, 0) + 1
		ans += dic[x] - 1
	print(ans)


def solve(l, n):
	mp = {}
	(s, ans) = (0, 0)
	for i in range(n):
		s += l[i]
		if s == 0:
			ans += 1
		if s in mp:
			ans += mp[s]
			mp[s] += 1
		else:
			mp[s] = 1
	print(ans)
for _ in range(int(input())):
	n = int(input())
	s = str(input())
	l = list(s)
	for i in range(n):
		l[i] = int(l[i]) - 1
	solve(l, n)


t = int(input())
for _ in range(t):
	n = int(input())
	a = input()
	a = [int(i) - 1 for i in a]
	(sum, mp) = (0, {})
	for i in range(n):
		sum += a[i]
		mp[sum] = 0
	(mp[0], sum, res) = (1, 0, 0)
	for i in range(n):
		sum += a[i]
		res += mp[sum]
		mp[sum] += 1
	print(res)


T = int(input())
for case in range(T):
	i = int(input())
	ls = [int(x) for x in input()]
	nums = [e - 1 for (n, e) in enumerate(ls)]
	d = {0: 1}
	tot = 0
	ret = 0
	for n in nums:
		tot += n
		if tot in d:
			ret += d[tot]
		try:
			d[tot] += 1
		except:
			d[tot] = 1
	print(ret)


for _ in range(int(input())):
	from collections import Counter
	c = Counter([0])
	n = int(input())
	s = input()
	ans = 0
	m = -1
	for (i, x) in enumerate(s):
		m += int(x)
		ans += c[m - i]
		c[m - i] += 1
	print(ans)


from sys import stdin, stdout
import math
from itertools import permutations, combinations
from collections import defaultdict
from bisect import bisect_left
from bisect import bisect_right

def L():
	return list(map(int, stdin.readline().split()))

def In():
	return map(int, stdin.readline().split())

def I():
	return int(stdin.readline())
P = 1000000007

def main():
	for t in range(I()):
		n = I()
		s = input()
		m = [1] + [0] * 9 * n
		(cur, ans) = (0, 0)
		for i in s:
			cur += ord(i) - 49
			ans += m[cur]
			m[cur] += 1
		print(ans)
main()


from collections import Counter

def call(a, n):
	count = 0
	P = [0] * (n + 1)
	for i in range(1, n + 1):
		P[i] = a[i - 1] + P[i - 1]
	for i in range(1, n + 1):
		P[i] = P[i] - i
	bitch = Counter(P)
	for (val, coun) in bitch.items():
		count += coun * (coun - 1) // 2
	print(count)
t = int(input())
for i in range(t):
	n = int(input())
	a = [int(x) for x in input()]
	call(a, n)


import math
t = int(input())
for w in range(t):
	n = int(input())
	l = list(map(int, list(input())))
	pref = [l[0]]
	for i in range(1, n):
		pref.append(pref[-1] + l[i])
	d = {}
	c = 0
	diff = 0
	for i in range(n):
		if -diff in d:
			d[-diff] += 1
		else:
			d[-diff] = 1
		diff += l[i] - 1
		if -diff in d:
			c += d[-diff]
	print(c)


for _ in range(int(input())):
	n = int(input())
	s = input()
	d = {0: 1}
	summ = 0
	ans = 0
	for i in range(n):
		summ += int(s[i])
		x = summ - i - 1
		ans += d.get(x, 0)
		d[x] = d.get(x, 0) + 1
	print(ans)


def solve(s):
	pre = [0] * (len(s) + 1)
	for i in range(len(s)):
		pre[i + 1] = pre[i] + s[i]
	counters = dict()
	balance = 0
	ans = 0
	counters[0] = 1
	for i in range(len(s)):
		balance -= 1
		balance += s[i]
		ans += counters.get(balance, 0)
		counters.setdefault(balance, 0)
		counters[balance] += 1
	return ans
for _ in range(int(input())):
	_ = int(input())
	s = input()
	s = [int(d) for d in s]
	print(solve(s))


from collections import defaultdict
t = int(input())
for i in range(t):
	n = int(input())
	a = [0] + list(map(int, list(input())))
	acc = 0
	counter = defaultdict(int)
	for i in range(n + 1):
		acc += a[i]
		counter[acc - i] += 1
	ans = 0
	for (i, m) in counter.items():
		ans += m * (m - 1) // 2
	print(ans)


from __future__ import division, print_function
import os, sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

def ii():
	return int(input())

def si():
	return input()

def mi():
	return map(int, input().strip().split(' '))

def msi():
	return map(str, input().strip().split(' '))

def li():
	return list(mi())

def dmain():
	sys.setrecursionlimit(1000000)
	threading.stack_size(1024000)
	thread = threading.Thread(target=main)
	thread.start()
abc = 'abcdefghijklmnopqrstuvwxyz'
abd = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
mod = 1000000007
inf = float('inf')
vow = ['a', 'e', 'i', 'o', 'u']
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def getKey(item):
	return item[1]

def sort2(l):
	return sorted(l, key=getKey, reverse=True)

def d2(n, m, num):
	return [[num for x in range(m)] for y in range(n)]

def isPowerOfTwo(x):
	return x and (not x & x - 1)

def decimalToBinary(n):
	return bin(n).replace('0b', '')

def ntl(n):
	return [int(i) for i in str(n)]

def ncr(n, r):
	return factorial(n) // (factorial(r) * factorial(max(n - r, 1)))

def ceil(x, y):
	if x % y == 0:
		return x // y
	else:
		return x // y + 1

def powerMod(x, y, p):
	res = 1
	x %= p
	while y > 0:
		if y & 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def read():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

def main():
	for _ in range(ii()):
		n = ii()
		s = si()
		a = []
		for i in range(n):
			a.append(int(s[i]))
		f = {}
		for i in range(n):
			if i:
				a[i] += a[i - 1]
			if f.get(a[i] - i - 1, 0) == 0:
				f[a[i] - i - 1] = 1
			else:
				f[a[i] - i - 1] += 1
		ans = 0
		prev = 0
		for i in range(n):
			val = prev - i
			ans += f.get(val, 0)
			f[a[i] - i - 1] -= 1
			prev = a[i]
		print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import sys, os, io

def rs():
	return sys.stdin.readline().rstrip()

def ri():
	return int(sys.stdin.readline())

def ria():
	return list(map(int, sys.stdin.readline().split()))

def ws(s):
	sys.stdout.write(s + '\n')

def wi(n):
	sys.stdout.write(str(n) + '\n')

def wia(a):
	sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
import math, datetime, functools, itertools, operator, bisect, fractions, statistics
from collections import deque, defaultdict, OrderedDict, Counter
from fractions import Fraction
from decimal import Decimal
from sys import stdout
from heapq import heappush, heappop, heapify, _heapify_max, _heappop_max

def main():
	starttime = datetime.datetime.now()
	if os.path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	for _ in range(ri()):
		n = ri()
		a = list(rs())
		a = [0] + a
		ps = []
		c = 0
		d = {}
		for i in range(n + 1):
			c += int(a[i])
			ps += [c]
			k = ps[i] - i
			if k in d:
				d[k] += 1
			else:
				d[k] = 1
		ans = 0
		for i in d:
			k = d[i]
			ans += k * (k - 1) // 2
		print(ans)
	endtime = datetime.datetime.now()
	time = (endtime - starttime).total_seconds() * 1000
	if os.path.exists('input.txt'):
		print('Time:', time, 'ms')

class FastReader(io.IOBase):
	newlines = 0

	def __init__(self, fd, chunk_size=1024 * 8):
		self._fd = fd
		self._chunk_size = chunk_size
		self.buffer = io.BytesIO()

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self, size=-1):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size if size == -1 else size))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

class FastWriter(io.IOBase):

	def __init__(self, fd):
		self._fd = fd
		self.buffer = io.BytesIO()
		self.write = self.buffer.write

	def flush(self):
		os.write(self._fd, self.buffer.getvalue())
		(self.buffer.truncate(0), self.buffer.seek(0))

class FastStdin(io.IOBase):

	def __init__(self, fd=0):
		self.buffer = FastReader(fd)
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

class FastStdout(io.IOBase):

	def __init__(self, fd=1):
		self.buffer = FastWriter(fd)
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.flush = self.buffer.flush
sys.stdin = FastStdin()
sys.stdout = FastStdout()
main()


import sys
import random
from fractions import Fraction
from math import *

def input():
	return sys.stdin.readline().strip()

def iinput():
	return int(input())

def finput():
	return float(input())

def tinput():
	return input().split()

def linput():
	return list(input())

def rinput():
	return map(int, tinput())

def fiinput():
	return map(float, tinput())

def flinput():
	return list(fiinput())

def rlinput():
	return list(map(int, input().split()))

def trinput():
	return tuple(rinput())

def srlinput():
	return sorted(list(map(int, input().split())))

def NOYES(fl):
	if fl:
		print('NO')
	else:
		print('YES')

def YESNO(fl):
	if fl:
		print('YES')
	else:
		print('NO')

def main():
	n = iinput()
	q = [int(i) for i in linput()]
	w = [0]
	for i in q:
		w.append(w[-1] + i)
	d = dict()
	for i in range(n + 1):
		t = w[i] - i
		if t in d:
			d[t] += 1
		else:
			d[t] = 1
	print(sum([(d[i] * d[i] - d[i]) // 2 for i in d]))
for TESTING in range(iinput()):
	main()


def ans(arr, n):
	hashMap = {}
	curr_sum = 0
	ans = 0
	for i in range(n):
		curr_sum += arr[i]
		if curr_sum == 0:
			ans += 1
		if curr_sum in hashMap:
			ans += len(hashMap[curr_sum])
			hashMap[curr_sum].append(i)
		else:
			hashMap[curr_sum] = [i]
	return ans
t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(char) - 1 for char in input()]
	print(ans(arr, n))


import os, sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from math import ceil
mod = 10 ** 9 + 7
for _ in range(int(input())):
	n = int(input())
	nums = [int(i) for i in list(input())]
	count_good_subarrays = 0
	for i in range(1, n):
		nums[i] += nums[i - 1]
	memo = {0: 1}
	for i in range(n):
		x = nums[i] - i - 1
		if x not in memo:
			memo[x] = 0
		memo[x] += 1
	for (key, value) in memo.items():
		count_good_subarrays += value * (value - 1) // 2
	print(count_good_subarrays)


import sys
input = lambda : sys.stdin.readline().rstrip()
from collections import Counter
for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input()]
	sp = []
	c = 1
	s = 0
	for i in a:
		s += i
		sp.append(s - c)
		c += 1
	d = Counter(sp)
	ans = 0
	for i in d:
		if i == 0:
			ans += d[i]
		ans += d[i] * (d[i] - 1) // 2
	print(ans)


import os
import sys
from io import BytesIO, IOBase
from collections import Counter, defaultdict
from collections import deque
from functools import cmp_to_key
import math

def sin():
	return input()

def ain():
	return list(map(int, sin().split()))

def sain():
	return input().split()

def iin():
	return int(sin())
MAX = float('inf')
MIN = float('-inf')
MOD = 1000000007

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	s = set()
	for p in range(2, n + 1):
		if prime[p]:
			s.add(p)
	return s

def readTree(n, m):
	adj = [deque([]) for _ in range(n + 1)]
	for _ in range(m):
		(u, v) = ain()
		adj[u].append(v)
		adj[v].append(u)
	return adj

def solve(l, suma, k, n):
	c = 0
	xs = sum(l[0:k])
	if xs == k:
		c += 1
	for j in range(k, n):
		xs -= l[j - k]
		xs += l[j]
		if xs == k:
			c += 1
	return c

def main():
	for _ in range(iin()):
		n = iin()
		s = sin()
		l = list(map(int, s))
		for i in range(n):
			l[i] -= 1
		d = dict()
		ans = 0
		currsum = 0
		for i in range(n):
			currsum += l[i]
			if currsum == 0:
				ans += 1
			if currsum in d:
				ans += d[currsum]
				d[currsum] += 1
			else:
				d[currsum] = 1
		print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
if os.getcwd() == 'D:\\code':
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
	input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	temp = [0]
	for v in input():
		temp.append(temp[-1] + int(v) - 1)
	print(sum((v * (v - 1) // 2 for v in Counter(temp).values())))


import io, os
from collections import deque
import bisect
from collections import deque
RL = lambda : list(map(int, input().split(' ')))
T = int(input())
for _ in range(T):
	n = int(input())
	l = input()
	su = 0
	dic = dict()
	dic[0] = 1
	ans = 0
	for i in range(n):
		su += int(l[i])
		x = su - i - 1
		if x not in dic.keys():
			dic[x] = 0
		dic[x] += 1
		ans += dic[x] - 1
	print(ans)


MAX = 100000 + 1000
t = int(input(''))
num = []
for i in range(t):
	int(input(''))
	num.append(input(''))
for i in num:
	sol = 0
	n = len(i)
	i = list(i)
	cnt = {}
	cnt[0] = 1
	sol = 0
	s = 0
	for j in range(n):
		s += int(i[j])
		ind = j + 1 - s
		try:
			cnt[ind]
		except:
			cnt[ind] = 0
		sol += cnt[ind]
		cnt[ind] += 1
	print(sol)


for _ in range(int(input())):
	n = int(input())
	s = input()
	ll = {}
	sol = 0
	b = 1
	for i in s:
		i = int(i)
		if b in ll:
			ll[b] += 1
		else:
			ll[b] = 1
		b = b + i - 1
		if b in ll:
			sol += ll[b]
	print(sol)


_ = int(input())
for i in range(_):
	n = int(input())
	arr = input()
	d = {0: 1}
	ans = 0
	pref = 0
	for j in range(n):
		pref += int(arr[j])
		x = pref - j - 1
		if x not in d:
			d[x] = 0
		d[x] += 1
		ans += d[x] - 1
	print(ans)


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	f = defaultdict(int)
	l = str(input())
	s = 0
	count = 0
	f[0] = 1
	for i in range(n):
		s += int(l[i])
		count += f[s - i - 1]
		f[s - i - 1] += 1
	print(count)


import sys, io, os, time
from collections import defaultdict
from collections import OrderedDict
from collections import deque
from itertools import combinations
from itertools import permutations
from itertools import accumulate
import bisect, math, heapq
alphabet = 'abcdefghijklmnopqrstuvwxyz'
input = sys.stdin.readline
for ii in range(int(input())):
	n = int(input())
	s = input()
	arr = []
	for i in s:
		if i != '\n':
			arr.append(int(i))
	m = defaultdict(int)
	m[0] = 1
	csum = 0
	ans = 0
	for i in range(n):
		csum += arr[i]
		ans += m[csum - (i + 1)]
		m[csum - (i + 1)] += 1
	print(ans)


from sys import stdin
nii = lambda : map(int, stdin.readline().split())
lnii = lambda : list(map(int, stdin.readline().split()))
from collections import Counter
t = int(input())
for tt in range(t):
	n = int(input())
	a = input()
	l = [0]
	t_sum = 0
	for i in range(n):
		t_sum += int(a[i])
		num = t_sum - i - 1
		l.append(num)
	c = Counter(l)
	ans = sum([i * (i - 1) // 2 for i in c.values()])
	print(ans)


from collections import defaultdict
t = int(input())
for _ in range(t):
	n = int(input())
	inp = input()
	arr = []
	ans = 0
	for i in inp:
		arr.append(ord(i) - ord('0'))
	prefix_sum = 0
	d = defaultdict(int)
	d[0] = 1
	for i in range(1, n + 1):
		prefix_sum += arr[i - 1]
		d[prefix_sum - i] += 1
	for j in d:
		ans += d[j] * (d[j] - 1) / 2
	print(int(ans))


for nt in range(int(input())):
	n = int(input())
	a = list(map(int, list(input())))
	p = [a[0]]
	for i in range(1, n):
		p.append(p[-1] + a[i])
	d = {}
	ans = 0
	diff = 0
	for i in range(n):
		if -diff in d:
			d[-diff] += 1
		else:
			d[-diff] = 1
		diff += a[i] - 1
		if -diff in d:
			ans += d[-diff]
	print(ans)


for i in range(int(input())):
	n = int(input())
	s = [int(i) for i in input()]
	sum = [0 for i in range(n)]
	count = 0
	sum[0] = s[0]
	for i in range(1, n):
		sum[i] = sum[i - 1] + s[i]
	for i in range(n):
		sum[i] = sum[i] - i
	sum.append(1)
	sum = sorted(sum)
	x = 0
	for i in range(n + 1):
		x = x + 1
		if i == n or sum[i] != sum[i + 1]:
			count += x * (x - 1) / 2
			x = 0
	print(int(count))


for _ in range(int(input())):
	n = int(input())
	s = input()
	a = [int(i) - 1 for i in s]
	pref = [0]
	for i in a:
		pref.append(pref[-1] + i)
	out = 0
	h = {}
	for i in pref:
		if i not in h:
			h[i] = 0
		h[i] += 1
	for i in h:
		if h[i] > 1:
			out += h[i] * (h[i] - 1) // 2
	print(out)


import sys

def rs():
	return sys.stdin.readline().rstrip()

def ri():
	return int(sys.stdin.readline())

def ria():
	return list(map(int, sys.stdin.readline().split()))

def ws(s):
	sys.stdout.write(s)
	sys.stdout.write('\n')

def wi(n):
	sys.stdout.write(str(n))
	sys.stdout.write('\n')

def wia(a, sep=' '):
	sys.stdout.write(sep.join([str(x) for x in a]))
	sys.stdout.write('\n')

def solve(n, a):
	pre = [0] * (n + 1)
	cnt = {0: 1}
	for i in range(n):
		pre[i + 1] = pre[i] + a[i]
		d = pre[i + 1] - i - 1
		if d not in cnt:
			cnt[d] = 0
		cnt[d] += 1
	ans = 0
	for (k, v) in cnt.items():
		if v > 0:
			ans += v * (v - 1) // 2
	return ans

def main():
	for _ in range(ri()):
		n = ri()
		a = [int(si) for si in rs()]
		wi(solve(n, a))
main()


def solve():
	occr = dict()
	n = int(input())
	a = list(map(lambda x: int(x) - 1, input()))
	ctr = 0
	occr[0] = 1
	last = 0
	for i in a:
		i += last
		key = i
		if key in occr:
			ctr += occr[key]
		if i not in occr:
			occr[i] = 0
		occr[i] += 1
		last = i
	print(ctr)
for _ in range(int(input())):
	solve()


ll = lambda : list(map(int, input().split()))
lls = lambda : list(map(str, input().split()))
st = lambda : input()
v = lambda : map(int, input().split())
ii = lambda : int(input())
from math import *
from sys import *
from datetime import datetime
from sys import stdin, stdout
import sys
from collections import *
for _ in range(ii()):
	x = ii()
	n = list(st())
	l = list(map(int, n))
	l1 = []
	l1.append(l[0])
	for i in range(1, len(l)):
		l1.append(l1[-1] + l[i])
	l1 = [0] + l1
	d = defaultdict(lambda : 0)
	ans = 0
	for i in range(len(l1)):
		d[l1[i] - i] += 1
	for i in d:
		ans += d[i] * (d[i] - 1) // 2
	print(ans)


n_inputs = int(input())
for _ in range(n_inputs):
	str_len = int(input())
	str_input = input()
	sums = 0
	sum_dict = {0: 1}
	current_sum = 0
	for char in str_input:
		current_sum += int(char) - 1
		if current_sum in sum_dict:
			sum_dict[current_sum] += 1
		else:
			sum_dict[current_sum] = 1
	ans = 0
	for n_occur in sum_dict.values():
		ans += n_occur * (n_occur - 1) // 2
	print(ans)


from collections import defaultdict as dd
import math
import sys
input = sys.stdin.readline

def nn():
	return int(input())

def li():
	return list(input())

def mi():
	return list(map(int, input().split()))

def lm():
	return list(map(int, input().split()))

def solve():
	n = nn()
	l = input()
	seen = dd(int)
	seen[0] = 1
	tally = 0
	good = 0
	for i in range(n):
		tally += int(l[i]) - 1
		seen[tally] += 1
	total = 0
	for s in seen:
		total += seen[s] * (seen[s] - 1) // 2
	print(total)
q = nn()
for _ in range(q):
	solve()


(T,) = list(map(int, input().split()))
for t in range(T):
	(N,) = list(map(int, input().split()))
	X = [0] * (N + 1)
	for (i, c) in enumerate(input().strip()):
		X[i + 1] = X[i] + int(c)
	d = dict()
	for i in range(N + 1):
		x = X[i] - i
		if x not in d:
			d[x] = 0
		d[x] += 1
	R = 0
	for k in d:
		R += d[k] * (d[k] - 1) // 2
	print(R)


from collections import defaultdict as d
t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	arr = [int(x) for x in s]
	p = [0]
	for x in arr:
		p.append(p[-1] + x)
	cnts = d(lambda : 0)
	for i in range(n + 1):
		diff = p[i] - i
		cnts[diff] += 1
	total = 0
	for v in cnts.values():
		if v >= 2:
			total += v * (v - 1) // 2
	print(total)


cases = int(input())
for t in range(cases):
	n = int(input())
	a = list(map(int, list(input())))
	a = [i - 1 for i in a]
	d = {}
	s = 0
	out = 0
	for i in range(n):
		s += a[i]
		if s == 0:
			out += 1
		if s not in d:
			d[s] = 1
		else:
			out += d[s]
			d[s] += 1
	print(out)


T = int(input())
for _ in range(T):
	n = int(input())
	a = input()
	sta = 0
	end = 0
	i = 1
	ans = 0
	summ = 0
	t = []
	kk = 0
	for i in range(n):
		kk += int(a[i])
		t.append(kk - i - 1)
	d = {0: 1}
	for i in range(n):
		if d.get(t[i], False):
			d[t[i]] += 1
		else:
			d[t[i]] = 1
	for i in d:
		ans += d[i] * (d[i] - 1) // 2
	print(ans)


t = int(input())
a = []
for i in range(t):
	n = int(input())
	s = input()
	a = []
	for i in range(n):
		g = int(s[i])
		a.append(g - 1)
		if len(a) > 1:
			a[len(a) - 1] = a[len(a) - 2] + a[len(a) - 1]
	a.sort()
	d = 1
	if a[0] == 0:
		t = 1
	else:
		t = 0
	for i in range(1, len(a)):
		if a[i] != a[i - 1] or i == len(a) - 1:
			if i == len(a) - 1 and a[i] == a[i - 1]:
				d += 1
			if d > 1:
				t = t + d * (d - 1) // 2
			d = 1
		else:
			d += 1
		if a[i] == 0:
			t += 1
	print(t)


t = int(input())
while t > 0:
	t -= 1
	(n, a) = (int(input()), input())
	(ans, summ, m) = (0, 0, {})
	for c in a:
		summ += int(c) - 1
		if summ == 0:
			ans += 1
		ans += m.get(summ, 0)
		m[summ] = m.get(summ, 0) + 1
	print(ans)


from collections import defaultdict

def main():
	for _ in range(int(input())):
		n = int(input())
		a = list(map(int, input()))
		b = [a[0]]
		for i in range(n - 1):
			b.append(b[-1] + a[i + 1])
		x = 0
		d = defaultdict(list)
		for i in range(n):
			d[b[i] - i - 1].append(i)
		for i in d:
			if i == 0:
				x += len(d[i])
			x += len(d[i]) * (len(d[i]) - 1) // 2
		print(x)
main()


from collections import defaultdict
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(input())
	d = defaultdict(int)
	d[0] = 1
	ans = 0
	now = 0
	for i in range(n):
		now += int(a[i])
		x = now - i - 1
		ans += d[x]
		d[x] += 1
	print(ans)


from collections import Counter
tests = int(input())
for test in range(tests):
	n = int(input())
	a = [int(i) - 1 for i in input()]
	p = []
	c = 0
	for i in range(n):
		c += a[i]
		p.append(c)
	d = Counter(p)
	ans = 0
	for k in d.keys():
		ans += d[k] * (d[k] - 1) // 2
	if 0 in d:
		ans += d[0]
	print(ans)


n = int(input())
for _ in range(n):
	m = int(input())
	s = input()
	ans = 0
	d = {0: 1}
	p = 0
	for c in s:
		p += ord(c) - ord('0') - 1
		cnt = d.setdefault(p, 0)
		ans += cnt
		d[p] = cnt + 1
	print(ans)


from collections import defaultdict
t = int(input())
for i in range(t):
	n = int(input())
	a = []
	st = input()
	for j in range(n):
		a.append(int(st[j]))
	for j in range(n):
		a[j] -= 1
	dic = defaultdict(lambda : 0)
	s = 0
	c = 0
	for j in range(n):
		s += a[j]
		if s == 0:
			c += 1
		if s in dic:
			c += dic[s]
		dic[s] += 1
	print(c)


from sys import stdin
t = int(stdin.readline())
for _ in range(t):
	length = int(stdin.readline())
	a = list(map(int, list(stdin.readline().rstrip())))
	currentSum = 0
	sumCounts = {0: 1}
	for n in a:
		currentSum += n - 1
		if currentSum not in sumCounts:
			sumCounts[currentSum] = 1
		else:
			sumCounts[currentSum] += 1
	good = 0
	for v in sumCounts.values():
		good += v * (v - 1) // 2
	print(good)


t = int(input())
for j in range(t):
	n = int(input())
	s = input()
	c = 0
	L = [0]
	C = [0 for i in range(9 * n + 1)]
	A = [int(s[0])]
	for i in range(1, len(s)):
		A.append(A[i - 1] + int(s[i]))
	for i in range(len(s)):
		L.append(A[i] - (i + 1))
	for i in range(len(L)):
		k = L[i]
		c = c + C[k]
		C[k] = C[k] + 1
	print(c)


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input()]
	prefix = [0]
	result = 0
	count = Counter()
	for val in a:
		prefix.append(prefix[-1] + val)
	for (i, p) in enumerate(prefix):
		curVal = p - i
		result += count[curVal]
		count[curVal] += 1
	print(result)


import sys
input = sys.stdin.readline
from collections import Counter
t = int(input())
for tests in range(t):
	n = int(input())
	A = input().strip()
	L = []
	for a in A:
		L.append(int(a) - 1)
	SUM = [0]
	for l in L:
		SUM.append(SUM[-1] + l)
	C = Counter(SUM)
	ANS = 0
	for c in C:
		ANS += C[c] * (C[c] - 1) // 2
	print(ANS)


def solve(string):
	val = string
	prefix = [0]
	for i in range(n):
		prefix.append(prefix[-1] + int(val[i]))
	one_sub = [i for i in range(n + 1)]
	after = [prefix[i] - one_sub[i] for i in range(n + 1)]
	hash_map = {}
	res = 0
	for i in after[::-1]:
		hash_map[i] = hash_map.get(i, 0)
		res += hash_map[i]
		hash_map[i] += 1
	print(res)
for _ in range(int(input())):
	n = int(input())
	string = list(map(int, input()))
	solve(string)


for _ in range(int(input())):
	n = int(input())
	s = input().strip()
	d = {}
	p = 0
	d[0] = 1
	res = 0
	for j in range(n):
		p = p + int(s[j])
		q = p - j - 1
		if q in d:
			res = res + d[q]
			d[q] = d[q] + 1
		else:
			d[q] = 1
	print(res)


from collections import defaultdict
t = int(input())
while t:
	t -= 1
	n = int(input())
	a = list(map(lambda x: x - 1, map(int, input())))
	prevSum = defaultdict(lambda : 0)
	currsum = 0
	res = 0
	for i in range(n):
		currsum += a[i]
		if currsum == 0:
			res += 1
		if currsum in prevSum:
			res += prevSum[currsum]
		prevSum[currsum] += 1
	print(res)


for _ in range(int(input())):
	sequence_length = int(input())
	sequence = input()
	partial_sums = {1: int(sequence[0])}
	temp_sum = partial_sums[1]
	for index in range(1, sequence_length):
		temp_sum += int(sequence[index])
		partial_sums[index + 1] = temp_sum
	good_sublines = 0
	prefixes = {0: 1}
	for index in range(1, sequence_length + 1):
		prefix = partial_sums[index] - index
		if prefix not in prefixes:
			prefixes[prefix] = 0
		prefixes[prefix] += 1
	for prefix in prefixes:
		if prefixes[prefix] >= 2:
			good_sublines += prefixes[prefix] * (prefixes[prefix] - 1) // 2
	print(good_sublines)


from collections import defaultdict

def findSubarraySum(arr, n, Sum):
	prevSum = defaultdict(lambda : 0)
	res = 0
	currsum = 0
	for i in range(0, n):
		currsum += arr[i]
		if currsum == Sum:
			res += 1
		if currsum - Sum in prevSum:
			res += prevSum[currsum - Sum]
		prevSum[currsum] += 1
	return res
for _ in range(int(input())):
	n = int(input())
	s = input()
	a = []
	for i in range(n):
		a.append(int(s[i]) - 1)
	print(findSubarraySum(a, n, 0))


import sys
input = sys.stdin.readline
t = int(input())
for t1 in range(t):
	n = int(input())
	s = input()
	s = s.strip()
	l = []
	for i in range(n):
		l.append(int(s[i]))
	for i in range(n):
		l[i] -= 1
	pre = [0] * n
	pre[0] = l[0]
	d = {}
	c = 0
	for i in range(n):
		if i == 0:
			if pre[0] == 0:
				c += 1
			d[pre[0]] = d.get(pre[0], 0) + 1
		else:
			pre[i] = pre[i - 1] + l[i]
			if pre[i] == 0:
				c += 1
			c += d.get(pre[i], 0)
			d[pre[i]] = d.get(pre[i], 0) + 1
	print(c)


from collections import defaultdict

def main():
	for case in range(int(input())):
		n = int(input())
		series = [int(x) - 1 for x in input()]
		counts = defaultdict(lambda : 0)
		pre_sum = 0
		ans = 0
		for x in series:
			pre_sum += x
			if pre_sum == 0:
				ans += 1
			req = pre_sum
			if req in counts:
				ans += counts[req]
			counts[pre_sum] += 1
		print(ans)
py2 = round(0.5)
if py2:
	from future_builtins import ascii, filter, hex, map, oct, zip
	range = xrange
import os, sys
from io import IOBase, BytesIO
BUFSIZE = 8192

class FastIO(BytesIO):
	newlines = 0

	def __init__(self, file):
		self._file = file
		self._fd = file.fileno()
		self.writable = 'x' in file.mode or 'w' in file.mode
		self.write = super(FastIO, self).write if self.writable else None

	def _fill(self):
		s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
		self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
		return s

	def read(self):
		while self._fill():
			pass
		return super(FastIO, self).read()

	def readline(self):
		while self.newlines == 0:
			s = self._fill()
			self.newlines = s.count(b'\n') + (not s)
		self.newlines -= 1
		return super(FastIO, self).readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.getvalue())
			(self.truncate(0), self.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		if py2:
			self.write = self.buffer.write
			self.read = self.buffer.read
			self.readline = self.buffer.readline
		else:
			self.write = lambda s: self.buffer.write(s.encode('ascii'))
			self.read = lambda : self.buffer.read().decode('ascii')
			self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


t = int(input())
for x in range(t):
	n = input()
	s = input()
	l = [int(i) for i in s]
	dp = [0 for i in range(len(l))]
	dp[0] = l[0]
	for i in range(1, len(l)):
		dp[i] = dp[i - 1] + l[i]
	dp2 = [0 for i in range(len(l))]
	for i in range(len(l)):
		dp2[i] = dp[i] - i
	d = {}
	for i in range(len(l)):
		if dp2[i] not in d:
			d[dp2[i]] = 1
		else:
			d[dp2[i]] = d[dp2[i]] + 1
	sum1 = 0
	for i in d:
		sum1 = sum1 + d[i] * (d[i] - 1) // 2
	for i in range(len(dp)):
		if dp[i] == i + 1:
			sum1 = sum1 + 1
	print(sum1)


def main():
	t = int(input())
	for tt in range(t):
		n = int(input())
		a = list(map(int, input()))
		s = 0
		c = 0
		d = {0: 1}
		for i in range(0, len(a)):
			s = s + (a[i] - 1)
			ss = s - 0
			if ss in d.keys():
				c = c + d[ss]
			d[s] = d.get(s, 0) + 1
		print(c)
main()


test = int(input())
from collections import defaultdict

def solve():
	n = int(input())
	seq = list(map(int, list(input())))
	d = defaultdict(int)
	ans = 0
	s = 0
	d[0] += 1
	for v in seq:
		s += v - 1
		d[s] += 1
	for v in d.values():
		ans += v * (v - 1) // 2
	print(ans)
	return
for i in range(test):
	solve()


for _ in range(int(input())):
	n = int(input())
	s = input()
	a = ''
	for i in s:
		a += chr(ord(i) - 1)
	d = {0: 1}
	s = 0
	ans = 0
	for i in a:
		s += ord(i) - ord('0')
		if s in d:
			ans += d[s]
			d[s] += 1
		else:
			d[s] = 1
	print(ans)


import math
import collections
from sys import stdin, stdout, setrecursionlimit
from bisect import bisect_left as bsl
from bisect import bisect_right as bsr
import heapq as hq
setrecursionlimit(2 ** 20)
t = 1
t = int(stdin.readline())
for _ in range(t):
	n = int(stdin.readline())
	s = stdin.readline().strip('\n')
	p = [0]
	for i in range(n):
		p.append(p[i] + int(s[i]))
	d = {0: 1}
	ans = 0
	for i in range(1, n + 1, 1):
		if p[i] - i not in d:
			d[p[i] - i] = 0
		d[p[i] - i] += 1
	for j in d:
		k = d[j]
		ans += k * (k - 1) // 2
	print(ans)


import collections

def solve(A, n):
	cnt = 0
	psum = 0
	d = collections.defaultdict(int)
	d[0] += 1
	for i in range(len(A)):
		psum += A[i]
		x = psum - i - 1
		d[x] += 1
		cnt += d[x] - 1
	return cnt
tests = int(input())
res = []
while tests > 0:
	n = int(input())
	line = input()
	line = '-'.join(line).split('-')
	A = [int(n) for n in line]
	res.append(solve(A, n))
	tests -= 1
for r in res:
	print(r)


from collections import defaultdict

def getNumArr(arr, n):
	currS = defaultdict(int)
	s = 0
	ns = 0
	currS[0] += 1
	for e in arr:
		s += e
		ns += currS[s]
		currS[s] += 1
	return ns
t = int(input())
for i in range(t):
	n = int(input())
	arr = input()
	arr = [int(i) - 1 for i in arr]
	print(getNumArr(arr, n))


def main():
	from collections import defaultdict as dd
	t = int(input())
	for _ in range(t):
		n = int(input())
		a = input()
		d = dd(lambda : 0)
		d[0] = 1
		s = 0
		for (idx, i) in enumerate(a):
			s += int(i)
			d[s - idx - 1] += 1
		ans = 0
		for (k, v) in d.items():
			ans += v * (v - 1) // 2
		print(ans)
	return
main()


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input()))
	pref = [0 for i in range(n + 1)]
	pref[0] = 0
	for i in range(1, n + 1):
		pref[i] = pref[i - 1] + a[i - 1] - 1
	pref = sorted(pref) + [9 * 10 ** 5 + 1]
	cnt = 1
	total = 0
	for i in range(1, n + 2):
		if pref[i] == pref[i - 1]:
			cnt += 1
		else:
			total += cnt * (cnt - 1) // 2
			cnt = 1
	print(total)


t = int(input())
for _ in range(t):
	n = int(input())
	(*a,) = map(int, list(input()))
	a = [ai - 1 for ai in a]
	bit = [0] * (n + 1)

	def add(t, x):
		while t <= n:
			bit[t] += x
			t += t & -t

	def que(t):
		res = 0
		while t:
			res += bit[t]
			t -= t & -t
		return res
	for i in range(n):
		add(i + 1, a[i])
	c = [que(i) for i in range(1, n + 1)]
	from collections import Counter
	cc = Counter(c)
	ans = 0
	for k in cc:
		ans += cc[k] * (cc[k] - 1) // 2
	if 0 in cc:
		ans += cc[0]
	print(ans)


for _ in range(int(input())):
	n = int(input())
	s = str(input())
	s1 = 0
	d = {0: 1}
	for i in range(n):
		s1 += int(s[i])
		x = s1 - i - 1
		if x not in d:
			d[x] = 0
		d[x] += 1
	ans = 0
	for i in d:
		ans += d[i] * (d[i] - 1) // 2
	print(ans)


from sys import stdin
for case in range(int(stdin.readline())):
	n = int(stdin.readline())
	s = [int(x) - 1 for x in stdin.readline().strip()]
	sums = [0]
	for x in s:
		sums.append(sums[-1] + x)
	nums = {}
	for x in sums:
		if x in nums:
			nums[x] += 1
		else:
			nums[x] = 1
	total = 0
	for x in nums:
		total += nums[x] * (nums[x] - 1) // 2
	print(total)


t = int(input())
for i in range(t):
	n = int(input())
	a = input()
	ls = {0: 1}
	lis = [0]
	for i in a:
		lis += [lis[-1] + int(i) - 1]
		if lis[-1] in ls:
			ls[lis[-1]] += 1
		else:
			ls[lis[-1]] = 1
	nat = 0
	for i in ls:
		i = ls[i]
		nat += i * (i - 1) / 2
	print(int(nat))


from _collections import defaultdict

def fun(arr, n):
	for i in range(n):
		arr[i] -= 1
	pref = [0] * n
	pref[0] = arr[0]
	for i in range(1, n):
		pref[i] = pref[i - 1] + arr[i]
	ans = 0
	a = defaultdict(lambda : 0)
	a[0] += 1
	for i in range(n):
		ans += a[pref[i]]
		a[pref[i]] += 1
	return ans
for _ in range(int(input())):
	n = int(input())
	arr = []
	for i in input():
		arr.append(int(i))
	print(fun(arr, n))


def CF(my_list, freq):
	for item in my_list:
		freq[item] = freq[item] + 1 if item in freq else 1
	return freq
for _ in range(int(input())):
	n = int(input())
	a = list(str(input()))
	for i in range(n):
		a[i] = int(a[i])
	s = 0
	suma = [0] * n
	for i in range(n):
		s += a[i]
		suma[i] = s
	k = 0
	for i in range(n):
		if suma[i] == i + 1:
			k += 1
	for i in range(n):
		suma[i] -= i
	d = CF(suma, {})
	for i in d:
		y = d[i]
		e = y * (y - 1) // 2
		k += e
	print(k)


for i in range(int(input())):
	n = int(input())
	s = input()
	l = [int(i) for i in s]
	cum = [l[0]]
	for i in range(1, n):
		cum.append(cum[-1] + l[i])
	d = {1: 1}
	for i in range(n):
		if d.get(cum[i] - i):
			d[cum[i] - i] += 1
		else:
			d[cum[i] - i] = 1
	ct = 0
	for i in d:
		ct += d[i] * (d[i] - 1) // 2
	print(ct)


for _ in range(int(input())):
	n = int(input())
	s = list(map(int, list(input())))
	cnt = [0] * (10 * n + 10)
	ind = 0
	ans = 0
	for i in range(n):
		cnt[ind] += 1
		ind += int(s[i]) - 1
		ans += cnt[ind]
	print(ans)


T = int(input())
for _ in range(T):
	N = int(input())
	st = input()
	d = {0: 1}
	sm = 0
	answer = 0
	for i in range(N):
		sm += int(st[i])
		if sm - (i + 1) in d:
			answer += d[sm - (i + 1)]
			d[sm - (i + 1)] += 1
		else:
			d[sm - (i + 1)] = 1
	print(answer)


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	a = [0] + list(map(int, input()))
	p = [a[0]]
	for i in range(1, len(a)):
		p.append(p[-1] + a[i] - 1)
	c = Counter(p[1:])
	ans = 0
	for i in c:
		if i == 0:
			ans += c[i]
		ans += c[i] * (c[i] - 1) // 2
	print(ans)


def i1():
	return int(input())
s1 = input

def redir(s):
	import sys
	sys.stdin = open(sys.argv[0] + f'/../in-{s}.txt')
from itertools import accumulate
from collections import Counter
t = i1()
for _ in range(t):
	n = i1()
	ss = s1()
	a = [int(i) - 1 for i in ss]
	n = len(a)
	s = list(accumulate(a))
	s.insert(0, 0)

	def cal(start, stop):
		cnt = 0
		if stop - start < 16:
			lst = s[start:stop + 1]
			for (i, x) in enumerate(lst):
				for y in lst[i + 1:]:
					if x == y:
						cnt += 1
		else:
			mid = (start + stop) // 2
			cnt += cal(start, mid) + cal(mid, stop)
			val = s[mid]
			l1 = (val - x for x in s[start:mid])
			lh = Counter(l1)
			l2 = (val - x for x in s[mid + 1:stop + 1])
			rh = Counter(l2)
			for i in lh & rh:
				cnt += lh[i] * rh[i]
		return cnt
	cnt = cal(0, n)
	print(cnt)


from math import *
from sys import *
t = int(stdin.readline().strip())
for _ in range(t):
	n = int(stdin.readline().strip())
	s = list(input())
	if n == 1:
		if s[0] == '1':
			print(1)
			continue
	l = {0: 1}
	k = 0
	ans = 0
	for i in range(n):
		k = k + int(s[i])
		a = k - i - 1
		if a not in l:
			l[a] = 0
		l[a] = l[a] + 1
		ans = ans + l[a] - 1
	print(ans)


from sys import stdin, stdout, setrecursionlimit
from math import gcd, ceil, sqrt
from collections import Counter, deque
from bisect import bisect_left, bisect_right
ii1 = lambda : int(stdin.readline().strip())
is1 = lambda : stdin.readline().strip()
iia = lambda : list(map(int, stdin.readline().strip().split()))
isa = lambda : stdin.readline().strip().split()
setrecursionlimit(100000)
mod = 1000000007
tc = ii1()
for _ in range(tc):
	n = ii1()
	arr = list(map(int, list(is1())))
	d = {0: [1]}
	res = tot = 0
	for i in range(n):
		tot += arr[i]
		d.setdefault(tot - i - 1, [0])[0] += 1
		res += d[tot - i - 1][0] - 1
	print(res)


import sys, os
from io import BytesIO, IOBase
import collections, itertools, bisect, heapq, math, string
from decimal import *
BUFSIZE = 8192
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def main():
	t = int(input())
	for i in range(t):
		n = int(input())
		a = str(input())
		d = collections.defaultdict(lambda : 0)
		d[0] = 1
		s = 0
		ans = 0
		for j in range(len(a)):
			s += int(a[j])
			ans += d[s - (j + 1)]
			d[s - (j + 1)] += 1
		print(ans)
main()


for _ in range(int(input())):
	n = int(input())
	a = input()
	d = {0: 1}
	res = 0
	pre = 0
	A = []
	for i in range(n):
		A.append(int(a[i]))
		A[i] -= 1
		pre += A[i]
		if pre not in d:
			d[pre] = 0
		d[pre] += 1
	for value in d.values():
		res += (value - 1) * value // 2
	print(res)


t = int(input())
for i in range(t):
	n = int(input())
	st = input()
	d = {0: 1}
	(s, res) = (0, 0)
	for i in range(n):
		s += int(st[i])
		x = s - (i + 1)
		if x not in d:
			d[x] = 0
		d[x] += 1
		res += d[x] - 1
	print(res)


import sys

def input():
	return sys.stdin.readline().rstrip()

def input_split():
	return [int(i) for i in input().split()]
testCases = int(input())
answers = []
for _ in range(testCases):
	n = int(input())
	arr = [int(i) for i in input()]
	d = {}
	ans = 0
	current = 0
	for (i, a) in enumerate(arr):
		current += a
		extra = current - (i + 1)
		if extra in d:
			d[extra] += 1
		else:
			d[extra] = 1
	val = 0
	if 0 in d:
		ans += d[val]
	till_now = {}
	current = 0
	for (i, a) in enumerate(arr):
		current += a
		extra = current - (i + 1)
		val = extra
		if extra in till_now:
			till_now[extra] += 1
		else:
			till_now[extra] = 1
		if val in d and val in till_now:
			ans += d[val] - till_now[val]
		elif val in d:
			ans += d[val]
	answers.append(ans)
print(*answers, sep='\n')


for time in range(int(input())):
	array = []
	n = int(input())
	num = input()
	for i in range(len(num)):
		array.append(int(num[i]))
	d = {}
	ans = 0
	diff = 0
	for i in range(n):
		if diff in d:
			d[diff] += 1
		else:
			d[diff] = 1
		diff += array[i] - 1
		if diff in d:
			ans += d[diff]
	print(ans)


def solution():
	for t in range(int(input())):
		n = int(input())
		l = [int(e) for e in input()]
		ans = 0
		d = {0: 1}
		idx = 0
		for e in l:
			idx += e - 1
			if idx in d:
				d[idx] += 1
			else:
				d[idx] = 1
			ans += d[idx] - 1
		print(ans)
	return
solution()


from sys import stdin
input = lambda : stdin.readline().strip()
l = len
for _ in range(int(input())):
	n = int(input())
	s = list(map(int, input()))
	dp = [0] * (9 * n)
	dp[0] = 1
	a = 0
	ans = 0
	for i in range(n):
		a += s[i]
		ans += dp[a - i - 1]
		dp[a - i - 1] += 1
	print(ans)


def solve():
	n = int(input())
	s = input()
	dp = [0] * (n * 10)
	dp[0] = 1
	check = 0
	ans = 0
	for i in range(n):
		check += int(s[i])
		x = check - i - 1
		dp[x] += 1
		ans += dp[x] - 1
	print(ans)
for _ in range(int(input())):
	solve()


from collections import defaultdict
t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	h = defaultdict(int)
	c = 0
	ans = 0
	for i in s:
		h[c] += 1
		c += int(i) - 1
		ans += h[c]
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	a = [int(i) for i in list(input())]
	modified_pref = [n + 1]
	for i in range(len(a)):
		modified_pref.append(modified_pref[-1] + a[i] - 1)
	modified_pref.sort()
	n_eq_pairs = 0
	prev = modified_pref[0]
	combo = 1
	for i in range(1, len(modified_pref)):
		current = modified_pref[i]
		if prev == current:
			combo += 1
		else:
			n_eq_pairs += combo * (combo - 1) / 2
			combo = 1
		prev = current
	n_eq_pairs += combo * (combo - 1) / 2
	print(int(n_eq_pairs))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, list(input())))
	s = []
	now_s = 0
	for i in range(n):
		now_s += a[i]
		s.append(now_s - i - 1)
	ans = 0
	data = {0: 1}
	for i in s:
		try:
			data[i] += 1
		except:
			data[i] = 1
		ans += data[i] - 1
	print(ans)


import sys
import math
from collections import defaultdict, Counter
t = int(input())
for i in range(t):
	n = int(input())
	s = list(map(int, list(input())))
	ans = 0
	j = 0
	curr = 0
	d = {0: 1}
	while j < len(s):
		curr += s[j]
		need = curr - j - 1
		s1 = d.get(need, 0)
		ans += s1
		d[need] = s1 + 1
		j += 1
	print(ans)


def solve(n, a):
	p = [0]
	for num in a:
		p.append(p[-1] + num)
	ans = 0
	ls = dict()
	ls[0] = 1
	for r in range(n):
		if p[r + 1] - r - 1 in ls:
			ans += ls[p[r + 1] - r - 1]
			ls[p[r + 1] - r - 1] += 1
		else:
			ls[p[r + 1] - r - 1] = 1
	return ans

def main():
	t = int(input())
	ns = []
	arrs = []
	for _ in range(t):
		n = int(input())
		r = input().strip()
		a = [int(x) for x in r]
		ns.append(n)
		arrs.append(a)
	for i in range(t):
		print(solve(ns[i], arrs[i]))
main()


from collections import Counter
T = int(input())
for test in range(T):
	n = int(input())
	a = [int(x) - 1 for x in input()]
	accum = [0]
	modcount = Counter()
	modcount[0] += 1
	count = 0
	for i in range(n):
		accum.append(accum[-1] + a[i])
		count += modcount[accum[-1]]
		modcount[accum[-1]] += 1
	print(count)


import sys
input = sys.stdin.readline

def inInt():
	return int(input())

def inStr():
	return input().strip('\n')

def inIList():
	return list(map(int, input().split()))

def inSList():
	return input().split()

def solve(l, N):
	m = {}
	m[1] = 1
	re = 0
	s = 0
	for i in range(N):
		s = s + int(l[i])
		f = s - i
		if f not in m:
			m[f] = 0
		re += m[f]
		m[f] += 1
	print(re)
tests = inInt()
for t in range(tests):
	n = inInt()
	s = inStr()
	solve(s, n)


for _ in range(int(input())):
	n = int(input())
	s = input()
	arr = [int(i) - 1 for i in s]
	dic = {}
	summ = 0
	ans = 0
	for i in range(n):
		summ += arr[i]
		if summ == 0:
			ans += 1
		if summ in dic:
			ans += dic[summ]
		try:
			dic[summ] += 1
		except:
			dic[summ] = 1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	s = input()
	d = {0: 1}
	summ = 0
	ans = 0
	for i in s:
		summ += int(i) - 1
		if d.get(summ, 0) > 0:
			ans += d.get(summ, 0)
			d[summ] += 1
		else:
			d[summ] = 1
	print(ans)


for data in [*open(0)][2::2]:
	d = {}
	d[0] = 1
	summ = 0
	ans = 0
	for a in data[0:-1]:
		c = int(a) - 1
		summ += c
		ans += d.get(summ, 0)
		if d.get(summ):
			d[summ] += 1
		else:
			d[summ] = 1
	print(ans)


import sys
read = lambda : sys.stdin.readline().strip()
from collections import defaultdict
for _ in range(int(read())):
	n = int(read())
	nums = list(map(int, list(read())))
	prefix = [0] * (n + 2)
	prefix[0] = 0
	for i in range(1, n + 1):
		prefix[i] = prefix[i - 1] + nums[i - 1]
	prefix[i + 1] = prefix[i]
	d = defaultdict(int)
	for i in range(n + 1):
		d[prefix[i] - i] += 1
	count = 0
	for val in d.values():
		count += val * (val - 1) // 2
	print(count)


from collections import Counter
from itertools import accumulate
import sys
buff_readline = sys.stdin.readline
readline = sys.stdin.readline
INF = 2 ** 62 - 1

def read_int():
	return int(buff_readline())

def read_int_n():
	return list(map(int, buff_readline().split()))

def read_float():
	return float(buff_readline())

def read_float_n():
	return list(map(float, buff_readline().split()))

def read_str():
	return readline().strip()

def read_str_n():
	return readline().strip().split()

def error_print(*args):
	print(*args, file=sys.stderr)

def mt(f):
	import time

	def wrap(*args, **kwargs):
		s = time.time()
		ret = f(*args, **kwargs)
		e = time.time()
		error_print(e - s, 'sec')
		return ret
	return wrap

@mt
def slv(N, A):
	S = [0] + list(accumulate(A))
	si = []
	for (i, v) in enumerate(S):
		si.append(v - i)
	c = Counter(si)
	ans = 0
	for v in c.values():
		ans += v * (v - 1) // 2
	return ans

def main():
	for _ in range(read_int()):
		N = read_int()
		A = [int(a) for a in read_str()]
		print(slv(N, A))
main()


for _ in range(int(input())):
	n = int(input())
	a = [int(s) - 1 for s in input()]
	pref = [0] * (n + 1)
	for i in range(1, n + 1):
		pref[i] = pref[i - 1] + a[i - 1]
	pref = sorted(pref)
	now = pref[0]
	tim = 0
	ans = 0
	for i in pref:
		if i == now:
			tim += 1
		else:
			ans += tim * (tim - 1) // 2
			tim = 1
			now = i
	ans += tim * (tim - 1) // 2
	print(ans)


for _ in range(int(input())):
	lg = int(input())
	n = [int(c) for c in input()]
	a = 0
	p = {0: 1}
	q = 0
	for k in n:
		q += k - 1
		if q in p:
			a += p[q]
		else:
			p[q] = 0
		p[q] += 1
	print(a)


from collections import defaultdict

def solve():
	n = int(input())
	A = list(map(int, list(input())))
	ret = 0
	sa = 0
	d = defaultdict(int)
	d[1] += 1
	for i in range(n):
		sa += A[i]
		z = sa - i
		ret += d[z]
		d[z] += 1
	return ret

def main():
	t = int(input())
	ANS = [solve() for _ in range(t)]
	print('\n'.join(map(str, ANS)))
main()


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, list(input())))
	pre = defaultdict(lambda : 0)
	pre[0] += 1
	s = 0
	for i in range(n):
		s += arr[i]
		pre[s - i - 1] += 1
	m = 0
	for i in pre:
		m += pre[i] * (pre[i] - 1) // 2
	print(m)


from collections import defaultdict
T = int(input())
for i in range(T):
	N = int(input())
	S = input()
	d = defaultdict(int)
	ar = []
	for j in range(N):
		ar.append(int(S[j]) - 1)
	ans = 0
	sm = 0
	for j in range(N):
		sm += ar[j]
		if sm == 0:
			ans += 1
		if sm in d:
			ans += d[sm]
		d[sm] += 1
	print(ans)


import math, sys
input = sys.stdin.readline
S = lambda : input().rstrip()
I = lambda : int(S())
M = lambda : map(int, S().split())
L = lambda : list(M())
mod1 = 1000000007
mod2 = 998244353
from collections import Counter
for _ in range(I()):
	n = I()
	s = S()
	l = [int(i) for i in s]
	pre = [0] * (n + 1)
	ans = 0
	for i in range(n):
		pre[i + 1] = pre[i] + l[i]
	for i in range(n + 1):
		pre[i] = pre[i] - i
	cnt = Counter(pre)
	for ele in cnt:
		ans += (cnt[ele] - 1) * cnt[ele] // 2
	print(ans)


T = int(input())
for t in range(T):
	n = int(input())
	arr = input()
	d = {0: 1}
	s = 0
	ans = 0
	for i in range(len(arr)):
		s += int(arr[i])
		x = s - i - 1
		if x not in d:
			d[x] = 0
		d[x] += 1
		ans += d[x] - 1
	print(ans)


for _ in range(int(input())):
	N = int(input())
	s = list(map(int, list(input())))
	presum = [s[0]]
	for i in range(1, len(s)):
		presum.append(s[i] + presum[-1])
	for i in range(len(presum)):
		presum[i] -= i + 1
	presum.insert(0, 0)
	dic = {}
	count = 0
	for i in range(len(presum)):
		if presum[i] not in dic:
			dic[presum[i]] = 1
		else:
			count += dic[presum[i]]
			dic[presum[i]] += 1
	print(count)


import sys
import io
import math
import collections
import decimal
import itertools
import bisect
import heapq

def input():
	return sys.stdin.readline()[:-1]
INF = 10 ** 10

def solve(N, S):
	B = [int(c) - 1 for c in S]
	T = [0] + list(itertools.accumulate(B))
	counter = collections.Counter(T)
	ans = 0
	for n in counter.values():
		ans += n * (n - 1) // 2
	return ans
T0 = int(input())
for _ in range(T0):
	N = int(input())
	S = input()
	print(solve(N, S))


def solve(n, a):
	(ans, cur_sum) = (0, 0)
	d = {0: 1}
	for i in range(n):
		cur_sum += int(a[i])
		diff = cur_sum - (i + 1)
		ans += d.get(diff, 0)
		d[diff] = d.get(diff, 0) + 1
	return ans
for t in range(int(input())):
	n = int(input())
	a = input()
	print(solve(n, a))


for j in range(int(input())):
	a = int(input())
	b = [int(j) for j in input()]
	su = 0
	d = {}
	d[0] = 1
	count = 0
	for j in range(a):
		su = su + b[j]
		su1 = su - (j + 1)
		if su1 in d:
			count += d[su1]
			d[su1] += 1
		else:
			d[su1] = 1
	print(count)


for _ in range(int(input())):
	n = int(input())
	st = input()
	prev = 0
	ans = 0
	dict = {}
	for i in range(n):
		prev += int(st[i]) - 1
		if dict.get(prev, None) != None:
			if prev == 0:
				ans += 1
			ans += dict[prev]
			dict[prev] += 1
		else:
			if prev == 0:
				ans += 1
			dict[prev] = 1
	print(ans)


import sys

def main():
	T = int(sys.stdin.readline())
	while T:
		T -= 1
		N = int(sys.stdin.readline())
		Arr = [int(i) for i in sys.stdin.readline().rstrip()]
		agg = [0] * N
		agg[0] = Arr[0]
		for i in range(1, N):
			agg[i] = agg[i - 1] + Arr[i]
		nxt_zero = [N] * (N + 1)
		nxt_gtone = [N] * (N + 1)
		nxt_gtone[N - 1] = N - 1
		nxt_zero[N - 1] = N - 1
		for i in range(N - 2, -1, -1):
			nxt_gtone[i] = i if Arr[i] > 1 else nxt_gtone[i + 1]
			nxt_zero[i] = i if not Arr[i] else nxt_zero[i + 1]
		ans = 0
		DP = [0] * (N + 1)
		for i in range(N - 1, -1, -1):
			j = i if Arr[i] == 1 else nxt_gtone[i]
			while j < N:
				cur = (agg[j] - agg[i - 1] if i else agg[j]) - (j - i + 1)
				if not cur:
					ans += 1
					DP[i] += 1
					if DP[j + 1]:
						ans += DP[j + 1]
						DP[i] += DP[j + 1]
					break
				if cur > 0:
					if cur <= nxt_gtone[j + 1] - j:
						j += cur
					else:
						j = nxt_zero[j + 1]
				else:
					j = nxt_gtone[j + 1]
		print(ans)
main()


import collections
T = int(input())
for t in range(1, T + 1):
	N = int(input())
	A = [int(s) for s in input()]
	pref = []
	tot = 0
	sum_count = collections.defaultdict(int)
	sum_count[0] = 1
	for j in range(N):
		tot += A[j] - 1
		sum_count[tot] += 1
	ans = 0
	for tot in sum_count:
		x = sum_count[tot]
		ans += x * (x - 1) // 2
	print(ans)


import sys
import string
import math
from collections import defaultdict
from functools import lru_cache
from collections import Counter
from fractions import Fraction

def mi(s):
	return map(int, s.strip().split())

def lmi(s):
	return list(mi(s))

def tmi(s):
	return tuple(mi(s))

def mf(f, s):
	return map(f, s)

def lmf(f, s):
	return list(mf(f, s))

def js(lst):
	return ' '.join((str(d) for d in lst))

def jsns(lst):
	return ''.join((str(d) for d in lst))

def line():
	return sys.stdin.readline().strip()

def linesp():
	return line().split()

def iline():
	return int(line())

def mat(n):
	matr = []
	for _ in range(n):
		matr.append(linesp())
	return matr

def matns(n):
	mat = []
	for _ in range(n):
		mat.append([c for c in line()])
	return mat

def mati(n):
	mat = []
	for _ in range(n):
		mat.append(lmi(line()))
	return mat

def pmat(mat):
	for row in mat:
		print(js(row))

def solve(arr):
	arr = [i - 1 for i in arr]
	pref_sum = defaultdict(int)
	for i in range(len(arr)):
		pref_sum[i] = pref_sum[i - 1] + arr[i]
	count = 0
	sums = defaultdict(int)
	sums[0] = 1
	for (e, n) in enumerate(arr):
		looking_for = pref_sum[e]
		count += sums[looking_for]
		sums[pref_sum[e]] += 1
	print(count)

def main():
	t = iline()
	for _ in range(t):
		line()
		arr = [int(i) for i in line()]
		solve(arr)
main()


t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	sumarray = []
	sum = 0
	for j in range(n):
		sum += int(s[j]) - 1
		sumarray.append(sum)
	count = 0
	d = {0: 1}
	for j in range(n):
		if sumarray[j] not in d:
			d[sumarray[j]] = 0
		d[sumarray[j]] += 1
		count += d[sumarray[j]] - 1
	print(count)


from sys import stdin
from collections import defaultdict
input = stdin.readline

def findSubArrays(a, n):
	hashMap = defaultdict(lambda : 0)
	out = 0
	s = 0
	for i in range(n):
		s += a[i]
		if s == 0:
			out += 1
		if s in hashMap:
			out += hashMap[s]
		hashMap[s] += 1
	return out
for _ in range(int(input())):
	n = int(input())
	a = [int(x) - 1 for x in input().strip()]
	print(findSubArrays(a, n))


for s in [*open(0)][2::2]:
	d = {0: 1}
	r = t = 0
	for x in s[:-1]:
		t += int(x) - 1
		x = d.get(t, 0)
		r += x
		d[t] = x + 1
	print(r)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = input()
	from collections import defaultdict
	store = defaultdict(int)
	store[0] = 1
	ans = 0
	sums = 0
	for ch in arr:
		num = int(ch) - 1
		sums += num
		ans += store[sums]
		store[sums] += 1
	print(ans)


for test_i in range(int(input())):
	n = int(input())
	arr = list(map(lambda x: int(x) - 1, list(input())))
	pref_arr = [0]
	sum_cur = 0
	for el in arr:
		sum_cur += el
		pref_arr.append(sum_cur)
	pref_arr.sort()
	ans = 0
	count = 1
	i = 1
	while i <= n:
		if pref_arr[i] == pref_arr[i - 1]:
			count += 1
		else:
			ans += count * (count - 1) // 2
			count = 1
		i += 1
	ans += count * (count - 1) // 2
	print(ans)


from collections import defaultdict

def findSubarraySum(arr, Sum):
	n = len(arr)
	prevSum = defaultdict(lambda : 0)
	res = 0
	currsum = 0
	for i in range(0, n):
		currsum += arr[i]
		if currsum == Sum:
			res += 1
		if currsum - Sum in prevSum:
			res += prevSum[currsum - Sum]
		prevSum[currsum] += 1
	return res
for _ in range(int(input())):
	n = int(input())
	ls = list(map(int, list(input())))
	for i in range(n):
		ls[i] -= 1
	print(findSubarraySum(ls, 0))


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	s = input()
	freq = defaultdict(int)
	arr = list(s)
	for i in range(n):
		arr[i] = int(arr[i])
	pf = [arr[0]]
	for i in range(1, n):
		pf.append(pf[-1] + arr[i])
	freq[0] = 1
	ans = 0
	for i in range(n):
		ans += freq[pf[i] - i - 1]
		freq[pf[i] - i - 1] += 1
	print(ans)


from itertools import accumulate
from collections import Counter
for _ in range(int(input())):
	n = int(input())
	a = [int(c) for c in input()]
	acm = [0] + list(accumulate(a))
	acum = [acm[i] - i for i in range(n + 1)]
	cnt = Counter(acum)
	ans = 0
	for v in cnt.values():
		ans += v * (v - 1) // 2
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	a = input()
	d = {0: 1}
	result = 0
	s = 0
	for j in range(n):
		s += int(a[j])
		x = s - j - 1
		if x not in d:
			d[x] = 0
		result += d[x]
		d[x] += 1
	print(result)


from collections import defaultdict
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input()]
	p = [0] * (n + 1)
	for i in range(1, n + 1):
		p[i] = p[i - 1] + a[i - 1]
	d = defaultdict(list)
	for i in range(n + 1):
		d[p[i] - i].append(i)
	ans = 0
	for v in d.values():
		ans += len(v) * (len(v) - 1) // 2
	print(ans)


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	a = [*map(int, input())]
	d = defaultdict(int)
	d[0] = 1
	cur = ans = 0
	for i in range(n):
		cur += a[i]
		t = cur - i - 1
		ans += d[t]
		d[t] += 1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = input()
	p = 0
	ans = 0
	good = [1] + [0] * 9 * n
	for i in a:
		p += int(i) - 1
		ans += good[p]
		good[p] += 1
	print('%d\n' % ans)


def main():
	t = int(input())
	for _ in range(t):
		n = int(input().strip())
		s = input().strip()
		a = [int(s[0]) - 1]
		for i in range(1, n):
			d = int(s[i]) - 1
			a.append(a[-1] + d)
		ma = {0: 1}
		ret = 0
		for i in range(n):
			ret += ma.get(a[i], 0)
			ma[a[i]] = ma.get(a[i], 0) + 1
		print(ret)
main()


import math
import sys
from collections import Counter
from math import factorial as fact
from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	ans = 0
	a = [int(i) for i in str(input())]
	d = defaultdict(lambda : 0)
	curr = 0
	for i in range(n):
		a[i] -= 1
	for i in a:
		curr += i
		if curr == 0:
			ans += 1
		if curr in d:
			ans += d[curr]
		d[curr] += 1
	print(ans)


import os
import sys
from io import BytesIO, IOBase
import math

def main():
	pass
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def binary(n):
	return bin(n).replace('0b', '')

def decimal(s):
	return int(s, 2)

def pow2(n):
	p = 0
	while n > 1:
		n //= 2
		p += 1
	return p

def primeFactors(n):
	l = []
	while n % 2 == 0:
		l.append(2)
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			l.append(i)
			n = n / i
	if n > 2:
		l.append(int(n))
	return l

def isPrime(n):
	if n == 1:
		return False
	else:
		root = int(n ** 0.5)
		root += 1
		for i in range(2, root):
			if n % i == 0:
				return False
		return True

def maxPrimeFactors(n):
	maxPrime = -1
	while n % 2 == 0:
		maxPrime = 2
		n >>= 1
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			maxPrime = i
			n = n / i
	if n > 2:
		maxPrime = n
	return int(maxPrime)

def reVal(num):
	if num >= 0 and num <= 9:
		return chr(num + ord('0'))
	else:
		return chr(num - 10 + ord('A'))

def strev(str):
	len = len(str)
	for i in range(int(len / 2)):
		temp = str[i]
		str[i] = str[len - i - 1]
		str[len - i - 1] = temp

def fromDeci(res, base, inputNum):
	index = 0
	while inputNum > 0:
		res += reVal(inputNum % base)
		inputNum = int(inputNum / base)
	res = res[::-1]
	return res

def norm(s, l):
	tba = '0' * (l - len(s))
	return tba + s
for xyz in range(0, int(input())):
	n = int(input())
	s = input()
	pre = [0]
	for i in range(0, n):
		pre.append(pre[i] + int(s[i]) - 1)
	cnt = {}
	for i in pre:
		if i in cnt:
			cnt[i] += 1
		else:
			cnt[i] = 1
	a = 0
	for i in cnt:
		a += cnt[i] * (cnt[i] - 1) // 2
	print(a)


from collections import Counter
from itertools import accumulate
for case in range(int(input())):
	n = int(input())
	ct = Counter(list(accumulate([int(x) - 1 for x in input()])))
	ans = ct[0]
	for z in ct.values():
		ans += (z - 1) * z // 2
	print(ans)


from bisect import bisect_left as bl
from bisect import bisect_right as br
from heapq import heappush, heappop, heapify
import math
from collections import *
from functools import reduce, cmp_to_key
import sys
input = sys.stdin.readline
from itertools import accumulate
M = mod = 10 ** 9 + 7

def factors(n):
	return sorted(set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0))))

def inv_mod(n):
	return pow(n, mod - 2, mod)

def li():
	return [int(i) for i in input().rstrip('\n').split()]

def st():
	return input().rstrip('\n')

def val():
	return int(input().rstrip('\n'))

def li2():
	return [i for i in input().rstrip('\n')]

def li3():
	return [int(i) for i in input().rstrip('\n')]
for _ in range(val()):
	n = val()
	d = defaultdict(int)
	l = li3()
	d[0] = 1
	ans = s = 0
	for i in range(n):
		s += l[i]
		ans += d[s - i - 1]
		d[s - i - 1] += 1
	print(ans)


from collections import defaultdict as dc

def solv():
	x = int(input())
	s = list(map(int, input()))
	res = 0
	sm = 0
	mp = dc(lambda : 0)
	mp[0] = 1
	for n in range(x):
		sm += s[n]
		v = sm - n - 1
		mp[v] += 1
		res += mp[v] - 1
	print(res)
for _ in range(int(input())):
	solv()


from collections import defaultdict as dd
for _ in range(int(input())):
	a = int(input())
	t = list(map(lambda i: int(i), list(input())))
	pre = []
	d = dd(lambda : 0)
	sm = 0
	cnt = 0
	for i in range(len(t)):
		sm += t[i]
		if sm == i + 1:
			cnt += 1
		d[sm - i - 1] += 1
	for j in d.values():
		if j > 1:
			cnt += j * (j - 1) // 2
	print(cnt)


from sys import stdin
fi = stdin
n_test = int(fi.readline())

def solve():
	n = int(fi.readline())
	a = fi.readline().strip()
	from collections import Counter
	c = Counter()
	f = [0]
	for v in a:
		f.append(f[-1] + int(v))
	c[0] = 1
	res = 0
	for (idx, v) in list(enumerate(f))[1:]:
		res += c[v - idx]
		c[v - idx] += 1
	print(res)
for i_test in range(n_test):
	solve()


from sys import stdin
g = lambda : stdin.readline().strip()
gl = lambda : g().split()
gil = lambda : [int(var) for var in gl()]
gcl = lambda : list(g())
gbs = lambda : [int(var) for var in g()]
mod = int(1000000000.0) + 7
inf = float('inf')
from collections import defaultdict

def findSubarraySum(arr, n, Sum):
	prevSum = defaultdict(lambda : 0)
	res = 0
	currsum = 0
	for i in range(0, n):
		currsum += arr[i]
		if currsum == Sum:
			res += 1
		if currsum - Sum in prevSum:
			res += prevSum[currsum - Sum]
		prevSum[currsum] += 1
	return res
(t,) = gil()
for _ in range(t):
	(n,) = gil()
	a = gbs()
	for i in range(n):
		a[i] -= 1
	print(findSubarraySum(a, n, 0))


for _ in range(int(input())):
	n = int(input())
	s = input()
	cnt = [0] * (10 * n + 20)
	a = ext = ans = 0
	for i in s:
		cnt[ext] += 1
		ext += int(i) - 1
		ans += cnt[ext]
	print(ans)


import math
for _ in range(int(input())):
	n = int(input())
	ar = list(map(int, input()))
	for i in range(n):
		ar[i] = ar[i] - 1
	presum = []
	temp = 0
	for i in range(n):
		temp += ar[i]
		presum.append(temp)
	ans = 0
	dp = {}
	for i in range(n):
		if presum[i] in dp:
			dp[presum[i]] += 1
		else:
			dp[presum[i]] = 1
	for i in dp.keys():
		if i == 0:
			ans += dp[i] * (dp[i] + 1) // 2
		else:
			ans += dp[i] * (dp[i] - 1) // 2
	print(ans)


from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	n = int(input())
	s = input()[:-1]
	p = [0]
	for i in s:
		p.append(p[-1] + int(i))
	d = {0: 1}
	ans = 0
	for i in range(n):
		ans += d.get(p[i + 1] - i - 1, 0)
		d[p[i + 1] - i - 1] = d.get(p[i + 1] - i - 1, 0) + 1
	print(ans)


for x in range(int(input())):
	n = int(input())
	d = {}
	m = list(map(int, input()))
	l = 0
	for i in range(n):
		l += m[i] - 1
		if l in d:
			d[l] += 1
		else:
			d[l] = 1
	s = 0
	for i in d:
		s += d[i] * (d[i] - 1) // 2
		if i == 0:
			s += d[i]
	print(s)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, list(input())))
	c = {}
	c[0] = 1
	b = 0
	for i in range(n):
		b += a[i]
		d = b - i - 1
		if d in c.keys():
			c[d] += 1
		else:
			c[d] = 1
	s = 0
	for i in c:
		s += c[i] * (c[i] - 1) // 2
	print(s)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input()))
	s = [0]
	for x in a:
		s += (x + s[-1],)
	b = [i for i in range(n + 1)]
	c = [s[i] - b[i] for i in range(n + 1)]
	d = {}
	ans = 0
	for x in c[::-1]:
		d[x] = d.get(x, 0)
		ans += d[x]
		d[x] += 1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	s = input()
	summ = 0
	mp = {}
	ans = 0
	mp[0] = 1
	for i in range(1, n + 1):
		summ += int(s[i - 1])
		if summ - i in mp:
			ans += mp[summ - i]
			mp[summ - i] += 1
		else:
			mp[summ - i] = 1
	print(ans)


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input()]
	ps = []
	s = 0
	d = defaultdict(int)
	for i in range(n):
		s += a[i]
		ps.append(s - i - 1)
	ans = 0
	ans += ps.count(0)
	for i in range(n):
		d[ps[i]] += 1
		t = d[ps[i]]
		if t > 1:
			ans += t * (t - 1) // 2 - (t - 1) * (t - 2) // 2
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	li = list(map(int, input()))
	prefix = [0] * (n + 1)
	for i in range(1, n + 1):
		prefix[i] = li[i - 1] + prefix[i - 1]
	dic = {}
	for i in range(n + 1):
		val = dic.get(prefix[i] - i, -1)
		if val == -1:
			dic[prefix[i] - i] = 1
		else:
			dic[prefix[i] - i] = val + 1
	ans = 0
	for item in dic.values():
		temp = item * (item - 1)
		temp //= 2
		ans += temp
	print(ans)


import sys
import math
import collections
from sys import stdin, stdout, setrecursionlimit
import bisect as bs
setrecursionlimit(2 ** 20)
M = 10 ** 9 + 7
T = int(stdin.readline())
for _ in range(T):
	n = int(stdin.readline())
	s = stdin.readline().strip('\n')
	p = [0]
	for i in range(n):
		p.append(p[i] + int(s[i]))
	d = {0: 1}
	ans = 0
	for i in range(1, n + 1, 1):
		if p[i] - i in d:
			ans += d[p[i] - i]
		else:
			d[p[i] - i] = 0
		d[p[i] - i] += 1
	print(ans)


for i in range(int(input())):
	n = int(input())
	a = [int(num) for num in input()]
	(ans, dic, temp) = (0, {0: 1}, 0)
	for j in a:
		temp += j - 1
		if temp in dic:
			ans += dic[temp]
			dic[temp] += 1
		else:
			dic[temp] = 1
	print(ans)


import sys
input = sys.stdin.readline

def solve():
	n = int(input())
	a = list(map(int, input()[:-1]))
	fr = {0: 1}
	res = 0
	acum = 0
	for x in a:
		acum += x - 1
		if acum in fr:
			res += fr[acum]
			fr[acum] += 1
		else:
			fr[acum] = 1
	return res

def main():
	T = int(input())
	while T > 0:
		T -= 1
		print(solve())
main()


from collections import defaultdict
for _ in range(int(input())):
	i_liczb = int(input())
	liczby = input()
	(suma, wynik) = (0, 0)
	d = defaultdict(int)
	d[0] += 1
	for i in range(i_liczb):
		suma += int(liczby[i])
		x = suma - i - 1
		d[x] += 1
		wynik += d[x] - 1
	print(wynik)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input()))
	d = {0: 1}
	sm = 0
	ans = 0
	for i in range(n):
		x = arr[i] - 1
		sm += x
		ans += d.get(sm, 0)
		d[sm] = d.get(sm, 0) + 1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	lis = input()
	dp = {0: 1}
	ans = 0
	cnt = 0
	for i in range(n):
		cnt += int(lis[i]) - 1
		ans += dp.get(cnt, 0)
		dp[cnt] = dp.get(cnt, 0) + 1
	print(ans)


from sys import stdin, stdout
import math
from collections import Counter, deque, defaultdict
L = lambda : list(map(int, stdin.readline().strip().split()))
M = lambda : map(int, stdin.readline().strip().split())
I = lambda : int(stdin.readline().strip())
S = lambda : stdin.readline().strip()
C = lambda : stdin.readline().strip().split()
mod = 1000000007

def pr(a):
	return ''.join(list(map(str, a)))

def solve():
	n = I()
	s = S()
	d = defaultdict(int)
	d[0] = 1
	su = 0
	ans = 0
	for i in range(n):
		su += int(s[i])
		x = su - i - 1
		d[x] += 1
		ans += d[x] - 1
	print(ans)
for _ in range(I()):
	solve()


T = int(input())
for t in range(T):
	n = int(input())
	arr = input()
	summ = dict()
	cnt = 0
	ans = 0
	for i in range(n):
		a = int(arr[i])
		a -= 1
		cnt += a
		if cnt == 0:
			ans += 1
		if cnt in summ:
			ans += summ[cnt]
			summ[cnt] += 1
		else:
			summ.update({cnt: 1})
	print(ans)


import math, sys
from collections import defaultdict, Counter, deque
INF = float('inf')
MOD = 10 ** 9 + 7

def gcd(a, b):
	while b:
		(a, b) = (b, a % b)
	return a

def isPrime(n):
	if n <= 1:
		return False
	i = 2
	while i ** 2 <= n:
		if n % i == 0:
			return False
		i += 1
	return True

def primeFactors(n):
	factors = []
	i = 2
	while i ** 2 <= n:
		while n % i == 0:
			factors.append(i)
			n //= i
		i += 1
	if n > 1:
		factors.append(n)
	return factors

def vars():
	return map(int, input().split())

def array():
	return list(map(int, input().split()))

def main():
	n = int(input())
	arr = list(map(int, input()))
	m = defaultdict(int)
	s = 0
	m[0] = 1
	for i in range(n):
		arr[i] -= 1
		s += arr[i]
		m[s] += 1
	ans = 0
	for s in m:
		ans += m[s] * (m[s] - 1) // 2
	print(ans)
t = int(input())
for _ in range(t):
	main()


from itertools import accumulate
from collections import Counter
for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input()]
	s = [0] + list(accumulate(a))
	for i in range(n + 1):
		s[i] -= i
	c = Counter(s)
	ans = 0
	for i in c:
		ans += c[i] * (c[i] - 1) // 2
	print(ans)


import sys
from typing import Counter
input = sys.stdin.readline

def print(*args, end='\n', sep=' '):
	sys.stdout.write(sep.join(map(str, args)) + end)

def solve():
	n = int(input())
	a = list(map(int, list(input()[:n])))
	res = 0
	s = [0]
	for x in a:
		s.append(s[-1] + x)
	cc = Counter()
	for i in range(n + 1):
		cc[s[i] - i] += 1
	for x in cc.values():
		res += x * (x - 1) // 2
	print(res)

def main():
	t = int(input())
	for _ in range(t):
		solve()
main()


import sys, math
input = sys.stdin.readline
t = int(input())
for r in range(t):
	n = int(input())
	s = input().strip('\n')
	n += 1
	l = [0]
	for i in s:
		l.append(int(i))
	d = {}
	prefix = [0]
	count = 0
	for i in range(1, n):
		prefix.append(prefix[i - 1] + l[i])
		try:
			d[prefix[i - 1] - i + 1] += 1
		except:
			d[prefix[i - 1] - i + 1] = 1
		try:
			count += d[prefix[i] - i]
		except:
			pass
	print(count)


from collections import Counter
from itertools import accumulate

def count_good_subsegments(array):
	array = tuple(array)
	sum_le = tuple(accumulate(array))
	p = tuple((sum_le[i] - i for i in range(len(array))))
	pf = lambda i: p[i] if i != -1 else 1
	result = 0
	ps_before = Counter({pf(-1): 1})
	for curr_i in range(len(array)):
		result += ps_before[p[curr_i]]
		ps_before[p[curr_i]] += 1
	return result

def main():
	t = int(input())
	for i in range(t):
		n = int(input())
		a = tuple(map(int, input()))
		result = count_good_subsegments(a)
		print(result)
main()


from collections import defaultdict

def findSubarraySum(arr, n, Sum):
	prevSum = defaultdict(lambda : 0)
	res = 0
	currsum = 0
	for i in range(0, n):
		currsum += arr[i]
		if currsum == Sum:
			res += 1
		if currsum - Sum in prevSum:
			res += prevSum[currsum - Sum]
		prevSum[currsum] += 1
	return res
t = int(input())
for i in range(t):
	n = int(input())
	arr = [int(i) - 1 for i in input()]
	Sum = 0
	n = len(arr)
	print(findSubarraySum(arr, n, Sum))


def anido(n, k, vals):
	resultado = 0
	atual = 0
	soma = {0: 1}
	i = 0
	for x in vals:
		atual += x
		try:
			resultado += soma[atual - k]
		except:
			pass
		if atual in soma.keys():
			soma[atual] += 1
		else:
			soma[atual] = 1
		i += 1
	return resultado
tc = int(input())
for _ in range(tc):
	n = int(input())
	s = input()
	vals = []
	for c in s:
		vals.append(ord(c) - ord('0') - 1)
	print(anido(n, 0, vals))


import os, sys
from io import BytesIO, IOBase
from collections import Counter

def main():
	for _ in range(int(input())):
		n = int(input())
		lst = list(map(int, list(input())))
		dev = Counter()
		su = 0
		for (i, val) in enumerate(lst):
			su += val
			dev[su - i - 1] += 1
		ans = dev[0]
		for i in dev:
			ans += dev[i] * (dev[i] - 1) // 2
		print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


for _ in range(int(input())):
	n = int(input())
	s = input()
	a = [int(i) - 1 for i in s]
	c = 0
	p = [0]
	d = {}
	d[0] = 1
	for i in a:
		c = c + i
		if c in d:
			d[c] += 1
		else:
			d[c] = 1
	ans = 0
	for i in d:
		ans += d[i] * (d[i] - 1) // 2
	print(ans)


t = int(input().split()[0])
for case in range(t):
	n = int(input().split()[0])
	s = input().split()[0]
	curr = 0
	ans = 0
	prev = {}
	for i in range(n):
		curr += int(s[i]) - 1
		if curr == 0:
			ans += 1
		if curr in prev:
			ans += prev[curr]
			prev[curr] += 1
		else:
			prev[curr] = 1
	print(ans)


import sys

def i():
	return sys.stdin.readline()[:-1]

def listDigits():
	return list(map(lambda x: int(x) - 1, i()))
cases = int(i())
for x in range(cases):
	n = int(i())
	digits = listDigits()
	goodSubArrays = 0
	sums = {}
	for (y, digit) in enumerate(digits):
		if digit == 0:
			goodSubArrays += 1
		if -digit in sums.keys():
			goodSubArrays += sums[-digit]
		newSums = {}
		for key in sums:
			if -200 < key + digit < 150:
				newSums[key + digit] = sums[key]
		if digit in newSums.keys():
			newSums[digit] += 1
		else:
			newSums[digit] = 1
		sums = newSums
	print(goodSubArrays)


for data in [*open(0)][2::2]:
	d = {0: 1}
	summ = ans = 0
	for a in data[0:-1]:
		c = int(a) - 1
		summ += c
		x = d.get(summ, 0)
		d[summ] = x + 1
		ans += x
	print(ans)


from collections import defaultdict
t = int(input())

def solve(n, a):
	s = [0] * (n + 1)
	u = [0] * (n + 1)
	for i in range(n):
		assert i < n, f'i'
		s[i + 1] = s[i] + a[i]
		u[i + 1] = s[i + 1] - (i + 1)
	dd = defaultdict(int)
	ans = 0
	for uu in u:
		ans += dd[uu]
		dd[uu] += 1
	return ans
for _ in range(t):
	n = int(input())
	a = list(map(int, list(input())))
	print(solve(n, a))


from collections import defaultdict

def solve():
	n = int(input())
	ls = list(map(lambda x: int(x) - 1, list(input())))
	prevSum = defaultdict(int)
	res = 0
	currsum = 0
	for i in range(n):
		currsum += ls[i]
		if currsum == 0:
			res += 1
		if currsum in prevSum:
			res += prevSum[currsum]
		prevSum[currsum] += 1
	return res
t = int(input())
for _ in range(t):
	print(solve())


q = int(input())
for _ in range(q):
	n = int(input())
	A = list((int(x) for x in input()))
	d = {0: 1}
	s = 0
	ans = 0
	for (i, a) in enumerate(A):
		s += a
		x = s - i - 1
		if x in d:
			d[x] += 1
		else:
			d[x] = 1
	for (k, v) in d.items():
		ans += v * (v - 1) // 2
	print(ans)


import os
import sys
from io import BytesIO, IOBase
import math
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from collections import defaultdict
t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	l = list(s)
	for i in range(n):
		l[i] = int(l[i])
	d = [0] * n
	lt = defaultdict(int)
	lt1 = {}
	if l[0] == 1:
		d[0] = 1
	tt = 1 - l[0]
	lt[tt] = 1
	for i in range(1, n):
		cnt = 1 - l[i]
		tt += 1 - l[i]
		if lt[tt] != 0:
			d[i] += d[lt[tt] - 1] + 1
		if cnt == 0 and lt[tt] != i:
			d[i] += 1
		if tt == 0 and lt[tt] == 0:
			d[i] += 1
		lt[tt] = i + 1
	ans = 0
	for i in d:
		ans += i
	print(ans)


def function(tests):
	for _ in range(tests):
		n = int(input())
		a = input()
		d = {0: 1}
		(res, s) = (0, 0)
		for i in range(n):
			s += int(a[i])
			x = s - i - 1
			if x not in d:
				d[x] = 0
			d[x] += 1
			res += d[x] - 1
		print(res)
t = int(input())
function(t)


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from itertools import *
import bisect
from heapq import *
from math import *
from copy import *
from collections import deque, defaultdict
from collections import Counter as counter
from itertools import combinations as comb
from itertools import permutations as permutate
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
from types import GeneratorType

def iterative(f, stack=[]):

	def wrapped_func(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		to = f(*args, **kwargs)
		while True:
			if type(to) is GeneratorType:
				stack.append(to)
				to = next(to)
				continue
			stack.pop()
			if not stack:
				break
			to = stack[-1].send(to)
		return to
	return wrapped_func
mod = 1000000007

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def zerolist(n):
	return [0] * n

def nextline():
	out('\n')

def testcase(t):
	for p in range(t):
		solve()

def printlist(a):
	for p in range(0, len(a)):
		out(str(a[p]) + ' ')

def lcm(a, b):
	return a * b // gcd(a, b)

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def ncr(n, r):
	return factorial(n) // (factorial(r) * factorial(max(n - r, 1)))

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def findSubarraySum(arr, n, Sum=0):
	prevSum = defaultdict(lambda : 0)
	res = 0
	currsum = 0
	for i in range(0, n):
		currsum += arr[i]
		if currsum == Sum:
			res += 1
		if currsum - Sum in prevSum:
			res += prevSum[currsum - Sum]
		prevSum[currsum] += 1
	return res

def solve():
	n = int(inp())
	a = list(inp())
	for i in range(n):
		a[i] = int(a[i]) - 1
	print(findSubarraySum(a, n))
testcase(int(inp()))


import sys
import math
import heapq
import collections
fast_reader = sys.stdin.readline
fast_writer = sys.stdout.write

def input():
	return fast_reader().strip()

def print(*argv):
	fast_writer(' '.join((str(i) for i in argv)))
	fast_writer('\n')

def printspace(*argv):
	fast_writer(' '.join((str(i) for i in argv)))
	fast_writer(' ')

def inputnum():
	return int(input())

def inputnums():
	return map(int, input().split())

def inputlist():
	return list(map(int, input().split()))

def inputstring():
	return [x for x in input()]

def inputmatrixchar(rows):
	arr2d = [[j for j in input().strip()] for i in range(rows)]
	return arr2d

def inputmatrixint(rows):
	arr2d = []
	for _ in range(rows):
		arr2d.append([int(i) for i in input().split()])
	return arr2d
t = int(input())
for q in range(t):
	n = inputnum()
	s = inputstring()
	a = [0]
	for i in range(n):
		a.append(a[i] + int(s[i]))
	d = {}
	ans = 0
	for i in range(n + 1):
		if a[i] - i in d:
			d[a[i] - i] += 1
		else:
			d[a[i] - i] = 1
		ans += d[a[i] - i] - 1
	print(ans)


from collections import defaultdict
t = int(input())
for i in range(t):
	n = int(input())
	a = [int(i) - 1 for i in input() if i != '\n']
	cursum = 0
	ans = 0
	dicta = defaultdict(lambda : 0)
	for i in range(n):
		cursum += a[i]
		if cursum == 0:
			ans += 1
		if cursum in dicta:
			ans += dicta[cursum]
		dicta[cursum] += 1
	print(ans)


t = int(input())
C = [0 for i in range(900001)]
C[0] = 1
for _ in range(t):
	n = int(input())
	A = [int(s) for s in input()]
	(ans, cur) = (0, 0)
	for i in range(n):
		cur += A[i] - 1
		ans += C[cur]
		C[cur] += 1
	cur = 0
	for i in range(n):
		cur += A[i] - 1
		C[cur] -= 1
	print(ans)


import sys, math
import io, os
from bisect import bisect_left as bl, bisect_right as br, insort
from heapq import heapify, heappush, heappop
from collections import defaultdict as dd, deque, Counter

def data():
	return sys.stdin.readline().strip()

def mdata():
	return list(map(int, data().split()))

def outl(var):
	sys.stdout.write('\n'.join(map(str, var)) + '\n')

def out(var):
	sys.stdout.write(str(var) + '\n')
from decimal import Decimal
from fractions import Fraction
INF = float('inf')
mod = int(1000000000.0) + 7
for t in range(int(data())):
	n = int(data())
	a = list(map(int, list(data())))
	d = dd(int)
	s = 0
	ans = 0
	for i in range(n):
		s += a[i]
		d[s - i - 1] += 1
	ans = d[0]
	s = 0
	for i in range(n):
		s += a[i]
		d[s - i - 1] -= 1
		ans += d[s - i - 1]
	out(ans)


for t in range(int(input())):
	n = int(input())
	a = list(input())
	a = list(map(int, a))
	for i in range(1, n):
		a[i] = a[i] + a[i - 1]
	a.insert(0, 0)
	for i in range(n + 1):
		a[i] = a[i] - i
	d = {}
	for el in a:
		d[el] = d.get(el, 0) + 1
	ans = 0
	for el in d:
		if d[el] > 1:
			e = d[el]
			ans = ans + e * (e - 1) // 2
	print(ans)


def prefix(arr):
	lst = [0] * len(arr)
	lst[0] = arr[0]
	for i in range(1, len(arr)):
		lst[i] = lst[i - 1] + arr[i]
	return lst

def good(arr):
	pre = prefix(arr)
	dic = {}
	ans = 0
	for i in range(len(pre)):
		if pre[i] == i + 1:
			ans += 1
		if pre[i] - i not in dic:
			dic[pre[i] - i] = 1
		else:
			dic[pre[i] - i] += 1
	for i in dic.values():
		ans += i * (i - 1) // 2
	return ans
t = int(input())
for i in range(t):
	n = int(input())
	st = input()
	arr = list(st)
	for i in range(len(arr)):
		arr[i] = int(arr[i])
	print(good(arr))


t = int(input())
for _ in range(t):
	n = int(input())
	a = input()
	l = []
	for i in a:
		l.append(int(i))
	lsum = [l[0]]
	for i in range(1, n):
		lsum.append(lsum[i - 1] + l[i])
	dic = dict()
	dic[0] = 0
	for i in range(len(lsum)):
		lsum[i] = lsum[i] - i - 1
	count = 0
	lsum.insert(0, 0)
	for i in range(len(lsum)):
		if lsum[i] not in dic:
			dic[lsum[i]] = 0
		count += dic[lsum[i]]
		dic[lsum[i]] += 1
	print(count)


t = int(input())
for _ in range(t):
	n = int(input())
	A = list(str(input()))
	A = [int(a) for a in A]
	from itertools import accumulate
	C = [0] + A
	C = list(accumulate(C))
	d = {}
	ans = 0
	for i in range(n + 1):
		if C[i] - i in d:
			ans += d[C[i] - i]
			d[C[i] - i] += 1
		else:
			d[C[i] - i] = 1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	ar = list(map(int, list(input())))
	li = []
	su = 0
	for i in range(n):
		su = su - (ar[i] - 1)
		li.append(su)
	dic = {0: 1}
	for i in li:
		if i in dic:
			dic[i] += 1
		else:
			dic[i] = 1
	ans = 0
	for i in dic:
		ans += (dic[i] - 1) * dic[i] // 2
	print(ans)


for _ in range(int(input())):
	n = int(input())
	temp = input()
	lst = []
	for i in temp:
		lst.append(int(i))
	main = [lst[0]]
	abtk = lst[0]
	for i in range(1, n):
		abtk += lst[i]
		main.append(abtk)
	d = {0: 1}
	for i in range(n):
		d[main[i] - i - 1] = d.get(main[i] - i - 1, 0) + 1
	ans = 0
	for key in d.keys():
		x = d[key]
		ans += x * (x - 1) // 2
	print(ans)


for _ in range(int(input())):
	n = int(input())
	s = input()
	d = {0: 1}
	sumc = 0
	out = 0
	for i in range(n):
		sumc += int(s[i])
		x = sumc - i - 1
		if x not in d:
			d[x] = 0
		d[x] += 1
		out += d[x] - 1
	print(out)


from sys import stdin
nii = lambda : map(int, stdin.readline().split())
lnii = lambda : list(map(int, stdin.readline().split()))
from collections import Counter
from itertools import accumulate
t = int(input())
for tt in range(t):
	n = int(input())
	a = list(input())
	a = [int(i) for i in a]
	b = [0] + list(accumulate(a))
	c = [b[i] - i for i in range(n + 1)]
	c = Counter(c)
	ans = 0
	for v in c.values():
		ans += v * (v - 1) // 2
	print(ans)


testcases = int(input())
for testcase in range(testcases):
	n = int(input())
	arr = []
	strs = input()
	for i in strs:
		arr.append(int(i) - 1)
	dicts = {}
	prefix = 0
	ans = 0
	dicts[0] = 1
	for i in arr:
		prefix += i
		if prefix in dicts:
			ans += dicts[prefix]
		if prefix not in dicts:
			dicts[prefix] = 1
		else:
			dicts[prefix] += 1
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = input()
	memo = {0: 1}
	res = 0
	prefix_sum = 0
	for i in range(len(a)):
		prefix_sum += int(a[i])
		res += memo.get(prefix_sum - i - 1, 0)
		memo[prefix_sum - i - 1] = memo.get(prefix_sum - i - 1, 0) + 1
	print(res)


t = int(input())
myList = []
for _ in range(t):
	p = []
	sum = 0
	res = 0
	d = {0: 1}
	n = int(input())
	a = list(input())
	a = list(map(int, a))
	k = 0
	for i in range(n):
		sum += a[i]
		x = sum - i - 1
		if x not in d:
			d[x] = 0
		d[x] += 1
	for s in d:
		res += d[s] * (d[s] - 1) // 2
	myList.append(res)
for elem in myList:
	print(elem)


from collections import defaultdict
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, list(input())))
	l = [(0, 0)]
	diff = [0]
	for ind in range(1, n + 1):
		l.append((l[ind - 1][0] + a[ind - 1], ind))
		diff.append(l[ind - 1][0] + a[ind - 1] - ind)
	result = 0
	tmp = defaultdict(int)
	for el in diff:
		tmp[el] += 1
	tmp = list(filter(lambda x: x != 1, tmp.values()))
	print(sum(map(lambda x: x * (x - 1) // 2, tmp)))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, list(input())))
	r = {0: 1}
	p = 0
	ans = 0
	for i in range(n):
		p += a[i]
		r[p - i - 1] = r.get(p - i - 1, 0) + 1
		ans += r[p - i - 1] - 1
	print(ans)


t = int(input())

def Solve(presum):
	cnt = 0
	dp = {}
	for i in range(len(presum)):
		if presum[i] == 0:
			cnt = cnt + 1
		if presum[i] in dp:
			cnt = cnt + dp[presum[i]]
		if presum[i] not in dp:
			dp[presum[i]] = 1
		else:
			dp[presum[i]] += 1
	return cnt
for _ in range(t):
	n = int(input())
	inp = input()
	s = 0
	Presum = []
	for i in range(len(inp)):
		s = s + int(inp[i]) - 1
		Presum.append(s)
	print(Solve(Presum))


for _ in range(int(input())):
	n = int(input())
	s = input()
	d = {0: 1}
	(summa, cnt) = (0, 0)
	ans = 0
	for i in s:
		summa += int(i)
		cnt += 1
		k = cnt - summa
		if k not in d:
			d[k] = 0
		ans += d[k]
		d[k] += 1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input()))
	x_pos = [0] * n
	cum_pos = [0] * n
	cum = 0
	ans = 0
	for i in range(n):
		cum += a[i]
		x_pos[i] = a[i] - i
		cum_pos[i] = cum - i
	occurences = {1: 1}
	for i in range(n):
		if cum_pos[i] in occurences:
			occurences[cum_pos[i]] += 1
		else:
			occurences[cum_pos[i]] = 1
	for x in occurences.values():
		ans += x * (x - 1) // 2
	print(ans)


def conv(c):
	return ord(c) - ord('0')

def solve():
	n = int(input().strip())
	a = list(map(conv, input().strip('')))
	umap = {0: 1}
	s = 0
	ans = 0
	for i in range(n):
		index = i + 1
		s += a[i]
		diff = s - index
		try:
			ans += umap[diff]
			umap[diff] += 1
		except:
			umap[diff] = 1
	print(ans)
t = int(input().strip())
for _ in range(t):
	solve()


q = int(input())
for _ in range(q):
	n = int(input())
	s = input()
	a = [0] * (10 * n)
	bal = 0
	ans = 0
	a[bal] = 1
	for i in range(n):
		bal += int(s[i]) - 1
		ans += a[bal]
		a[bal] += 1
	print(ans)


res = ''
for _ in range(int(input())):
	input()
	a = [int(x) for x in input()]
	r = 0
	p = {0: 1}
	w = 0
	for i in a:
		w += i - 1
		if w in p:
			r += p[w]
		else:
			p[w] = 0
		p[w] += 1
	res += f'{r}\n'
print(res)


T = int(input())
for _ in range(0, T):
	n = int(input())
	arr = input()
	s = []
	for i in range(0, len(arr)):
		s.append(int(arr[i]))
	pre = [s[0]]
	for i in range(1, len(s)):
		pre.append(pre[-1] + s[i])
	kk = [0] * (n + 1)
	diff = dict()
	org = n
	exp = 1
	for i in range(len(s) - 1, -1, -1):
		if s[i] != 1:
			fnd = 0
			if i > 0:
				fnd = org - 1 - pre[i - 1]
			if diff.get(fnd) == None:
				kk[i] = n
			else:
				kk[i] = diff[fnd]
			diff[org - pre[i]] = i
		exp += 1
		org -= 1
	loss = [0] * (n + 2)
	exp = 1
	ans = 0
	for i in range(len(s) - 1, -1, -1):
		if s[i] == 1:
			loss[i] = loss[i + 1]
		else:
			loss[i] = kk[i] - i + loss[kk[i] + 1]
		ans += exp - loss[i]
		exp += 1
	print(ans)


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	s = input()
	dp = [int(s[0]) - 1]
	for i in range(1, n):
		dp.append(dp[-1] + int(s[i]) - 1)
	d = defaultdict(int)
	for i in dp:
		d[i] += 1
	count = 0
	for i in d:
		count += d[i] * (d[i] - 1) // 2
	print(count + d[0])


from collections import defaultdict
T = int(input())
for t in range(T):
	n = int(input())
	A = input()
	prefix = [0] * (n + 1)
	for i in range(1, n + 1):
		prefix[i] = prefix[i - 1] + int(A[i - 1])
	d = defaultdict(int)
	for i in range(n + 1):
		d[prefix[i] - i] += 1
	ans = 0
	for i in d:
		ans += d[i] * (d[i] - 1) // 2
	print(ans)


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	a = list(map(lambda x: int(x) - 1, list(input())))
	ans = 0
	curr = 0
	prev = defaultdict(lambda : 0)
	for i in range(0, n):
		curr += a[i]
		if curr == 0:
			ans += 1
		if curr - 0 in prev:
			ans += prev[curr - 0]
		prev[curr] += 1
	print(ans)


from collections import defaultdict as dft
for _ in range(int(input())):
	n = int(input())
	s = input()
	arr = [0] * n
	for i in range(n):
		arr[i] = int(s[i]) - 1
	dct = dft(int)
	dct[0] = 1
	sm = res = 0
	for i in range(n):
		sm += arr[i]
		res += dct[sm]
		dct[sm] += 1
	print(res)


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	y = int(input())
	x = input().rstrip()
	arr = [0]
	total = 0
	for char in x:
		total += int(char) - 1
		arr.append(total)
	seen = {}
	segm = []
	J = {}
	for i in range(len(arr)):
		if arr[i] not in seen:
			seen[arr[i]] = i
		else:
			x = seen[arr[i]]
			segm.append((x, i))
			J[x] = i
			seen[arr[i]] = i
	bonus = []
	V = set([])
	for seg in segm:
		c = 1
		x = seg[1]
		if x not in V:
			V.add(x)
			while x in J:
				c += 1
				x = J[x]
				V.add(x)
			bonus.append(c * (c + 1) // 2)
	print(sum(bonus))


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

class union_find:

	def __init__(self, n):
		self.n = n
		self.rank = [0] * n
		self.parent = [int(j) for j in range(n)]

	def union(self, i, j):
		i = self.find(i)
		j = self.find(j)
		if self.rank[i] == self.rank[j]:
			self.parent[i] = j
			self.rank[j] += 1
		elif self.rank[i] > self.rank[j]:
			self.parent[j] = i
		else:
			self.parent[i] = j

	def find(self, i):
		temp = i
		if self.parent[temp] != temp:
			self.parent[temp] = self.find(self.parent[temp])
		return self.parent[temp]
from math import log2, ceil
from collections import deque, Counter as CC

def main():
	for t in range(int(input())):
		n = int(input())
		l = [int(j) for j in list(input())]
		pre = [0]
		for i in range(n):
			pre.append(pre[i] + l[i])
		d = dict()
		ans = 0
		for i in range(n):
			if l[i] == 1:
				ans += 1
			tmp = pre[i + 1] - i
			tmp2 = pre[i] - i + 1
			if tmp in d:
				ans += d[tmp]
			if tmp2 in d:
				d[tmp2] += 1
			else:
				d[tmp2] = 1
		print(ans)
main()


def find(arr, n):
	d = {}
	sm = 0
	cnt = 0
	for i in range(n):
		sm += arr[i]
		if sm == 0:
			cnt += 1
		if d.get(sm):
			cnt += d[sm]
		d[sm] = d.get(sm, 0) + 1
	return cnt
t = int(input())
for _ in range(t):
	n = int(input())
	s = list(input())
	ans = []
	for i in range(n):
		if s[i] == '1':
			ans.append(0)
		elif s[i] == '0':
			ans.append(-1)
		else:
			ans.append(int(s[i]) - 1)
	print(find(ans, n))


t = int(input())
from collections import defaultdict as dd
for i in range(t):
	n = int(input())
	l = list(map(int, input()))
	l1 = [0]
	c = 0
	d = dd(int)
	for j in range(n):
		c += l[j]
		l1.append(c)
		d[l1[j] - j] += 1
	d[c - n] += 1
	ans = 0
	for j in d:
		ans += d[j] * (d[j] - 1) // 2
	print(ans)


from collections import defaultdict
t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	d = {}
	d = defaultdict(lambda : 0, d)
	d1 = {}
	d1 = defaultdict(lambda : 0, d1)
	ans = 0
	cnt = 0
	for i in range(n):
		l = i + 1
		w = int(s[i]) - 0
		cnt += w
		if cnt == l:
			ans += 1
		if d1[cnt - l]:
			ans += d[cnt - l]
		d[cnt - l] += 1
		d1[cnt - l] += 1
	print(ans)


from itertools import accumulate
t = int(input())
for _ in range(t):
	n = int(input())
	sumi = 0
	a = list(map(int, input()))
	arr = list(accumulate(a))
	c = dict()
	for i in range(n):
		c[arr[i] - i] = c.get(arr[i] - i, 0) + 1
	res = 0
	for x in c:
		cnt = c[x]
		res += cnt * (cnt - 1) // 2
	for i in range(n):
		if arr[i] == i + 1:
			res += 1
	print(res)


for _ in range(int(input())):
	n = int(input())
	a = input()
	d = {0: 1}
	(res, s) = (0, 0)
	for i in range(n):
		s += int(a[i])
		x = s - i - 1
		if x not in d:
			d[x] = 0
		d[x] += 1
	for (k, v) in d.items():
		res += (v - 1) * v // 2
	print(res)


for _ in range(int(input())):
	a = int(input())
	n = input()
	arr = []
	for i in n:
		arr.append(int(i) - 1)
	ans = 0
	dic = {}
	sum1 = 0
	for i in range(a):
		sum1 += arr[i]
		if dic.get(sum1, -1) == -1:
			dic[sum1] = 1
		else:
			ans += dic[sum1]
			dic[sum1] += 1
	if dic.get(0, -1) != -1:
		ans += dic[0]
	print(ans)


import sys
from math import sqrt, gcd, ceil, log, floor
from bisect import bisect, bisect_left
from collections import defaultdict, Counter, deque
from heapq import heapify, heappush, heappop
input = sys.stdin.readline
read = lambda : list(map(int, input().strip().split()))

def main():
	for _ in range(int(input())):
		n = int(input())
		s = [0] + list(map(int, list(input().strip()))) + [0]
		dd = defaultdict(int)
		dd[0] = 1
		ans = 0
		for i in range(1, n + 1):
			ans += dd[s[i] - i]
			dd[s[i] - i] += 1
			s[i + 1] += s[i]
		print(ans)
main()


for _ in range(int(input())):
	n = int(input())
	digits = list(input())
	for i in range(n):
		digits[i] = int(digits[i]) - 1
	prefix = {}
	prefix[0] = 1
	so_far = 0
	answer = 0
	for x in digits:
		so_far += x
		if so_far in prefix:
			answer += prefix[so_far]
			prefix[so_far] += 1
		else:
			prefix[so_far] = 1
	print(answer)


import sys
input = sys.stdin.readline

def inInt():
	return int(input())

def inStr():
	return input().strip('\n')

def inIList():
	return list(map(int, input().split()))

def inSList():
	return input().split()

def solve(n, case):
	freq = {}
	freq[1] = 1
	s = 0
	res = 0
	for i in range(n):
		s += int(case[i])
		if s - i not in freq:
			freq[s - i] = 0
		res += freq[s - i]
		freq[s - i] += 1
	print(res)
t = inInt()
for case in range(t):
	n = inInt()
	case = inStr()
	solve(n, case)


def solve():
	pref = [0]
	count = 0
	m = {}
	for i in range(1, n + 1):
		pref.append(pref[-1] + nums[i - 1])
		temp = pref[-1] - i
		if temp == 0:
			count += 1
		if temp in m:
			count += m[temp]
			m[temp] += 1
		else:
			m[temp] = 1
	return count
t = int(input())
for i in range(t):
	n = int(input())
	nums = list(map(int, list(str(input()))))
	print(solve())


import os
import sys
from io import BytesIO, IOBase
from itertools import accumulate

def main():
	for _ in range(int(input())):
		n = int(input())
		a = list((int(x) for x in input()))
		d = {}
		pre = 0
		ans = 0
		d[0] = 1
		for i in range(n):
			pre += a[i] - 1
			ans += d.get(pre, 0)
			d[pre] = d.get(pre, 0) + 1
		print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import sys

def minp():
	return sys.stdin.readline().strip()

def mint():
	return int(minp())

def mints():
	return map(int, minp().split())

def solve():
	n = mint()
	s = minp()
	d = dict()
	a = [0] * (n + 1)
	p = 0
	d[0] = 1
	for i in range(n):
		p += int(s[i]) - 1
		a[i + 1] = p
		d[a[i + 1]] = d.get(a[i + 1], 0) + 1
	r = 0
	for v in a:
		d[v] = d.get(v, 0) - 1
		r += d.get(v, 0)
	print(r)
for i in range(mint()):
	solve()


import sys
I = lambda : int(input())
RL = readline = lambda : sys.stdin.readline().strip('\n')
RM = readmap = lambda x=int: map(x, readline().split(' '))
for _ in range(I()):
	(n, s) = (I(), list(map(int, readline())))
	(l, d, count) = ([0] * n, {1: 1}, 0)
	for i in range(n):
		l[i] = l[i - 1] + s[i]
		hash0 = l[i] - i
		d[hash0] = d.get(hash0, 0) + 1
	for (k, v) in d.items():
		count += v * (v - 1) >> 1
	print(count)
quit()
for _ in range(I()):
	s = RL()
	l = [0]
	for i in s + '0':
		if i == '1':
			l[-1] += 1
		else:
			l.append(0)
	l.sort(reverse=True)
	print(sum(l[::2]))
quit()
for _ in range(I()):
	(n, l) = (I(), [*RM()])
	if l[0] + l[1] <= l[-1]:
		print(1, 2, n)
	else:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	a = input()
	prefix_sum = [0]
	for i in a:
		prefix_sum.append(prefix_sum[-1] + int(i))
	for i in range(n + 1):
		prefix_sum[i] = prefix_sum[i] - i
	count = {}
	for i in prefix_sum:
		count[i] = count.get(i, 0) + 1
	sum = 0
	for i in count:
		sum += count[i] * (count[i] - 1) // 2
	print(sum)


import sys
import math
import itertools
import functools
import collections
import operator
import fileinput
import copy
ORDA = 97

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return [int(i) for i in input().split()]

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def revn(n):
	return str(n)[::-1]

def dd():
	return collections.defaultdict(int)

def ddl():
	return collections.defaultdict(list)

def sieve(n):
	if n < 2:
		return list()
	prime = [True for _ in range(n + 1)]
	p = 3
	while p * p <= n:
		if prime[p]:
			for i in range(p * 2, n + 1, p):
				prime[i] = False
		p += 2
	r = [2]
	for p in range(3, n + 1, 2):
		if prime[p]:
			r.append(p)
	return r

def divs(n, start=2):
	r = []
	for i in range(start, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n / i == i:
				r.append(i)
			else:
				r.extend([i, n // i])
	return r

def divn(n, primes):
	divs_number = 1
	for i in primes:
		if n == 1:
			return divs_number
		t = 1
		while n % i == 0:
			t += 1
			n //= i
		divs_number *= t

def prime(n):
	if n == 2:
		return True
	if n % 2 == 0 or n <= 1:
		return False
	sqr = int(math.sqrt(n)) + 1
	for d in range(3, sqr, 2):
		if n % d == 0:
			return False
	return True

def convn(number, base):
	new_number = 0
	while number > 0:
		new_number += number % base
		number //= base
	return new_number

def cdiv(n, k):
	return n // k + (n % k != 0)

def ispal(s):
	for i in range(len(s) // 2 + 1):
		if s[i] != s[-i - 1]:
			return False
	return True
for _ in range(ii()):
	n = ii()
	s = input()
	p = [0] * (n + 1)
	for i in range(n):
		p[i + 1] += int(s[i]) + p[i]
	for i in range(n + 1):
		p[i] -= i
	d = collections.Counter(p)
	ans = 0
	for i in d:
		ans += d[i] * (d[i] - 1) // 2
	print(ans)


from collections import defaultdict as dd
for _ in range(int(input())):
	n = int(input())
	ar = list(map(int, list(input())))
	ar.insert(0, 0)
	pre = [0 for i in range(n + 1)]
	for i in range(1, n + 1):
		pre[i] = pre[i - 1] + ar[i]
	d = dd(int)
	for i in range(1, n + 1):
		d[pre[i] - i] += 1
	ans = 0
	for i in d:
		ans += d[i] * (d[i] + 1) // 2
		if i != 0:
			ans -= d[i]
	print(ans)


from math import *
from collections import *
from random import *
from decimal import Decimal
from heapq import *
from bisect import *
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 5)

def lis():
	return list(map(int, input().split()))

def ma():
	return map(int, input().split())

def inp():
	return int(input())

def st1():
	return input().rstrip('\n')
t = inp()
while t:
	t -= 1
	n = inp()
	s = st1()
	a = [int(s[0])]
	for i in range(1, n):
		a.append(a[-1] + int(s[i]))
	d = dict()
	for i in range(n):
		d[int(a[i]) - i] = d.get(int(a[i]) - i, 0) + 1
	re = 0
	for i in d.keys():
		x = d[i]
		re += x * (x - 1) // 2
	print(re + d.get(1, 0))


import os
import heapq
import sys
import math
import operator
from collections import defaultdict
from io import BytesIO, IOBase

def inpt():
	return [int(k) for k in input().split()]

def main():
	for _ in range(int(input())):
		n = int(input())
		s = input()
		ar = list(s)
		for i in range(n):
			ar[i] = ord(ar[i]) - ord('1')
		ans = 0
		d = defaultdict(int)
		d[0] = 1
		sm = 0
		for i in ar:
			sm += i
			if sm in d:
				ans += d[sm]
			d[sm] += 1
		print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import math
T = int(input())

def c2(g):
	if g >= 2:
		return math.factorial(g) // (math.factorial(g - 2) * 2)
	return 0
for i in range(T):
	n = int(input())
	a = list(map(int, list(input())))
	if n == 1 and a[0] == 1:
		print(1)
		continue
	elif n == 1:
		print(0)
		continue
	os = []
	m = max(sum(a), n * 2)
	v = 0
	B = [0] * (2 * m)
	B[m + 1] = 1
	for i in range(n):
		v += a[i]
		B[v - i + m] += 1
	ans = 0
	for i in range(2 * m):
		ans += c2(B[i])
	print(ans)


import time, math as mt, bisect as bs, sys
from sys import stdin, stdout
from collections import deque
from fractions import Fraction
from collections import Counter
from collections import OrderedDict
pi = 3.141592653589793

def II():
	return int(stdin.readline())

def IO():
	return stdin.readline()

def IP():
	return map(int, stdin.readline().split())

def L():
	return list(map(int, stdin.readline().split()))

def P(x):
	return stdout.write(str(x) + '\n')

def PI(x, y):
	return stdout.write(str(x) + ' ' + str(y) + '\n')

def lcm(a, b):
	return a * b // gcd(a, b)

def gcd(a, b):
	if a == 0:
		return b
	elif b == 0:
		return a
	if a > b:
		return gcd(a % b, b)
	else:
		return gcd(a, b % a)

def readTree():
	v = int(input())
	adj = [set() for i in range(v + 1)]
	for i in range(v - 1):
		(u1, u2) = In()
		adj[u1].add(u2)
		adj[u2].add(u1)
	return (adj, v)

def bfs(adj, v):
	visited = [False] * (v + 1)
	q = deque()
	while q:
		pass

def sieve():
	li = [True] * 1000001
	(li[0], li[1]) = (False, False)
	for i in range(2, len(li), 1):
		if li[i] == True:
			for j in range(i * i, len(li), i):
				li[j] = False
	prime = []
	for i in range(1000001):
		if li[i] == True:
			prime.append(i)
	return prime

def setBit(n):
	count = 0
	while n != 0:
		n = n & n - 1
		count += 1
	return count
mx = 10 ** 7
spf = [mx] * (mx + 1)

def SPF():
	spf[1] = 1
	for i in range(2, mx + 1):
		if spf[i] == mx:
			spf[i] = i
			for j in range(i * i, mx + 1, i):
				if i < spf[j]:
					spf[j] = i
	return
mod = 10 ** 9 + 7

def solve():
	n = II()
	a = input()
	li = []
	for ele in a:
		li.append(int(ele) - 1)
	ans = 0
	d = {}
	d[0] = 1
	s = 0
	for i in range(n):
		s += li[i]
		ans += d.get(s, 0)
		d[s] = d.get(s, 0) + 1
	P(ans)
	return
t = II()
for i in range(t):
	solve()


from sys import stdin, stdout
from collections import defaultdict

def calc_num(arr, n):
	prev_sum = defaultdict(lambda : 0)
	res = 0
	curr_sum = 0
	for i in range(len(arr)):
		curr_sum += arr[i]
		if curr_sum == n:
			res += 1
		if curr_sum - n in prev_sum:
			res += prev_sum[curr_sum - n]
		prev_sum[curr_sum] += 1
	return res

def main():
	t = int(input())
	for _ in range(t):
		input()
		arr = list(map(int, list(stdin.readline().replace('\n', ''))))
		arr = [x - 1 for x in arr]
		ans = calc_num(arr, 0)
		print(ans)
main()


from sys import stdin, stdout
input = stdin.readline
from collections import defaultdict as dd, deque as dq, Counter as C
n = int(input())
for test in range(n):
	siz = int(input())
	li = list(map(lambda i: int(i), list(input().strip())))
	d = dd(lambda : 0)
	sm = 0
	ans = 0
	for i in range(siz):
		sm += li[i]
		k = sm - i
		ans += d[k]
		if k == 1:
			ans += 1
		d[k] += 1
	print(ans)


from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	n = int(input())
	a = '0' + input().rstrip()
	d = dict()
	b = [0]
	cnt = 0
	for x in range(n):
		b.append(b[-1] + int(a[x + 1]))
	i = 0
	for x in b:
		if x - i in d.keys():
			d[x - i] += 1
		else:
			d[x - i] = 1
		i += 1
	for x in d.values():
		cnt += x * (x - 1) // 2
	print(cnt)


t = int(input())
for _ in range(t):
	maps = {}
	l = int(input())
	s = input()
	p = [0]
	for (i, v) in enumerate(s):
		p.append(p[i] + int(v))
	for i in range(l + 1):
		maps[p[i] - i] = maps.get(p[i] - i, 0) + 1
	cnt = 0
	for v in maps.values():
		cnt += v * (v - 1) // 2
	print(cnt)


t = int(input())
for test_case in range(t):
	n = int(input())
	a = input()
	b = 0
	d = {0: 1}
	for e in a:
		b += int(e) - 1
		if d.get(b):
			d[b] += 1
		else:
			d[b] = 1
	v = list(d.values())
	ans = 0
	for f in v:
		ans += f * (f - 1) // 2
	print(ans)


from functools import lru_cache
from sys import stdin, stdout
import sys
from math import *
for __ in range(int(input())):
	n = int(input())
	ar = list(input())
	ar = list(map(int, ar))
	p = [0]
	for i in range(n):
		p.append(p[-1] + ar[i])
	d = {0: 1}
	ans = 0
	for i in range(1, n + 1):
		if p[i] - i == 0:
			ans += d[0]
			d[0] += 1
		elif d.get(p[i] - i) != None:
			ans += d[p[i] - i]
			d[p[i] - i] += 1
		else:
			d[p[i] - i] = 1
	print(ans)


import sys

def inp():
	inp = input().split()
	if len(inp) == 1:
		return int(inp[0])
	else:
		return (int(i) for i in inp)

def inpl():
	inp = input().split()
	for i in range(len(inp)):
		inp[i] = int(inp[i])
	return inp

def findSubArrays(arr, n):
	hashMap = {}
	out = []
	sum1 = 0
	for i in range(n):
		sum1 += arr[i]
		if sum1 == 0:
			out.append((0, i))
		al = []
		if sum1 in hashMap:
			al = hashMap.get(sum1)
			for it in range(len(al)):
				out.append((al[it] + 1, i))
		al.append(i)
		hashMap[sum1] = al
	return out

def printOutput(output):
	for i in output:
		print('Subarray found from Index ' + str(i[0]) + ' to ' + str(i[1]))
from collections import defaultdict

def findSubarraySum(arr, n, Sum):
	prevSum = defaultdict(lambda : 0)
	res = 0
	currsum = 0
	for i in range(0, n):
		currsum += arr[i]
		if currsum == Sum:
			res += 1
		if currsum - Sum in prevSum:
			res += prevSum[currsum - Sum]
		prevSum[currsum] += 1
	return res
testcases = inp()
for testcase in range(testcases):
	n = inp()
	a = input()
	s = []
	for i in a:
		s.append(int(i) - 1)
	print(findSubarraySum(s, n, 0))


import collections
test_case_cnt = int(input())

def get_good_count(nums: list):
	p_sum = [0]
	for num in nums:
		p_sum.append(p_sum[-1] + num)
	counter = collections.Counter()
	for i in range(len(p_sum)):
		counter[p_sum[i] - i] += 1
	res = 0
	for value in counter.values():
		res += value * (value - 1) // 2
	return res
	res = 0
	for left in range(len(p_sum)):
		for right in range(left + 1, len(p_sum)):
			res += p_sum[right] - p_sum[left] == right - left
	return res
for _ in range(test_case_cnt):
	N = int(input())
	_nums = map(int, input())
	print(get_good_count(list(_nums)))


from collections import defaultdict

def FindSubArraySum(l, n, sum1):
	res = 0
	curr = 0
	m = defaultdict(lambda : 0)
	for i in range(0, n):
		curr += l[i]
		if curr == sum1:
			res += 1
		if curr - sum1 in m:
			res += m[curr - sum1]
		m[curr] += 1
	return res
for cases in range(int(input())):
	l = []
	n = int(input())
	l1 = list(input())
	for i in l1:
		l.append(int(i) - 1)
	print(FindSubArraySum(l, n, 0))


import io
import os
import sys
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def prdbg(*args, **kwargs):
	print(*args, **kwargs)
	pass

def get_str():
	return input().decode().strip()

def rint():
	return map(int, input().split())

def oint():
	return int(input())
t = oint()
for _ in range(t):
	n = oint()
	aa = get_str()
	a = []
	for i in range(n):
		a.append(int(aa[i]))
	a = [0] + a
	ps = [a[0]]
	for i in range(1, len(a)):
		ps.append(ps[-1] + a[i])
	diff = dict()
	for i in range(-(n + 1), n + 1):
		diff[i] = 0
	ans = 0
	for i in range(1, n + 1):
		ss = i - ps[i]
		if ss == 0:
			ans += 1
		if ss in diff:
			ans += diff[ss]
			diff[ss] += 1
		else:
			diff[ss] = 1
	print(ans)


import math
import heapq, bisect
import sys
from collections import deque, defaultdict
from fractions import Fraction
import sys
import threading
from collections import defaultdict
threading.stack_size(10 ** 8)
mod = 10 ** 9 + 7
mod1 = 998244353
import os
import sys
from io import BytesIO, IOBase
sys.setrecursionlimit(300000)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

class TreeNode:

	def __init__(self, k, v):
		self.key = k
		self.value = v
		self.left = None
		self.right = None
		self.parent = None
		self.height = 1
		self.num_left = 1
		self.num_total = 1

class AvlTree:

	def __init__(self):
		self._tree = None

	def add(self, k, v):
		if not self._tree:
			self._tree = TreeNode(k, v)
			return
		node = self._add(k, v)
		if node:
			self._rebalance(node)

	def _add(self, k, v):
		node = self._tree
		while node:
			if k < node.key:
				if node.left:
					node = node.left
				else:
					node.left = TreeNode(k, v)
					node.left.parent = node
					return node.left
			elif node.key < k:
				if node.right:
					node = node.right
				else:
					node.right = TreeNode(k, v)
					node.right.parent = node
					return node.right
			else:
				node.value = v
				return

	@staticmethod
	def get_height(x):
		return x.height if x else 0

	@staticmethod
	def get_num_total(x):
		return x.num_total if x else 0

	def _rebalance(self, node):
		n = node
		while n:
			lh = self.get_height(n.left)
			rh = self.get_height(n.right)
			n.height = max(lh, rh) + 1
			balance_factor = lh - rh
			n.num_total = 1 + self.get_num_total(n.left) + self.get_num_total(n.right)
			n.num_left = 1 + self.get_num_total(n.left)
			if balance_factor > 1:
				if self.get_height(n.left.left) < self.get_height(n.left.right):
					self._rotate_left(n.left)
				self._rotate_right(n)
			elif balance_factor < -1:
				if self.get_height(n.right.right) < self.get_height(n.right.left):
					self._rotate_right(n.right)
				self._rotate_left(n)
			else:
				n = n.parent

	def _remove_one(self, node):
		replacement = node.left or node.right
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = replacement
			else:
				node.parent.right = replacement
			replacement.parent = node.parent
			node.parent = None
		else:
			self._tree = replacement
			replacement.parent = None
		node.left = None
		node.right = None
		node.parent = None
		self._rebalance(replacement)

	def _remove_leaf(self, node):
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = None
			else:
				node.parent.right = None
			self._rebalance(node.parent)
		else:
			self._tree = None
		node.parent = None
		node.left = None
		node.right = None

	def remove(self, k):
		node = self._get_node(k)
		if not node:
			return
		if AvlTree._is_leaf(node):
			self._remove_leaf(node)
			return
		if node.left and node.right:
			nxt = AvlTree._get_next(node)
			node.key = nxt.key
			node.value = nxt.value
			if self._is_leaf(nxt):
				self._remove_leaf(nxt)
			else:
				self._remove_one(nxt)
			self._rebalance(node)
		else:
			self._remove_one(node)

	def get(self, k):
		node = self._get_node(k)
		return node.value if node else -1

	def _get_node(self, k):
		if not self._tree:
			return None
		node = self._tree
		while node:
			if k < node.key:
				node = node.left
			elif node.key < k:
				node = node.right
			else:
				return node
		return None

	def get_at(self, pos):
		x = pos + 1
		node = self._tree
		while node:
			if x < node.num_left:
				node = node.left
			elif node.num_left < x:
				x -= node.num_left
				node = node.right
			else:
				return (node.key, node.value)
		raise IndexError('Out of ranges')

	@staticmethod
	def _is_left(node):
		return node.parent.left and node.parent.left == node

	@staticmethod
	def _is_leaf(node):
		return node.left is None and node.right is None

	def _rotate_right(self, node):
		if not node.parent:
			self._tree = node.left
			node.left.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.left
			node.left.parent = node.parent
		else:
			node.parent.right = node.left
			node.left.parent = node.parent
		bk = node.left.right
		node.left.right = node
		node.parent = node.left
		node.left = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	def _rotate_left(self, node):
		if not node.parent:
			self._tree = node.right
			node.right.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.right
			node.right.parent = node.parent
		else:
			node.parent.right = node.right
			node.right.parent = node.parent
		bk = node.right.left
		node.right.left = node
		node.parent = node.right
		node.right = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	@staticmethod
	def _get_next(node):
		if not node.right:
			return node.parent
		n = node.right
		while n.left:
			n = n.left
		return n

class SegmentTree1:

	def __init__(self, data, default='z', func=lambda a, b: min(a, b)):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class SegmentTree:

	def __init__(self, data, default=0, func=lambda a, b: a + b):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class Factorial:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorials = [1, 1]
		self.invModulos = [0, 1]
		self.invFactorial_ = [1, 1]

	def calc(self, n):
		if n <= -1:
			print('Invalid argument to calculate n!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.factorials):
			return self.factorials[n]
		nextArr = [0] * (n + 1 - len(self.factorials))
		initialI = len(self.factorials)
		prev = self.factorials[-1]
		m = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * i % m
		self.factorials += nextArr
		return self.factorials[n]

	def inv(self, n):
		if n <= -1:
			print('Invalid argument to calculate n^(-1)')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		p = self.MOD
		pi = n % p
		if pi < len(self.invModulos):
			return self.invModulos[pi]
		nextArr = [0] * (n + 1 - len(self.invModulos))
		initialI = len(self.invModulos)
		for i in range(initialI, min(p, n + 1)):
			next = -self.invModulos[p % i] * (p // i) % p
			self.invModulos.append(next)
		return self.invModulos[pi]

	def invFactorial(self, n):
		if n <= -1:
			print('Invalid argument to calculate (n^(-1))!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.invFactorial_):
			return self.invFactorial_[n]
		self.inv(n)
		nextArr = [0] * (n + 1 - len(self.invFactorial_))
		initialI = len(self.invFactorial_)
		prev = self.invFactorial_[-1]
		p = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * self.invModulos[i % p] % p
		self.invFactorial_ += nextArr
		return self.invFactorial_[n]

class Combination:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorial = Factorial(MOD)

	def ncr(self, n, k):
		if k < 0 or n < k:
			return 0
		k = min(k, n - k)
		f = self.factorial
		return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD

def powm(a, n, m):
	if a == 1 or n == 0:
		return 1
	if n % 2 == 0:
		s = powm(a, n // 2, m)
		return s * s % m
	else:
		return a * powm(a, n - 1, m) % m

def sort_list(list1, list2):
	zipped_pairs = zip(list2, list1)
	z = [x for (_, x) in sorted(zipped_pairs)]
	return z

def product(l):
	por = 1
	for i in range(len(l)):
		por *= l[i]
	return por

def binarySearchCount(arr, n, key):
	left = 0
	right = n - 1
	count = 0
	while left <= right:
		mid = int((right + left) / 2)
		if arr[mid] < key:
			count = mid + 1
			left = mid + 1
		else:
			right = mid - 1
	return count

def countdig(n):
	c = 0
	while n > 0:
		n //= 10
		c += 1
	return c

def binary(x, length):
	y = bin(x)[2:]
	return y if len(y) >= length else '0' * (length - len(y)) + y

def countGreater(arr, n, k):
	l = 0
	r = n - 1
	leftGreater = n
	while l <= r:
		m = int(l + (r - l) / 2)
		if arr[m] >= k:
			leftGreater = m
			r = m - 1
		else:
			l = m + 1
	return n - leftGreater

def findSubarraySum(arr, n, Sum):
	prevSum = defaultdict(lambda : 0)
	res = 0
	currsum = 0
	for i in range(0, n):
		currsum += arr[i]
		if currsum == Sum:
			res += 1
		if currsum - Sum in prevSum:
			res += prevSum[currsum - Sum]
		prevSum[currsum] += 1
	return res
for ik in range(int(input())):
	n = int(input())
	l = list(input())
	l = [int(l[i]) - 1 for i in range(n)]
	print(findSubarraySum(l, n, 0))


t = int(input())
while t:
	n = int(input())
	s = list(input())
	s = list(map(int, s))
	s = list(map(lambda x: x - 1, s))
	sumi = 0
	seen = {0: 1}
	ans = 0
	for i in range(n):
		sumi += s[i]
		if sumi in seen:
			ans += seen[sumi]
			seen[sumi] += 1
		else:
			seen[sumi] = 1
	print(ans)
	t -= 1


import math
from decimal import Decimal
import heapq
import copy
import heapq
from collections import deque
from collections import defaultdict
MOD = 1000000007

def na():
	n = int(input())
	b = [int(x) for x in input().split()]
	return (n, b)

def nab():
	n = int(input())
	b = [int(x) for x in input().split()]
	c = [int(x) for x in input().split()]
	return (n, b, c)

def dv():
	(n, m) = map(int, input().split())
	return (n, m)

def da():
	(n, m) = map(int, input().split())
	a = list(map(int, input().split()))
	return (n, m, a)

def dva():
	(n, m) = map(int, input().split())
	a = [int(x) for x in input().split()]
	b = [int(x) for x in input().split()]
	return (n, m, b)

def eratosthenes(n):
	sieve = list(range(n + 1))
	for i in sieve:
		if i > 1:
			for j in range(i + i, len(sieve), i):
				sieve[j] = 0
	return sorted(set(sieve))

def lol(lst, k):
	k = k % len(lst)
	ret = [0] * len(lst)
	for i in range(len(lst)):
		if i + k < len(lst) and i + k >= 0:
			ret[i] = lst[i + k]
		if i + k >= len(lst):
			ret[i] = lst[i + k - len(lst)]
		if i + k < 0:
			ret[i] = lst[i + k + len(lst)]
	return ret

def nm():
	n = int(input())
	b = [int(x) for x in input().split()]
	m = int(input())
	c = [int(x) for x in input().split()]
	return (n, b, m, c)

def dvs():
	n = int(input())
	m = int(input())
	return (n, m)

def fact(n):
	tc = []
	ans = {}
	d = 2
	while d * d <= n:
		if n % d == 0:
			tc.append(d)
			n //= d
		else:
			d += 1
	if n > 1:
		tc.append(n)
	for i in tc:
		ans[i] = ans.get(i, 0) + 1
	return ans

def to_zero(n, s):
	lst = s[0]
	ans = []
	for i in range(n):
		if lst == s[i]:
			continue
		elif lst == '0':
			lst = '1'
			ans.append(i)
		else:
			lst = '0'
			ans.append(i)
	if lst == '1':
		ans.append(n)
	return ans
for _ in range(int(input())):
	n = int(input())
	s = input()
	a = [0]
	for i in s:
		a.append(int(i) - 1 + a[-1])
	d = {}
	for i in a:
		d[i] = d.get(i, 0) + 1
	ans = 0
	for i in d:
		k = d[i] - 1
		ans += (k + 1) * (k // 2) + k % 2 * (k // 2 + 1)
	print(ans)


from itertools import accumulate
from collections import defaultdict
t = int(input())
for _ in range(t):
	jisho = defaultdict(int)
	n = int(input())
	A = list(map(int, list(input())))
	cumsumA = list(accumulate([0] + A))
	for i in range(n + 1):
		temp = cumsumA[i] - i
		jisho[temp] += 1
	ans = 0
	for (key, val) in jisho.items():
		ans += val * (val - 1) // 2
	print(ans)


for _ in range(int(input())):
	n = int(input())
	s = input()
	a = []
	pref = 0
	d = {0: 1}
	ans = 0
	for i in range(n):
		pref += int(s[i])
		x = pref - i - 1
		if x not in d:
			d[x] = 1
		else:
			d[x] += 1
		ans += d[x] - 1
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	data = list(map(int, s))
	for i in range(len(data)):
		data[i] = data[i] - 1
	fdata = {data[0]: 1}
	ndata = [data[0]]
	ans = 0
	if data[0] == 0:
		ans += 1
	for i in range(1, len(data)):
		ndata.append(ndata[i - 1] + data[i])
		if ndata[i - 1] + data[i] == 0:
			if 0 in fdata:
				ans += fdata[0] + 1
				fdata[0] += 1
			else:
				ans += 1
				fdata[0] = 1
			continue
		if ndata[i - 1] + data[i] in fdata:
			ans += fdata[ndata[i - 1] + data[i]]
			fdata[ndata[i - 1] + data[i]] += 1
		else:
			fdata[ndata[i - 1] + data[i]] = 1
	print(ans)


import sys
from collections import Counter

def main():
	tn = int(sys.stdin.readline())
	for ti in range(tn):
		n = int(sys.stdin.readline())
		A = [int(a) - 1 for a in sys.stdin.readline().strip()]
		for i in range(1, n):
			A[i] += A[i - 1]
		r = 0
		c = Counter()
		for a in A:
			if a == 0:
				r += 1
			r += c[a]
			c[a] += 1
		print(r)
main()


t = int(input())
for q in range(t):
	n = int(input())
	ch = input()
	L = [int(i) for i in ch]
	L2 = [0]
	for i in range(n):
		L2.append(L2[-1] + L[i] - 1)
	L2.sort()
	nb = 0
	L3 = []
	for i in range(1, n + 1):
		if L2[i] != L2[i - 1]:
			L3.append(nb)
			nb = 0
		else:
			nb += 1
	L3.append(nb)
	for i in range(len(L3)):
		L3[i] = L3[i] * (L3[i] + 1) // 2
	print(sum(L3))


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(input())
	checkdict = {0: 1}
	summation = 0
	arrcnt = 0
	for j in range(n):
		summation += int(arr[j])
		x = summation - j - 1
		if x not in checkdict:
			checkdict[x] = 0
		checkdict[x] += 1
		arrcnt += checkdict[x] - 1
	print(arrcnt)


def process(arr):
	arr = list(arr)
	map = {}
	count = 0
	sum = 0
	for i in range(len(arr)):
		sum += int(arr[i])
		if sum - i - 1 == 0:
			count += 1
		if sum - i - 1 in map:
			count += len(map[sum - i - 1])
			map[sum - i - 1].append(i)
		else:
			map[sum - i - 1] = [i]
	print(count)
for _ in range(int(input())):
	n = int(input())
	s = input()
	process(s)


for _ in range(int(input())):
	n = int(input())
	s = input()
	dp = [0, int(s[0])]
	for i in range(1, n):
		dp.append(int(s[i]) + dp[-1])
	d = {}
	for i in range(len(dp)):
		tmp = dp[i] - i
		if tmp in d:
			d[tmp] += 1
		else:
			d[tmp] = 1
	ans = 0
	for i in d:
		ans += d[i] * (d[i] - 1) // 2
	print(ans)


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	arr = input()
	pref = 0
	d = defaultdict(int)
	d[0] = 1
	ans = 0
	for i in arr:
		pref += ord(i) - ord('0') - 1
		ans += d[pref]
		d[pref] += 1
	print(ans)


for iii in range(int(input())):
	n = int(input())
	l = list(map(int, input()))
	s = 0
	d = {0: 1}
	c = 0
	for i in range(n):
		s = s + l[i]
		c = s - 1 - i
		if c in d:
			d[c] = d[c] + 1
		else:
			d[c] = 1
	c = 0
	for i in d.values():
		c = c + i * (i - 1) // 2
	print(c)


import sys
input = sys.stdin.readline
(inp, ip) = (lambda : int(input()), lambda : [int(w) for w in input().split()])
for _ in range(inp()):
	n = inp()
	x = [int(i) for i in input().strip()]
	dp = [0] * (n + 1)
	for i in range(1, n + 1):
		dp[i] = dp[i - 1] + x[i - 1]
	for i in range(1, n + 1):
		dp[i] -= i
	dt = {}
	for i in dp:
		dt[i] = dt.get(i, 0) + 1
	ans = 0
	for (u, v) in dt.items():
		ans += v * (v - 1) // 2
	print(ans)


from collections import defaultdict

def find(arr, n, s):
	d = defaultdict(int)
	c = 0
	ans = 0
	for i in range(0, n):
		c += arr[i]
		if c == s:
			ans += 1
		if c - s in d:
			ans += d[c - s]
		d[c] += 1
	return ans
t = int(input())
for _ in range(t):
	n = int(input())
	ar = [int(i) - 1 for i in input().strip()]
	print(find(ar, n, 0))


for item in [0] * int(input()):
	n = int(input())
	a = list(map(int, input()))
	for i in range(n):
		a[i] = a[i] - 1
	hashmap = {}
	hashmap[0] = 1
	count = 0
	s = 0
	for i in range(n):
		if a[i] == 0 and i == 0:
			count += 1
			hashmap[0] = 2
		else:
			s += a[i]
			if s in hashmap:
				count += hashmap[s]
				hashmap[s] += 1
			if s not in hashmap:
				hashmap[s] = 1
	print(count)


from sys import stdin
tt = int(stdin.readline())
for loop in range(tt):
	n = int(stdin.readline())
	a = stdin.readline()
	a = a[:-1]
	dic = {}
	now = 0
	dic[0] = 1
	for i in range(1, n + 1):
		now = now + int(a[i - 1])
		if now - i not in dic:
			dic[now - i] = 1
		else:
			dic[now - i] += 1
	ans = 0
	for i in dic:
		ans += dic[i] * (dic[i] - 1) // 2
	print(ans)


import sys
input = sys.stdin.readline
from collections import defaultdict

def main():
	n = int(input())
	a = input().strip()
	total = 0
	dd = defaultdict(int)
	dd[0] += 1
	for i in range(n):
		total += int(a[i])
		dd[total - i - 1] += 1
	ans = 0
	for (_, value) in dd.items():
		ans += value * (value - 1) // 2
	print(ans)
for _ in range(int(input())):
	main()


for _ in range(int(input())):
	n = int(input())
	array = []
	for c in input():
		array.append(int(c))
	d = {0: 1}
	res = sum = 0
	for (i, num) in enumerate(array):
		sum += num
		x = sum - (i + 1)
		if x not in d:
			d[x] = 0
		res += d[x]
		d[x] += 1
	print(res)


def countsumzero(lst):
	prefixsums = [0]
	for x in lst:
		prefixsums.append(prefixsums[-1] + x)
	freq = {}
	for y in prefixsums:
		if y in freq:
			freq[y] += 1
		else:
			freq[y] = 1
	return sum((v * (v - 1) // 2 for v in freq.values()))
for _ in range(int(input())):
	n = int(input())
	a = input()
	lst = [int(i) - 1 for i in a]
	print(countsumzero(lst))


from collections import defaultdict
t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	d = defaultdict(int)
	d[0] = 1
	nums = [int(i) - 1 for i in s]
	ans = 0
	sums = 0
	for i in range(n):
		sums += nums[i]
		if d[sums]:
			ans += d[sums]
			d[sums] += 1
		else:
			d[sums] += 1
	print(ans)


import collections
for _ in range(int(input())):
	n = input()
	s = input()
	(c, m, a) = (0, collections.Counter(), 0)
	m[0] = 1
	for i in range(int(n)):
		c += int(s[i])
		x = c - i - 1
		m[x] += 1
		a += m[x] - 1
	print(a)


import collections
for _ in range(int(input())):
	n = int(input())
	A = str(input())
	ans = 0
	s1 = 0
	s2 = []
	s2.append(0)
	for j in range(n):
		s1 += int(A[j]) - 1
		s2.append(s1)
	s3 = collections.Counter(s2)
	for (key, value) in s3.items():
		ans += value * (value - 1) // 2
	print(ans)


from collections import defaultdict
for i in range(int(input())):
	n = int(input())
	a = [int(x) for x in list(input())]
	x = defaultdict(lambda : 0)
	s = 0
	p = []
	for j in range(n):
		s += a[j]
		p.append(s)
	for j in range(n):
		x[p[j] - j - 1] += 1
	an = 0
	for i in x:
		an += x[i] * (x[i] - 1) // 2
	an += x[0]
	print(an)


def solve():
	n = int(input())
	a = input()
	s = 0
	ans = 0
	d = {0: 0}
	for i in range(n):
		s += int(a[i]) - 1
		if s in d:
			d[s] += 1
		else:
			d[s] = 0
		ans += d[s]
	print(ans)
for t in range(int(input())):
	solve()


import sys
import collections
import heapq
from collections import Counter
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())
t = inp()
for _ in range(t):
	n = inp()
	nums = insr()
	c = Counter()
	s = 0
	res = 0
	c[1] += 1
	for (i, num) in enumerate(nums):
		s += int(num)
		key = s - i
		res += c[key]
		c[key] += 1
	print(res)


import sys
import math
import bisect
from sys import stdin, stdout
from math import gcd, floor, sqrt, log
from collections import defaultdict as dd
from bisect import bisect_left as bl, bisect_right as br
from collections import Counter
inp = lambda : int(input())
strng = lambda : input().strip()
jn = lambda x, l: x.join(map(str, l))
strl = lambda : list(input().strip())
mul = lambda : map(int, input().strip().split())
mulf = lambda : map(float, input().strip().split())
seq = lambda : list(map(int, input().strip().split()))
ceil = lambda x: int(x) if x == int(x) else int(x) + 1
ceildiv = lambda x, d: x // d if x % d == 0 else x // d + 1
flush = lambda : stdout.flush()
stdstr = lambda : stdin.readline()
stdint = lambda : int(stdin.readline())
stdpr = lambda x: stdout.write(str(x))
stdarr = lambda : map(int, stdstr().split())
mod = 1000000007
for _ in range(stdint()):
	n = stdint()
	s = input()
	pref = []
	for i in range(n):
		if i == 0:
			pref.append(int(s[i]))
		else:
			pref.append(pref[-1] + int(s[i]))
	freq = Counter()
	freq[0] = 1
	for i in range(n):
		freq[pref[i] - i - 1] += 1
	res = 0
	for i in freq:
		res += freq[i] * (freq[i] - 1) // 2
	print(res)


from __future__ import division, print_function
import os, sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

def ii():
	return int(input())

def si():
	return input()

def mi():
	return map(int, input().strip().split(' '))

def msi():
	return map(str, input().strip().split(' '))

def li():
	return list(mi())

def dmain():
	sys.setrecursionlimit(1000000)
	threading.stack_size(1024000)
	thread = threading.Thread(target=main)
	thread.start()
import sys
input = sys.stdin.readline
scanner = lambda : int(input())
string = lambda : input().rstrip()
get_list = lambda : list(read())
read = lambda : map(int, input().split())
get_float = lambda : map(float, input().split())

def ceil(x):
	if x != int(x):
		x = int(x) + 1
	return x

def factorial(x, m):
	val = 1
	while x > 0:
		val = val * x % m
		x -= 1
	return val

def fact(x):
	val = 1
	while x > 0:
		val *= x
		x -= 1
	return val

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def lcm(a, b):
	return a * b // math.gcd(a, b)

def is_integer(n):
	return math.ceil(n) == math.floor(n)

def nCr(n, k):
	if k > n:
		return 0
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return int(res)

def primefs(n):
	primes = {}
	while n % 2 == 0 and n > 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0 and n > 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	if y == 0:
		return 1
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	p = x
	while p != link[p]:
		p = link[p]
	while x != p:
		nex = link[x]
		link[x] = p
		x = nex
	return p

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	(prime[0], prime[1]) = (False, False)
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime

def phi(n):
	result = n
	p = 2
	while p * p <= n:
		if n % p == 0:
			while n % p == 0:
				n = n // p
			result = result * (1.0 - 1.0 / float(p))
		p = p + 1
	if n > 1:
		result = result * (1.0 - 1.0 / float(n))
	return int(result)

def is_prime(n):
	if n == 0:
		return False
	if n == 1:
		return True
	for i in range(2, int(n ** (1 / 2)) + 1):
		if not n % i:
			return False
	return True

def next_prime(n, primes):
	while primes[n] != True:
		n += 1
	return n
MAXN = int(100000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i
spf = [0 for i in range(MAXN)]

def factoriazation(x):
	res = []
	for i in range(2, int(x ** 0.5) + 1):
		while x % i == 0:
			res.append(i)
			x //= i
	if x != 1:
		res.append(x)
	return res

def factors(n):
	res = []
	for i in range(1, int(n ** 0.5) + 1):
		if n % i == 0:
			res.append(i)
			res.append(n // i)
	return list(set(res))

def int_array():
	return list(map(int, input().strip().split()))

def float_array():
	return list(map(float, input().strip().split()))

def str_array():
	return input().strip().split()

def binary_search(low, high, w, h, n):
	while low < high:
		mid = low + (high - low) // 2
		if check(mid, w, h, n):
			low = mid + 1
		else:
			high = mid
	return low

def check(val, pair):
	summ = 0
	for x in pair:
		if x[1] > val:
			summ += x[0]
	return summ > val

def sortSecond(val):
	return val[1]
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
alphs = 'abcdefghijklmnopqrstuvwxyz'
from itertools import permutations
import math
import bisect as bis
import random
import sys
import collections as collect
import functools as fnt
from decimal import Decimal

def solve():
	n = scanner()
	s = [int(x) for x in string()]
	for i in range(1, n):
		s[i] += s[i - 1]
	mp = {1: 1}
	for i in range(n):
		x = s[i] - i
		d = mp.get(x, 0)
		mp[x] = d + 1
	ans = 0
	for x in mp.values():
		ans += x * (x - 1) // 2
	print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
t = scanner()
for i in range(t):
	solve()


import sys
import heapq, functools, collections
import math, random
from collections import Counter, defaultdict

def solve(strr):
	console('----- solving ------')
	arr = [int(x) for x in strr]
	brr = [0]
	cs = 0
	for a in arr:
		cs += a
		brr.append(cs)
	c = Counter([])
	for (i, b) in enumerate(brr):
		c[b - i] += 1
	return sum([v * (v - 1) // 2 for (k, v) in c.items()])

def console(*args):
	print('\x1b[36m', *args, '\x1b[0m', file=sys.stderr)
	return
for case_num in range(int(input())):
	_ = input()
	strr = input()
	res = solve(strr)
	print(res)


test = int(input())
for each_test in range(test):
	d = {0: 1}
	n = int(input())
	s = input()
	curr_sum = 0
	res = 0
	for i in range(n):
		num = int(s[i])
		curr_sum += num
		x = curr_sum - i - 1
		if x not in d:
			d[x] = 0
		d[x] += 1
		res += d[x] - 1
	print(res)


t = int(input())
while t:
	n = int(input())
	a = list(input())
	for i in range(0, n):
		a[i] = int(a[i])
	temp = []
	for i in a:
		temp.append(i - 1)
	count = 0
	curr = 0
	sums = {}
	for i in range(0, n):
		curr = curr + temp[i]
		if curr == 0:
			count = count + 1
		if curr in sums:
			count = count + sums[curr]
			sums[curr] = sums[curr] + 1
		else:
			sums[curr] = 1
	print(count)
	t = t - 1


mod = 10 ** 9 + 7

def solve():
	n = int(input())
	t = list(input())
	a = list(map(lambda x: int(x), t))
	s = [0 for i in range(n + 1)]
	t = 0
	for i in range(n + 1):
		s[i] = n - i
		if i > 0:
			s[i] += a[i - 1] + t
			t += a[i - 1]
	d = {}
	ans = 0
	for i in range(n + 1):
		if s[i] in d:
			ans += d[s[i]]
			d[s[i]] += 1
		else:
			d[s[i]] = 1
	print(ans)
t = 1
t = int(input())
for _ in range(t):
	solve()


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, copy, functools
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
mod2 = 998244353
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI():
	return [list(map(int, l.split())) for l in sys.stdin.readlines()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def pe(s):
	return print(str(s), file=sys.stderr)

def JA(a, sep):
	return sep.join(map(str, a))

def JAA(a, s, t):
	return s.join((t.join(map(str, b)) for b in a))

def IF(c, t, f):
	return t if c else f

def YES(c):
	return IF(c, 'YES', 'NO')

def Yes(c):
	return IF(c, 'Yes', 'No')

def main():
	t = I()
	rr = []
	for _ in range(t):
		n = I()
		s = S()
		r = 0
		c = collections.defaultdict(int)
		c[0] = 1
		t = 0
		for (i, k) in enumerate(s):
			k = int(k)
			t += k - 1
			r += c[t]
			c[t] += 1
		rr.append(r)
	return JA(rr, '\n')
print(main())


from collections import defaultdict
t = int(input())
for case in range(t):
	n = int(input())
	arr = [int(num) - 1 for num in input()]
	mymap = defaultdict(lambda : 0)
	mymap[0] += 1
	ans = 0
	summ = 0
	for i in range(n):
		summ += arr[i]
		ans += mymap[summ]
		mymap[summ] += 1
	print(ans)


from collections import Counter
import sys
input = sys.stdin.readline

def prog():
	for _ in range(int(input())):
		n = int(input())
		s = list(map(int, input().strip()))
		for i in range(n):
			s[i] -= 1
		psum = [0] * (n + 1)
		a = Counter([0])
		for i in range(n):
			psum[i + 1] = s[i] + psum[i]
			a[psum[i + 1]] += 1
		ans = 0
		for val in a.values():
			ans += val * (val - 1) // 2
		print(ans)
prog()


for _ in range(int(input())):
	n = int(input())
	s = input()
	m = [1] + [0] * 9 * n
	(cur, ans) = (0, 0)
	for i in s:
		cur += ord(i) - 49
		ans += m[cur]
		m[cur] += 1
	print(ans)


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	s = input()
	cum = 0
	count = 0
	pre_sum = {0: 0}
	for i in range(len(s)):
		val = int(s[i]) - 1
		cum += val
		if cum == 0:
			count += 1
		if cum in pre_sum:
			count += pre_sum[cum]
			pre_sum[cum] += 1
		else:
			pre_sum[cum] = 1
	print(count)


for _ in range(int(input())):
	n = int(input())
	g = {0: 1}
	s = input()
	cs = ans = 0
	for i in range(n):
		cs += int(s[i])
		ans += g.get(cs - i - 1, 0)
		g[cs - i - 1] = g.get(cs - i - 1, 0) + 1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	s = input()
	ans = 0
	sumi = 0
	mp = {}
	mp[0] = 1
	for i in range(n):
		sumi += int(s[i])
		ans += mp.get(sumi - i - 1, 0)
		mp[sumi - i - 1] = mp.get(sumi - i - 1, 0) + 1
	print(ans)


from collections import defaultdict

def get_good_subs_num(line):
	offset = 0
	good_subs_num = 0
	sub_count = defaultdict(int)
	for c in line:
		num = ord(c) - ord('0')
		offset -= num - 1
		sub_count[num - 1 + offset] += 1
		good_subs_num += sub_count[offset]
	return good_subs_num
t = int(input())
for _ in range(t):
	n = input()
	line = input()
	print(get_good_subs_num(line))


import time
from collections import Counter

def inpt():
	return int(input())

def inpl():
	return list(map(int, input().split()))

def inpm():
	return map(int, input().split())

def solve():
	n = inpt()
	l = list(map(int, input()))
	for i in range(n):
		l[i] -= 1
	s = [l[0]]
	for i in range(1, n):
		s.append(s[-1] + l[i])
	ans = 0
	x = Counter(s)
	for (i, j) in x.items():
		ans += j * (j - 1) // 2
	print(ans + s.count(0))

def main():
	m = 10 ** 9 + 7
	t = int(input())
	while t:
		t -= 1
		solve()
main()


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(input())
	sumi = 0
	dicti = {}
	dicti[0] = 1
	count = 0
	for i in range(n):
		sumi = sumi + int(l[i]) - 1
		if dicti.get(sumi, 0) == 0:
			dicti[sumi] = 1
		else:
			count += dicti[sumi]
			dicti[sumi] += 1
	print(count)


import math

def dtb(n):
	return bin(n).replace('0b', '')

def btd(n):
	return int(n, 2)
t = int(input())
for k in range(t):
	n = int(input())
	s = input()
	dic = {0: 1}
	(ans, su) = (0, 0)
	for i in range(n):
		su += int(s[i])
		x = su - i - 1
		if x not in dic:
			dic[x] = 0
		dic[x] += 1
		ans += dic[x] - 1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	s = str(input())
	d = {0: 1}
	c = 0
	x = 0
	for i in s:
		x += int(i) - 1
		if x in d:
			c += d[x]
			d[x] += 1
		else:
			d[x] = 1
	print(c)


for _ in range(int(input())):
	n = int(input())
	s = input()
	dic = {}
	sum = 0
	dic[0] = 1
	ans = 0
	for i in range(n):
		sum += int(s[i])
		temp = sum - i + -1
		if temp in dic:
			ans += dic[temp]
			dic[temp] += 1
		else:
			dic[temp] = 1
	print(ans)


from itertools import combinations, accumulate
from itertools import combinations_with_replacement
for _ in range(int(input())):
	n = int(input())
	arr = [int(i) for i in input()]
	arr[0] -= 1
	d = {0: 1}
	res = 0
	for (idx, s) in enumerate(accumulate(arr)):
		x = s - idx
		d[x] = d.get(x, 0)
		res += d[x]
		d[x] += 1
	print(res)


from functools import reduce
import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from heapq import *
from io import BytesIO, IOBase
input = lambda : sys.stdin.readline().rstrip('\r\n')

def value():
	return tuple(map(int, input().split()))

def arr():
	return [int(i) for i in input().split()]

def sarr():
	return [int(i) for i in input()]

def inn():
	return int(input())
mo = 1000000007
for _ in range(int(input())):
	n = inn()
	a = [int(x) for x in input()]
	d = defaultdict(int)
	ans = 0
	sm = 0
	d[0] = 1
	for i in range(1, n + 1):
		sm += a[i - 1]
		ans += d[sm - i]
		d[sm - i] += 1
	print(ans)


from sys import stdin

def inp():
	return stdin.buffer.readline().rstrip().decode('utf8')

def itg():
	return int(stdin.buffer.readline())

def mpint():
	return map(int, stdin.buffer.readline().split())
from itertools import accumulate
from collections import Counter

def brute_force():
	ans = 0
	for i in range(n):
		for j in range(i + 1, n + 1):
			if sum(arr[i:j]) == j - i:
				ans += 1
	return ans
for case in range(1, itg() + 1):
	n = itg()
	aa = tuple(map(int, inp()))
	arr = [a - i - 1 for (i, a) in enumerate(accumulate(aa))]
	counter = Counter(arr)
	ans = counter[0]
	lazy = 0
	for i in range(n - 1):
		counter[arr[i]] -= 1
		lazy += 1 - aa[i]
		ans += counter[-lazy]
	print(ans)


def multiple_input():
	return map(int, input().split())

def list_input():
	return list(map(int, input().split()))
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().strip()))
	for i in range(n):
		a[i] -= 1
	count = 0
	s = 0
	d = {}
	for i in a:
		s += i
		if s in d:
			d[s] += 1
		else:
			d[s] = 1
	for i in d:
		if i == 0:
			count += d[i] * (d[i] + 1) // 2
		else:
			count += d[i] * (d[i] - 1) // 2
	print(count)


a = int(input())
for i in range(a):
	s = int(input())
	s = input()
	ans = []
	for i in range(len(s)):
		ans.append(int(s[i]))
	from collections import *
	al = defaultdict(int)
	total = 0
	pre = 0
	for i in range(len(ans)):
		ans[i] -= 1
		pre += ans[i]
		if pre == 0:
			total += 1
		total += al[pre]
		al[pre] += 1
	print(total)


t = int(input())
for ii in range(t):
	n = int(input())
	s = list(input())
	count = 0
	for i in range(n):
		s[i] = int(s[i]) - 1
	m = {}
	sm = 0
	count = 0
	for i in range(n):
		sm += s[i]
		diff = sm
		if sm == 0:
			count += 1
		if diff in m:
			count += m[diff]
		if sm in m:
			m[sm] += 1
		else:
			m[sm] = 1
	print(count)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, list(input())))
	sa = [0] * (n + 1)
	C = dict()
	C[0] = 1
	for i in range(n):
		sa[i + 1] = sa[i] + a[i]
		if sa[i + 1] - i - 1 in C:
			C[sa[i + 1] - i - 1] += 1
		else:
			C[sa[i + 1] - i - 1] = 1
	ans = 0
	for x in C:
		ans += C[x] * (C[x] - 1) // 2
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	q = input()
	d = {0: 1}
	ans = 0
	summ = 0
	for i in range(n):
		summ += int(q[i])
		if summ - i - 1 in d:
			ans += d[summ - i - 1]
			d[summ - i - 1] += 1
		else:
			d[summ - i - 1] = 1
	print(ans)


import sys
T = int(sys.stdin.readline())
for _ in range(T):
	n = int(sys.stdin.readline())
	list_a = list(map(int, list(sys.stdin.readline().strip())))
	res = 0
	now_sum = 0
	dict_check = {0: 1}
	for i in range(n):
		now_sum += list_a[i]
		temp = now_sum - i - 1
		if temp in dict_check:
			res += dict_check[temp]
		else:
			dict_check[temp] = 0
		dict_check[temp] += 1
	print(res)


for s in [*open(0)][2::2]:
	g = {0: 1}
	c = a = i = 0
	while i + 1 < len(s):
		c += int(s[i])
		i += 1
		f = g.get(c - i, 0)
		a += f
		g[c - i] = f + 1
	print(a)


import math, sys, bisect, heapq
from collections import defaultdict, Counter, deque
from itertools import groupby, accumulate
input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__
ilele = lambda : map(int, input().split())
alele = lambda : list(map(int, input().split()))

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def Y(c):
	print(['NO', 'YES'][c])

def y(c):
	print(['no', 'yes'][c])

def Yy(c):
	print(['No', 'Yes'][c])
for _ in range(int(input())):
	N = int(input())
	S = input()
	T = [0] + [int(i) - 1 for i in S]
	print(sum([i * (i - 1) // 2 for i in Counter(accumulate(T)).values()]))


import sys
import math
import bisect
from sys import stdin, stdout
from math import gcd, floor, sqrt, log2, ceil
from collections import defaultdict as dd
from bisect import bisect_left as bl, bisect_right as br
from collections import Counter
from collections import deque
from heapq import heappush, heappop, heapify
mod = int(1000000000.0) + 7
ip = lambda : int(stdin.readline())
inp = lambda : map(int, stdin.readline().split())
ips = lambda : stdin.readline().rstrip()
t = ip()
for _ in range(t):
	n = ip()
	s = ips()
	arr = []
	for i in s:
		arr.append(int(i) - 1)
	pref = []
	a = Counter([0])
	ch = 0
	for i in range(n):
		ch += arr[i]
		pref.append(ch)
		a[ch] += 1
	ans = 0
	for i in a.values():
		ans += i * (i - 1) // 2
	print(ans)


T = int(input())
for _ in range(T):
	n = int(input())
	s = input()
	s = [int(s[i]) for i in range(n)]
	s.insert(0, 0)
	su = []
	su.append(0)
	for i in range(1, n + 1):
		su.append(su[i - 1] + s[i])
	mmp = {0: 1}
	ans = 0
	for i in range(1, n + 1):
		if i - su[i] not in mmp:
			mmp[i - su[i]] = 0
		ans += mmp[i - su[i]]
		mmp[i - su[i]] += 1
	print(ans)


tests = int(input())
for t in range(tests):
	b = int(input())
	ls = [int(x) for x in input()]
	res = 0
	p_dict = {0: 1}
	p_sum = 0
	for item in ls:
		item -= 1
		p_sum += item
		if p_sum in p_dict:
			res += p_dict[p_sum]
			p_dict[p_sum] += 1
		else:
			p_dict[p_sum] = 1
	print(res)


def countsumzero(z, lst, vl):
	prefixsums = [z]
	for x in lst:
		prefixsums.append(prefixsums[z - vl] + x - z)
	freq = {}
	for y in prefixsums:
		if y not in freq:
			freq[z + y] = vl + z
		else:
			freq[z + y] = freq[y] + vl + z
	return sum(((v * v - v + z) // (vl + vl - 4 * z) for v in freq.values()))

def main():
	for _ in range(int(input())):
		n = int(input())
		lst = (int(i) - 1 for i in list(input()))
		print(countsumzero(0, lst, 1))
main()


MOD = 1000000007
from collections import defaultdict as dd, Counter, deque

def si():
	return input()

def ii():
	return int(input())

def li():
	return list(map(int, input().split()))

def mi():
	return map(int, input().split())

def out(v):
	print(v)

def spout():
	print(v, end=' ')

def d2b(n):
	return bin(n).replace('0b', '')

def twod(n, m, num):
	return [[num for x in range(m)] for y in range(n)]

def vow():
	return ['a', 'e', 'i', 'o', 'u']

def let():
	return [chr(i) for i in range(97, 123)]

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def ispow2(x):
	return x and (not x & x - 1)

def prime_factors(n):
	i = 2
	factors = []
	while i * i <= n:
		if n % i:
			i += 1
		else:
			n //= i
			factors.append(i)
	if n > 1:
		factors.append(n)
	return list(factors)
from collections import Counter
t = ii()
while t:
	t -= 1
	n = ii()
	a = list(map(lambda x: int(x) - 1, list(input())))
	pre = [0]
	for i in range(n):
		pre.append(pre[i] + a[i])
	a = Counter(pre)
	ans = 0
	for i in a.keys():
		ans += a[i] * (a[i] - 1) // 2
	print(ans)


for _ in range(int(input())):
	n = int(input())
	s = input()
	d = {0: 1}
	ans = ext = 0
	for i in range(n):
		ext += int(s[i]) - 1
		if ext not in d:
			d[ext] = 0
		ans += d[ext]
		d[ext] += 1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input()))
	c = {0: 1}
	t = 0
	v = 0
	for (i, x) in enumerate(a):
		t += x
		s = i + 1 - t
		v += c.get(s, 0)
		c[s] = c.get(s, 0) + 1
	print(v)


t = int(input())
for q in range(t):
	n = int(input())
	arr = [int(i) for i in input()]
	dict = {1: 1}
	count = 0
	sum = 0
	i = 0
	for item in arr:
		sum += item
		temp = sum - i
		if temp in dict:
			count += dict[temp]
			dict[temp] += 1
		else:
			dict[temp] = 1
		i += 1
	print(count)


for _ in range(int(input())):
	lens = int(input())
	nums = [int(x) for x in input()]
	r = 0
	p = {0: 1}
	w = 0
	for i in nums:
		w += i - 1
		if w in p:
			r += p[w]
		else:
			p[w] = 0
		p[w] += 1
	print(r)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, list(input())))
	cnt = {}
	cnt[0] = 1
	sum = 0
	answer = 0
	for i in range(0, n):
		sum += arr[i]
		if sum - i - 1 in cnt:
			answer += cnt[sum - i - 1]
			cnt[sum - i - 1] += 1
		else:
			cnt[sum - i - 1] = 1
	print(answer)


import os
import sys
import math
import heapq
from decimal import *
from io import BytesIO, IOBase
from collections import defaultdict, deque

def r():
	return int(input())

def rm():
	return map(int, input().split())

def rl():
	return list(map(int, input().split()))
for _ in range(r()):
	n = r()
	a = list(map(int, input()))
	ans = 0
	have = defaultdict(int)
	cs = 0
	have[0] += 1
	for (i, j) in enumerate(a):
		cs += j
		x = cs - (i + 1)
		ans += have[x]
		have[x] += 1
	print(ans)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, list(input()[:n])))
	ans = 0
	d = [0] * (n * 10)
	x = 0
	for i in range(n):
		d[x] += 1
		x += a[i] - 1
		ans += d[x]
	print(ans)


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	aa = list(input())
	a = [int(i) for i in aa]
	b = []
	ex = 0
	for i in range(n):
		ex += a[i] - 1
		b.append(ex)
	dic = defaultdict(lambda : 0)
	count = 0
	for i in range(n):
		if b[i] == 0:
			count += 1
		count += dic[b[i]]
		dic[b[i]] += 1
	print(count)


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	string = input()
	l = [int(i) for i in string]
	for i in range(n):
		l[i] -= 1
	dp = [0] * n
	dp[0] = l[0]
	for i in range(1, n):
		dp[i] = dp[i - 1] + l[i]
	d = defaultdict(list)
	for i in range(n):
		d[dp[i]].append(i)
	cnt = 0
	x = d.keys()
	for i in x:
		n = len(d[i])
		if i != 0:
			cnt += n * (n - 1) // 2
		else:
			cnt += n * (n + 1) // 2
	print(cnt)


from collections import Counter
T = int(input())
for t in range(T):
	n = int(input())
	s = list(input())
	L = list(map(int, s))
	P = [0]
	for i in range(n):
		L[i] -= 1
		P.append(P[i] + L[i])
	C = Counter(P)
	ans = 0
	for i in set(P):
		ans += C[i] * (C[i] - 1) / 2
	print(int(ans))
	P.clear()
	L.clear()


from collections import defaultdict
t = int(input())
answers = []
while t:
	t -= 1
	n = int(input())
	psum = 0
	cnt = defaultdict(int)
	cnt[1] += 1
	ans = 0
	a = [*map(int, input())]
	for i in range(n):
		psum += a[i]
		val = psum - i
		ans += cnt[val]
		cnt[val] += 1
	answers += (ans,)
print(*answers, sep='\n')


import sys
input = sys.stdin.readline
from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	s = [int(item) - 1 for item in input().rstrip()]
	cumsum = [0]
	for item in s:
		cumsum.append(item + cumsum[-1])
	cnt = Counter(cumsum)
	ans = 0
	for (key, val) in cnt.items():
		ans += val * (val - 1) // 2
	print(ans)


import collections

def main():
	for _ in range(int(input())):
		n = int(input())
		a = list(map(int, input()))
		a = [x - 1 for x in a]
		ans = 0
		prev = 0
		count = collections.Counter()
		for x in a:
			count[prev] += 1
			prev += x
			ans += count[prev]
		print(ans)
main()


def nc2(x):
	if x < 2:
		return 0
	return round(x * (x - 1) / 2)
t = int(input())
for tt in range(t):
	n = int(input())
	s = input()
	a = [int(x) - 1 for x in s]
	b = [0]
	for i in range(n):
		b.append(b[-1] + a[i])
	b.sort()
	pcks = [[b[0]]]
	for i in range(1, len(b)):
		if pcks[-1][0] == b[i]:
			pcks[-1].append(b[i])
		else:
			pcks.append([b[i]])
	lens = [len(x) for x in pcks]
	q = 0
	for x in lens:
		q += nc2(x)
	print(q)


def solution():
	n = int(input())
	s = list(input())
	l = []
	for i in range(n):
		l.append(int(s[i]))
	d = {}
	d[0] = 1
	pre = [0] * (n + 2)
	for i in range(1, n + 1):
		pre[i] += pre[i - 1] + l[i - 1]
	ans = 0
	for i in range(1, n + 1):
		x = pre[i] - i
		ans += d.get(x, 0)
		d[x] = d.get(x, 0) + 1
	print(ans)
t = int(input())
for _ in range(t):
	solution()


from collections import *
for _ in range(int(input())):
	(n, a) = (int(input()), input())
	a = [int(x) - 1 for x in a]
	mp = defaultdict(int)
	mp[0] = 1
	sm = 0
	ans = 0
	for x in a:
		sm += x
		ans += mp[sm]
		mp[sm] += 1
	print(ans)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	s = input()
	a = [int(s[i]) - 1 for i in range(n)]
	data = [0] + a
	for i in range(1, n + 1):
		data[i] += data[i - 1]
	dic = {}
	for i in range(n + 1):
		if data[i] not in dic:
			dic[data[i]] = 0
		dic[data[i]] += 1
	ans = 0
	for d in dic:
		ans += dic[d] * (dic[d] - 1) // 2
	print(ans)


from collections import Counter
for _ in range(int(input())):
	C = Counter([0])
	v = -1
	r = 0
	input()
	for (i, x) in enumerate(map(int, input())):
		v += x
		r += C[v - i]
		C[v - i] += 1
	print(r)


import collections

def solve(lst, k):
	m = [int(i) for i in lst]
	s = 0
	ans = 0
	p = {0: 1}
	for i in m:
		i -= 1
		s += i
		if s in p:
			ans += p[s]
			p[s] += 1
		else:
			p[s] = 1
	return ans

def sort(key):
	return key[0]
ans = []
n = int(input())
for i in range(n):
	k = int(input())
	st = input()
	ans.append(solve(st, k))
for i in ans:
	print(i)


for t in range(int(input())):
	n = int(input())
	s = input()
	a = [0]
	last = 0
	for i in range(n):
		if int(s[i]) == 0:
			a.append(a[i] - 1)
		else:
			a.append(a[i] + (int(s[i]) - 1))
	rama = []
	b = {}
	b[0] = 1
	for i in range(1, n + 1):
		if a[i] in b:
			b[a[i]] += 1
		if a[i] not in b:
			if int(s[i - 1]) == 1:
				b[a[i]] = 2
			else:
				b[a[i]] = 1
	add = 0
	for i in b:
		d = b[i]
		for i in range(1, d):
			add += i
	print(add)


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(i) for i in list(input().strip())]
	a.insert(0, 0)
	for i in range(1, n + 1):
		a[i] += a[i - 1]
	for i in range(n + 1):
		a[i] = a[i] - i
	freq = {}
	count = 0
	for i in range(n + 1):
		x = a[i]
		y = freq.get(x, 0)
		count += y
		freq[x] = y + 1
	print(count)


for _ in range(int(input())):
	n = int(input())
	s = input()
	dp = [0] * (9 * n)
	dp[0] = 1
	a = 0
	lol = 0
	for i in range(n):
		a += int(s[i])
		lol += dp[a - i - 1]
		dp[a - i - 1] += 1
	print(lol)


for _ in range(int(input())):
	n = int(input())
	a = input().strip()
	currsum = 0
	d = {0: 1}
	ans = 0
	for i in a:
		currsum += int(i) - 1
		if currsum in d:
			ans += d[currsum]
			d[currsum] += 1
		else:
			d[currsum] = 1
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	dic = {}
	dic[1] = 1
	temp = 0
	arr = input()
	for i in range(n):
		temp += int(arr[i])
		try:
			dic[temp - i] += 1
		except:
			dic[temp - i] = 1
	ans = 0
	for value in dic.values():
		ans += value * (value - 1) // 2
	print(ans)


import sys
import os
import math
import copy
from math import gcd
from bisect import bisect
from io import BytesIO, IOBase
from math import sqrt, floor, factorial, gcd, log, ceil
from collections import deque, Counter, defaultdict
from itertools import permutations, combinations
import itertools

def Int():
	return int(sys.stdin.readline())

def Mint():
	return map(int, sys.stdin.readline().split())

def Lstr():
	return list(sys.stdin.readline().strip())

def Str():
	return sys.stdin.readline().strip()

def Mstr():
	return map(str, sys.stdin.readline().strip().split())

def List():
	return list(map(int, sys.stdin.readline().split()))

def Hash():
	return dict()

def Mod():
	return 1000000007

def Ncr(n, r, p):
	return fact[n] * (ifact[r] * ifact[n - r] % p) % p

def Most_frequent(list):
	return max(set(list), key=list.count)

def Mat2x2(n):
	return [List() for _ in range(n)]

def Lcm(x, y):
	return x * y // gcd(x, y)

def dtob(n):
	return bin(n).replace('0b', '')

def btod(n):
	return int(n, 2)

def common(l1, l2):
	return set(l1).intersection(l2)

def solution():
	for _ in range(Int()):
		n = Int()
		a = Lstr()
		a = list(map(int, a))
		res = defaultdict(int)
		res[0] = 1
		c = 0
		s = 0
		for i in range(n):
			s = s + a[i]
			c += res[s - i - 1]
			res[s - i - 1] += 1
		print(c)
solution()


def comb(n, r):
	return n * (n - 1) // 2
a = int(input())
for i in range(0, a):
	b = int(input())
	s = input()
	arr = []
	for i in s:
		arr.append(int(i))
	summ = [0]
	for i in range(0, len(arr)):
		summ.append(summ[len(summ) - 1] + arr[i])
	fsumm = []
	for i in range(0, len(summ)):
		fsumm.append(summ[i] - i)
	s = set(fsumm)
	d = dict()
	for i in s:
		d[i] = 0
	for i in fsumm:
		d[i] = d[i] + 1
	summ = 0
	for i in d.keys():
		if not d[i] == 1:
			summ = summ + comb(d[i], 2)
	print(int(summ))


from collections import Counter
q = int(input())
for _ in range(q):
	(n, s) = (int(input()), input())
	dat = [0] * (n + 1)
	for i in range(n):
		dat[i + 1] = dat[i] + (int(s[i]) - 1)
	c = Counter(dat)
	res = 0
	for k in c.keys():
		if c[k] < 2:
			pass
		x = c[k] - 1
		res += x * (x + 1) // 2
	print(res)


gans = []
for _ in range(int(input())):
	n = int(input())
	u = list(map(int, list(input())))
	d = [0] * n
	mx = 10 * n + 1
	cnst = n
	cnt = [0] * mx
	ans = 0
	d[0] = u[0] - 1
	if d[0] == 0:
		ans += 1
	cnt[d[0] + cnst] += 1
	for i in range(1, n):
		d[i] = d[i - 1] + u[i] - 1
		ans += cnt[d[i] + cnst]
		if d[i] == 0:
			ans += 1
		cnt[d[i] + cnst] += 1
	gans.append(ans)
print('\n'.join(map(str, gans)))


t = int(input())
for tt in range(t):
	n = int(input())
	s = input().strip()
	d = {}
	d[0] = 1
	pre = [0] * n
	ans = 0
	for i in range(n):
		pre[i] = int(s[i])
		if i > 0:
			pre[i] += pre[i - 1]
		xx = pre[i] - (i + 1)
		if xx in d:
			ans += d[xx]
		if xx in d:
			d[pre[i] - (i + 1)] += 1
		else:
			d[xx] = 1
	print(ans)


def good_count(arr):
	prefix = [0]
	for num in arr:
		prefix.append(prefix[-1] + num)
	prefixMinusIdx = [prefix[i] - i for i in range(len(prefix))]
	counter = {}
	for val in prefixMinusIdx:
		if counter.get(val):
			counter[val] += 1
		else:
			counter[val] = 1
	ans = 0
	for count in counter.values():
		ans += count * (count - 1) // 2
	return ans
t = int(input())
for _ in range(t):
	n = int(input())
	arr = [*map(lambda ch: ord(ch) - ord('0'), input())]
	print(good_count(arr))


from collections import defaultdict
test = int(input())
for _ in range(test):
	n = int(input())
	arr = input()
	arr = [int(x) for x in arr]
	prefix = [0]
	memo = defaultdict(int)
	for x in arr:
		prefix.append(prefix[-1] + x)
	ans = 0
	for (index, value) in enumerate(prefix):
		ans += memo[value - index]
		memo[value - index] += 1
	print(ans)


def solve(b, m):
	p_dict = {0: 1}
	x = 0
	for i in range(m):
		x += b[i] - 1
		if x in p_dict:
			p_dict[x] += 1
		else:
			p_dict[x] = 1
	r = sum([v * (v - 1) // 2 for (k, v) in p_dict.items()])
	return r
t = int(input())
for case in range(t):
	n = int(input())
	a = [int(x) for x in list(input())]
	print(solve(a, n))


for _ in range(int(input())):
	n = int(input())
	a = input()
	d = {0: 1}
	ss = 0
	for i in range(n):
		ss += int(a[i])
		k = ss - (i + 1)
		if k not in d:
			d[k] = 1
		else:
			d[k] += 1
	ss = 0
	for i in d:
		ss += d[i] * (d[i] - 1) // 2
	print(ss)


from collections import Counter
T = int(input())

def ans(A):
	A = list(map(int, A))
	B = [0]
	for a in A:
		B += [B[-1] + a - 1]
	cB = Counter(B)
	ret = 0
	for k in cB:
		v = cB[k]
		ret += v * (v - 1) // 2
	return ret
for t in range(T):
	input()
	print(ans(input()))


import sys
T = int(sys.stdin.readline().strip())
for t in range(0, T):
	n = int(sys.stdin.readline().strip())
	a = sys.stdin.readline().strip()
	b = [int(a[i]) - 1 for i in range(0, n)]
	ans = 0
	s = [0] * (10 * n + 1)
	s[0 + n] = 1
	c = 0
	for i in range(0, n):
		c = c + b[i]
		ans = ans + s[c + n]
		s[c + n] = s[c + n] + 1
	print(ans)


import sys
import bisect
t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	ans = 0
	curr = 0
	d = {}
	pref = [0] * (n + 1)
	for i in range(n):
		pref[i + 1] = int(s[i]) + pref[i]
	for i in range(n):
		d[i - pref[i] - 1] = d.get(i - pref[i] - 1, 0) + 1
		ans += d.get(i - pref[i + 1], 0)
	print(ans)


from collections import *
from math import *

def count(arr, n, k, sumV):
	cnt = 0
	curr_sum = 0
	for i in range(0, k):
		curr_sum += arr[i]
	if curr_sum == sumV:
		cnt += 1
	for j in range(k, n):
		curr_sum = curr_sum + arr[j] - arr[j - k]
		if curr_sum == sumV:
			cnt += 1
	return cnt
for _ in range(int(input())):
	n = int(input())
	a = input()
	mp = defaultdict(int)
	s = 0
	mp[0] += 1
	for i in range(n):
		s += int(a[i])
		mp[s - i - 1] += 1
	ans = 0
	for m in mp:
		ans += mp[m] * (mp[m] - 1) // 2
	print(ans)


t = int(input())
while t:
	t -= 1
	n = int(input())
	s = input()
	l = [int(i) for i in s]
	pre = 0
	d = dict()
	d[0] = 1
	for i in range(n):
		pre += l[i]
		temp = pre - (i + 1)
		if temp not in d:
			d[temp] = 1
		else:
			d[temp] += 1
	ans = 0
	for i in d:
		ans += d[i] * (d[i] - 1) // 2
	print(ans)


for _ in range(int(input())):
	m = int(input())
	lst = [int(c) for c in input()]
	(a, p, q) = (0, {0: 1}, 0)
	for elem in lst:
		q += elem - 1
		if q in p:
			a += p[q]
		else:
			p[q] = 0
		p[q] += 1
	print(a)


from sys import stdin
from itertools import accumulate
from collections import Counter
for i in range(int(stdin.readline())):
	_ = int(stdin.readline())
	a = [int(k) - 1 for k in stdin.readline().rstrip()]
	print(sum((k * (k - 1) // 2 for k in Counter([0] + list(accumulate(a))).values())))


from collections import Counter

def read_int():
	return int(input().strip())

def read_ints():
	return list(map(int, input().strip().split(' ')))

def solve():
	N = read_int()
	A = input()
	count = Counter({0: 1})
	c = 0
	answer = 0
	for a in A:
		c += int(a)
		c -= 1
		answer += count[c]
		count[c] += 1
	return answer
T = read_int()
for _ in range(T):
	print(solve())


def fact(x):
	if x == 0:
		return 1
	else:
		return x * fact(x - 1)

def lower_bound(li, num):
	answer = len(li) - 1
	start = 0
	end = len(li) - 1
	while start <= end:
		middle = (end + start) // 2
		if li[middle] >= num:
			answer = middle
			end = middle - 1
		else:
			start = middle + 1
	return answer

def upper_bound(li, num):
	answer = -1
	start = 0
	end = len(li) - 1
	while start <= end:
		middle = (end + start) // 2
		if li[middle] <= num:
			answer = middle
			start = middle + 1
		else:
			end = middle - 1
	return answer

def abs(x):
	return x if x >= 0 else -x

def binary_search(li, val, lb, ub):
	ans = 0
	while lb <= ub:
		mid = (lb + ub) // 2
		if li[mid] > val:
			ub = mid - 1
		elif val > li[mid]:
			lb = mid + 1
		else:
			ans = 1
			break
	return ans

def sieve_of_eratosthenes(n):
	ans = []
	arr = [1] * (n + 1)
	(arr[0], arr[1], i) = (0, 0, 2)
	while i * i <= n:
		if arr[i] == 1:
			j = i + i
			while j <= n:
				arr[j] = 0
				j += i
		i += 1
	for k in range(n):
		if arr[k] == 1:
			ans.append(k)
	return ans

def nc2(x):
	if x == 1:
		return 0
	else:
		return x * (x - 1) // 2

def kadane(x):
	sum_so_far = 0
	current_sum = 0
	for i in x:
		current_sum += i
		if current_sum < 0:
			current_sum = 0
		else:
			sum_so_far = mpos(sum_so_far, current_sum)
	return sum_so_far

def mex(li):
	check = [0] * 1001
	for i in li:
		check[i] += 1
	for i in range(len(check)):
		if check[i] == 0:
			return i

def sumdigits(n):
	ans = 0
	while n != 0:
		ans += n % 10
		n //= 10
	return ans

def product(li):
	ans = 1
	for i in li:
		ans *= i
	return ans

def mpospower(n, k):
	cnt = 0
	while n > 1:
		cnt += 1
		n //= k
	return cnt

def knapsack(li, sumi, s, n, dp):
	if s == sumi // 2:
		return 1
	elif n == 0:
		return 0
	elif dp[n][s] != -1:
		return dp[n][s]
	else:
		if li[n - 1] <= s:
			dp[n][s] = knapsack(li, sumi, s - li[n - 1], n - 1, dp) or knapsack(li, sumi, s, n - 1, dp)
		else:
			dp[n][s] = knapsack(li, sumi, s, n - 1, dp)
		return dp[n][s]

def pref(li):
	pref_sum = [0]
	for i in li:
		pref_sum.append(pref_sum[-1] + i)
	return pref_sum
from math import *
for _ in range(int(input())):
	n = int(input())
	s = input()
	a = [int(x) for x in list(s)]
	pre = pref(a)
	pos = [i for i in range(n + 1)]
	di = {}
	for i in range(n + 1):
		di[pos[i] - pre[i]] = 1 if pos[i] - pre[i] not in di else di[pos[i] - pre[i]] + 1
	ans = 0
	for each in di:
		ans += di[each] * (di[each] - 1) // 2
	print(ans)


for _ in range(int(input())):
	w = int(input())
	s = input() + '0'
	A = []
	z = 0
	for i in range(len(s)):
		A.append(z)
		z += int(s[i]) - 1
	A.sort()
	Ans = 0
	q = 1
	for i in range(1, len(A)):
		if A[i] == A[i - 1]:
			q += 1
		else:
			Ans += (q - 1) * q // 2
			q = 1
	Ans += (q - 1) * q // 2
	print(Ans)


t = int(input())
for te in range(t):
	n = int(input())
	s = '0' + input()
	psum = [0]
	for i in range(1, n + 1):
		x = psum[i - 1] + int(s[i])
		psum.append(x)
	cnt = {}
	cnt[0] = 1
	ans = 0
	for i in range(1, n + 1):
		if psum[i] - i in cnt:
			ans += cnt[psum[i] - i]
			cnt[psum[i] - i] += 1
		else:
			cnt[psum[i] - i] = 1
	print(ans)


from collections import Counter
from sys import stdin, gettrace
from itertools import accumulate
if gettrace():
	inputi = input
else:

	def input():
		return next(stdin)[:-1]

	def inputi():
		return stdin.buffer.readline()

def solve():
	n = int(input())
	aa = [int(a) - 1 for a in input()]
	aasc = Counter(accumulate(aa))
	aasc[0] += 1
	res = 0
	for c in aasc.values():
		res += c * (c - 1) // 2
	print(res)

def main():
	t = int(input())
	for _ in range(t):
		solve()
main()


cases = int(input())
for i in range(cases):
	n = int(input())
	s = input()
	d = {0: 1}
	prefixsum = 0
	ans = 0
	for j in range(n):
		prefixsum += int(s[j])
		check = prefixsum - j - 1
		if check in d:
			d[check] += 1
		else:
			d[check] = 1
	for val in d:
		v = d[val]
		ans += int(v * (v - 1) / 2)
	print(ans)


for q in range(int(input())):
	input()
	numList = []
	for i in input():
		numList.append(int(i) - 1)
	sumDict = {0: 1}
	tally = 0
	subLists = 0
	for i in numList:
		tally += int(i)
		if tally in sumDict:
			subLists += sumDict[tally]
			sumDict[tally] += 1
		else:
			sumDict[tally] = 1
	print(str(subLists) + '\n')


from sys import stdin
from collections import deque
mod = 10 ** 9 + 7
import sys
import random
sys.setrecursionlimit(10 ** 6)
from queue import PriorityQueue
from collections import Counter as cc
from bisect import bisect_right
from bisect import bisect_left
from collections import defaultdict
from math import sqrt, factorial, gcd, log2, inf, ceil
import heapq
from collections import deque
mod = 10 ** 9 + 7
import sys
import random
sys.setrecursionlimit(10 ** 6)
from queue import PriorityQueue
from collections import Counter as cc
from bisect import bisect_right
from bisect import bisect_left
from collections import defaultdict
from math import sqrt, factorial, gcd, log2, inf, ceil
import heapq
input = lambda : sys.stdin.readline().rstrip()
from sys import stdin, stdout
from heapq import heapify, heappush, heappop
from itertools import permutations
from math import factorial as f

def ncr(n, r, p):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p
import sys
from sys import stdin, stdout
from heapq import heapify, heappush, heappop
from itertools import permutations
from math import factorial as f

def ncr(n, r, p):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p
import sys
t = int(input())
for _ in range(t):
	hash = defaultdict(int)
	n = int(input())
	s = list(input())
	for i in range(n):
		s[i] = int(s[i])
	sum = 0
	ans = 0
	for i in range(n):
		sum += s[i]
		ans += hash[sum - (i + 1)]
		if sum - (i + 1) != 0:
			hash[sum - (i + 1)] += 1
		else:
			ans += 1
			hash[sum - (i + 1)] += 1
	print(ans)


def countPairs(arr, n):
	map = dict()
	for i in range(n):
		map[arr[i] - i] = map.get(arr[i] - i, 0) + 1
	res = 0
	for x in map:
		cnt = map[x]
		res += cnt * (cnt - 1) // 2
	if 1 in map:
		return res + map[1]
	else:
		return res
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(input())
	for i in range(n):
		a[i] = int(a[i])
	prefix = [a[0]]
	for i in range(1, n):
		prefix.append(prefix[-1] + a[i])
	print(countPairs(prefix, n))


import sys
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())

def solve(a, n):
	d = {0: 1}
	(res, s) = (0, 0)
	for i in range(n):
		s += int(a[i])
		x = s - i - 1
		if x not in d:
			d[x] = 0
		d[x] += 1
		res += d[x] - 1
	return res
t = inp()
while t > 0:
	n = inp()
	s = input()
	print(solve(s, n))
	t -= 1


import sys
from itertools import accumulate
from collections import Counter

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 19
MOD = 10 ** 9 + 7
for _ in range(INT()):
	N = INT()
	A = [a - 1 for a in list(map(int, input()))]
	acc = [0] + list(accumulate(A))
	C = Counter(acc)
	ans = 0
	for v in C.values():
		ans += v * (v - 1) // 2
	print(ans)


def scanInt():
	return int(input())

def scanList():
	return [i for i in input()]

def scanIntList():
	return [int(i) for i in scanList()]
t = int(input())
for _ in range(t):
	n = scanInt()
	a = scanIntList()
	d = {1: 0}
	sum = 0
	ans = 0
	for i in range(len(a)):
		sum += a[i]
		if sum - i not in d.keys():
			d[sum - i] = 0
		d[sum - i] += 1
	for i in d.keys():
		x = d[i]
		ans += x * (x - 1) // 2
	print(ans + d[1])


from collections import defaultdict
t = int(input())
for i in range(t):
	n = int(input())
	arr = [int(d) for d in input()]
	prevsum = defaultdict(lambda : 0)
	currsum = 0
	count = 0
	for i in arr:
		i -= 1
		currsum += i
		if currsum == 0:
			count += 1
		if currsum in prevsum:
			count += prevsum[currsum]
		prevsum[currsum] += 1
	print(count)


import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
import collections
from itertools import permutations
from collections import defaultdict
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
mod = 10 ** 9 + 7
for i in range(int(input())):
	n = int(input())
	l = list(input())
	for i in range(n):
		l[i] = int(l[i])
	dp = [0] * n
	end = defaultdict(int)
	end2 = defaultdict(int)
	if l[0] == 1:
		dp[0] = 1
	total = 1 - l[0]
	end[total] = 1
	for j in range(1, n):
		c = 1 - l[j]
		total += 1 - l[j]
		if end[total] != 0:
			dp[j] += dp[end[total] - 1] + 1
		if c == 0 and end[total] != j:
			dp[j] += 1
		if total == 0 and end[total] == 0:
			dp[j] += 1
		end[total] = j + 1
	print(sum(dp))


for i in range(int(input())):
	n = int(input())
	ar = [int(i) for i in input()]
	summ = 0
	d = {0: 1}
	ans = 0
	for i in range(n):
		summ += ar[i] - 1
		if summ in d:
			ans += d[summ]
			d[summ] += 1
		else:
			d[summ] = 1
	print(ans)


from collections import defaultdict

def solve():
	T = int(input())
	ans = [0] * T
	for t in range(T):
		d = defaultdict(lambda : 0)
		d[0] = 1
		N = int(input())
		A = list(map(int, list(input())))
		cum = [0] * (N + 1)
		for i in range(N):
			cum[i + 1] = cum[i] + A[i] - 1
			d[cum[i + 1]] += 1
		for v in d.values():
			ans[t] += v * (v - 1) // 2
	return ans
print(*solve(), sep='\n')


import os
import heapq
import sys, threading
import math
import bisect
import operator
from collections import defaultdict
sys.setrecursionlimit(10 ** 5)
from io import BytesIO, IOBase

def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)

def power(x, p, m):
	res = 1
	while p:
		if p & 1:
			res = res * x % m
		x = x * x % m
		p >>= 1
	return res

def inar():
	return [int(k) for k in input().split()]

def lcm(num1, num2):
	return num1 * num2 // gcd(num1, num2)

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		string = input()
		sm = 0
		dic = defaultdict(int)
		dic[0] = 1
		ans = 0
		for i in range(n):
			sm += int(string[i]) - 1
			if sm in dic:
				ans += dic[sm]
				dic[sm] += 1
			else:
				dic[sm] = 1
		print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


t = int(input())
for o in range(t):
	n = int(input())
	s = input()
	l = []
	for i in range(n):
		l.append(int(s[i]))
	overflow = {}
	overflow[0] = 1
	curSum = 0
	ans = 0
	for i in range(n):
		curSum += l[i]
		if curSum - i - 1 not in overflow:
			overflow[curSum - i - 1] = 0
		overflow[curSum - i - 1] += 1
	for key in overflow.keys():
		ans += overflow[key] * (overflow[key] - 1) / 2
	print(int(ans))


import collections
nb_tests = int(input())
for i in range(nb_tests):
	_ = input()
	a = [int(d) for d in list(input())]
	p = [0] * (len(a) + 1)
	m = collections.defaultdict(int)
	m[0] = 1
	total = 0
	for j in range(len(a)):
		p[j + 1] = p[j] + a[j]
		x = p[j + 1] - (j + 1)
		m[x] += 1
		total += m[x] - 1
	print(total)


for tc in range(int(input())):
	n = int(input())
	a = input()
	m = {0: 1}
	(res, cur) = (0, 0)
	for i in range(n):
		cur += int(a[i])
		x = cur - i - 1
		if x not in m:
			m[x] = 0
		res += m[x]
		m[x] += 1
	print(res)


t = int(input())
for you in range(t):
	n = int(input())
	l = input()
	hashi = dict()
	hashi[0] = 1
	count = 0
	sumi = 0
	for i in range(n):
		sumi = sumi + int(l[i])
		z = sumi - (i + 1)
		if z in hashi:
			count = count + hashi[z]
			hashi[z] += 1
		else:
			hashi[z] = 1
	print(count)


for t in range(int(input())):
	n = int(input())
	a = input()
	dp = {0: 1}
	count = 0
	ans = 0
	for i in range(n):
		count += int(a[i])
		temp = count - (i + 1)
		if temp not in dp:
			dp[temp] = 1
		else:
			ans += dp[temp]
			dp[temp] += 1
	print(ans)


from itertools import accumulate
from collections import defaultdict
import sys
input = sys.stdin.readline
t = int(input())

def solve():
	n = int(input())
	d = defaultdict(int)
	arr = list(map(int, list(input().strip())))
	cum = list(accumulate(arr, initial=0))
	for i in range(0, len(cum)):
		d[cum[i] - i] += 1
	ans = 0
	for v in d.values():
		ans += v * (v - 1) // 2
	print(ans)
for i in range(t):
	solve()


import sys
input = sys.stdin.readline
for kek in range(int(input())):
	n = int(input())
	a = list(map(int, input()[:-1]))
	x = 0
	s = dict()
	for i in range(n + 1):
		if x - i in s:
			s[x - i] += 1
		else:
			s[x - i] = 1
		if i >= n:
			break
		x += a[i]
	ans = 0
	for i in s:
		ans += s[i] * (s[i] - 1) // 2
	print(ans)


from collections import Counter
import sys
input = sys.stdin.readline
T = int(input())

def psum(l):
	res = [0] * (len(l) + 1)
	for i in range(1, len(res)):
		res[i] = res[i - 1] + l[i - 1] - 1
	return res
for t in range(T):
	N = int(input())
	s = input()[:-1]
	A = [int(_) for _ in s]
	cc = Counter(psum(A))
	answer = 0
	for (_, c) in cc.most_common():
		if c > 1:
			answer += c * (c - 1) / 2
	print(int(answer))


from collections import Counter

def goodSubarrays(A):
	ans = 0
	prefix = 0
	prevsum = Counter()
	prevsum[0] += 1
	for i in range(len(A)):
		prefix += A[i]
		ans += prevsum[prefix - i - 1]
		prevsum[prefix - i - 1] += 1
	return ans
test_cases = int(input())
for _ in range(test_cases):
	len_ = input()
	test_case = input()
	test_case = [int(c) for c in test_case]
	print(goodSubarrays(test_case))


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, list(input())))
	(ans, itr) = (0, 0)
	sums = dict()
	for i in range(n):
		back_l = itr - i
		try:
			sums[back_l] += 1
		except:
			sums[back_l] = 1
		back_l = itr + l[i] - i - 1
		if back_l in sums:
			ans += sums[back_l]
		itr += l[i]
	print(ans)


def solve(n, ar):
	d = {0: 1}
	(res, s) = (0, 0)
	for i in range(n):
		s += int(ar[i])
		x = s - i - 1
		if x not in d:
			d[x] = 0
		d[x] += 1
		res += d[x] - 1
	print(res)
t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	solve(n, s)


a = input()
for i in range(int(a)):
	x = input()
	A = list(input())
	C = {0: 1}
	t = 0
	for j in range(len(A)):
		t = int(A[j]) - 1 + t
		if t in C:
			C[t] += 1
		else:
			C[t] = 1
	k = 0
	for j in C:
		k += C[j] * (C[j] - 1) / 2
	print(int(k))


from sys import stdin, stdout
from math import gcd, sqrt, factorial
from collections import deque, defaultdict
input = stdin.readline
R = lambda : map(int, input().split())
I = lambda : int(input())
S = lambda : input().rstrip('\n')
L = lambda : list(R())
P = lambda x: stdout.write(x)
lcm = lambda x, y: x * y // gcd(x, y)
hg = lambda x, y: (y + x - 1) // x * x
pw = lambda x: 0 if x == 1 else 1 + pw(x // 2)
chk = lambda x: chk(x // 2) if not x % 2 else True if x == 1 else False
sm = lambda x: (x ** 2 + x) // 2
N = 10 ** 9 + 7
for _ in range(I()):
	(s, res) = (0, 0)
	d = {0: 1}
	I()
	for (i, j) in enumerate(map(int, S())):
		s += j
		x = s - i - 1
		if x not in d:
			d[x] = 0
		d[x] += 1
		res += d[x] - 1
	print(res)


from collections import defaultdict

def func(a):
	culmulative = 0
	dicto = defaultdict(int, {0: 1})
	for i in range(len(a)):
		culmulative += int(a[i])
		pr_r = culmulative - (i + 1)
		dicto[pr_r] += 1
	output = 0
	for i in dicto.values():
		output += (i - 1) * i // 2
	return output

def main():
	for i in range(int(input())):
		input()
		print(func(input()))
main()


for _ in range(int(input())):
	n = int(input())
	arr = [int(k) - 1 for k in input()]
	for i in range(1, len(arr)):
		arr[i] += arr[i - 1]
	d = {0: 1}
	ans = 0
	for i in arr:
		if i in d:
			d[i] += 1
		else:
			d[i] = 1
		ans += d[i] - 1
	print(ans)


from collections import defaultdict as dd
from sys import stdin
stdin.readline

def mp():
	return list(map(int, stdin.readline().strip().split()))

def it():
	return int(stdin.readline().strip())
for _ in range(it()):
	n = it()
	l = input()
	v = []
	for i in l:
		v.append(int(i))
	ans = 0
	d = dd(lambda : 0)
	d[1] += 1
	s = 0
	for i in range(n):
		s += v[i]
		ans += d[s - i]
		d[s - i] += 1
	print(ans)


Q = int(input())
for q in range(Q):
	N = int(input())
	l = list(input())
	R = [int(l[0]) - 1]
	for i in range(1, N):
		R.append(R[-1] + int(l[i]) - 1)
	D = {}
	D[0] = 1
	ans = 0
	for i in range(N):
		if R[i] not in D:
			D[R[i]] = 1
		else:
			ans += D[R[i]]
			D[R[i]] += 1
	print(ans)


from sys import stdin
input = stdin.readline

def A():
	t = int(input())
	for _ in range(t):
		n = int(input())
		a = list(map(int, input().split()))
		a.sort()
		found = False
		if a[0] + a[1] <= a[n - 1]:
			print(1, 2, n)
		else:
			print(-1)

def B():
	from collections import deque
	t = int(input())
	for _ in range(t):
		s = [int(x) for x in input().rstrip()]
		d = deque()
		curr = s[0]
		streak = s[0]
		for i in range(1, len(s)):
			if s[i] != curr:
				if curr == 1:
					d.append(streak)
				curr = s[i]
				streak = 1
			elif curr == 1:
				streak += 1
		if curr == 1:
			d.append(streak)
		d = list(d)
		d.sort(reverse=True)
		tot = 0
		for i in range(0, len(d), 2):
			tot += d[i]
		print(tot)

def C():
	t = int(input())
	for _ in range(t):
		n = int(input())
		a = [int(x) - 1 for x in input().rstrip()]
		d = [0 for i in range(n * 10)]
		t = n
		d[t] = 1
		for i in range(n):
			t += a[i]
			d[t] += 1
		tot = 0
		for i in d:
			if i >= 2:
				tot += i * (i - 1) // 2
		print(tot)

def D():
	t = int(input())
	for _ in range(t):
		print(0)
C()


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	s = input()
	ans = 0
	d = defaultdict(lambda : 0)
	su = 0
	d[0] = 1
	for i in range(n):
		su += int(s[i])
		x = su - i - 1
		d[x] += 1
		ans += d[x] - 1
	print(ans)


n = int(input())
for i in range(n):
	m = int(input())
	s = input()
	a = []
	for c in s:
		a.append(int(c))
	cum = []
	cum.append(0)
	for j in range(1, len(a) + 1):
		cum.append(cum[j - 1] + int(a[j - 1]))
	for j in range(len(cum)):
		cum[j] = cum[j] - j
	b = []
	cum.sort()
	cum.append(-1)
	tmp = 1
	for j in range(len(cum) - 1):
		if cum[j] != cum[j + 1]:
			b.append(tmp)
			tmp = 1
		else:
			tmp += 1
	summ = 0
	for j in range(len(b)):
		summ += b[j] * (b[j] - 1) / 2
	print(int(summ))


def solve(n):
	s = input()
	ans = [0] * (n + 1)
	ans[0] = int(s[0])
	for i in range(1, n):
		ans[i] = int(s[i]) + ans[i - 1]
	sums = {}
	for i in range(n):
		sums[ans[i] - i] = sums.get(ans[i] - i, 0) + 1
	answer = 0
	for i in sums:
		answer += sums[i] * (sums[i] - 1) // 2
	for i in range(n):
		if ans[i] == i + 1:
			answer += 1
	print(answer)
for _ in range(int(input())):
	n = int(input())
	solve(n)


for s in [*open(0)][2::2]:
	g = {0: 1}
	c = a = i = 0
	for z in s[:-1]:
		c += int(z)
		i += 1
		f = g.get(c - i, 0)
		a += f
		g[c - i] = f + 1
	print(a)


ncases = int(input())
for tcase in range(ncases):
	n = int(input())
	nums = list(map(int, [i for i in input()]))
	sums = {}
	sums[0] = 1
	current = 0
	for i in nums:
		current += i - 1
		try:
			sums[current] += 1
		except KeyError:
			sums[current] = 1
	total = 0
	for key in sums:
		total += sums[key] * (sums[key] - 1) // 2
	print(total)


def main():
	t = int(input())
	for i in range(t):
		n = int(input())
		s = input()
		sums = dict()
		sums[0] = 1
		ans = 0
		now = 0
		for i in range(n):
			a = int(s[i]) - 1
			now += a
			if now in sums:
				ans += sums[now]
			if now in sums:
				sums[now] += 1
			else:
				sums[now] = 1
		print(ans)
main()


for _ in range(int(input())):
	length = int(input())
	array = list(map(int, list(input())))
	pi = [0, array[0]]
	for i in range(2, length + 1):
		pi += [pi[i - 1] + array[i - 1]]
	pi_i = [pi[i] - i for i in range(length + 1)]
	equal_pi_i = {}
	for i in range(length + 1):
		if pi_i[i] not in equal_pi_i:
			equal_pi_i[pi_i[i]] = 1
		else:
			equal_pi_i[pi_i[i]] += 1
	solution = 0
	for value in equal_pi_i.values():
		solution += (value - 1) * value / 2
	print(int(solution))


from collections import defaultdict
t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	b = []
	for j in s:
		b.append(int(j))
	j = 0
	d = defaultdict(lambda : 0)
	curr = 0
	ans = 0
	while j < n:
		curr += b[j]
		des = j + 1 - curr
		ans += d[des]
		if des == 0:
			ans += 1
		d[des] += 1
		j += 1
	print(ans)


tc = int(input())
while tc:
	n = int(input())
	s = input()
	pre = []
	pre.append(0)
	my_dic = {}
	ans = 0
	sm = 0
	for i in range(1, n + 1):
		if pre[i - 1] - i + 1 in my_dic.keys():
			my_dic[pre[i - 1] - i + 1] = my_dic[pre[i - 1] - i + 1] + 1
		else:
			my_dic[pre[i - 1] - i + 1] = 1
		v = int(s[i - 1])
		sm += v
		pre.append(sm)
		tmp = sm - i
		if tmp in my_dic.keys():
			ans += my_dic[tmp]
	print(ans)
	tc -= 1


import sys
import collections as cc
f = lambda x: int(x) - 1
I = lambda : list(map(int, input().split()))
for tc in range(int(input())):
	(n,) = I()
	s = input()
	l = [int(i) - 1 for i in s]
	f = cc.defaultdict(int)
	ans = 0
	su = 0
	f[0] = 1
	for i in range(n):
		su += l[i]
		ans += f[su]
		f[su] += 1
	print(ans)


T = int(input())
for _ in range(T):
	N = int(input())
	a = list(map(int, list(input())))
	d = {1: 1}
	cnt = 0
	s = 0
	for i in range(N):
		s += a[i]
		tmp = d.get(s - i, 0)
		cnt += tmp
		d[s - i] = tmp + 1
	print(cnt)


import sys
input = sys.stdin.readline
from collections import *
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, list(input()[:-1])))
	cnt = defaultdict(int)
	cnt[0] = 1
	now = 0
	ans = 0
	for i in range(1, n + 1):
		now += a[i - 1]
		ans += cnt[now - i]
		cnt[now - i] += 1
	print(ans)


def solve():
	n = int(input())
	a = list(map(int, list(input())))
	p = {0: 1}
	ans = s = 0
	for x in range(n):
		s += a[x]
		y = s - x - 1
		if y not in p:
			p[y] = 0
		p[y] += 1
		ans += p[y] - 1
	print(ans)
t = int(input())
for x in range(t):
	solve()


import random

def myfunc(a, n):
	s = set()
	freq = {0: 1}
	rs = 0
	res = 0
	for i in range(n):
		rs += a[i] - 1
		if rs in freq:
			res += freq[rs]
		if rs in freq:
			freq[rs] += 1
		else:
			freq[rs] = 1
	return res
t = int(input())
for tt in range(t):
	n = int(input())
	a = [int(x) for x in input()]
	print(myfunc(a, n))


from sys import stdin, stdout
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	a = [int(ch) - 1 for ch in input()]
	s = ans = 0
	d = {0: 1}
	for v in a:
		s += v
		ans += d.get(s, 0)
		d[s] = d.get(s, 0) + 1
	print(ans)


t = int(input())

def goodarrays(s, n):
	arr = [int(s[i]) - 1 for i in range(n)]
	(dp, H) = ({-1: 0}, {0: -1})
	(Sum, Count) = (0, 0)
	for i in range(n):
		Sum += arr[i]
		if Sum in H:
			dp[i] = 1 + dp[H[Sum]]
		else:
			dp[i] = 0
		(H[Sum], Count) = (i, Count + dp[i])
	return Count
for i in range(t):
	N = int(input())
	string = input()
	print(goodarrays(string, N))


t = int(input())
for tt in range(t):
	n = int(input())
	s = input()
	a = []
	for i in range(n):
		a.append(int(s[i]))
	p = [0 for i in range(n)]
	p[0] = a[0]
	for i in range(1, n):
		p[i] = p[i - 1] + a[i]
	cnt = {0: 1}
	ans = 0
	for i in range(n):
		x = p[i] - i - 1
		if x not in cnt:
			cnt[x] = 0
		cnt[x] += 1
		ans += cnt[x] - 1
	print(ans)


from itertools import accumulate
from collections import defaultdict
for t in range(int(input())):
	n = int(input())
	v = input().strip()
	di = defaultdict(int)
	val = [int(c) for c in v]
	acc = list(accumulate(val))
	per = [acc[i] - i for i in range(n)]
	res = 0
	for ee in reversed(per):
		res += di[ee]
		di[ee] += 1
	res += di[1]
	print(res)


from sys import stdin, stdout
import math, queue, heapq
fastinput = stdin.readline
fastout = stdout.write
from collections import defaultdict

def findSubarraySum(arr, n, Sum):
	prevSum = defaultdict(lambda : 0)
	res = 0
	currsum = 0
	for i in range(0, n):
		currsum += arr[i]
		if currsum == Sum:
			res += 1
		if currsum - Sum in prevSum:
			res += prevSum[currsum - Sum]
		prevSum[currsum] += 1
	return res
t = int(fastinput())
while t:
	t -= 1
	arr = []
	n = int(input())
	s = input()
	for i in s:
		arr.append(int(i) - 1)
	print(findSubarraySum(arr, n, 0))


from collections import defaultdict
t = int(input())
for _ in range(t):
	n = int(input())
	counts = defaultdict(int)
	counts[0] = 1
	total = 0
	currSum = 0
	a = list(map(int, input()))
	for v in a:
		currSum += v - 1
		total += counts[currSum]
		counts[currSum] += 1
	print(total)


import sys
import math, bisect
from collections import defaultdict
from itertools import groupby, accumulate
from heapq import heapify, heappop, heappush
from collections import deque, Counter, OrderedDict

def neo():
	return map(int, input().split())

def Neo():
	return list(map(int, input().split()))
for _ in range(int(input())):
	N = int(input())
	s = input()
	A = [int(i) for i in s]
	(Ans, tot) = (0, 0)
	Z = {0: 1}
	for i in range(1, N + 1):
		tot += A[i - 1]
		p = tot - i
		q = i - tot
		Ans += Z.get(p, 0)
		if Z.get(-q, 0):
			Z[-q] += 1
		else:
			Z[-q] = 1
	print(Ans)


M = int(input())
for i in range(M):
	N = int(input())
	s = input()
	tc = 0
	ts = 0
	prevc = {0: 1}
	for ch in s:
		ts += int(ch) - 1
		if ts in prevc:
			tc += prevc[ts]
			prevc[ts] += 1
		else:
			prevc[ts] = 1
	print(tc)


for sd in range(int(input())):
	n = int(input())
	s = input()
	p = [int(s[i]) for i in range(n)]
	p.reverse()
	p.append(0)
	p.reverse()
	f = dict()
	for i in range(1, n + 1):
		p[i] += p[i - 1]
	for i in range(n + 1):
		p[i] -= i - 1
	for i in range(n + 1):
		c = p[i]
		if c in f:
			f[c] += 1
		else:
			f[c] = 1
	m = 0
	for elem in f:
		c = f[elem]
		m += c * (c - 1)
	print(m // 2)


MOD = 1000000007
ii = lambda : int(input())
si = lambda : input()
dgl = lambda : list(map(int, input()))
f = lambda : map(int, input().split())
il = lambda : list(map(int, input().split()))
ls = lambda : list(input())
from collections import defaultdict
for _ in range(ii()):
	n = ii()
	a = si()
	k = []
	for i in range(0, n):
		k.append(int(a[i]) - 1)
	ps = {}
	s = 0
	ans = 0
	for i in range(0, n):
		s = s + k[i]
		if s == 0:
			ans = ans + 1
		if s in ps:
			ans = ans + ps[s]
		if s not in ps:
			ps[s] = 1
		else:
			ps[s] = ps[s] + 1
	print(ans)


def subarray_sum(s, k, n):
	curr = 0
	m = {}
	ans = 0
	for i in range(n):
		curr += s[i]
		if curr == k:
			ans += 1
		if curr - k in m:
			ans += m[curr - k]
		m[curr] = 1 + m.get(curr, 0)
	return ans
for _ in range(int(input())):
	n = int(input())
	s = list(input())
	for i in range(n):
		s[i] = int(s[i]) - 1
	ans = subarray_sum(s, 0, n)
	print(ans)


import sys
sys.setrecursionlimit(10 ** 5)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep='\n')

def II():
	return int(sys.stdin.readline())

def MI():
	return map(int, sys.stdin.readline().split())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI(rows_number):
	return [LI() for _ in range(rows_number)]

def SI():
	return sys.stdin.readline()[:-1]
for _ in range(II()):
	n = II()
	cs = 0
	cnt = [0] * n * 10
	cnt[0] = 1
	ans = 0
	for (i, a) in enumerate(SI()):
		cs += int(a)
		cur = cs - (i + 1)
		ans += cnt[cur]
		cnt[cur] += 1
	print(ans)


def answer(A):
	n = len(A)
	A = list(A)
	for i in range(n):
		A[i] = int(A[i]) - 1
	count = 0
	d = {0: 1}
	s = 0
	for i in range(n):
		s += int(A[i])
		if s in d:
			count += d[s]
			d[s] += 1
		else:
			d[s] = 1
	return count
t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	print(answer(s))


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

def RLL():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def N():
	return int(input())

def print_list(l):
	print(' '.join(map(str, l)))
from collections import defaultdict as dc
from math import ceil, floor, sqrt
for _ in range(N()):
	n = N()
	a = list(map(int, input()))
	res = 0
	pre = [0] * 9 * n
	pre[0] = 1
	now = 0
	for i in range(n):
		now += a[i]
		tmp = now - (i + 1)
		res += pre[tmp]
		pre[tmp] += 1
	print(res)


for _ in range(int(input())):
	n = int(input())
	s = input()
	pref = [0]
	su = 0
	for x in s:
		su += int(x)
		pref.append(su)
	d = {}
	ans = 0
	for i in range(n + 1):
		if not d.get(pref[i] - i):
			d[pref[i] - i] = 1
		else:
			ans += d[pref[i] - i]
			d[pref[i] - i] += 1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input()))
	a = [0] + a
	d = {0: 1}
	ans = 0
	cursum = 0
	for z in range(1, n + 1):
		cursum += a[z]
		if cursum - z in d:
			ans += d[cursum - z]
			d[cursum - z] += 1
		else:
			d[cursum - z] = 1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input()]
	v = dict()
	v[1] = 1
	(s, ans) = (0, 0)
	for i in range(n):
		s += a[i]
		try:
			ans += v[s - i]
			v[s - i] += 1
		except KeyError:
			v[s - i] = 1
	print(ans)


import sys
import os.path
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
depth = 1000005
mod = 1000000007
lim = mod * mod
sys.setrecursionlimit(depth)

def mapinput():
	return map(int, input().split())

def listinput():
	return list(map(int, input().split()))
from math import inf, ceil, sqrt
from collections import defaultdict
for _ in range(int(input())):
	(n, s) = (int(input()), str(input()))
	d = defaultdict(lambda : 0)
	(ans, cur) = (0, 0)
	for i in range(1, n + 1):
		cur += int(s[i - 1])
		val = cur - i
		if not val:
			ans += 1
		ans += d[val]
		d[val] += 1
	print(ans)


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	s = input()
	ans = 0
	pref = [0 for x in range(n * 10 + 1)]
	curSUm = 0
	pref[0] = 1
	for i in range(n):
		curSUm += ord(s[i]) - ord('0')
		ans += pref[curSUm - (i + 1)]
		pref[curSUm - (i + 1)] += 1
	print(ans)


for t in range(int(input())):
	n = int(input())
	l = list(map(int, list(input())))
	ans = 0
	d = {0: 1}
	csl = 0
	for i in range(n):
		csl += l[i] - 1
		if csl in d:
			ans += d[csl]
		if csl in d:
			d[csl] += 1
		else:
			d[csl] = 1
	print(ans)


T = int(input())
for t in range(T):
	n = int(input())
	S = input()
	mp = {}
	arr = [0]
	for s in S:
		arr.append(arr[-1] + int(s) - 1)
	for a in arr:
		if a in mp:
			mp[a] += 1
		else:
			mp[a] = 1
	ans = 0
	for (k, v) in mp.items():
		ans += v * (v - 1) // 2
	print(ans)


from collections import Counter

def good_subarray(arr):
	res = 0
	pre = [arr[0]]
	for i in range(1, len(arr)):
		pre.append(pre[i - 1] + arr[i])
	for i in range(len(pre)):
		pre[i] -= i + 1
	new_d = Counter(pre) + Counter({0: 1})
	for key in new_d:
		res += new_d[key] * (new_d[key] - 1) // 2
	return res
t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	print(good_subarray([int(i) for i in s]))


from collections import defaultdict
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input()))
	cnt = defaultdict(int)
	pre = 0
	res = 0
	cnt[0] = 1
	for (i, x) in enumerate(a):
		pre += x
		res += cnt[pre - (i + 1)]
		cnt[pre - (i + 1)] += 1
	print(res)


import math
from decimal import *
import random
try:
	for _ in range(int(input())):
		d = {0: 1}
		n = int(input())
		arr = list(input())
		(ans, s) = (0, 0)
		for i in range(n):
			s += int(arr[i])
			tmp = s - i - 1
			if tmp not in d:
				d[tmp] = 0
			d[tmp] += 1
			ans += d[tmp] - 1
		print(ans)
except EOFError as e:
	pass


t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(i) - 1 for i in input()]
	freq = {0: 1}
	pre = 0
	for i in range(n):
		pre += arr[i]
		freq[pre] = freq.get(pre, 0) + 1
	ans = 0
	for i in freq:
		m = freq[i]
		ans += m * (m - 1) // 2
	print(ans)


from itertools import accumulate
for a in range(int(input())):
	n = int(input())
	num = [0] + list(accumulate([int(a) for a in input()]))
	dic = {}
	ans = 0
	for b in range(n + 1):
		val = num[b] - b
		if val not in dic:
			dic[val] = 0
		dic[val] += 1
	for key in dic:
		ans += dic[key] * (dic[key] - 1) // 2
	print(ans)


from collections import defaultdict
t = int(input())
for _ in range(t):
	dic = defaultdict(lambda : 0)
	dic[0] = 1
	n = int(input())
	l = list(input())
	for i in range(n):
		l[i] = int(l[i]) - 1
	s = 0
	res = 0
	for i in range(n):
		s += l[i]
		dic[s] += 1
		res += dic[s] - 1
	print(res)


def solve(n, a):
	d = {}
	answ = 0
	prefix = [0] * (n + 1)
	prefix[1] = a[0]
	for i in range(2, n + 1):
		prefix[i] = a[i - 1] + prefix[i - 1]
	for i in range(1, n + 1):
		left_part = i - prefix[i - 1]
		right_part = i - prefix[i] + 1
		if d.get(left_part, -1) == -1:
			d[left_part] = 1
		else:
			d[left_part] += 1
		answ += d.get(right_part, 0)
	print(answ)
t = int(input())
while t:
	n = int(input())
	a = [int(i) for i in input()]
	solve(n, a)
	t -= 1


(z, zz) = (input, lambda : list(map(int, z().split())))
(szz, dgraphs, mod) = (lambda : sorted(zz()), {}, 10 ** 9 + 7)
from string import *
from collections import *
from queue import *
from sys import *
from collections import *
from math import *
from heapq import *
from itertools import *
from bisect import *
from collections import Counter as cc
from math import factorial as f
from bisect import bisect as bs
from itertools import accumulate as ac

def lcd(xnum1, xnum2):
	return xnum1 * xnum2 // gcd(xnum1, xnum2)

def prime(x):
	p = ceil(x ** 0.5) + 1
	for i in range(2, p):
		if x % i == 0 and x != 2:
			return 0
	return 1

def dfs(u, visit, graph):
	visit[u] = True
	for i in graph[u]:
		if not visit[i]:
			dfs(i, visit, graph)
num = int(z())
for _ in range(num):
	n = int(z())
	l = list(ac(map(int, z())))
	ans = 0
	li = [1] + [0] * (9 * n)
	for i in range(n):
		ans += li[l[i] - i - 1]
		li[l[i] - i - 1] += 1
	print(ans)


testcase = int(input())
for index in range(testcase):
	size = int(input())
	temp = input()
	arr = []
	ans = 0
	for i in range(size):
		arr.append(int(temp[i]))
	diff = 0
	diff_arr = [0] * (size * 10 + 20)
	for i in range(size):
		diff_arr[diff] += 1
		diff += arr[i] - 1
		ans += diff_arr[diff]
	print(ans)


for _ in range(int(input())):
	n = int(input())
	s = input()
	m = {0: 1}
	(sm, ans) = (0, 0)
	for i in range(n):
		sm += int(s[i])
		diff = sm - (i + 1)
		if diff not in m:
			m[diff] = 0
		m[diff] += 1
		ans += m[diff] - 1
	print(ans)


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	s = input()
	dp = [int(s[0])]
	for i in range(1, n):
		dp.append(int(s[i]) + dp[i - 1])
	ans = 0
	for i in range(n):
		dp[i] -= i
	c = Counter(dp)
	for i in c.keys():
		if i == 1:
			ans += c[i]
		ans += c[i] * (c[i] - 1) // 2
	print(ans)


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	S = 0
	Count = defaultdict(int)
	Count[0] += 1
	for a in input():
		S += int(a) - 1
		Count[S] += 1
	print(sum([v * (v - 1) // 2 for v in Count.values()]))


from collections import defaultdict
t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	list1 = [int(x) - 1 for x in s]
	cursum = 0
	ans = 0
	dic = defaultdict(int)
	for i in range(n):
		cursum += list1[i]
		if cursum == 0:
			ans += 1
		if cursum - 0 in dic:
			ans += dic[cursum]
		dic[cursum] += 1
	print(ans)


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	string = input()
	px = [0]
	ans = 0
	m = defaultdict(int)
	for s in string:
		px.append(px[-1] + int(s))
	for (idx, el) in enumerate(px):
		ans += m[el - idx]
		m[el - idx] += 1
	print(ans)


t = int(input())
while t > 0:
	n = int(input())
	s = input()
	l = [int(i) for i in s]
	l1 = []
	l1.append(l[0])
	for i in range(1, len(l)):
		l1.append(l1[-1] + l[i])
	l1 = [0] + l1
	d = {}
	ans = 0
	for i in range(len(l1)):
		if l1[i] - i not in d:
			d[l1[i] - i] = 1
		else:
			d[l1[i] - i] += 1
	for i in d:
		ans = ans + d[i] * (d[i] - 1) // 2
	print(ans)
	t -= 1


def fs(arr, n):
	hashMap = {}
	out = 0
	s = 0
	for i in range(n):
		s += arr[i]
		if s == 0:
			out += 1
		bk = []
		if s in hashMap:
			bk = hashMap.get(s)
			out += len(bk)
		bk.append(i)
		hashMap[s] = bk
	return out
test = int(input())
for _ in range(test):
	n = int(input())
	arr = list(input())
	count = 0
	for i in range(n):
		a = int(arr[i])
		arr[i] = a - 1
	count = fs(arr, n)
	print(count)


list_ans = []
for _ in range(int(input())):
	n = int(input())
	dict1 = {}
	list1 = [0] + list((int(x) for x in input()))
	for i in range(1, n + 1):
		list1[i] = list1[i] + list1[i - 1]
	for i in range(n + 1):
		list1[i] -= i
	for x in list1:
		dict1[x] = dict1.get(x, 0) + 1
	ans = 0
	for s in dict1.values():
		ans += s * (s - 1) // 2
	list_ans.append(ans)
print(*list_ans, sep='\n')


for _ in range(int(input())):
	n = int(input())
	s = input()
	l = len(s)
	prefix_arr = [0] * l
	prefix_arr[0] = int(s[0]) - 1
	for i in range(1, l):
		prefix_arr[i] = prefix_arr[i - 1] + int(s[i]) - 1
	ans = 0
	count = 0
	d = dict()
	d[0] = 1
	for i in range(l):
		item = prefix_arr[i]
		if item in d:
			ans = ans + d[item]
			d[item] += 1
		else:
			d[item] = 1
	print(ans)


import sys, math, cmath, time, collections
from collections import deque, Counter, OrderedDict, defaultdict
from heapq import nsmallest, nlargest, heapify, heappop, heappush, heapreplace
from math import ceil, floor, log, log2, sqrt, gcd, factorial, pow, pi
from bisect import bisect_left, bisect_right
start_time = time.time()

def solve(a, n):
	d = {0: 1}
	(res, s) = (0, 0)
	for i in range(n):
		s += int(a[i])
		x = s - i - 1
		if x not in d:
			d[x] = 0
		d[x] += 1
		res += d[x] - 1
	print(res)

def main():
	n = int(input())
	arr = input()
	arr = [int(c) for c in arr]
	solve(arr, n)
LOCAL = False
if LOCAL:
	sys.stdin = open('data.in', 'r')
	sys.stdout = open('data.out', 'w')
testcases = int(input())
for i in range(testcases):
	main()
if LOCAL:
	print(f'Time Elapsed: {time.time() - start_time} seconds')
	sys.stdout.close()


def fact(n):
	f = 1
	for i in range(1, n + 1):
		f *= i
	return f

def ncr(n, r):
	if r > n:
		return 0
	if n - r < r:
		r = n - r
	f = 1
	for i in range(n, n - r, -1):
		f *= i
	return f // fact(r)

def subarrays(arr):
	counts = {0: 1}
	summ = 0
	for i in range(len(arr)):
		summ += arr[i]
		counts[summ - i - 1] = counts.get(summ - i - 1, 0) + 1
	arrs = 0
	for r in counts.values():
		arrs += ncr(r, 2)
	return arrs
t = int(input())
for i in range(t):
	n = int(input())
	s = [int(i) for i in input()]
	print(subarrays(s))


[(lambda N, n, s, d: (lambda ss, ds: ([(ss(0, s[0] + i - 1), ds(s[0], d.get(s[0], 0) + 1)) for i in n], print(sum((d[k] * (d[k] - 1) >> 1 for k in d)))))(s.__setitem__, d.__setitem__))(int(input()), map(int, input()), [0], {0: 1}) for t in range(int(input()))]


def good(arr):
	d = {0: 1}
	s = 0
	ans = 0
	for i in range(0, len(arr)):
		s += arr[i]
		x = s - i - 1
		d[x] = d.get(x, 0) + 1
		ans += d[x] - 1
	return ans
for i in range(int(input())):
	a = input()
	lst = [int(i) for i in input()]
	print(good(lst))


import io
import os
from collections import Counter, defaultdict, deque

def solve(N, A):
	pref = [0]
	for x in A:
		pref.append(pref[-1] + x)
	freq = Counter()
	for i in range(N + 1):
		freq[pref[i] - i] += 1
	return sum((v * (v - 1) // 2 for v in freq.values()))
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
TC = int(input())
for tc in range(1, TC + 1):
	(N,) = [int(x) for x in input().split()]
	A = [int(c) for c in input().decode().rstrip()]
	ans = solve(N, A)
	print(ans)


import sys
import math
import bisect
import heapq
from bisect import bisect_right
from sys import stdin, stdout

def get_ints_in_variables():
	return map(int, sys.stdin.readline().strip().split())

def get_int():
	return int(sys.stdin.readline())

def get_ints_in_list():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_list_of_list(n):
	return [list(map(int, sys.stdin.readline().strip().split())) for _ in range(n)]

def get_string():
	return sys.stdin.readline().strip()

def myceil(x, y):
	return (x + y - 1) // y

def Solution(arr, n):
	hm = {}
	hm[0] = 1
	sm = 0
	ans = 0
	for i in range(n):
		sm += arr[i]
		if sm in hm:
			ans += hm[sm]
		else:
			hm[sm] = 0
		hm[sm] += 1
	print(ans)

def main():
	for _ in range(get_int()):
		n = get_int()
		arr = [int(c) - 1 for c in get_string()]
		Solution(arr, n)
main()


import sys
import math
import collections
import operator as op
from collections import deque
from math import gcd, inf, sqrt, pi, cos, sin, ceil, log2
from bisect import bisect_right, bisect_left
from functools import reduce
from sys import stdin, stdout, setrecursionlimit
setrecursionlimit(2 ** 20)

def factorial(n):
	if n == 0:
		return 1
	return n * factorial(n - 1)

def ncr(n, r):
	r = min(r, n - r)
	numer = reduce(op.mul, range(n, n - r, -1), 1)
	denom = reduce(op.mul, range(1, r + 1), 1)
	return numer // denom

def prime_factors(n):
	i = 2
	factors = []
	while i * i <= n:
		if n % i:
			i += 1
		else:
			n //= i
			factors.append(i)
	if n > 1:
		factors.append(n)
	return list(factors)

def sumDigits(no):
	return 0 if no == 0 else int(no % 10) + sumDigits(int(no / 10))
MOD = 1000000007
PMOD = 998244353
N = 10 ** 5
T = 1
T = int(stdin.readline())
for _ in range(T):
	n = int(stdin.readline())
	a = list(stdin.readline().strip('\n'))
	pref = [0] * (n + 1)
	for i in range(1, n + 1):
		pref[i] = pref[i - 1] + int(a[i - 1])
	d = {}
	d[0] = 1
	ans = 0
	for i in range(1, n + 1):
		if pref[i] - i in d:
			ans += d[pref[i] - i]
		else:
			d[pref[i] - i] = 0
		d[pref[i] - i] += 1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	s = input()
	p = [int(s[0])]
	for i in range(1, len(s)):
		p.append(p[i - 1] + int(s[i]))
	c = {}
	for i in range(len(s)):
		p[i] -= i
		if p[i] in c:
			c[p[i]] += 1
		else:
			c[p[i]] = 1
	f = 0
	for d in c:
		if c[d] != 1:
			f += c[d] * (c[d] - 1) // 2
	print(f + (0 if 1 not in c else c[1]))


test_cases = int(input())
for test_case in range(test_cases):
	n = int(input())
	s = input()
	sum = []
	count = 0
	ans = 0
	a = list(map(int, list(s)))
	for i in range(n):
		a[i] = a[i] - 1
		count += a[i]
		sum.append(count)
	d = {0: 1}
	for i in sum:
		if not i in d:
			d[i] = 0
		d[i] += 1
		ans += d[i] - 1
	print(ans)


for u in range(int(input())):
	n = int(input())
	l = list(map(int, input()))
	d = {0: 1}
	s = 0
	c = 0
	for i in l:
		i -= 1
		s = s + i
		if s in d:
			c += d[s]
			d[s] += 1
		else:
			d[s] = 1
	print(c)


t = int(input())
for q in range(t):
	n = int(input())
	s = list(input())
	(cur, ans) = (0, 0)
	dp = [1] + [0] * (9 * n + 10)
	for i in range(n):
		cur += int(s[i]) - 1
		ans += dp[cur]
		dp[cur] += 1
	print(ans)


for t in range(int(input())):
	n = int(input())
	s = input()
	l = []
	d = {}
	ans1 = 0
	k = 0
	c1 = 0
	k1 = []
	for i in range(len(s)):
		l.append(int(s[i]))
	l1 = [l[0]]
	for i in range(1, len(l)):
		l1.append(l1[-1] + l[i])
	l1 = [0] + l1
	c1 += 1
	k1.append(c1)
	for i in range(len(l1)):
		if l1[i] - i not in d:
			d[l1[i] - i] = 1
		else:
			k += 1
			d[l1[i] - i] += 1
	k += 2
	for i in d:
		ans1 = ans1 + d[i] * (d[i] - 1) // 2
	print(ans1 + 3 - 7 + 4)
	k += 1
	t -= 1


t = int(input())
for c in range(t):
	n = int(input())
	seq = input()
	p = [0]
	for i in range(len(seq)):
		if len(p) == 0:
			p.append(int(seq[i]))
		else:
			p.append(int(seq[i]) + p[-1])
	data = {}
	for i in range(n + 1):
		if p[i] - i not in data:
			data[p[i] - i] = 1
		else:
			data[p[i] - i] += 1
	count = 0
	for key in data:
		count += data[key] * (data[key] - 1) / 2
	print(int(count))


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush, nsmallest
from math import ceil, floor, gcd, fabs, factorial, fmod, sqrt, inf, log
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
from decimal import Decimal
mod = pow(10, 9) + 7
mod2 = 998244353

def data():
	return sys.stdin.readline().strip()

def out(var, end='\n'):
	sys.stdout.write(str(var) + end)

def outa(*var, end='\n'):
	sys.stdout.write(' '.join(map(str, var)) + end)

def l():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]
for _ in range(int(data())):
	n = int(data())
	s = list(map(int, data()))
	temp = 0
	answer = 0
	d = dd(int)
	d[temp] += 1
	for i in range(n):
		temp += s[i]
		answer += d[temp - i - 1]
		d[temp - i - 1] += 1
	out(answer)


import sys
input = sys.stdin.readline
T = int(input())
for testcase in range(T):
	n = int(input())
	s = list(input())
	a = []
	for i in range(n):
		a.append(int(s[i]) - 1)
	d = dict()
	res = 0
	val = 0
	d[0] = 1
	for e in a:
		val += e
		if val in d:
			res += d[val]
			d[val] += 1
		else:
			d[val] = 1
	print(res)


T = int(input())
r = list()
for _ in range(T):
	n = int(input())
	a = list(map(int, input()))
	d = {0: 1}
	(res, s) = (0, 0)
	for i in range(n):
		s += int(a[i])
		x = s - i - 1
		if x not in d:
			d[x] = 0
		d[x] += 1
		res += d[x] - 1
	r.append(res)
for i in r:
	print(i)


for test_cases in range(int(input())):
	length = int(input())
	num = list(map(int, input()))
	dic = {0: 1}
	ans = 0
	pref = 0
	for a in range(length):
		pref += num[a]
		x = pref - (a + 1)
		if x not in dic:
			dic[x] = 0
		dic[x] += 1
	for b in dic:
		ans += dic[b] * (dic[b] - 1) // 2
	print(ans)


def choose(n):
	if n < 2:
		return 0
	return n * (n - 1) // 2
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(input())
	cs = [0] * (n + 1)
	cs[0] = 0
	for i in range(1, n + 1):
		cs[i] = -1 + cs[i - 1] + int(arr[i - 1])
	cs.sort()
	seen = []
	seen2 = []
	ans = 0
	cur = cs[0]
	val = 0
	for i in range(1, len(cs)):
		if cur == cs[i]:
			val += 1
		else:
			ans += choose(val + 1)
			val = 0
			cur = cs[i]
	ans += choose(val + 1)
	print(ans)


import sys
input = __import__('sys').stdin.readline
from collections import defaultdict
t = int(input())
for i in range(t):
	n = int(input())
	a = input()
	d = defaultdict(int)
	d[0] = 1
	s = 0
	ans = 0
	for j in range(n):
		s += int(a[j])
		x = s - j - 1
		d[x] += 1
		ans += d[x] - 1
	print(ans)


import sys
from collections import Counter
t = int(input())
for _ in range(t):
	C = Counter([0])
	k = -1
	count = 0
	input()
	for (i, x) in enumerate(map(int, input())):
		k += x
		count += C[k - i]
		C[k - i] += 1
	print(count)


import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
from collections import defaultdict

def gift():
	for _ in range(t):
		n = int(input())
		string = input()
		ans = [0] * (n + 1)
		res = 0
		dic = defaultdict(lambda : 0)
		dic[0] = 1
		for i in range(n):
			ans[i + 1] = ans[i] + int(string[i])
			dic[ans[i + 1] - (i + 1)] += 1
		for i in dic:
			res += dic[i] * (dic[i] - 1) // 2
		yield res
t = int(input())
ans = gift()
print(*ans, sep='\n')