package main

import (
	"bufio"
	"fmt"
	"os"
)

var (
	reader = bufio.NewReader(os.Stdin)
	writer = bufio.NewWriter(os.Stdout)
)

func readInt() int {
	var i int
	fmt.Fscan(reader, &i)
	return i
}

func max(u, v int) int {
	if u < v {
		u = v
	}

	return u
}

func min(u, v int) int {
	if u > v {
		u = v
	}

	return u
}

func main(){
	defer writer.Flush()
	T := readInt()

	for ;T > 0; T-- {
		n := readInt()
		a := make([]int, n)
		dp := make([]bool, n + 1)
		for i := 0; i < n; i++ {
			a[i] = readInt()
		}
		mp := make(map[int] []int)
		dp[0] = true

		for i := 1; i <= n; i++ {
			if i >= a[i - 1] + 1 && dp[i - a[i-1] - 1]{
				dp[i] = true
			}
			_, found := mp[i + a[i-1]]
			if !found {
				mp[i + a[i-1]] = make([]int, 0)
			}
			candidates, found := mp[i]
			for _, candidate := range candidates {
				if dp[candidate - 1] {
					dp[i] = true
					break
				}
			}
			mp[i + a[i-1]] = append(mp[i + a[i-1]], i)
		}
		if dp[n] {
			fmt.Fprintln(writer, "YES")
		} else {
			fmt.Fprintln(writer, "NO")
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	r := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	defer w.Flush()

	var T int
	for fmt.Fscan(r, &T); T > 0; T-- {

		var n int
		fmt.Fscan(r, &n)

		b := make([]int, n)
		for i := range b { fmt.Fscan(r, &b[i]) }

		dp := make([]bool, n+1)
		dp[0] = true

		for i := range b {
			if dp[i] && i+b[i] < n {
				dp[i+b[i]+1] = true
			}
			if i-b[i] >= 0 && dp[i-b[i]] {
				dp[i+1] = true
			}
		}

		if dp[n] {
			fmt.Fprintln(w, "YES")
		} else {
			fmt.Fprintln(w, "NO")
		}
	}

}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

func main() {
	e1740(os.Stdin, os.Stdout)
}

func e1740(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var T int
	var n int
	var x int

	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)

		a := make([]int, n)
		for i := 0; i < n; i++ {
			Fscan(in, &x)
			a[i] = x
		}

		d := make([]map[int]bool, n)
		for i := 0; i < n; i++ {
			d[i] = map[int]bool{}
			if a[i]+i < n {
				d[i][a[i]+i] = true
			}
		}
		for i := n - 1; i >= 0; i-- {
			if i-a[i] >= 0 {
				d[i-a[i]][i] = true
			}
		}
		//Fprintln(out, d)

		vis := make([]bool, n)
		var dfs func(i int) bool
		dfs = func(i int) bool {
			if vis[i]  {
				return false
			}
			for x, _:= range d[i] {
				if x == n-1 {
					return true
				}
				if dfs(x+1) {
					return true
				}
			}

			vis[i] = true
			return false
		}

		bl := dfs(0)
		ans := "YES"
		if !bl {
			ans = "NO"
		}

		Fprintln(out, ans)
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{}) { fmt.Fscanf(reader, f, a...) }

func solve() {
	var n int
	scanf("%d\n", &n)
	a := make([]int, n+1)
	for i:=1; i<=n; i++ {
		scanf("%d ", &a[i])
	}
	scanf("\n")

	// dpFront := make([]bool, n+1)
	// dpBack := make([]bool, n+1)
	// dpFront[0] = true
	// dpBack[0] = true

	dp := make([]bool, n+1)
	dp[0] = true

	for i:=1; i<=n; i++ {
		if  dp[i-1] && i + a[i] <= n {
			dp[i+a[i]] = true
		}
		if i-a[i] >= 1 && dp[i-a[i]-1] {
			dp[i] = true
		}
	}

	if dp[n] {
		printf("YES\n")
		return
	}
	printf("NO\n")
}

func main() {
	defer writer.Flush()

	var T int
	scanf("%d\n", &T)
	for tt:=1; tt<=T; tt++ {
		solve()
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var sc = bufio.NewScanner(os.Stdin)

const INF = 1000000000
const N = 2e5 + 5

func main() {
	sc.Buffer(make([]byte, 500000*1+1), 500000)
	sc.Split(bufio.ScanWords)

	T := nextInt()
	for T > 0 {
		T--
		n := nextInt()
		a := make([]int, n)
		for i := 0; i < n; i++ {
			a[i] = nextInt()
		}
		f := make([]bool, n)
		for i := 0; i < n; i++ {
			if i-a[i] == 0 || (i-a[i]-1 >= 0 && f[i-a[i]-1]) {
				f[i] = true
			}
			if (i == 0 || f[i-1]) && i+a[i] < n {
				f[i+a[i]] = true
			}
		}
		// for i := 0; i < n; i++ {
		if f[n-1] {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
		// }
		// fmt.Println()
	}
}

func max(args ...int) int {
	res := args[0]
	for _, v := range args {
		if v > res {
			res = v
		}
	}
	return res
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func nextString() string {
	sc.Scan()
	return sc.Text()
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var n int
		fmt.Fscan(in, &n)

		aa := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &aa[i])
		}

		mm := make([]bool, n+1)
		mm[0] = true
		for i, v := range aa {
			if mm[i] && i+v+1 < n+1 {
				mm[i+v+1] = true
			}
			if i-v >= 0 && mm[i-v] {
				mm[i+1] = true
			}
		}
		if mm[n] {
			fmt.Fprintln(out, "YES")
		} else {
			fmt.Fprintln(out, "NO")
		}

	}
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

const N int = 2e5 + 10

var dp [N][2]bool
var n, res int
var a [N]int

func solve(_r io.Reader, _w io.Writer) {

	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var T int
	Fscan(in, &T)
	for ; T > 0; T-- {
		Fscan(in, &n)
		dp[0][0] = true
		for i := 1; i <= n; i++ {
			Fscan(in, &a[i])
			for j := 0; j < 2; j++ {
				dp[i][j] = false
			}
		}
		for i := 1; i <= n; i++ {
			if i+a[i] <= n && (dp[i - 1][0] || dp[i - 1][1]) {
				dp[i+a[i]][0] = true
			}
			if i-a[i] > 0 && (dp[i-a[i]-1][0] || dp[i-a[i]-1][1]) {
				dp[i][1] = true
			}
		}
// 		for i := 1; i <= n; i ++ {
// 			Fprintln(out, "i = ", i, " ", dp[i][0], dp[i][1])
// 		}
		if dp[n][0] || dp[n][1] {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}

	}

}

func abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}

func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

func max(x, y int) int {
	if x > y {
		return x
	}
	return y
}

func gcd(x, y int) int {
	if y == 0 {
		return x
	}
	return gcd(y, x%y)
}

func lcm(x, y int) int {
	return x / gcd(x, y) * y
}

func main() { solve(os.Stdin, os.Stdout) }

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
)

const cf = true

func solve() {
	var n int
	fmt.Fscan(in, &n)
	arr := make([]int, n+1)
	for i := 1; i <= n; i++ {
		fmt.Fscan(in, &arr[i])
	}
	dp := make([]bool, n+1)
	dp[0] = true
	good := func(i int) bool {
		if i > n || i < 0 {
			return false
		}
		return true
	}
	check := func(i int) bool {
		if !good(i) {
			return false
		}
		return dp[i]
	}
	for i := 1; i <= n; i++ {
		if check(i - arr[i] - 1) {
			dp[i] = true
		}
		if dp[i-1] && good(i+arr[i]) {
			dp[i+arr[i]] = true
		}
	}
	if dp[n] == true {
		fmt.Fprintln(out, "YES")
	} else {
		fmt.Fprintln(out, "NO")
	}
}

func main() {
	defer out.Flush()
	t := 1
	fmt.Fscan(in, &t)
	for ; t > 0; t-- {
		solve()
	}
}

type SqrtInt interface {
	SignedSqrt | UnsignedSqrt
}

type SignedSqrt interface {
	~int | ~int32 | ~int64
}

type UnsignedSqrt interface {
	~uint | ~uint32 | ~uint64 | ~uintptr
}

func sqrt[T SqrtInt](x T) T {
	l, r := T(0), T(2000000123)
	for l < r {
		mid := (l + r) / 2
		if mid*mid > x {
			r = mid
		} else {
			l = mid + 1
		}
	}
	return l - 1
}

func ConvertInt(val string, base, toBase int) (string, error) {
	i, err := strconv.ParseInt(val, base, 64)
	if err != nil {
		return "", err
	}
	return strconv.FormatInt(i, toBase), nil
}

type set[T Ordered] struct {
	itms map[T]struct{}
}

func (s *set[T]) init() {
	if s.itms == nil {
		s.itms = make(map[T]struct{})
	}
}

func (s *set[T]) add(item T) *set[T] {
	s.init()
	s.itms[item] = exists
	return s
}

func (s *set[T]) remove(item T) *set[T] {
	s.init()
	delete(s.itms, item)
	return s
}

func (s *set[T]) has(item T) bool {
	s.init()
	_, c := s.itms[item]
	return c
}

func (s *set[T]) sortedItems() []T {
	s.init()
	items := make([]T, 0, s.size())
	for item := range s.itms {
		items = append(items, item)
	}
	sort.Slice(items, func(i, j int) bool {
		return items[i] < items[j]
	})
	return items
}

func (s *set[T]) items() []T {
	s.init()
	items := make([]T, 0, s.size())
	for item := range s.itms {
		items = append(items, item)
	}
	return items
}

func (s *set[T]) size() int {
	s.init()
	return len(s.itms)
}

var exists = struct{}{}

const MaxUint = ^uint(0)
const MinUint = 0
const MaxInt = int(MaxUint >> 1)
const MinInt = -MaxInt - 1

func reverseStr(s string) string {
	r := []rune(s)
	for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
		r[i], r[j] = r[j], r[i]
	}
	return string(r)
}

