Slavic has an array of length $n$ consisting only of zeroes and ones. In one operation, he removes either the first or the last element of the array.

What is the minimum number of operations Slavic has to perform such that the total sum of the array is equal to $s$ after performing all the operations? In case the sum $s$ can't be obtained after any amount of operations, you should output -1.


-----Input-----

The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases.

The first line of each test case contains two integers $n$ and $s$ ($1 \leq n, s \leq 2 \cdot 10^5$) — the length of the array and the needed sum of elements.

The second line of each test case contains $n$ integers $a_i$ ($0 \leq a_i \leq 1$) — the elements of the array.

It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.


-----Output-----

For each test case, output a single integer — the minimum amount of operations required to have the total sum of the array equal to $s$, or -1 if obtaining an array with sum $s$ isn't possible.


-----Examples-----

Input
7
3 1
1 0 0
3 1
1 1 0
9 3
0 1 0 1 1 1 0 0 1
6 4
1 1 1 1 1 1
5 1
0 0 1 1 0
16 2
1 1 0 0 1 0 0 1 1 0 0 0 0 0 1 1
6 3
1 0 1 0 0 0
Output
0
1
3
2
2
7
-1


-----Note-----

In the first test case, the sum of the whole array is $1$ from the beginning, so we don't have to make any operations.

In the second test case, the sum of the array is $2$ and we want it to be equal to $1$, so we should remove the first element. The array turns into $[1, 0]$, which has a sum equal to $1$.

In the third test case, the sum of the array is $5$ and we need it to be $3$. We can obtain such a sum by removing the first two elements and the last element, doing a total of three operations. The array turns into $[0, 1, 1, 1, 0, 0]$, which has a sum equal to $3$.

Source: codeforces | Difficulty: EASY
Tags: Sorting

Amortized analysis

Implementation


Solutions:
import sys
input = sys.stdin.readline

def solve():
	(n, S) = map(int, input().split())
	A = [int(x) for x in input().split()]
	if S > sum(A):
		return -1
	ans = 0
	l = t = 0
	for r in range(n):
		t += A[r]
		if t == S:
			ans = max(ans, r - l + 1)
		while l < r and t > S:
			t -= A[l]
			l += 1
	return n - max(ans, r - l + 1)
for _ in range(int(input())):
	print(solve())


import sys
import collections
import bisect
import math
from functools import lru_cache
sys.setrecursionlimit(10 ** 6)

def helper(arr, n, k):
	mydict = dict()
	sum = 0
	maxLen = 0
	for i in range(n):
		sum += arr[i]
		if sum == k:
			maxLen = i + 1
		elif sum - k in mydict:
			maxLen = max(maxLen, i - mydict[sum - k])
		if sum not in mydict:
			mydict[sum] = i
	return maxLen
for _ in range(int(input())):
	(n, k) = list(map(int, sys.stdin.readline().split()))
	arr = list(map(int, sys.stdin.readline().split()))
	s = sum(arr)
	if s < k:
		print(-1)
	elif s == k:
		print(0)
	else:
		maxLen = helper(arr, n, k)
		print(n - maxLen)


numCases = int(input())
for case in range(numCases):
	(longitud, suma) = (int(i) for i in input().split(' '))
	array = input().split(' ')
	unosParaEliminar = array.count('1') - suma
	if unosParaEliminar < 0:
		print(-1)
		continue
	if unosParaEliminar == 0:
		print(0)
		continue
	distsIzq = [0]
	distsIzq2 = [i + 1 for (i, valor) in enumerate(array) if valor == '1']
	distsIzq = distsIzq + distsIzq2
	array.reverse()
	distsDer = [0]
	distsDer2 = [i + 1 for (i, valor) in enumerate(array) if valor == '1']
	distsDer = distsDer + distsDer2
	print(min([distsIzq[unosParaEliminar - i] + distsDer[i] for i in range(unosParaEliminar + 1)]))


import sys
input = sys.stdin.readline

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

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

def find(array, s, n):
	left = right = 0
	if sum(array) < s:
		return -1
	su_m = 0
	minSize = n
	while right < n:
		su_m += array[right]
		right += 1
		while su_m > s:
			su_m -= array[left]
			left += 1
		size = n - (right - left)
		minSize = min(minSize, size)
	return minSize if minSize != n else -1
for _ in range(n):
	(n, s) = inlt()
	array = inlt()
	print(find(array, s, n))


import sys
from math import inf
sys.setrecursionlimit(2 * 10 ** 6)

def test():
	pass

def main():
	N = int(input())
	for _ in range(N):
		(n, s) = map(int, input().split())
		arr = list(map(int, input().split()))
		t = sum(arr)
		if s == t:
			print(0)
			continue
		elif s > t:
			print(-1)
			continue
		i = 0
		j = 0
		t = 0
		while t < s:
			if arr[j]:
				t += 1
			j += 1
		while j < len(arr) and arr[j] == 0:
			j += 1
		ans = n - j
		while j < len(arr):
			if arr[j]:
				while arr[i] != 1:
					i += 1
				i += 1
			ans = min(ans, n - j + i - 1)
			j += 1
		print(ans)

def init():
	import sys
	import os
	if 'DOCKER_HOST' in os.environ:
		test()
		sys.stdin = open('test.txt')
init()
main()


strng = lambda : input()
inp = lambda : int(input())
mul = lambda : map(int, input().split())
jn = lambda x, l: x.join(map(str, l))
strlst = lambda : list(input())
lst = lambda : list(map(int, input().split()))
dd = lambda : defaultdict(lambda : 0)
import sys
input = sys.stdin.readline
from collections import defaultdict
from math import ceil, log, floor, sqrt, gcd, log10
for _ in range(inp()):
	(n, s) = mul()
	a = lst()
	mxs = 0
	si = 0
	cursum = 0
	ts = sum(a)
	if ts < s:
		print(-1)
		continue
	if ts == s:
		print(0)
		continue
	for i in range(n):
		cursum += a[i]
		if cursum > s:
			cursum -= a[si]
			si += 1
		if i - si >= mxs:
			mxs = i - si
	print(n - (mxs + 1))