type Float interface {
	~float32 | ~float64
}

type Integer interface {
	Signed | Unsigned
}

type Signed interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64
}

type Unsigned interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

type Ordered interface {
	Integer | Float | ~string
}

func abs[T Signed](x T) T {
	if x < 0 {
		return -x
	}
	return x
}

func min[T Ordered](x, y T) T {
	if x < y {
		return x
	}
	return y
}

func max[T Ordered](x, y T) T {
	if x > y {
		return x
	}
	return y
}

var (
	in  = getReader()
	out = getWriter()
)

func getReader() *bufio.Reader {
	if cf {
		return bufio.NewReader(os.Stdin)
	} else {
		in, _ := os.Open("input.txt")
		return bufio.NewReader(in)
	}
}

func getWriter() *bufio.Writer {
	if cf {
		return bufio.NewWriter(os.Stdout)
	} else {
		out, _ := os.Create("output.txt")
		return bufio.NewWriter(out)
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var in, out = bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)

func solve() string {
	var n int
	fmt.Fscan(in, &n)
	v := make([]int, n+1)
	for i := 1; i <= n; i++ {
		fmt.Fscan(in, &v[i])
	}

	dp := make([]bool, n+1)
	dp[0] = true

	for i := 1; i <= n; i++ {
		if i+v[i] <= n && dp[i-1] {
			dp[i+v[i]] = true
		}
		if i-v[i]-1 >= 0 && dp[i-v[i]-1] {
			dp[i] = true
		}
	}

	if dp[n] {
		return "YES"
	}
	return "NO"
}

func main() {
	defer out.Flush()
	var t int
	for fmt.Fscanln(in, &t); t > 0; t-- {
		fmt.Fprintln(out, solve())
	}
}

// util functions
func max(x, y int) int {
	if x > y {
		return x
	}
	return y
}

func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

func abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}

func makeMatrix(n, m int) [][]int {
	x := make([][]int, n)
	for i := range x {
		x[i] = make([]int, m)
	}
	return x
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	r := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	defer w.Flush()

	var T int
	for fmt.Fscan(r, &T); T > 0; T-- {

		var n int
		fmt.Fscan(r, &n)

		b := make([]int, n+1)
		for i := 1; i <= n; i++ { fmt.Fscan(r, &b[i]) }

		dp := make([]bool, n+1)
		dp[0] = true

		for i := 1; i <= n; i++ {
			if dp[i-1] && i+b[i] <= n {
				dp[i+b[i]] = true
			} else if i-b[i] > 0 && dp[i-b[i]-1] {
				dp[i] = true
			}
		}

		if dp[n] {
			fmt.Fprintln(w, "YES")
		} else {
			fmt.Fprintln(w, "NO")
		}
	}

}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	r := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	defer w.Flush()

	var T int
	for fmt.Fscan(r, &T); T > 0; T-- {

		var n int
		fmt.Fscan(r, &n)

		b := make([]int, n)
		for i := range b { fmt.Fscan(r, &b[i]) }

		dp := make([]bool, n+1)
		dp[0] = true

		for i := range b {
			if dp[i] && i+b[i] < n {
				dp[i+b[i]+1] = true
			} else if i-b[i] >= 0 && dp[i-b[i]] {
				dp[i+1] = true
			}
		}

		if dp[n] {
			fmt.Fprintln(w, "YES")
		} else {
			fmt.Fprintln(w, "NO")
		}
	}

}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

func main() {
	e1740(os.Stdin, os.Stdout)
}

func e1740(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var T int
	var n int
	var x int

	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)

		a := make([]int, n)
		for i := 0; i < n; i++ {
			Fscan(in, &x)
			a[i] = x
		}

		d := make([]map[int]bool, n)
		for i := 0; i < n; i++ {
			d[i] = map[int]bool{}
			if a[i]+i < n {
				d[i][a[i]+i] = true
			}
		}
		for i := n - 1; i >= 0; i-- {
			if i-a[i] >= 0 {
				d[i-a[i]][i] = true
			}
		}

		vis := make([]bool, n)
		var dfs func(i int) bool
		dfs = func(i int) bool {
			if vis[i]  {
				return false
			}
			for x, _:= range d[i] {
				if x == n-1 {
					return true
				}
				if dfs(x) {
					return true
				}
			}

			vis[i] = true
			return false
		}

		bl := dfs(0)
		ans := "YES"
		if !bl {
			ans = "NO"
		}

		Fprintln(out, ans)
	}
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