def solve():
	(n, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	temp = [-1]
	for i in range(n):
		if arr[i] == 1:
			temp.append(i)
	temp.append(n)
	if len(temp) - 2 < k:
		print(-1)
		return
	ans = n
	for i in range(len(temp) - k - 1):
		ans = min(ans, temp[i] + n - temp[i + k + 1] + 1)
	print(ans)
for T in range(int(input())):
	solve()


def solve():
	(n, s) = map(int, input().split())
	lis = list(map(int, input().split()))
	ans = -1
	start = 0
	sum = 0
	for i in range(n):
		sum += lis[i]
		while sum > s:
			sum -= lis[start]
			start += 1
		if sum == s:
			ans = max(ans, i - start + 1)
	print(ans if ans == -1 else n - ans)
for _ in range(int(input())):
	solve()


def solve(arr, target):
	i = 0
	j = 0
	sumVal = 0
	n = len(arr)
	maxLen = float('-inf')
	while j < n:
		sumVal += arr[j]
		if sumVal <= target:
			j += 1
		else:
			currLen = j - 1 - i + 1
			maxLen = max(maxLen, currLen)
			while sumVal > target:
				sumVal -= arr[i]
				i += 1
			j += 1
	return len(arr) - max(maxLen, j - 1 - i + 1)
for _ in range(int(input())):
	(n, s) = map(int, input().split())
	arr = list(map(int, input().split()))
	if sum(arr) < s:
		print(-1)
	elif sum(arr) == s:
		print(0)
	else:
		print(solve(arr, s))


t = int(input())
import sys

def lenOfLongSubarr(A, N, K):
	(i, j, sum) = (0, 0, 0)
	maxLen = -sys.maxsize - 1
	while j < N:
		sum += A[j]
		if sum < K:
			j += 1
		elif sum == K:
			maxLen = max(maxLen, j - i + 1)
			j += 1
		elif sum > K:
			while sum > K:
				sum -= A[i]
				i += 1
			if sum == K:
				maxLen = max(maxLen, j - i + 1)
			j += 1
	return maxLen
for tt in range(t):
	(n, s) = map(int, input().split())
	a1 = list(map(int, input().split()))
	if s > a1.count(1):
		print(-1)
		continue
	a = lenOfLongSubarr(a1, n, s)
	print(n - a)


for i in range(int(input())):
	(n, u) = list(map(int, input().split()))
	arr = list(map(int, input().split()))
	(l, r) = ([0], [])
	for (i, x) in enumerate(arr):
		if x:
			l.append(i + 1)
			r.append(n - i)
	r.append(0)
	s = sum(arr)
	d = s - u
	if d < 0:
		print(-1)
	elif d == 0:
		print(0)
	else:
		l = l[:d + 1]
		r = r[-d - 1:]
		ans = l[-1]
		for (x, y) in zip(l, r):
			if x + y < ans:
				ans = x + y
		print(ans)


import sys, math, functools, itertools, collections
input = sys.stdin.readline

def fun(a, n, s):
	dic = dict()
	sm = 0
	mxlen = 0
	for i in range(n):
		sm += a[i]
		if sm == s:
			mxlen = i + 1
		elif sm - s in dic:
			mxlen = max(mxlen, i - dic[sm - s])
		if sm not in dic:
			dic[sm] = i
	return mxlen

def solve():
	(n, s) = map(int, input().split())
	a = [*map(int, input().split())]
	if sum(a) < s:
		print('-1')
		return
	else:
		best = fun(a, n, s)
		print(n - best)
for _ in range(int(input())):
	solve()


res = []

def binaryDeque(nums, n, target):
	sz = float('-inf')
	l = r = 0
	temp = 0
	if sum(nums) < target:
		res.append(-1)
		return
	while True:
		if r == n:
			sz = max(sz, r - l)
			break
		if temp + nums[r] <= target:
			temp += nums[r]
			r += 1
		else:
			sz = max(sz, r - l)
			temp -= nums[l]
			l += 1
	res.append(n - sz)
	return
t = int(input())
while t:
	(s, req) = input().split()
	A = list(map(int, input().split()))
	binaryDeque(A, int(s), int(req))
	t -= 1
for n in res:
	print(n)


def solve():
	(n, ss) = map(int, input().split())
	arr = list(map(int, input().split()))
	tsum = sum(arr)
	s = tsum - ss
	if s < 0:
		print(-1)
		return
	if s == 0:
		print(0)
		return
	left = 0
	summ = 0
	while left < n and summ != s:
		if arr[left] == 1:
			summ += 1
		if summ == s:
			break
		left += 1
	right = n - 1
	ans = left + 1
	while left >= 0:
		if arr[left] == 1:
			left -= 1
			while left >= 0 and arr[left] == 0:
				left -= 1
			while arr[right] == 0:
				right -= 1
			right -= 1
		ans = min(ans, left + 1 + (n - right - 1))
	print(ans)
t = int(input())
for i in range(t):
	solve()


for _ in range(int(input())):
	(n, s) = map(int, input().split())
	arr = list(map(int, input().split()))
	x = sum(arr)
	a = []
	ans = 0
	if x == s:
		ans = 0
	elif x < s:
		ans = -1
	else:
		for i in range(n):
			if arr[i] == 1:
				a.append(i)
		m = a[s] + 1
		for i in range(x - s - 1):
			m = max(m, a[i + s + 1] - a[i])
		m = max(m, n - a[x - s - 1])
		ans = n - m + 1
	print(ans)


def process(n, s, arr):
	tot = sum(arr)
	if tot < s:
		return -1
	if tot == s:
		return 0
	arr = [i for (i, item) in enumerate(array) if item == 1]
	arr = [-1] + arr + [n]
	(p1, p2) = (0, s + 1)
	maxdist = -1
	while p2 < len(arr):
		maxdist = max(maxdist, arr[p2] - arr[p1] - 2)
		p1 += 1
		p2 += 1
	return n - maxdist - 1
for _ in range(int(input())):
	(n, s) = map(int, input().split(' '))
	array = list(map(int, input().split(' ')))
	print(process(n, s, array))


import math
from collections import *

def solve():
	(n, s) = map(int, input().split())
	a = [int(i) for i in input().split()]
	if a.count(1) < s:
		print(-1)
		return
	elif a.count(1) == s:
		print(0)
		return
	else:
		ans = []
		for i in range(len(a)):
			if a[i] == 1:
				ans.append(i + 1)
		mini = n - ans[s] + 1
		for i in range(1, len(ans) - s):
			mini = min(ans[i - 1] + (n - ans[i + s] + 1), mini)
		mini = min(ans[len(ans) - s - 1], mini)
		print(mini)
t = int(input())
for _ in range(t):
	solve()


from itertools import accumulate
for _ in range(int(input())):
	(n, s) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	if sum(a) < s:
		print(-1)
	elif sum(a) == s:
		print(0)
	else:
		kill = sum(a) - s
		now = 1
		diff = []
		for aa in a:
			if aa != 1:
				now += 1
			else:
				diff.append(now)
				now = 1
		diff.append(now)
		rest = len(diff) - (sum(a) - s)
		maxn = 0
		preSum = list(accumulate(diff, initial=0))
		for i in range(len(diff) - rest + 1):
			maxn = max(maxn, preSum[i + rest] - preSum[i])
		print(sum(diff) - maxn)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	lst = list(map(int, input().split()))
	total = sum(lst)
	lst.append(1)
	if total < k:
		print(-1)
		continue
	if total == k:
		print(0)
		continue
	x = 0
	y = 0
	cnt = 0
	while cnt < k:
		cnt += lst[y]
		y += 1
	mx = 0
	while y <= n:
		if lst[y] == 1:
			cnt += 1
			mx = max(mx, y - x)
			while cnt != k:
				cnt -= lst[x]
				x += 1
		y += 1
	print(n - mx)


def main(nums, x):
	target = x
	(curr_sum, max_len) = (0, 0)
	start_idx = 0
	if target == 0:
		return len(nums)
	for end_idx in range(len(nums)):
		curr_sum += nums[end_idx]
		while start_idx <= end_idx and curr_sum > target:
			curr_sum -= nums[start_idx]
			start_idx += 1
		if curr_sum == target:
			max_len = max(max_len, end_idx - start_idx + 1)
	return len(nums) - max_len if max_len else -1
t = int(input())
for _ in range(t):
	(n, x) = map(int, input().split())
	nums = list(map(int, input().split()))
	print(main(nums, x))


def func():
	(n, s) = map(int, input().split())
	l = list(map(int, input().split()))
	i = 0
	j = 0
	ans = n
	while j != n:
		if l[j] == 1:
			s -= 1
		if s == -1:
			break
		j += 1
	if s > 0:
		print(-1)
		return
	ans = min(ans, n - j)
	while j < n:
		while j < n and l[j] != 1:
			j += 1
		ans = min(n - (j - i), ans)
		while i < n and l[i] != 1:
			i += 1
		i += 1
		j += 1
	print(ans)
t = int(input())
for _ in range(t):
	func()


n = int(input())
for i in range(n):
	l = list(map(int, input().split()))
	long = l[0]
	equ = l[1]
	s = list(map(int, input().split()))
	tip = [-1]
	for j in range(long):
		if s[j]:
			tip.append(j)
	tip.append(long)
	if len(tip) - 2 < equ:
		print(-1)
	elif len(tip) - 2 == equ:
		print(0)
	else:
		deep = 0
		for p in range(1, len(tip) - equ):
			x = tip[p + equ] - tip[p - 1]
			if x > deep:
				deep = x
		print(long - deep + 1)


def FindMinNumber(arr, n, k):
	i = 0
	j = 0
	min_num = 10 ** 9
	found = False
	Sum = 0
	while i < n:
		Sum = Sum + arr[i]
		if Sum == k:
			min_num = min(min_num, n - (i + 1) + j)
			found = True
		elif Sum > k:
			while Sum > k:
				Sum = Sum - arr[j]
				j += 1
			if Sum == k:
				min_num = min(min_num, n - (i + 1) + j)
				found = True
		i += 1
	if found:
		return min_num
	return -1
for i in range(int(input())):
	(n, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	print(FindMinNumber(arr, n, k))


def sol():
	(n, s) = map(int, input().split())
	ar = list(map(int, input().split()))
	if sum(ar) < s:
		print(-1)
	elif sum(ar) == s:
		print(0)
	else:
		left = 0
		right = 0
		cs = 0
		while True:
			cs += ar[left]
			if cs == s:
				break
			else:
				left += 1
		maxx = left + 1
		while True:
			left += 1
			cs += ar[left]
			while cs > s:
				cs -= ar[right]
				right += 1
			maxx = max(maxx, abs(left - right + 1))
			if left == n - 1:
				break
		print(n - maxx)
for t in range(int(input())):
	sol()


for _ in range(int(input())):
	(n, s) = map(int, input().split())
	a = [int(i) for i in input().split()]
	ss = sum(a)
	if ss == s:
		print(0)
		continue
	if ss < s:
		print(-1)
		continue
	ns = 0
	l = 0
	max_length = []
	for i in range(n):
		ns += a[i]
		if ns == s:
			max_length.append(i - l + 1)
		while ns > s:
			ns -= a[l]
			l += 1
	print(n - max(max_length))


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

def solve():
	(n, s) = read()
	a = read()
	ones = [0]
	for i in range(n):
		if a[i] == 0:
			continue
		ones.append(i + 1)
	if len(ones) - 1 < s:
		print(-1)
		return
	ans = n
	s = len(ones) - s - 1
	for i in range(s + 1):
		left = i
		right = s - i
		ans = min(ans, ones[left] + (0 if right == 0 else n - ones[-right] + 1))
	print(ans)
t = int(input())
for i in range(t):
	solve()


def minCount(arr, n, k):
	ans = 0
	sum = 0
	for i in range(n):
		sum += arr[i]
	sum -= k
	l = 0
	r = 0
	tot = 0
	while l < n:
		if tot <= sum:
			ans = max(ans, r - l)
			if r == n:
				break
			tot += arr[r]
			r += 1
		else:
			tot -= arr[l]
			l += 1
	return n - ans
tests = 1
tests = int(input())
for test in range(tests):
	(n, x) = map(int, input().split())
	arr = list(map(int, input().split()))
	tot = sum(arr)
	k = tot - x
	ans = minCount(arr, n, k)
	print(ans if tot - x >= 0 else -1)


def minOperations(nums, x):
	maxLen = total = windowStart = 0
	targetSum = sum(nums)
	if targetSum < x:
		return -1
	if targetSum == x:
		return len(nums)
	k = targetSum - x
	for windowEnd in range(len(nums)):
		total += nums[windowEnd]
		while total > k:
			total -= nums[windowStart]
			windowStart += 1
		if total == k:
			maxLen = max(maxLen, windowEnd - windowStart + 1)
	return len(nums) - maxLen if maxLen != 0 else -1
for _ in range(int(input())):
	(n, x) = map(int, input().split())
	lst = list(map(int, input().split()))
	print(minOperations(lst, sum(lst) - x))


import math
t = int(input())
while t:
	t -= 1
	(h, i) = input().split()
	a = int(h)
	b = int(i)
	lis = input().split()
	lis = [int(x) for x in lis]
	if sum(lis) < b:
		print(-1)
		continue
	indlist = [-1]
	for (ind, i) in enumerate(lis):
		if i == 1:
			indlist.append(ind)
	ma = 0
	indlist.append(a)
	for i in range(1, len(indlist) - b):
		new = indlist[i + b] - indlist[i - 1] - 1
		ma = max(new, ma)
	print(a - ma)


def binaryDeque(arr, k, length):
	left = right = 0
	count = 0
	res = -1
	while right < length:
		while left < length and count > k:
			if arr[left]:
				count -= 1
			left += 1
		if arr[right]:
			count += 1
		right += 1
		if count == k:
			res = max(res, right - left)
	if res == -1:
		return res
	return length - res
case = int(input())
for i in range(case):
	(length, k) = list(map(int, input().split(' ')))
	arr = list(map(int, input().split(' ')))
	print(binaryDeque(arr, k, length))


import collections
for _ in range(int(input())):
	(n, s) = map(int, input().split())
	a = list(map(int, input().split()))
	summ = sum(a)
	if summ < s:
		print(-1)
	else:
		i = j = 0
		cnt = 0
		ans = 0
		for k in range(n):
			cnt += a[k]
			while cnt > s:
				cnt -= a[i]
				i += 1
			if cnt == s:
				ans = max(ans, k - i + 1)
		print(n - ans)


from collections import deque
t = int(input())
for _ in range(t):
	(n, s) = map(int, input().split())
	a = list(map(int, input().split()))
	if sum(a) < s:
		print(-1)
	elif sum(a) == s:
		print(0)
	else:
		ind = []
		for i in range(n):
			if a[i] == 1:
				ind.append(i)
		remnant = sum(a) - s
		cost = [n - ind[-remnant], ind[remnant - 1] + 1]
		for i in range(1, remnant):
			cost.append(ind[i - 1] + 1 + n - ind[-(remnant - i)])
		print(min(cost))


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	(n, s) = map(int, input().split())
	a = [int(i) for i in input().split()]
	ones = []
	ans = 0
	for (i, c) in enumerate(a):
		if c == 1:
			ones.append(i)
	(i, j, x, y) = (0,) * 4
	m = ones.__len__()
	j = m - 1
	y = n
	x = -1
	if m < s:
		print(-1)
	elif m == s:
		print(0)
	else:
		cost = 1000000
		c = m - s
		for i in range(1, c):
			j = c - i
			cost = min(ones[i - 1] + 1 + n - ones[-j], cost)
		cost = min(cost, ones[c - 1] + 1)
		cost = min(cost, n - ones[-c])
		print(cost)


t = int(input())

def lenOfLongSubarr(arr, n, k):
	mydict = dict()
	sum = 0
	maxLen = 0
	for i in range(n):
		sum += arr[i]
		if sum == k:
			maxLen = i + 1
		elif sum - k in mydict:
			maxLen = max(maxLen, i - mydict[sum - k])
		if sum not in mydict:
			mydict[sum] = i
	return maxLen
while t:
	(n, k) = map(int, input().split())
	arr = [int(i) for i in input().split()]
	s = sum(arr)
	if k > s:
		print('-1')
	else:
		temp = lenOfLongSubarr(arr, n, k)
		print(n - temp)
	t -= 1


def deq(nums, n, s):
	A = []
	for i in range(n):
		if nums[i] == 1:
			A.append(i)
	sm = len(A)
	min = n
	for i in range(0, sm - s + 1):
		start = i
		end = i + s - 1
		if start == 0:
			value = n - A[end + 1]
		elif end == sm - 1:
			value = A[start - 1] + 1
		else:
			value = A[start - 1] + 1 + n - A[end + 1]
		if value < min:
			min = value
	return min
tests = int(input())
for _ in range(tests):
	(n, s) = map(int, input().split())
	nums = list(map(int, input().split()))
	sm = 0
	for i in range(n):
		sm += nums[i]
	if sm < s:
		print('-1')
	elif sm == s:
		print('0')
	else:
		print(deq(nums, n, s))


e = lambda : map(int, input().split())
for d in range(int(input())):
	(n, s) = e()
	a = [*e()]
	if sum(a) < s:
		z = -1
	else:
		l = s2 = z = 0
		for r in range(n):
			s2 += a[r]
			if s2 == s:
				z = max(z, r - l + 1)
			while s2 > s:
				s2 -= a[l]
				l += 1
		z = n - z
	print(z)


t = int(input())
for _ in range(t):
	(n, s) = map(int, input().split())
	arr = list(map(int, input().split()))
	win_sum = 0
	res = -1
	left = 0
	for right in range(len(arr)):
		win_sum += arr[right]
		while win_sum > s:
			win_sum -= arr[left]
			left += 1
		if win_sum == s:
			res = max(res, right - left + 1)
	if res == -1:
		print(res)
	else:
		print(len(arr) - res)


def getMaxLn(a):
	curs = 0
	i = 0
	while curs < s:
		curs += a[i]
		i += 1
	i -= 1
	ln = i
	maxLn = ln
	right = i
	left = 0
	while True:
		if right < len(a) - 1:
			right += 1
			while a[right] == 0:
				right += 1
				if right > n - 1:
					right -= 1
					break
			if a[right] == 1:
				right -= 1
			ln = right - left + 1
			if ln > maxLn:
				maxLn = ln
		else:
			return maxLn
		if a[left] == 1:
			left += 1
		else:
			while a[left] == 0:
				left += 1
			left += 1
		if right + 1 < n and a[right + 1] == 1:
			right += 1
		ln = right - left + 1
		if ln > maxLn:
			maxLn = ln
t = int(input())
for _ in range(t):
	(n, s) = map(int, input().split())
	a = list(map(int, input().split()))
	suma = sum(a)
	if suma < s:
		print(-1)
	elif suma == s:
		print(0)
	else:
		maxln = getMaxLn(a)
		print(n - maxln)


MOD = 10 ** 9 + 7
fac_mem = [1]

def fac(n):
	while len(fac_mem) < n + 1:
		fac_mem.append(fac_mem[-1] * len(fac_mem) % MOD)
	return fac_mem[n]

def perm(n, k):
	return fac(n) * pow(fac(k), MOD - 2, MOD) % MOD

def comb(n, k):
	return fac(n) * pow(fac(k) * fac(n - k) % MOD, MOD - 2, MOD) % MOD

class Tree:

	def __init__(self, id):
		self.id = id
		self.parent = None
		self.children = []

	def __repr__(self):
		if self.children:
			return f'{self.id} -> [' + ', '.join((str(c) for c in self.children)) + ']'
		else:
			return str(self.id)
for case in range(int(input())):
	(n, s) = [int(j) for j in input().split()]
	a = [int(j) for j in input().split()]
	if sum(a) < s:
		print(-1)
	else:
		ones = [-1]
		for i in range(n):
			if a[i] == 1:
				ones.append(i)
		ones.append(n)
		r = 0
		start = -1
		for i in range(len(ones) - s - 1):
			r = max(r, ones[i + s + 1] - ones[i] - 1)
		print(len(a) - r)


def f():
	(n, s) = map(int, input().split())
	nums = list(map(int, input().split()))
	acc2idx = {0: -1}
	acc = 0
	ans = float('inf')
	for (i, num) in enumerate(nums):
		acc += num
		if acc - s in acc2idx:
			ans = min(ans, n - (i - acc2idx[acc - s]))
		if acc not in acc2idx:
			acc2idx[acc] = i
	return -1 if ans == float('inf') else ans
for _ in range(int(input())):
	print(f())


for _ in range(int(input())):
	(n, s) = map(int, input().split())
	a = list(map(int, input().split()))
	if sum(a) < s:
		ans = -1
	else:
		s2 = l = ans = 0
		for r in range(n):
			s2 += a[r]
			if s2 == s:
				ans = max(ans, r - l + 1)
			while s2 > s:
				s2 -= a[l]
				l += 1
		ans = n - ans
	print(ans)


a = int(input())
for e in range(a):
	(n, s) = map(int, input().split())
	arr = list(map(int, input().split()))
	sm = sum(arr)
	place = [0]
	fin = []
	for i in range(n):
		if arr[i] == 1:
			place.append(i + 1)
	place.append(n + 1)
	k = len(place)
	delt = sm - s
	if sm < s:
		print(-1)
	else:
		for i in range(delt + 1):
			forw = place[i]
			backw = n + 1 - place[k - 1 - delt + i]
			fin.append(forw + backw)
		print(min(fin))


import sys
sys.setrecursionlimit(10 ** 6)
from sys import stdin, stdout
I = stdin.readline
O = stdout.write

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

def solve():
	(n, s) = list(map(int, I().split()))
	arr = list(map(int, input().split()))
	if sum(arr) < s:
		print(-1)
	else:
		i = 0
		st = 0
		ma = 0
		cnt = 0
		while i < n:
			cnt += arr[i]
			while st < n and cnt > s:
				cnt -= arr[st]
				st += 1
			ma = max(ma, i - st + 1)
			i += 1
		print(n - ma)
for tc in range(int(input())):
	solve()


def foo(nums, target):
	if sum(nums) < target:
		return len(nums) + 1
	if sum(nums) == target:
		return len(nums)
	i = 0
	ans = []
	j = 0
	s = 0
	while j < len(nums):
		s += nums[j]
		if s == target:
			ans.append(j - i + 1)
		while s > target:
			s -= nums[i]
			i += 1
		if s == target:
			ans.append(j - i + 1)
		j += 1
	return max(ans)
n = int(input())
for i in range(n):
	(_, x) = input().split()
	x = int(x)
	a = input().split()
	a = list(map(int, a))
	print(len(a) - foo(a, x))


for _ in [0] * int(input()):
	(n, s) = map(int, input().split())
	a = [*map(int, input().split())]
	one = a.count(1)
	if one <= s:
		print(-(one < s))
		continue
	head = [0]
	tail = [0]
	for i in range(n):
		if a[i]:
			head += (i + 1,)
	for i in range(n):
		if a[~i]:
			tail += (-~i,)
	print(min((head[i] + tail[one - s - i] for i in range(one - s + 1))))


for t in range(int(input())):
	(n, s) = map(int, input().split())
	a = list(map(int, input().split()))
	if sum(a) < s:
		print(-1)
	elif sum(a) == s:
		print(0)
	else:
		(l, r) = (0, 0)
		(cs, ans) = (0, 0)
		while r < n:
			cs += a[r]
			r += 1
			while cs > s:
				cs -= a[l]
				l += 1
			if cs == s:
				ans = max(ans, r - l)
		print(n - ans)


T = int(input())
for _ in range(T):
	(n, s) = map(int, input().split())
	a = list(map(int, input().split()))
	ans = 0
	marker = []
	for i in range(n):
		if a[i] == 1:
			marker.append(i + 1)
	if len(marker) < s:
		print(-1)
	elif len(marker) == s:
		print(0)
	else:
		for i in range(0, len(marker) - s + 1):
			if i == 0:
				ans = marker[s] - 1
			elif i < len(marker) - s:
				ans = max(ans, marker[i + s] - marker[i - 1] - 1)
			else:
				ans = max(ans, n - marker[i - 1])
		print(n - ans)


t = int(input())
for _ in range(t):
	(n, s) = map(int, input().split())
	l = list(map(int, input().split()))
	if sum(l) < s:
		print(-1)
	else:
		c = 0
		dist = 0
		dlst = []
		for i in range(n):
			if i == n - 1:
				if l[i]:
					dlst.append(dist)
					dlst.append(0)
				else:
					dlst.append(dist + 1)
				break
			if l[i] != 1:
				dist += 1
			else:
				dlst.append(dist)
				dist = 0
		(cur, mx) = (0, 0)
		start = 0
		end = s
		for i in range(s + 1):
			cur += dlst[i]
		mx = max(cur, mx)
		for i in range(s + 1, len(dlst)):
			start += 1
			end += 1
			cur -= dlst[start - 1]
			cur += dlst[end]
			mx = max(mx, cur)
		print(n - mx - s)


import collections
import math
import sys
from queue import PriorityQueue
from random import randint

def main(testnumber):
	(n, s) = map(int, input().split())
	a = list(map(int, input().split()))
	if sum(a) < s:
		print(-1)
	else:
		l = 0
		r = 0
		ans = 0
		sm = 0
		while l <= r and r < n:
			while sm < s and r < n:
				sm += a[r]
				r += 1
			while r < n and sm + a[r] == s:
				sm += a[r]
				r += 1
			if sm == s:
				ans = max(r - l, ans)
			sm -= a[l]
			l += 1
			r = max(r, l)
		print(n - ans)
t = int(input())
cnt = 1
while t > 0:
	main(cnt)
	cnt += 1
	t -= 1


import math
import collections
import itertools
import functools

def fun(t):

	def lenOfLongSubarr(arr, n, k):
		mydict = dict()
		sum = 0
		maxLen = 0
		for i in range(n):
			sum += arr[i]
			if sum == k:
				maxLen = i + 1
			elif sum - k in mydict:
				maxLen = max(maxLen, i - mydict[sum - k])
			if sum not in mydict:
				mydict[sum] = i
		return maxLen
	(n, s) = map(int, input().split(' '))
	arr = list(map(int, input().split(' ')))
	tot = sum(arr)
	if tot < s:
		print('-1')
		return
	print(n - lenOfLongSubarr(arr, n, s))
for tc in range(int(input())):
	fun(tc)


t = int(input())
for i in range(t):
	(n, s) = list(map(int, input().split()))
	arr = list(map(int, input().split()))
	one = arr.count(1)
	if s > one:
		print(-1)
	elif one == s:
		print(0)
	else:
		ind = []
		req = one - s
		ones = []
		for j in range(n):
			if arr[j] and len(ind) < req:
				ind.append(j)
			if arr[j]:
				ones.append(j)
		l = len(ind) - 1
		ans = ind[-1] + 1
		r = len(ones) - 1
		while l >= 0:
			l -= 1
			ans = min(ans, ind[l] + 1 + n - ones[r])
			r -= 1
		ans = min(ans, n - ones[r + 1])
		print(ans)


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	sm = sum(arr)
	if sm < k:
		print(-1)
	else:
		i = 0
		sm = 0
		while i < n and sm < k:
			sm += arr[i]
			i += 1
		while i < n and arr[i] == 0:
			i += 1
		j = 0
		ans = i
		while i < n:
			if arr[i] == 0:
				i += 1
				ans = max(ans, i - j)
				continue
			if 1 == 1:
				while j < i and arr[j] != 1:
					j += 1
				if j == i:
					break
				else:
					j += 1
					tm = i - j + 1
					ans = max(ans, tm)
				i += 1
		print(n - ans)


import sys

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

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

def get_string():
	return sys.stdin.readline().strip()
t = int(input())
for _ in range(t):
	(n, s) = get_ints()
	arr = get_list()
	l = 0
	r = -1
	res = -1
	cur = 0
	while r < n:
		while cur <= s:
			r += 1
			if r == n:
				break
			cur += arr[r]
		if cur >= s:
			res = max(res, r - l)
		while cur > s:
			cur -= arr[l]
			l += 1
	if res != -1:
		print(n - res)
	else:
		print(-1)


t = int(input())
for _ in range(t):
	(n, s) = list(map(int, input().split()))
	arr = input().split()
	arr = list(map(int, arr))
	maxx = -1
	summ = 0
	i = 0
	j = 0
	while j < len(arr):
		summ += arr[j]
		if summ < s:
			j += 1
		elif summ == s:
			maxx = max(maxx, j - i + 1)
			j += 1
		else:
			while summ > s:
				summ -= arr[i]
				i += 1
			if summ == s:
				maxx = max(maxx, j - i + 1)
			j += 1
	if maxx == -1:
		print(-1)
	else:
		print(n - maxx)


def solve():
	(n, s) = map(int, input().split())
	lis = list(map(int, input().split()))
	ans = -1
	dp = [0] * n
	dp[0] = lis[0]
	for i in range(1, n):
		dp[i] = dp[i - 1] + lis[i]
	di = dict()
	for i in range(n - 1, -1, -1):
		di[dp[i]] = i
	for i in range(n):
		if dp[i] == s:
			ans = i + 1
		elif dp[i] > s:
			left_ptr = dp[i] - s
			ind = di[left_ptr]
			ans = max(ans, i - ind)
	print(n - ans if ans != -1 else ans)
for _ in range(int(input())):
	solve()


for t in range(int(input())):
	(n, s) = map(int, input().split())
	a = list(map(int, input().split()))
	j = 0
	res = 0
	tmp = 0
	for i in range(n):
		tmp += a[i]
		if tmp < s:
			continue
		else:
			while j < i and tmp > s:
				tmp -= a[j]
				j += 1
		if tmp == s:
			res = max(res, i - j + 1)
	if res == 0:
		print(-1)
	else:
		print(n - res)


def calculate(arr, n, s):
	start = 0
	end = 0
	curr_sum = 0
	min_size = n
	while end < n and curr_sum < s:
		curr_sum += arr[end]
		end += 1
	min_size = min(min_size, end - start)
	end -= 2
	start -= 1
	while end >= 0:
		while end >= 0 and arr[end] == 0:
			end -= 1
		if end < 0:
			break
		start_index = (start + n) % n
		while arr[start_index] == 0:
			start -= 1
			start_index = (start + n) % n
		min_size = min(min_size, end + 1 + (n - start_index))
		end -= 1
		start -= 1
	curr_sum = 0
	j = n - 1
	while j >= 0 and curr_sum < s:
		curr_sum += arr[j]
		j -= 1
	min_size = min(min_size, n - 1 - j)
	return min_size
t = int(input())
for _ in range(t):
	(n, target) = map(int, input().split())
	arr = list(map(int, input().split()))
	total = sum(arr)
	if total < target:
		print(-1)
	elif total == target:
		print(0)
	else:
		print(calculate(arr, n, total - target))


from math import inf

def solve(n, s, a):
	if sum(a) < s:
		return -1
	current_sum = a[0]
	(left_index, right_index) = (0, 0)
	best = inf
	while left_index < n and right_index < n:
		while right_index < n - 1 and (current_sum < s or (current_sum == s and a[right_index + 1] == 0)):
			right_index += 1
			current_sum += a[right_index]
		if current_sum == s:
			best = min(best, left_index + (n - 1 - right_index))
		current_sum -= a[left_index]
		left_index += 1
	return best
t = int(input())
for _ in range(t):
	(n, s) = [int(component) for component in input().split(' ')]
	a = [int(component) for component in input().split(' ')]
	print(solve(n, s, a))


import sys
t = int(sys.stdin.readline())
for i in range(t):
	(n, s) = map(int, sys.stdin.readline().split())
	lst = list(map(int, sys.stdin.readline().split()))
	dic = {}
	su = 0
	ma = 0
	for j in range(n):
		ele = lst[j]
		su += ele
		if su == s:
			ma = max(ma, j + 1)
		else:
			req = su - s
			if req in dic:
				q = j - dic[req]
				ma = max(ma, q)
		if su not in dic:
			dic[su] = j
	if ma == 0:
		print(-1)
	else:
		print(n - ma)


t = int(input())
for i in range(t):
	(n, s) = map(int, input().split())
	a = list(map(int, input().split()))
	if sum(a) < s:
		print(-1)
	else:
		b = [-1]
		for j in range(n):
			if a[j] == 1:
				b.append(j)
		b.append(n)
		j = s
		k = 1
		cur = 0
		ans = 100000000
		while j < len(b) - 1:
			cur = b[k - 1] + 1 + (n - b[j + 1])
			if ans > cur:
				ans = cur
			j += 1
			k += 1
		print(ans)


def solve():
	(n, s) = map(int, input().split())
	l = list(map(int, input().split()))
	m = dict()
	sum = 0
	target = s
	if target == 0:
		print(0)
		return
	sum = 0
	mx = -1
	for i in range(n):
		sum += l[i]
		if sum == target:
			mx = max(mx, i + 1)
		if sum - target in m:
			mx = max(mx, i - m[sum - target])
		if sum not in m:
			m[sum] = i
	if mx == -1:
		print(-1)
	else:
		print(n - mx)
t = int(input())
while t > 0:
	solve()
	t -= 1


t = int(input())
for k in range(t):
	(n, s) = map(int, input().split())
	arr = [int(i) for i in input().split()]
	ss = sum(arr)
	if s == ss:
		print(0)
	elif s > ss:
		print(-1)
	else:
		ma = 0
		left = 0
		curSum = 0
		for right in range(left, n):
			curSum += arr[right]
			while curSum > s:
				curSum -= arr[left]
				left += 1
			ma = max(right - left + 1, ma)
		print(n - ma)


import sys

def lenOfLongSubarr(A, N, K):
	(i, j, sum) = (0, 0, 0)
	maxLen = -sys.maxsize - 1
	while j < N:
		sum += A[j]
		if sum < K:
			j += 1
		elif sum == K:
			maxLen = max(maxLen, j - i + 1)
			j += 1
		elif sum > K:
			while sum > K:
				sum -= A[i]
				i += 1
			if sum == K:
				maxLen = max(maxLen, j - i + 1)
			j += 1
	return maxLen
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	one = 0
	for i in range(n):
		if a[i] == 1:
			one += 1
	if one < k:
		print(-1)
		continue
	print(n - lenOfLongSubarr(a, n, k))


for x in range(int(input())):
	(a, b) = map(int, input().split())
	l = list(map(int, input().split()))
	curr_sum = sum(l)
	if curr_sum < b:
		print(-1)
	elif curr_sum - b == 0:
		print(0)
	else:
		ma = 0
		left = 0
		s = 0
		for x in range(left, a):
			s += l[x]
			if s > b:
				s -= l[left]
				left += 1
			ma = max(ma, x - left + 1)
		print(a - ma)


for _ in range(int(input())):
	(n, s) = [int(a) for a in input().split()]
	arr = [int(b) for b in input().split()]
	t = sum(arr)
	if t == s:
		print(0)
		continue
	if t < s:
		print(-1)
		continue
	t -= s
	table = {0: 0}
	pre = 0
	for (i, num) in enumerate(arr):
		if num:
			pre += 1
			table[pre] = i + 1
	best = table[t]
	suf = 0
	for (i, num) in enumerate(arr[::-1]):
		if num:
			suf += 1
			if suf <= t:
				best = min(best, i + 1 + table[t - suf])
			else:
				break
	print(best)


t = int(input())
while t != 0:
	(n, s) = map(int, input().split())
	a = list(map(int, input().split()))
	i1 = 0
	i2 = 0
	ns = 0
	ml = 0
	while i2 < n:
		ns += a[i2]
		i2 += 1
		while ns > s:
			ns -= a[i1]
			i1 += 1
		if ns == s:
			ml = max(ml, i2 - i1)
	if ml == 0:
		print(-1)
	else:
		print(n - ml)
	t -= 1


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	l = list(map(int, input().split()))
	sm = sum(l)
	p = []
	s = []
	if m > sm:
		print(-1)
	elif m == sm:
		print(0)
	else:
		for i in range(n):
			if l[i] == 1:
				s.append(i)
		p += s
		p = p[::-1]
		temp = sm - m
		a = -1
		b = n
		ans = 200001
		for i in range(temp + 1):
			last = temp - i
			res = 0
			if i == 0:
				res = b - s[-last]
			elif i == temp:
				res = p[-i] - a
			else:
				res = b - s[-last] + p[-i] - a
			ans = min(ans, res)
		print(ans)


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

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

def si():
	return str(input())

def ni():
	return int(input())
for T in range(int(input())):
	(n, s) = mi()
	a = li()
	ones = [-1]
	for i in range(n):
		if a[i] == 1:
			ones.append(i)
	ones.append(n)
	ans = 0
	if s > sum(a):
		print(-1)
		continue
	i = 1
	j = i + s - 1
	while j < len(ones) - 1:
		curr = ones[j + 1] - ones[i - 1] - 1
		ans = max(ans, curr)
		i += 1
		j += 1
	print(n - ans)


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	(n, s) = map(int, input().split())
	l = [int(i) for i in input().split()]
	if sum(l) < s:
		print(-1)
		continue
	left = 0
	smid = 0
	ans = 0
	for right in range(n):
		smid += l[right]
		while smid > s:
			smid -= l[left]
			left += 1
		if smid == s:
			ans = max(ans, right - left + 1)
	print(n - ans)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	left = []
	l = 0
	r = 0
	sm = 0
	if sum(arr) < k:
		print(-1)
		continue
	while True:
		if r == n:
			left.append(r - l)
			break
		if sm + arr[r] <= k:
			sm += arr[r]
			r += 1
		else:
			left.append(r - l)
			sm -= arr[l]
			l += 1
	print(n - max(left))


t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	l = list(map(int, input().split()))
	res = -1
	j = 0
	c = 0
	f = False
	for i in range(n):
		if l[i] == 1:
			c += 1
		if c >= m:
			f = True
		while c > m and j < i:
			if l[j] == 1:
				c -= 1
			j += 1
		res = max(res, i - j + 1)
	if not f:
		print(-1)
	else:
		print(n - res)


answers = []
for _ in range(int(input())):
	(n, s) = map(int, input().split())
	a = [int(x) for x in input().split()]
	diff = sum(a) - s
	if s > sum(a):
		answers.append(-1)
		continue
	elif s == sum(a):
		answers.append(0)
		continue
	ta = a.copy()
	lp = [0]
	for i in range(n):
		if ta[i] == 1:
			lp.append(i + 1)
	ta.reverse()
	rp = [0]
	for i in range(n):
		if ta[i] == 1:
			rp.append(i + 1)
	ans = n
	for i in range(diff + 1):
		ans = min(lp[diff - i] + rp[i], ans)
	answers.append(ans)
for ans in answers:
	print(ans)


for _ in range(int(input())):
	(n, s) = [int(i) for i in input().split()]
	A = [int(i) for i in input().split()]
	(i, res) = (0, -1)
	for j in range(n):
		s -= A[j]
		while s < 0:
			s += A[i]
			i += 1
			pass
		if s == 0:
			res = max(res, j - i + 1)
		pass
	if res != -1:
		res = n - res
	print(res)


for _ in range(int(input())):
	(n, s) = map(int, input().split())
	x = list(map(int, input().split()))
	if x.count(1) < s:
		print(-1)
		continue
	(i, j) = (0, 0)
	c = 0
	m = 0
	while j < n:
		c += x[j]
		while c > s:
			c -= x[i]
			i += 1
		m = max(m, j - i)
		j += 1
	print(n - m - 1)


for i in range(int(input())):
	(n, s) = map(int, input().split())
	a = list(map(int, input().split()))
	summ = 0
	ans = -1
	idx = 0
	for i in range(n):
		summ += a[i]
		while summ > s and idx < i:
			summ -= a[idx]
			idx += 1
		if summ == s:
			ans = max(ans, i - idx + 1)
	if ans == -1:
		print(-1)
	else:
		print(n - ans)


for h in range(int(input())):
	(n, s) = map(int, input().split())
	x = list(map(int, input().split()))
	if sum(x) < s:
		print(-1)
		continue
	(i, j) = (0, 0)
	c = 0
	m = 0
	while j < n:
		c += x[j]
		while c > s:
			c -= x[i]
			i += 1
		m = max(m, j - i)
		j += 1
	print(n - m - 1)


def check(a, n, s):
	i = 0
	j = 0
	sm = 0
	mx = 0
	while j < n:
		sm += a[j]
		if sm < s:
			j += 1
		elif sm == s:
			mx = max(mx, j - i + 1)
			j += 1
		else:
			while sm > s:
				sm -= a[i]
				i += 1
			if sm == s:
				mx = max(mx, j - i + 1)
			j += 1
	return mx
t = int(input())
for i in range(t):
	(n, s) = map(int, input().split())
	a = list(map(int, input().split()))
	sm = 0
	for i in range(n):
		sm += a[i]
	if sm < s:
		print(-1)
	elif sm == s:
		print(0)
	else:
		print(n - check(a, n, s))


for _ in range(int(input())):
	(n, s) = map(int, input().split())
	a = list(map(int, input().split()))
	(ans, sum, start) = (-1, 0, 0)
	for i in range(n):
		sum += a[i]
		while sum > s:
			sum -= a[start]
			start += 1
		if sum == s:
			ans = max(ans, i - start + 1)
	print(ans if ans == -1 else n - ans)


t = int(input())
for _ in range(t):
	(n, s) = map(int, input().split())
	arr = list(map(int, input().split()))
	if sum(arr) < s:
		print(-1)
		continue
	(i, j) = (0, 0)
	(cur, ans) = (0, 0)
	while i < n:
		cur += arr[i]
		while cur > s:
			cur -= arr[j]
			j += 1
		ans = ans if ans > i - j else i - j
		i += 1
	print(n - ans - 1)


for _ in range(int(input())):
	(u, x) = map(int, input().split())
	z = list(map(int, input().split()))
	if x > sum(z):
		print(-1)
	else:
		ans = 0
		i = 0
		j = 0
		curr = 0
		while j < u:
			curr += z[j]
			if curr < x:
				j += 1
			elif curr == x:
				ans = max(ans, j - i + 1)
				j += 1
			else:
				while curr > x:
					curr -= z[i]
					i += 1
				if curr == x:
					ans = max(ans, j - i + 1)
				j += 1
		print(u - ans)


t = int(input())
for _ in range(t):
	(n, s) = map(int, input().split())
	nums = list(map(int, input().split()))
	total = sum(nums)
	ans = 0
	res = []
	if total < s:
		ans = -1
	elif total == s:
		ans = 0
	else:
		left = 0
		add = 0
		for index in range(len(nums)):
			add += nums[index]
			if add > s:
				res.append(len(nums) - index + left)
				while add != s:
					add -= nums[left]
					left += 1
			if add == s and index == len(nums) - 1:
				res.append(left)
		ans = min(res)
	print(ans)


from collections import defaultdict
from itertools import groupby
for _ in range(int(input())):
	(n, s) = map(int, input().split())
	a = list(map(int, input().split()))
	m = defaultdict(int)
	sum1 = 0
	temp = 0
	for i in range(n):
		sum1 += a[i]
		if sum1 == s:
			temp = i + 1
		if sum1 not in m:
			m[sum1] = i
		if sum1 - s in m:
			if temp < i - m[sum1 - s]:
				temp = i - m[sum1 - s]
	if temp != 0:
		print(n - temp)
	else:
		print(-1)


t = int(input())
while t > 0:
	t -= 1
	(n, s) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	fidx = [-1]
	num_one = 0
	for i in range(n):
		if a[i] == 1:
			fidx.append(i)
			num_one += 1
	if num_one < s:
		print('-1')
		continue
	ans = fidx[num_one - s] + 1
	for i in range(n - 1, 0, -1):
		if a[i] == 1:
			num_one -= 1
		ans = min(ans, n - i + fidx[num_one - s] + 1)
	print(ans)


m = int(input())
for _ in range(m):
	(n, k) = map(int, input().split())
	mas = map(int, input().split())
	dists = []
	dist = 0
	total = 0
	for el in mas:
		if el:
			total += 1
			dists.append(dist)
			dist = 0
		else:
			dist += 1
	dists.append(dist)
	d = total - k
	if d < 0:
		print(-1)
	elif d == 0:
		print(0)
	else:
		rev_dists = dists[::-1]
		for q in range(1, d):
			dists[q] += dists[q - 1]
			rev_dists[q] += rev_dists[q - 1]
		ans = min(rev_dists[d - 1], dists[d - 1])
		for i in range(d - 1):
			ans = min(ans, dists[i] + rev_dists[d - 2 - i])
		print(ans + d)


for _ in range(int(input())):
	(n, s) = map(int, input().split())
	x = list(map(int, input().split()))
	y = []
	sum1 = 0
	l = 0
	r = 0
	if sum(x) < s:
		print(-1)
		continue
	else:
		while True:
			if r == n:
				y.append(r - l)
				break
			if sum1 + x[r] <= s:
				sum1 += x[r]
				r += 1
			else:
				y.append(r - l)
				sum1 -= x[l]
				l += 1
	print(n - max(y))


t = int(input())
for i in range(t):
	(n, s) = map(int, input().split())
	a = list(map(int, input().split()))
	if a.count(1) < s:
		print(-1)
		continue
	n = len(a)
	(i, j) = (0, 0)
	(sum, maxi) = (0, 0)
	for i in range(n):
		sum += a[i]
		while sum > s:
			sum -= a[j]
			j += 1
		maxi = max(maxi, i - j)
		i += 1
	print(n - maxi - 1)