func main() {
	e1740(os.Stdin, os.Stdout)
}

func e1740(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var T int
	var n int
	var x int

	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)

		a := make([]int, n)
		for i := 0; i < n; i++ {
			Fscan(in, &x)
			a[i] = x
		}

		d := make([][]int, n)
		for i := 0; i < n; i++ {
			if a[i]+i < n {
				d[i] = append(d[i], a[i]+i)
			}
		}
		for i := n - 1; i >= 0; i-- {
			if i-a[i] >= 0 {
				d[i-a[i]] = append(d[i-a[i]], i)
			}
		}

		vis := make([]int, n)
		var dfs func(i int) bool
		dfs = func(i int) bool {

			if vis[i] != 0 {
				return vis[i] == 1
			}
			for _, x:= range d[i] {
				if x == n-1 {
					return true
				}
				if dfs(x) {
					return true
				}
			}

			vis[i] = -1
			return false
		}

		bl := dfs(0)
		ans := "YES"
		if !bl {
			ans = "NO"
		}

		Fprintln(out, ans)
	}
}


package main

import (
	"bufio"
	"bytes"
	"fmt"
	"os"
	"sort"
)

func main() {
	reader := bufio.NewReader(os.Stdin)

	tc := readNum(reader)

	var buf bytes.Buffer

	for tc > 0 {
		tc--
		n, W := readTwoNums(reader)
		boxes := readNNums(reader, n)
		res := solve(W, boxes)
		buf.WriteString(fmt.Sprintf("%d\n", res))
	}

	fmt.Print(buf.String())
}

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

func solve(W int, boxes []int) int {
	sort.Ints(boxes)
	reverse(boxes)
	n := len(boxes)
	stack := make([]int, n)

	for i := 0; i < n; i++ {
		stack[i] = W
	}

	for i := 0; i < n; i++ {
		cur := boxes[i]
		j := sort.Search(n, func(j int) bool {
			return stack[j] >= cur
		})
		stack[j] -= cur
	}
	j := sort.Search(n, func(j int) bool {
		return stack[j] == W
	})

	return j
}

func reverse(arr []int) {
	for i, j := 0, len(arr)-1; i < j; i, j = i+1, j-1 {
		arr[i], arr[j] = arr[j], arr[i]
	}
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
)

var in = bufio.NewReader(os.Stdin)
var a []int

func main() {
	var t int
	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		a = make([]int, 20)
		var n, w int
		fmt.Fscan(in, &n, &w)
		for i := 0; i < n; i++ {
			var tp float64
			fmt.Fscan(in, &tp)
			a[int(math.Round(math.Log2(tp)))]++
		}
		rs := 0
		tp := 0
		for !done() {
			if tp == 0 {
				rs++
				tp = w
			}
			tp = fit(tp)
		}
		fmt.Println(rs)
	}
}

func fit(x int) int {
	for i := 19; i >= 0; i-- {
		tp := int(math.Pow(2, float64(i)))
		if a[i] > 0 && x >= tp {
			a[i]--
			return x - tp
		}
	}
	return 0
}

func done() bool {
	for i := 0; i < 20; i++ {
		if a[i] > 0 {
			return false
		}
	}
	return true
}

package main

import (
	"bufio"
	"fmt"
	"math/bits"
	"os"
)

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var n, w int
		fmt.Fscan(in, &n, &w)

		m := make([]int, 32)
		for i := 0; i < n; i++ {
			var v uint
			fmt.Fscan(in, &v)

			m[bits.Len(v)-1]++
		}

		z := n
		var tot int
		for z > 0 {
			k := w
			l := bits.Len(uint(k)) - 1
			x := 1 << l
			for i := l; i >= 0; i-- {
				if k <= 0 {
					break
				}

				d := min(m[i], k/x)
				m[i] -= d
				z -= d
				k -= d * x
				x /= 2
			}

			tot++
		}

		fmt.Fprintln(out, tot)
	}
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strconv"
)

type Scanner struct {
	bScanner *bufio.Scanner
}

func NewScanner() *Scanner {
	bScanner := bufio.NewScanner(os.Stdin)
	bScanner.Split(bufio.ScanWords)
	bScanner.Buffer([]byte{}, math.MaxInt32)
	return &Scanner{
		bScanner,
	}
}

func (s *Scanner) next() string {
	s.bScanner.Scan()
	return s.bScanner.Text()
}

func (s *Scanner) nextInt() int {
	s.bScanner.Scan()
	res, err := strconv.Atoi(s.bScanner.Text())
	if err != nil {
		panic(err)
	}
	return res
}

func (s *Scanner) nextInt64() int64 {
	s.bScanner.Scan()
	res, err := strconv.ParseInt(s.bScanner.Text(), 10, 64)
	if err != nil {
		panic(err)
	}
	return res
}

func (s *Scanner) nextFloat() float64 {
	s.bScanner.Scan()
	res, err := strconv.ParseFloat(s.bScanner.Text(), 64)
	if err != nil {
		panic(err)
	}
	return res
}

func (s *Scanner) nextInts(N int) []int {
	res := make([]int, N)
	for i := 0; i < N; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextInt64s(N int) []int64 {
	res := make([]int64, N)
	for i := 0; i < N; i++ {
		res[i] = s.nextInt64()
	}
	return res
}

func min(nums ...int) int {
	ans := math.MaxInt32
	for _, num := range nums {
		if num < ans {
			ans = num
		}
	}
	return ans
}

func max(nums ...int) int {
	ans := math.MinInt32
	for _, num := range nums {
		if num > ans {
			ans = num
		}
	}
	return ans
}

func main() {
	sc := NewScanner()
	wr := bufio.NewWriter(os.Stdout)
	defer wr.Flush()

	TC := sc.nextInt()
	for tc := 0; tc < TC; tc++ {
		N := sc.nextInt()
		W := sc.nextInt()
		arr := sc.nextInts(N)

		count := make([]int, 32)
		for i := 0; i < N; i++ {
			count[int(math.Log2(float64(arr[i])+1e-5))]++
		}
		ans := 0
		remWidth := 0
		remBox := N

		for remBox > 0 {
			ans++
			remWidth = W
			for i := 31; i >= 0; i-- {
				for count[i] > 0 && remWidth >= (1<<i) {
					count[i]--
					remWidth -= (1 << i)
					remBox--
				}
			}
		}

		fmt.Fprintln(wr, ans)
	}

}

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
)

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)

func scanf(f string, args ...interface{})  { fmt.Fscanf(reader, f, args...) }
func printf(f string, args ...interface{}) { fmt.Fprintf(writer, f, args...) }

var in = bufio.NewReader(os.Stdin)

func main() {
	var l int
	fmt.Scanf("%d\n", &l)
	for i := 0; i < l; i++ {
		m := make([]int, 20)
		var n, W int
		fmt.Fscan(in, &n, &W)
		for i := 0; i < n; i++ {
			var tp float64
			fmt.Fscan(in, &tp)
			m[int(math.Round(math.Log2(tp)))]++
		}
		fmt.Printf("%d\n", fn(W, m))
	}
}

func fn(W int, m []int) int {
	i := 0
	isnotnul := true
	for ; isnotnul; i++ {
		d := W
		for e := 19; e >= 0; e-- {
			itemWidth := int(math.Pow(2, float64(e)))
			if (m[e] > 0) && (d >= itemWidth) {
				m[e]--
				d = d - itemWidth
				e++
			}
		}
		isnotnul = false
		for e := 0; e < len(m); e++ {
			if m[e] > 0 {
				isnotnul = true
				break
			}
		}

	}

	return i
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
)

//Box Fitting
var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
var scanner *bufio.Scanner = bufio.NewScanner(reader)

func scanf(f string, args ...interface{})  { fmt.Fscanf(reader, f, args...) }
func printf(f string, args ...interface{}) { fmt.Fprintf(writer, f, args...) }

func boxFitting( W int, N int) {
	slots := make([]int, 32)
	minHeight := 0
	for x :=0 ; x< N ;x ++ {
		var v int
		if x != N-1 {
			scanf("%d", &v)
		} else {
			scanf("%d\n", &v)
		}
		slots[int(math.Log2(float64(v)))] += 1
	}
	var cur int
	for N> 0 {
		cur = W
		for i := 31; i >= 0; i-- {
			for slots[i] > 0 && cur >= (1<<i) {
				slots[i] -=1
				N -=1
				cur -= (1 << i)
			}
		}
		minHeight += 1
	}
	fmt.Println(minHeight)
}

func main() {

	var testCases int
	scanf("%d\n", &testCases)
	for i:=0 ; i< testCases; i++ {
		var N int
		var W int
		scanf("%d %d\n", &N, &W)
		boxFitting( W , N)

	}
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
)

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
var scanner *bufio.Scanner = bufio.NewScanner(reader)

func scanf(f string, args ...interface{})  { fmt.Fscanf(reader, f, args...) }
func printf(f string, args ...interface{}) { fmt.Fprintf(writer, f, args...) }

func toInt(buf []byte) (n int) {
	for _, v := range buf {
		n = (n * 10) + int(v-'0')
	}
	return
}

type Pair struct {
	a, b int
}

type Pairs []Pair

func (a Pairs) Len() int           { return len(a) }
func (a Pairs) Less(i, j int) bool { return a[i].a < a[j].a }
func (a Pairs) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

func main() {
	defer writer.Flush()

	var t int
	scanf("%d\n", &t)

	for t > 0 {
		var n, w, width int
		scanf("%d %d\n", &n, &w)
		count := make([]int, 20)
		for i := 0; i < n; i++ {
			if i == n-1 {
				scanf("%d\n", &width)
			} else {
				scanf("%d ", &width)
			}
			count[int(math.Log2(float64(width)))]++
		}
		height, spaceLeft := 1, w
		for i := 0; i < n; i++ {
			largest := -1
			for size := 19; size >= 0; size-- {
				if count[size] > 0 && ((1 << size) <= spaceLeft) {
					largest = size
					break
				}
			}
			if largest == -1 {
				spaceLeft = w
				height++
				for size := 19; size >= 0; size-- {
					if count[size] > 0 && ((1 << size) <= spaceLeft) {
						largest = size
						break
					}
				}
			}

			count[largest] -= 1
			spaceLeft -= (1 << largest)
		}
		printf("%d\n", height)
		t--
	}

}

package main

import (
	"fmt"
	"sort"
)

//Box Fitting

func boxFitting(listOfSizes []int , W int, N int) int {
	sort.Slice(listOfSizes, func(i, j int) bool {
		return listOfSizes[i] > listOfSizes[j]
	})
	slabs := []int{W}
	minHeight := 1
	for _, weight := range listOfSizes {
		if slabsX, flag := processExistingSlab(slabs, weight); !flag {
			if slabW := W - weight; slabW > 0 {
				slabs = append(slabs, slabW)
			}
			minHeight += 1
		} else {
			slabs = slabsX
		}
	}
	return minHeight
}

func processExistingSlab(slabs []int, weight int) ([]int,bool) {
	for slab_index := 0; slab_index < len(slabs); slab_index++ {
		if slabs[slab_index] >= weight {
			slabs[slab_index] -= weight
			return slabs, true

		}
	}
	return slabs, false
}

func main() {
	var testCases int
	fmt.Scanf("%d\n", &testCases)
	for i:=0 ; i< testCases; i++ {
		var N int
		var W int
		var numbs []int
		fmt.Scanf("%d %d\n", &N, &W)
		for x :=0 ; x< N;x ++ {
			var v int
			fmt.Scanf("%d", &v)
			numbs = append(numbs, v)
		}

		fmt.Println(boxFitting(numbs, W , N))
	}
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
)

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
var scanner *bufio.Scanner = bufio.NewScanner(reader)

func scanf(f string, args ...interface{})  { fmt.Fscanf(reader, f, args...) }
func printf(f string, args ...interface{}) { fmt.Fprintf(writer, f, args...) }

func toInt(buf []byte) (n int) {
	for _, v := range buf {
		n = (n * 10) + int(v-'0')
	}
	return
}

type Pair struct {
	a, b int
}

type Pairs []Pair

func (a Pairs) Len() int           { return len(a) }
func (a Pairs) Less(i, j int) bool { return a[i].a < a[j].a }
func (a Pairs) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

func main() {
	defer writer.Flush()

	var t int
	scanf("%d\n", &t)

	for t > 0 {
		var n, w, width int
		scanf("%d %d\n", &n, &w)
		count := make([]int, 20)
		for i := 0; i < n; i++ {
			if i == n-1 {
				scanf("%d\n", &width)
			} else {
				scanf("%d ", &width)
			}
			count[int(math.Log(float64(width)))]++
		}
		height, spaceLeft := 1, w
		for i := 0; i < n; i++ {
			largest := -1
			for size := 19; size >= 0; size-- {
				if count[size] != 0 && ((1 << size) <= spaceLeft) {
					largest = size
					break
				}
			}
			if largest == -1 {
				spaceLeft = w
				height++
				for size := 19; size >= 0; size-- {
					if count[size] != 0 && ((1 << size) <= spaceLeft) {
						largest = size
						break
					}
				}
			}

			count[largest] -= 1
			spaceLeft -= (1 << largest)
		}
		printf("%d\n", height+1)
		t--
	}

}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
var scanner *bufio.Scanner = bufio.NewScanner(reader)

func scanf(f string, args ...interface{})  { fmt.Fscanf(reader, f, args...) }
func printf(f string, args ...interface{}) { fmt.Fprintf(writer, f, args...) }

func toInt(buf []byte) (n int) {
	for _, v := range buf {
		n = (n * 10) + int(v-'0')
	}
	return
}

type Pair struct {
	a, b int
}

type Pairs []Pair

func (a Pairs) Len() int           { return len(a) }
func (a Pairs) Less(i, j int) bool { return a[i].a < a[j].a }
func (a Pairs) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

func main() {
	defer writer.Flush()

	var t int
	scanf("%d\n", &t)

	for t > 0 {
		var n, w int
		scanf("%d %d\n", &n, &w)
		arr := make([]int, n)
		for i := 0; i < n; i++ {
			if i == n-1 {
				scanf("%d\n", &arr[i])
			} else {
				scanf("%d ", &arr[i])
			}
		}
		sort.Ints(arr)
		height, width := 1, 0
		for i := n - 1; i >= 0; i-- {
			width += arr[i]
			if width > w {
				width = 0
				height++
			}
		}
		printf("%d\n", height)
		t--
	}

}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
var scanner *bufio.Scanner = bufio.NewScanner(reader)

func scanf(f string, args ...interface{})  { fmt.Fscanf(reader, f, args...) }
func printf(f string, args ...interface{}) { fmt.Fprintf(writer, f, args...) }

func toInt(buf []byte) (n int) {
	for _, v := range buf {
		n = (n * 10) + int(v-'0')
	}
	return
}

type Pair struct {
	a, b int
}

type Pairs []Pair

func (a Pairs) Len() int           { return len(a) }
func (a Pairs) Less(i, j int) bool { return a[i].a < a[j].a }
func (a Pairs) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

func main() {
	defer writer.Flush()

	var t int
	scanf("%d\n", &t)

	for t > 0 {
		var n, w int
		scanf("%d %d\n", &n, &w)
		arr := make([]int, n)
		for i := 0; i < n; i++ {
			if i == n-1 {
				scanf("%d\n", &arr[i])
			} else {
				scanf("%d ", &arr[i])
			}
		}
		sort.Ints(arr)
		height, width := 1, 0
		for i := 0; i < n; i++ {
			width += arr[i]
			if width > w {
				width = 0
				height++
			}
		}
		printf("%d\n", height)
		t--
	}

}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var in = bufio.NewReader(os.Stdin)

func main() {
	var t int
	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		var n int
		fmt.Scan(&n)
		for ; ; n++ {
			s := calSum(n)
			j := s
			for j = s; j >= 1; j-- {
				if n%j == 0 && s%j == 0 {
					break
				}
			}
			if j > 1 {
				fmt.Println(n)
				break
			}
		}
	}
}

func calSum(n int) int {
	s := 0
	for n > 0 {
		s += n % 10
		n /= 10
	}
	return s
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strconv"
)

type Scanner struct {
	bScanner *bufio.Scanner
}

func NewScanner() *Scanner {
	bScanner := bufio.NewScanner(os.Stdin)
	bScanner.Split(bufio.ScanWords)
	bScanner.Buffer([]byte{}, math.MaxInt32)
	return &Scanner{
		bScanner,
	}
}

func (s *Scanner) next() string {
	s.bScanner.Scan()
	return s.bScanner.Text()
}

func (s *Scanner) nextInt() int {
	s.bScanner.Scan()
	res, err := strconv.Atoi(s.bScanner.Text())
	if err != nil {
		panic(err)
	}
	return res
}

func (s *Scanner) nextInt64() int64 {
	s.bScanner.Scan()
	res, err := strconv.ParseInt(s.bScanner.Text(), 10, 64)
	if err != nil {
		panic(err)
	}
	return res
}

func (s *Scanner) nextFloat() float64 {
	s.bScanner.Scan()
	res, err := strconv.ParseFloat(s.bScanner.Text(), 64)
	if err != nil {
		panic(err)
	}
	return res
}

func (s *Scanner) nextInts(N int) []int {
	res := make([]int, N)
	for i := 0; i < N; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextInt64s(N int) []int64 {
	res := make([]int64, N)
	for i := 0; i < N; i++ {
		res[i] = s.nextInt64()
	}
	return res
}

func min(nums ...int) int {
	ans := math.MaxInt32
	for _, num := range nums {
		if num < ans {
			ans = num
		}
	}
	return ans
}

func max(nums ...int) int {
	ans := math.MinInt32
	for _, num := range nums {
		if num > ans {
			ans = num
		}
	}
	return ans
}

func main() {
	sc := NewScanner()
	wr := bufio.NewWriter(os.Stdout)
	defer wr.Flush()

	TC := sc.nextInt()
	for tc := 0; tc < TC; tc++ {
		N := sc.nextInt()
		W := sc.nextInt64()
		arr := sc.nextInt64s(N)

		sum := int64(0)
		for i := 0; i < N; i++ {
			sum += arr[i]
		}
		ans := (sum + W - 1) / W
		fmt.Fprintln(wr, ans)
	}

}


package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
)

var scanner *bufio.Scanner = bufio.NewScanner(os.Stdin)

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func ReadInt() int {
	scanner.Scan()
	n, err := strconv.Atoi(scanner.Text())
	if err != nil {
		panic(err)
	}
	return n
}

func main() {
	scanner.Split(bufio.ScanWords)

	n := ReadInt()
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		arr[i] = ReadInt()
		if arr[i] < 0 {
			arr[i] *= -1
		}
	}

	sort.Ints(arr)

	j := 0
	var ct int64
	for i := 1; i < n; i++ {
		for 2*arr[j] < arr[i] && j < i {
			j++
		}
		ct += int64(i - j)
	}
	fmt.Println(ct)
}

// http://codeforces.com/contest/1166/problem/C

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var n int
	fmt.Fscanf(in, "%d\n", &n)
	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(in, "%d", &a[i])
	}
	fmt.Fprintln(out, Solve(n, a))
}

func Solve(n int, a []int) int64 {
	b := make([]int, n)
	for i := int(0); i < n; i++ {
		if a[i] < 0 {
			b[i] = -a[i]
		} else {
			b[i] = a[i]
		}
	}
	sort.Ints(b)

	cnt := int64(0)
	for l := int(0); l < n; l++ {
		r := BinSearch(b, l, b[l]*2)
		cnt += int64(r - l)
	}

	return cnt
}

func BinSearch(a []int, l, n int) int {
	l = l - 1
	r := len(a)
	for r-l > 1 {
		m := l + (r-l)/2
		if a[m] <= n {
			l = m
		} else {
			r = m
		}
	}
	return l
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
)

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())
	a := make([]int, n)
	for i := 0; i < n; i++ {
		scanner.Scan()
		a[i], _ = strconv.Atoi(scanner.Text())
		if a[i] < 0 {
			a[i] = -a[i]
		}
	}
	sort.Ints(a)
	ans := int64(0)
	for i, j := 0, 0; i < n; i++ {
		for j < i && a[j] * 2 < a[i] {
			j++
		}
		ans += int64(i - j)
	}
	fmt.Print(ans)
}

// Author: sighduck
// URL: https://codeforces.com/problemset/problem/1166/C

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

func abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}

func valid(x int, y int) bool {
	return y <= 2*x && x <= 2*y
}

func search(i, n, start, end int, a []int) int {
	if start < 0 || end > n-1 || start > end {
		return 0
	}
	m := (start + end + 1) / 2
	if valid(a[m], a[i]) {
		if start == end {
			return start - i
		}
		return search(i, n, m, end, a)
	}
	return search(i, n, start, m-1, a)
}

func Solve(n int, a []int) int64 {
	for i := 0; i < n; i++ {
		a[i] = abs(a[i])
	}
	sort.Ints(a)
	ans := int64(0)
	for i, _ := range a {
		ans += int64(search(i, n, i+1, n-1, a))
	}
	return ans
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var n int
	fmt.Fscanf(reader, "%d\n", &n)
	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &a[i])
	}

	fmt.Fprintf(writer, "%d\n", Solve(n, a))
}

// http://codeforces.com/contest/1166/problem/C

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var n int
	fmt.Fscanf(in, "%d\n", &n)
	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(in, "%d", &a[i])
	}
	fmt.Fprintln(out, Solve(n, a))
}

func Solve(n int, a []int) int {
	b := make([]int, n)
	for i := 0; i < n; i++ {
		if a [i] < 0 {
			b[i] = -a[i]
		} else {
			b[i] = a[i]
		}
	}
	sort.Ints(b)

	cnt := 0
	for l := 0; l < n; l++ {
		r := BinSearch(b, l, b[l]*2)
		cnt += r - l
	}

	return cnt
}

func BinSearch(a []int, l, n int) int {
	l = l  -  1
	r := len(a)
	for r-l > 1 {
		m := l + (r-l)/2
		if a[m] <= n {
			l = m
		} else {
			r = m
		}
	}
	return l
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
)

var scanner *bufio.Scanner = bufio.NewScanner(os.Stdin)

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func ReadInt() int {
	scanner.Scan()
	n, err := strconv.Atoi(scanner.Text())
	if err != nil {
		panic(err)
	}
	return n
}

func main() {
	scanner.Split(bufio.ScanWords)

	n := ReadInt()
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		arr[i] = ReadInt()
		if arr[i] < 0 {
			arr[i] *= -1
		}
	}

	sort.Ints(arr)

	j := 0
	ct := 0
	for i := 1; i < n; i++ {
		for 2*arr[j] < arr[i] && j < n {
			j++
		}
		ct += i - j
	}

	fmt.Println(ct)
}

// Author: sighduck
// URL: https://codeforces.com/problemset/problem/1166/C

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

func abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}

func valid(x int, y int) bool {
	return y <= 2*x && x <= 2*y
}

func search(i, n, start, end int, a []int) int {
	if start < 0 || end > n-1 || start > end {
		return 0
	}
	m := (start + end + 1) / 2
	if valid(a[m], a[i]) {
		if start == end {
			return start - i
		}
		return search(i, n, m, end, a)
	}
	return search(i, n, start, m-1, a)
}

func Solve(n int, a []int) int {
	for i := 0; i < n; i++ {
		a[i] = abs(a[i])
	}
	sort.Ints(a)
	ans := 0
	for i, _ := range a {
		ans += search(i, n, i+1, n-1, a)
	}
	return ans
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var n int
	fmt.Fscanf(reader, "%d\n", &n)
	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &a[i])
	}

	fmt.Fprintf(writer, "%d\n", Solve(n, a))
}