package main

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

func main() {
	defer Flush()

	n := readi()
	s := readb()
	a := make([]int, n)
	b := make([]int, n)
	for i := range a {
		a[i] = readi()
		b[i] = readi()
	}

	m := make(map[int]bool)
	for i, c := range s {
		if c == '1' {
			m[i] = true
		}
	}

	ans := len(m)
	for t := 1; t <= 2*3*4*5+100; t++ {
		for i := range a {
			if t < b[i] {
				continue
			}
			if (t-b[i])%a[i] == 0 {
				if m[i] {
					delete(m, i)
				} else {
					m[i] = true
				}
			}
		}
		ans = max(ans, len(m))
	}
	println(ans)

}

// -----------------------------------------------------------------------------

// IO helper functions

// Returns next token from input.  It must be initialized by SetInput()
// before the first call.
var nextToken func() ([]byte, error)
var nextLine func() ([]byte, error)

// Holds a buffer for output.  It must be initialized by SetOutput().
// All IO fucntions (read*() and [e]print*()) should write to OutputWriter
// instead of this.
var OutputBuffer *bufio.Writer

// Holds an io.Writer.  It must be initialized by SetOutput()
var OutputWriter io.Writer

// Set IO functions for interactive input/output.
func SetInteractive(w io.Writer, r io.Reader) {
	SetUnbefferedInput(r)
	OutputBuffer = nil
	OutputWriter = w
}

// Setup OutputBuffer and OutputWriter.
func SetOutput(w io.Writer) {
	OutputBuffer = bufio.NewWriter(w)
	OutputWriter = OutputBuffer
}

// Flushes OutputBuffer
func Flush() {
	if OutputBuffer != nil {
		OutputBuffer.Flush()
	}
}

// Returns true if c is a white space
func IsSpace(c byte) bool {
	switch c {
	case '\t', '\n', '\v', '\f', '\r', ' ':
		return true
	}
	return false
}

func IsNewLine(c byte) bool {
	switch c {
	case '\n', '\r':
		return true
	}
	return false
}

// Setup nextToken with input buffer.
func SetInput(r io.Reader) {
	buf := new(bytes.Buffer)
	var b []byte

	var i int
	rest := func() ([]byte, error) {
		for i < len(b) && IsSpace(b[i]) {
			i++
		}
		if i == len(b) {
			return nil, io.ErrUnexpectedEOF
		}
		j := i
		for i < len(b) && !IsSpace(b[i]) {
			i++
		}
		return b[j:i], nil
	}
	initial := func() ([]byte, error) {
		io.Copy(buf, r)
		b = buf.Bytes()
		nextToken = rest
		return rest()
	}
	nextToken = initial

	restLn := func() ([]byte, error) {
		for i < len(b) && IsNewLine(b[i]) {
			i++
		}
		if i == len(b) {
			return nil, io.ErrUnexpectedEOF
		}
		j := i
		for i < len(b) && !IsNewLine(b[i]) {
			i++
		}
		return b[j:i], nil
	}

	initialLn := func() ([]byte, error) {
		io.Copy(buf, r)
		b = buf.Bytes()
		nextLine = restLn
		return restLn()
	}
	nextLine = initialLn
}

// Setup nextToken without input buffer.
func SetUnbefferedInput(r io.Reader) {
	buf := bufio.NewReader(r)
	var b []byte

	var i int
	nextToken = func() ([]byte, error) {
		var err error
		if i == len(b) {
			b, err = buf.ReadBytes('\n')
			if err != nil {
				return nil, err
			}
			i = 0
			j := len(b) - 1
			for 0 <= j && IsSpace(b[j]) {
				j--
			}
			b = b[0 : j+1]
		}
		for i < len(b) && IsSpace(b[i]) {
			i++
		}
		j := i
		for i < len(b) && !IsSpace(b[i]) {
			i++
		}
		if i == j {
			return nil, io.ErrUnexpectedEOF
		}
		return b[j:i], nil
	}
}

// -----------------------------------------------------------------------------

// IO functions

// Reads next token and return it as []byte
func readb() []byte {
	b, err := nextToken()
	if err != nil {
		panic(err)
	}
	return b[:len(b):len(b)]
}

// Reads next token and return it as string
func reads() string {
	return string(readb())
}

// Read next line as []byte.  Trailing '\n' will not be included.
// See also comments on readb()
func readbln() []byte {
	b, err := nextLine()
	if err != nil {
		panic(err)
	}
	return b[:len(b):len(b)]
}

// Read next line as string
func readsln() string {
	return string(readbln())
}

// Reads next token and return it as int64
func readll() int64 {
	i, err := strconv.ParseInt(reads(), 10, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// Reads next token and return it as int
func readi() int {
	return int(readll())
}

// Reads next token and return it as float64
func readf() float64 {
	f, err := strconv.ParseFloat(reads(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// Write args to OutputWriter with the format f
func printf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(OutputWriter, f, args...)
}

// Write args to OutputWriter without format
func println(args ...interface{}) (int, error) {
	return fmt.Fprintln(OutputWriter, args...)
}

// Write args to stderr with the format f
func eprintf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(os.Stderr, f, args...)
}

// Write args to stderr without format
func eprintln(args ...interface{}) (int, error) {
	return fmt.Fprintln(os.Stderr, args...)
}

// -----------------------------------------------------------------------------

// Utilities

func sumSlice(a []int) int {
	var res int
	for _, v := range a {
		res += v
	}
	return res
}

func sumSlicell(a []int64) int64 {
	var res int64
	for _, v := range a {
		res += v
	}
	return res
}

func readInts(N int) (int, []int) {
	if N == 0 {
		N = readi()
	}
	a := make([]int, N)
	for i := range a {
		a[i] = readi()
	}
	return N, a
}

func readIntsll(N int) (int, []int64) {
	if N == 0 {
		N = readi()
	}
	a := make([]int64, N)
	for i := range a {
		a[i] = readll()
	}
	return N, a
}

// reverse slice in place
func reverse(a []int) {
	for i, j := 0, len(a)-1; i < j; i, j = i+1, j-1 {
		a[i], a[j] = a[j], a[i]
	}
}

// areverse: allocating reverse
func areverse(a []int) []int {
	b := make([]int, len(a))
	for i := range a {
		b[i] = a[len(a)-i-1]
	}
	return b
}

// -----------------------------------------------------------------------------

// Simple math functions

const (
	// big prime
	INF  = 1000000007
	INF2 = 1000000009
	INF3 = 998244353
)

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

func minll(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

func minf(a, b float64) float64 {
	if a < b {
		return a
	}
	return b
}

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

func maxll(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}

func maxf(a, b float64) float64 {
	if a < b {
		return b
	}
	return a
}

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

func absll(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}

func absf(a float64) float64 {
	if a < 0 {
		return -a
	}
	return a
}

// egcd(a, b) returns d, x, y:
//   d is gcd(a,b)
//   x, y are  integers that satisfy ax + by = d
func egcd(a, b int) (int, int, int) {
	if b == 0 {
		return a, 1, 0
	}
	d, x, y := egcd(b, a%b)
	return d, y, x - a/b*y
}

func egcdll(a, b int64) (int64, int64, int64) {
	if b == 0 {
		return a, 1, 0
	}
	d, x, y := egcdll(b, a%b)
	return d, y, x - a/b*y
}

func gcd(a, b int) int {
	d, _, _ := egcd(a, b)
	return d
}

func gcdll(a, b int64) int64 {
	d, _, _ := egcdll(a, b)
	return d
}

// set up IO functions
func init() {
	// for non-interactive
	SetInput(os.Stdin)
	SetOutput(os.Stdout)

	// Enable below when interactive.  Its ok to leave above intact.
	// SetInteractive(os.Stdout, os.Stdin)
}

/*
URL:
https://codeforces.com/problemset/problem/1209/B
*/

package main

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

/********** FAU standard libraries **********/

//fmt.Sprintf("%b\n", 255) 	// binary expression

/********** I/O usage **********/

//str := ReadString()
//i := ReadInt()
//X := ReadIntSlice(n)
//S := ReadRuneSlice()
//a := ReadFloat64()
//A := ReadFloat64Slice(n)

//str := ZeroPaddingRuneSlice(num, 32)
//str := PrintIntsLine(X...)

/*******************************************************************/

const (
	// General purpose
	MOD          = 1000000000 + 7
	ALPHABET_NUM = 26
	INF_INT64    = math.MaxInt64
	INF_BIT60    = 1 << 60
	INF_INT32    = math.MaxInt32
	INF_BIT30    = 1 << 30
	NIL          = -1

	// for dijkstra, prim, and so on
	WHITE = 0
	GRAY  = 1
	BLACK = 2
)

func init() {
	// bufio.ScanWords <---> bufio.ScanLines
	ReadString = newReadString(os.Stdin, bufio.ScanWords)
	stdout = bufio.NewWriter(os.Stdout)
}

var (
	n    int
	S    []rune
	A, B []int

	L [10000 + 50]int
)

func main() {
	n = ReadInt()
	S = ReadRuneSlice()
	for i := 0; i < n; i++ {
		a, b := ReadInt2()
		A = append(A, a)
		B = append(B, b)
	}

	for i := 0; i < n; i++ {
		a, b := A[i], B[i]

		cur := 1
		if S[i] == '1' {
			cur = -1
			L[0]++
		}

		for j := b + 1; j <= 10000; j += a {
			L[j] += cur
			if cur == 1 {
				cur = -1
			} else {
				cur = 1
			}
		}
	}

	for i := 1; i <= 10000; i++ {
		L[i] += L[i-1]
	}

	ans := 0
	for i := 0; i <= 10000; i++ {
		ChMax(&ans, L[i])
	}
	fmt.Println(ans)
}

// ChMax accepts a pointer of integer and a target value.
// If target value is LARGER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMax(updatedValue *int, target int) bool {
	if *updatedValue < target {
		*updatedValue = target
		return true
	}
	return false
}

/*******************************************************************/

/*********** I/O ***********/

var (
	// ReadString returns a WORD string.
	ReadString func() string
	stdout     *bufio.Writer
)

func newReadString(ior io.Reader, sf bufio.SplitFunc) func() string {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	r.Split(sf)

	return func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
}

// ReadInt returns an integer.
func ReadInt() int {
	return int(readInt64())
}
func ReadInt2() (int, int) {
	return int(readInt64()), int(readInt64())
}
func ReadInt3() (int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64())
}
func ReadInt4() (int, int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64()), int(readInt64())
}

// ReadInt64 returns as integer as int64.
func ReadInt64() int64 {
	return readInt64()
}
func ReadInt64_2() (int64, int64) {
	return readInt64(), readInt64()
}
func ReadInt64_3() (int64, int64, int64) {
	return readInt64(), readInt64(), readInt64()
}
func ReadInt64_4() (int64, int64, int64, int64) {
	return readInt64(), readInt64(), readInt64(), readInt64()
}

func readInt64() int64 {
	i, err := strconv.ParseInt(ReadString(), 0, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// ReadIntSlice returns an integer slice that has n integers.
func ReadIntSlice(n int) []int {
	b := make([]int, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt()
	}
	return b
}

// ReadInt64Slice returns as int64 slice that has n integers.
func ReadInt64Slice(n int) []int64 {
	b := make([]int64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt64()
	}
	return b
}

// ReadFloat64 returns an float64.
func ReadFloat64() float64 {
	return float64(readFloat64())
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(ReadString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// ReadFloatSlice returns an float64 slice that has n float64.
func ReadFloat64Slice(n int) []float64 {
	b := make([]float64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadFloat64()
	}
	return b
}

// ReadRuneSlice returns a rune slice.
func ReadRuneSlice() []rune {
	return []rune(ReadString())
}

/*********** Debugging ***********/

// ZeroPaddingRuneSlice returns binary expressions of integer n with zero padding.
// For debugging use.
func ZeroPaddingRuneSlice(n, digitsNum int) []rune {
	sn := fmt.Sprintf("%b", n)

	residualLength := digitsNum - len(sn)
	if residualLength <= 0 {
		return []rune(sn)
	}

	zeros := make([]rune, residualLength)
	for i := 0; i < len(zeros); i++ {
		zeros[i] = '0'
	}

	res := []rune{}
	res = append(res, zeros...)
	res = append(res, []rune(sn)...)

	return res
}

// Strtoi is a wrapper of strconv.Atoi().
// If strconv.Atoi() returns an error, Strtoi calls panic.
func Strtoi(s string) int {
	if i, err := strconv.Atoi(s); err != nil {
		panic(errors.New("[argument error]: Strtoi only accepts integer string"))
	} else {
		return i
	}
}

// PrintIntsLine returns integers string delimited by a space.
func PrintIntsLine(A ...int) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.Itoa(A[i])
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintIntsLine returns integers string delimited by a space.
func PrintInts64Line(A ...int64) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.FormatInt(A[i], 10) // 64bit int version
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintfDebug is wrapper of fmt.Fprintf(os.Stderr, format, a...)
func PrintfDebug(format string, a ...interface{}) {
	fmt.Fprintf(os.Stderr, format, a...)
}

// PrintfBufStdout is function for output strings to buffered os.Stdout.
// You may have to call stdout.Flush() finally.
func PrintfBufStdout(format string, a ...interface{}) {
	fmt.Fprintf(stdout, format, a...)
}


package main

import "fmt"

func main() {
	is_prime := make([]bool, 101, 101)
	for i := 1; i <= 100; i++ {
		is_prime[i] = true
	}

	for i := 2; i <= 10; i++ {
		if !is_prime[i] {
			continue
		}
		for j := i + i; j <= 100; j += i {
			is_prime[j] = false
		}
	}

	var judge string
	cnt := 0
	for i := 2; i <= 50; i++ {
		if is_prime[i] {
			fmt.Println(i)
			fmt.Scan(&judge)
			if judge == "yes" {
				cnt++
			}

			if i*i <= 100 {
				fmt.Println(i * i)
				fmt.Scan(&judge)
				if judge == "yes" {
					cnt++
				}
			}

			if cnt >= 2 {
				break
			}
		}
	}

	if cnt >= 2 {
		fmt.Println("composite")
	} else {
		fmt.Println("prime")
	}
}

package main

import "fmt"
import "os"

func main() {
    var p = []int{4, 9, 25, 49, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47}
    var i int = 0
    var count int = 0
    for i < 19 {
        fmt.Printf("%d\n", p[i])
        os.Stdout.Sync()
        var line string
        fmt.Scanln(&line)
        if line == "yes" {
            count++
            if (count > 1 || i < 4) {
                fmt.Printf("composite")
                os.Stdout.Sync()
                os.Exit(0)
            }
        }
        i++
    }
    fmt.Printf("prime")
    os.Stdout.Sync()
}

package main

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

func main() {
	primes := []int{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47}
	cum := 1
	divisors := 0
	bufin, bufout := bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
	for _, p := range primes {
		if p*cum > 100 {
			break
		}
		_, err := fmt.Fprintln(bufout, p)
		if err != nil {
			panic(err)
		}
		err = bufout.Flush()
		if err != nil {
			panic(err)
		}
		var s string
		_, err = fmt.Fscanln(bufin, &s)
		if err != nil {
			panic(err)
		}
		if s == "yes" {
			divisors++
			cum *= p
			if divisors < 2 && p*p <= 100 {
				_, err := fmt.Fprintln(bufout, p*p)
				if err != nil {
					panic(err)
				}
				err = bufout.Flush()
				if err != nil {
					panic(err)
				}
				var s string
				_, err = fmt.Fscanln(bufin, &s)
				if err != nil {
					panic(err)
				}
				if s == "yes" {
					divisors++
					cum *= p
				}
			}
		}
		if divisors >= 2 {
			break
		}
	}
	isprime := divisors < 2
	results := map[bool]string{
		false: "composite",
		true:  "prime",
	}
	_, err := fmt.Fprintln(bufout, results[isprime])
	if err != nil {
		panic(err)
	}
	err = bufout.Flush()
	if err != nil {
		panic(err)
	}
}

package main

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

const maxCapacity = 1024

var scanner *bufio.Scanner
var writer *bufio.Writer

func ioPrepare() {
	scanner = bufio.NewScanner(os.Stdin)
	writer = bufio.NewWriter(os.Stdout)

	buf := make([]byte, maxCapacity)
	scanner.Buffer(buf, maxCapacity)
}

func solve() {
	primes := []int{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47}
	count := 0
	prime := 0
	for _, i := range primes {
		fmt.Println(i)
		scanner.Scan()
		result := scanner.Text()
		if result == "yes" {
			prime = i
			count++
		}
	}
	if count == 0 {
		fmt.Println("prime")
	} else if count >= 2 {
		fmt.Println("composite")
	} else {
		num := prime * prime
		if num > 100 {
			fmt.Println("prime")
		} else {
			fmt.Println(num)
			scanner.Scan()
			result := scanner.Text()
			if result == "yes" {
				fmt.Println("composite")
			} else {
				fmt.Println("prime")
			}
		}
	}
}

func main() {
	ioPrepare()
	solve()
}

package main

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

const maxCapacity = 1024 * 1024 * 10

var scanner *bufio.Scanner
var writer *bufio.Writer

func ioPrepare() {
	scanner = bufio.NewScanner(os.Stdin)
	writer = bufio.NewWriter(os.Stdout)

	buf := make([]byte, maxCapacity)
	scanner.Buffer(buf, maxCapacity)
}

func solve() {
	primes := []int{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47}
	count := 0
	prime := 0
	for _, i := range primes {
		fmt.Println(i)
		scanner.Scan()
		result := scanner.Text()
		if result == "yes" {
			prime = i
			count++
		}
	}
	if count == 0 {
		fmt.Println("prime")
	} else if count >= 2 {
		fmt.Println("composite")
	} else {
		num := prime * prime
		if num > 100 {
			fmt.Println("prime")
		} else {
			fmt.Println(num)
			scanner.Scan()
			result := scanner.Text()
			if result == "yes" {
				fmt.Println("composite")
			} else {
				fmt.Println("prime")
			}
		}
	}
}

func main() {
	ioPrepare()
	solve()
}

package main

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

const maxCapacity = 1024 * 1024 * 10

var scanner *bufio.Scanner
var writer *bufio.Writer

func ioPrepare() {
	scanner = bufio.NewScanner(os.Stdin)
	writer = bufio.NewWriter(os.Stdout)

	buf := make([]byte, maxCapacity)
	scanner.Buffer(buf, maxCapacity)
}

func solve() {
	primes := []int{2, 3, 5, 7}
	count := 0
	prime := 0
	for _, i := range primes {
		fmt.Println(i)
		scanner.Scan()
		result := scanner.Text()
		if result == "yes" {
			prime = i
			count++
		}
	}
	if count == 0 {
		fmt.Println("prime")
	} else if count >= 2 {
		fmt.Println("composite")
	} else {
		num := prime * prime
		fmt.Println(num)
		scanner.Scan()
		result := scanner.Text()
		if result == "yes" {
			fmt.Println("composite")
		} else {
			fmt.Println("prime")
		}
	}
}

func main() {
	ioPrepare()
	solve()
}

package main

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

const maxCapacity = 1024 * 1024 * 10

var scanner *bufio.Scanner
var writer *bufio.Writer

func ioPrepare() {
	scanner = bufio.NewScanner(os.Stdin)
	writer = bufio.NewWriter(os.Stdout)

	buf := make([]byte, maxCapacity)
	scanner.Buffer(buf, maxCapacity)
}

func solve() {
	primes := []int{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47}
	count := 0
	prime := 0
	for _, i := range primes {
		fmt.Println(i)
		scanner.Scan()
		result := scanner.Text()
		if result == "yes" {
			prime = i
			count++
		}
	}
	if count == 0 {
		fmt.Println("prime")
	} else if count >= 2 {
		fmt.Println("composite")
	} else {
		num := prime * prime
		fmt.Println(num)
		scanner.Scan()
		result := scanner.Text()
		if result == "yes" {
			fmt.Println("composite")
		} else {
			fmt.Println("prime")
		}
	}
}

func main() {
	ioPrepare()
	solve()
}

package main

import "fmt"

func main() {
	is_prime := make([]bool, 101, 101)
	for i := 1; i <= 100; i++ {
		is_prime[i] = true
	}

	for i := 2; i <= 10; i++ {
		if !is_prime[i] {
			continue
		}
		for j := i + i; j <= 10; j += i {
			is_prime[j] = false
		}
	}

	var judge string
	cnt := 0
	for i := 2; i <= 100; i++ {
		if is_prime[i] {
			fmt.Println(i)
			fmt.Scan(&judge)
			if judge == "yes" {
				cnt++
			}

			if i*i <= 100 {
				fmt.Println(i * i)
				fmt.Scan(&judge)
				if judge == "yes" {
					cnt++
				}
			}

			if cnt >= 2 {
				break
			}
		}
	}

	if cnt >= 2 {
		fmt.Println("composite")
	} else {
		fmt.Println("prime")
	}
}

package main

import "fmt"

func main() {
	is_prime := make([]bool, 101, 101)
	for i := 1; i <= 100; i++ {
		is_prime[i] = true
	}

	for i := 2; i <= 10; i++ {
		if !is_prime[i] {
			continue
		}
		for j := i + i; j <= 10; j += i {
			is_prime[j] = false
		}
	}

	var judge string
	cnt := 0
	for i := 2; i <= 50; i++ {
		if is_prime[i] {
			fmt.Println(i)
			fmt.Scan(&judge)
			if judge == "yes" {
				cnt++
			}

			if i*i <= 100 {
				fmt.Println(i * i)
				fmt.Scan(&judge)
				if judge == "yes" {
					cnt++
				}
			}

			if cnt >= 2 {
				break
			}
		}
	}

	if cnt >= 2 {
		fmt.Println("composite")
	} else {
		fmt.Println("prime")
	}
}

package main

import "fmt"
import "os"

func main() {
    var p = []int{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71}
    var i int = 0
    var count int = 0
    for i < 20 {
        fmt.Printf("%d\n", p[i])
        os.Stdout.Sync()
        var line string
        fmt.Scanln(&line)
        if line == "yes" {
            count++
        }
        i++
        if (count > 1) {
            fmt.Printf("composite")
            os.Stdout.Sync()
            os.Exit(0)
        }
    }
    fmt.Printf("prime")
    os.Stdout.Sync()
}


package main

import (
    "fmt"
    )

func main() {
    var n int
    fmt.Scan(&n)

    lines := make([]string, n, n)
    for i := 0; i < n; i++ {
        fmt.Scan(&lines[i])
    }

    moves := [...][2]int {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}

    ans := true
    for i := 0; i < n; i++ {
        for j := 0; j < n; j++ {
            count := 0
            for k := 0; k < 4; k++ {
                x, y := i + moves[k][0], j + moves[k][1]
                if x >= 0 && x < n && y >= 0 && y < n {
                    if lines[x][y] == 'o' {
                        count++
                    }
                }
            }

            if count % 2 != 0 {
                ans = false
                break
            }
        }

        if ans == false {
            break
        }
    }

    if ans == true {
        fmt.Println("YES")
    } else {
        fmt.Println("NO")
    }
}

package main

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

func I(i interface{}, delim string) error {
	_, err := fmt.Fscanf(r, "%v"+delim, i)
	return err
}

func O(o interface{}, delim string) {
	fmt.Fprintf(w, "%v"+delim, o)
}

var (
	r *bufio.Reader
	w *bufio.Writer
)

func init() {
	if f, err := os.Open("in.txt"); err == nil {
		r = bufio.NewReader(f)
	} else {
		r = bufio.NewReader(os.Stdin)
	}
	w = bufio.NewWriter(os.Stdout)
}

func main() {
	defer w.Flush()
	var n int
	for I(&n, "\n") == nil {
		solve(n)
	}
}

func solve(n int) {
	a := make([]string, n)
	for i := 0; i < n; i++ {
		I(&a[i], "\n")
	}
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			t := 0
			if j+1 < n && a[i][j+1] == 'o' {
				t++
			}
			if j-1 >= 0 && a[i][j-1] == 'o' {
				t++
			}
			if i+1 < n && a[i+1][j] == 'o' {
				t++
			}
			if i-1 >= 0 && a[i-1][j] == 'o' {
				t++
			}
			if t%2 != 0 {
				O("NO", "\n")
				return
			}
		}
	}
	O("YES", "\n")
}

package main

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

var scanner = bufio.NewScanner(os.Stdin)
var writer = bufio.NewWriter(os.Stdout)

//Wrong
func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	n := nextInt()
	a := make([]string ,n)
	count := 0

	for i := 0; i < n; i++ {
		a[i] = next()
	}

	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			count = 0

			if j > 0 && a[i][j - 1] == 'o' {
				count++
			}

			if j < n - 1 && a[i][j + 1] == 'o' {
				count++
			}

			if i > 0 && a[i - 1][j] == 'o' {
				count++
			}

			if i < n - 1 && a[i + 1][j] == 'o' {
				count++
			}

			if count % 2 != 0 {
				println("NO")
				return
			}
		}
	}

	println("YES")
}

func next() string {
	if !scanner.Scan() {
		panic("Scan error")
	}
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(next())
	if err != nil {
		panic(err)
	}
	return n
}

func nextFloat() float64 {
	n, err := strconv.ParseFloat(next(), 64)
	if err != nil {
		panic(err)
	}
	return n
}

func println(a ...interface{}) {
	fmt.Fprintln(writer, a...)
}

func print(a ...interface{}) {
	fmt.Fprint(writer, a...)
}

func printf(format string, a ...interface{}) {
	fmt.Fprintf(writer, format, a...)
}

package main

//14:17

import (
    "fmt"
)

func main() {
    var n int
    fmt.Scanf("%d ", &n)
    array := make([][]int8, n)
    for i:=0;i<n;i++{
        array[i] = make([]int8,n)
    }
    for i:=0;i<n;i++ {
        for j:=0;j<n;j++{
            fmt.Scanf("%c", &array[i][j])
        }
        fmt.Scanf(" ")
    }
    for i:=0;i<n;i++ {
        for j:=0;j<n;j++{
            var o int
            if i>0 && array[i-1][j]== 'o'{
                o += 1
            }
            if i<n-1 && array[i+1][j] =='o'{
                o += 1
            }
            if j>0 && array[i][j-1]== 'o'{
                o += 1
            }
            if j<n-1 && array[i][j+1] =='o'{
                o += 1
            }
            if o%2 !=0 {
                fmt.Printf("NO")
                return
            }
        }
    }
    fmt.Printf("YES")
}

package main

import (
	"fmt"
)

func main() {
	var n int
	var board [100][100]rune
	fmt.Scan(&n)
	for i := 0; i < n; i++ {
		var s string
		fmt.Scan(&s)
		rs := []rune(s)
		for j, r := range rs {
			board[i][j] = r
		}
	}
	check := func() string {
		xs := [4]int{1, 0, -1, 0}
		ys := [4]int{0, 1, 0, -1}
		for i := 0; i < n; i++ {
			for j := 0; j < n; j++ {
				z := 0
				for k := 0; k < 4; k++ {
					dx := xs[k]
					dy := ys[k]
					x := i + dx
					y := j + dy
					if x < 0 || n <= x || y < 0 || n <= y {
						continue
					}
					if board[x][y] == 'o' {
						z++
					}
				}
				if (z & 1) > 0 {
					return "NO"
				}
			}
		}
		return "YES"
	}
	fmt.Println(check())
}

package main

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

func main() {

	dx := []int {0, 0, -1, 1}
	dy := []int {-1, 1, 0, 0}

	var n int
	reader := bufio.NewReader(os.Stdin)

	line, _ := reader.ReadString('\n')

	n,_ = strconv.Atoi(strings.TrimSpace(line))

	lines := []string{}

	for i:=0; i<n; i++ {
		line, _ := reader.ReadString('\n')
		lines = append(lines, line)
	}
	for i:=0; i<n; i++ {
		for j:=0; j<n; j++{
			num := 0
			for dir:=0; dir<4; dir ++ {
				cx := i + dx[dir]
				cy := j + dy[dir]
				if cx>=0 && cx < n && cy >= 0 && cy < n && lines[cx][cy] == 'o' {
					num ++
				}
			}
			if ((num&1)==1) {
				goto exit
			}
		}
	}

    fmt.Println("YES")
    return
    exit:
    fmt.Println("NO")
    return
}

package main

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

var scanner = bufio.NewScanner(os.Stdin)
var writer = bufio.NewWriter(os.Stdout)

//Wrong
func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	n := nextInt()
	a := make([]string ,n)
	count := 0

	for i := 0; i < n; i++ {
		a[i] = next()
	}

	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			count = 0

			if a[i][j] == 'x' {
				if j > 0 && a[i][j - 1] == 'o' {
					count++
				}

				if j < n - 1 && a[i][j + 1] == 'o' {
					count++
				}

				if i > 0 && a[i - 1][j] == 'o' {
					count++
				}

				if i < n - 1 && a[i + 1][j] == 'o' {
					count++
				}

				if count % 2 != 0 {
					println("NO")
					return
				}
			}
		}
	}

	println("YES")
}

func next() string {
	if !scanner.Scan() {
		panic("Scan error")
	}
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(next())
	if err != nil {
		panic(err)
	}
	return n
}

func nextFloat() float64 {
	n, err := strconv.ParseFloat(next(), 64)
	if err != nil {
		panic(err)
	}
	return n
}

func println(a ...interface{}) {
	fmt.Fprintln(writer, a...)
}

func print(a ...interface{}) {
	fmt.Fprint(writer, a...)
}

func printf(format string, a ...interface{}) {
	fmt.Fprintf(writer, format, a...)
}

package main

import (
	"fmt"
)

func main() {
	var n int
	var board [100][100]rune
	fmt.Scan(&n)
	for i := 0; i < n; i++ {
		var s string
		fmt.Scan(&s)
		rs := []rune(s)
		for j, r := range rs {
			board[i][j] = r
		}
	}
	check := func() string {
		xs := [4]int{1, 0, -1, 0}
		ys := [4]int{0, 1, 0, -1}
		for i := 0; i < n; i++ {
			for j := 0; j < n; j++ {
				z := 0
				for k := 0; k < 4; k++ {
					dx := xs[k]
					dy := ys[k]
					x := i + dx
					y := k + dy
					if x < 0 || n <= x || y < 0 || n <= y {
						continue
					}
					if board[x][y] == 'o' {
						z++
					}
				}
				if (z & 1) > 0 {
					return "NO"
				}
			}
		}
		return "YES"
	}
	fmt.Println(check())
}

package main

import (
	"fmt"
)

func main() {
	var n int
	var board [100][100]rune
	fmt.Scan(&n)
	for i := 0; i < n; i++ {
		var s string
		fmt.Scan(&s)
		rs := []rune(s)
		for j, r := range rs {
			board[i][j] = r
		}
	}
	check := func() string {
		xs := [4]int{1, 0, -1, 0}
		ys := [4]int{0, 1, 0, -1}
		for i := 0; i < n; i++ {
			for j := 0; j < n; j++ {
				z := 0
				for k := 0; k < 4; k++ {
					dx := xs[k]
					dy := ys[k]
					x := i + dx
					y := k + dy
					if x < 0 || n <= x || y < 0 || n <= y {
						continue
					}
					if board[x][y] == 'o' {
						z++
					}
				}
				if (z & 1) > 0 {
					return "No"
				}
			}
		}
		return "Yes"
	}
	fmt.Println(check())
}


package main

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

var sc = bufio.NewScanner(os.Stdin)

const mod int = 998244353
const N int = 5e5 + 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)
		b := make([]int, n)
		x, y := 0, 0
		for i := 0; i < n; i++ {
			a[i] = nextInt()
			for a[i]%2 == 0 {
				a[i] /= 2
				x++
			}
			t, tt := i+1, 0
			for t%2 == 0 {
				t /= 2
				tt++
			}
			b[i] = tt
			y += tt
		}
		if x+y < n {
			fmt.Println("-1")
			continue
		}
		sort.Ints(b)
		i := n - 1
		for x < n {
			x += b[i]
			i--
		}
		fmt.Println(n - i - 1)
	}
}

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"
	"sort"
)

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

func main() {
	defer writer.Flush()

	var T int
	fmt.Fscan(reader, &T)

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

		a := make([]int, n)
		tot := 0
		for i := 0; i < n; i++ {
			var val int
			fmt.Fscan(reader, &val)

			for ; val%2 == 0; val /= 2 {
				tot += 1
			}

			for tmp := i + 1; tmp%2 == 0; tmp /= 2 {
				a[i] += 1
			}
		}

		if tot >= n {
			fmt.Fprintf(writer, "%d\n", 0)
		} else {
			ans := 0

			sort.Slice(a, func(i, j int) bool {
				return a[i] > a[j]
			})

			for i := 0; i < n && tot < n; i++ {
				tot += a[i]
				ans += 1
			}

			if tot < n {
				ans = -1
			}
			fmt.Fprintf(writer, "%d\n", ans)
		}
	}
}

package main

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

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

func solve() {
    var n int
    Fscan(in, &n)

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

        for ; x % 2 == 0; x /= 2 {
            cnt++
        }

        c := 0
        for j := i; j % 2 == 0; j /= 2 {
            c++
        }
        a = append(a, c)
    }

    sort.Sort(sort.Reverse(sort.IntSlice(a)))

    ans := 0
    for i := range a {
        if cnt >= n {
            break
        }

        ans++
        cnt += a[i]
    }

    if cnt >= n {
        Fprintln(out, ans)
    } else {
        Fprintln(out, -1)
    }
}

func main() {
    var T int
    Fscan(in, &T)
    for ; T > 0; T-- {
        solve()
    }
    out.Flush()
}

package main

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

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

func solve() {
	n := _readInt()
	var cnt, res int
	for i := 0; i < n; i++ {
		x := _readInt()
		for x > 0 && x%2 == 0 {
			x /= 2
			cnt++
		}
	}

	if cnt >= n {
		_print(0)
		return
	}

	counts := make([]int, 0)
	for i := 1; i <= n; i++ {
		x := i
		if x%2 != 0 {
			continue
		}
		var tmp int
		for x > 0 && x%2 == 0 {
			x /= 2
			tmp++
		}
		counts = append(counts, tmp)
	}
	sort.Ints(counts)
	_reverseInts(counts)

	for _, v := range counts {
		res++
		cnt += v
		if cnt >= n {
			_print(res)
			return
		}
	}

	_print(-1)
}

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

// ====
func _readInt() int {
	var x int
	fmt.Fscan(in, &x)
	return x
}
func _readStr() string {
	var x string
	fmt.Fscan(in, &x)
	return x
}
func _readArr() (int, []int) {
	var n int
	fmt.Fscan(in, &n)
	var v = make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &v[i])
	}
	return n, v
}
func _print(x ...any) {
	fmt.Fprintln(out, x...)
}
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 _sortStr(x string) string {
	r := []rune(x)
	sort.Slice(r, func(i, j int) bool {
		return r[i] < r[j]
	})
	return string(r)
}
func _reverseInts(x []int) {
	n := len(x)
	for i := 0; i < n/2; i++ {
		x[i], x[n-1-i] = x[n-1-i], x[i]
	}
}
func _reverseStr(s string) string {
	x := []rune(s)
	n := len(x)
	for i := 0; i < n/2; i++ {
		x[i], x[n-1-i] = x[n-1-i], x[i]
	}
	return string(x)
}

package main

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

const cf = true

func solve() {
	var n int
	fmt.Fscan(in, &n)
	var num int
	needed := n
	have := 0
	divs := make([]int, 0, n/4)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &num)
		for num%2 == 0 {
			have++
			num /= 2
		}
		cur := i + 1
		val := 0
		for cur%2 == 0 {
			val++
			cur /= 2
		}
		if val > 0 {
			divs = append(divs, val)
		}
	}
	if have >= needed {
		fmt.Fprintln(out, 0)
		return
	}
	sort.Ints(divs)
	ans := 0
	for i := len(divs) - 1; i >= 0; i-- {
		have += divs[i]
		ans++
		if have >= needed {
			break
		}
	}
	if have < needed {
		fmt.Fprintln(out, -1)
		return
	}
	fmt.Fprintln(out, ans)
}

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

func GCD(a, b int) int {
	for a != b {
		if a > b {
			a -= b
		} else {
			b -= a
		}
	}

	return a
}

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 := []byte(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"
	"sort"
)

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

func main() {
	var t int
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		var n int
		fmt.Fscan(in, &n)
		arr := make([]int, n)
		for j := 0; j < n; j++ {
			fmt.Fscan(in, &arr[j])
		}
		//s := ""
		//if i == 52 {
		//	for _, x := range arr {
		//		s = s + " " + strconv.FormatInt(int64(x), 10)
		//	}
		//	fmt.Println(s)
		//}
		sum2Pos := 0
		indexes := make([]int, n)

		for i, x := range arr {
			sum2Pos += countPow2(x)
			indexes[i] = countPow2(i + 1)
		}
		sort.Ints(indexes)
		//diff := int(math.Pow(2, float64(n-sum2Pos)))
		diff := n - sum2Pos
		if diff <= 0 {
			fmt.Println(0)
		} else {
			count := 0
			for k := len(indexes) - 1; k >= 0; k-- {
				if diff == 0 {
					break
				}
				x := indexes[k]
				if x > 0 && x <= diff {
					count++
					diff = diff - x
				}
			}
			if diff == 0 {
				fmt.Println(count)
			} else {
				fmt.Println(-1)
			}
		}
	}
}

func countPow2(x int) int {
	temp := x
	count := 0
	for temp%2 == 0 {
		temp = temp / 2
		count++
	}
	return count
}

func GCD(a, b int) int {
	for a != b {
		if a > b {
			a -= b
		} else {
			b -= a
		}
	}

	return a
}

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

type Ordered interface {
	int | float64 | ~string
}

package main

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

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)

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

			aa[i] += bits.TrailingZeros(v)
			z += aa[i]
			if i%2 != 0 {
				d := bits.TrailingZeros(uint(i + 1))
				bb = append(bb, d)
			}
		}

		sort.Slice(bb, func(i, j int) bool {
			return bb[i] > bb[j]
		})

		var c int
		for _, v := range bb {
			if z >= n {
				break
			}
			z += v
			c++
		}
		if z < n {
			fmt.Fprintln(out, -1)
		} else {
			fmt.Fprintln(out, c)
		}
	}
}

// LUOGU_RID: 92429608
package main

import (
	"bufio"
	. "fmt"
	"io"
	"math/bits"
	"os"
	"sort"
)

// https://space.bilibili.com/206214
func CF1744D(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var T, n, v int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		s := 0
		c2 := make([]int, n)
		for i := range c2 {
			Fscan(in, &v)
			s += bits.TrailingZeros(uint(v))
			c2[i] = bits.TrailingZeros(uint(i + 1))
		}
		sort.Ints(c2)
		i := n - 1
		for ; i >= 0 && s < n; i-- {
			s += c2[i]
		}
		if i < 0 {
			Fprintln(out, -1)
		} else {
			Fprintln(out, n-1-i)
		}
	}
}
func main() { CF1744D(os.Stdin, os.Stdout) }

package main
import (
	"bufio"
	. "fmt"
	"io"
	"os"
	//. "strconv" Itoa
)
var mp=[200010][22]int{}
func init(){
	for i:=2;i<=200000;i++{
		cn:=i
		cnt:=0
		for (cn%2==0)&&cn!=0{
			cn/=2
			cnt++
		}
		for j:=1;j<=20;j++{
			mp[i][j]=mp[i-1][j]
		}
		mp[i][cnt]++
	}
}
func solve(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)

	defer out.Flush()

	var T,x int
	for Fscan(in,&T);T>0;T--{
		var n int
		Fscan(in,&n)
		cn:=n
		for i:=0;i<n;i++{
			Fscan(in,&x)
			for (x%2)==0&&x!=0{
				x/=2
				cn--
			}
		}
		if cn<=0{
			Fprintln(out,"0")
			continue
		}else{
			ans:=0
			for i:=20;i>=1&&cn>0;i--{
				if mp[n][i]>0{
					mn:=cn/i
					if cn%i!=0{mn++}
					mn=min(mn,mp[n][i])
					cn-=mn*i
					ans+=mn
				}
			}
			if cn<=0{
				Fprintln(out,ans)
			}else{
				Fprintln(out,"-1")
			}
		}
		// 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 20 21 22 23 24
		//   1   2   1   3   1     2     1     4     1  2     1     3
	}
	// Fprintln(out,mp[200000])
}
func main() { solve(os.Stdin, os.Stdout) }

func max(i int, j int) int {
	if (i > j) {
		return i;
	} else {
		return j;
	}
}
func min(i int, j int) int {
	if (i < j) {
		return i;
	} else {
		return j;
	}
}

package main

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

var reader = bufio.NewReader(os.Stdin)

var n, m int

func main() {
	var t int

	fmt.Fscan(reader, &t)

	for t > 0 {
		t--
		var n int
		fmt.Fscan(reader, &n)
		var num int
		needed := n
		have := 0
		divs := make([]int, 0, n/4)
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &num)
			for num%2 == 0 {
				have++
				num /= 2
			}
			cur := i + 1
			val := 0
			// 2 4 6 // the middle 4 gives most, we will first multiple by the 4
			for cur%2 == 0 { // cur_index % 2 = 0
				val++    //
				cur /= 2 // cur_index /= 2
			}
			if val > 0 {
				divs = append(divs, val) // index will contribute val,
			}
		}

		if have >= needed { // already satisfied
			fmt.Println(0)
			continue
		}
		sort.Ints(divs)

		ans := 0
		// count the contribution
		for i := len(divs) - 1; i >= 0; i-- {
			have += divs[i]
			ans++
			if have >= needed {
				break
			}
		}
		if have < needed {
			fmt.Println(-1)
			continue
		}
		fmt.Println(ans)

	}
}

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

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

package main

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

func solution_1(input io.Reader, output io.Writer) {
	in := bufio.NewReader(input)
	var n int
	_, err := fmt.Fscanf(in, "%d\n", &n)
	if err != nil {
		panic(err)
	}

	for i := 0; i < n; i++ {
		var narr int
		_, err := fmt.Fscanf(in, "%d\n", &narr)
		if err != nil {
			panic(err)
		}

		arr := make([]int, 0, narr)
		for j := 0; j < narr; j++ {
			var v int
			_, err := fmt.Fscanf(in, "%d", &v)
			if err != nil {
				panic(err)
			}
			arr = append(arr, v)
		}
		fmt.Fscanf(in, "\n")
		var s string
		_, err = fmt.Fscanf(in, "%s\n", &s)
		fmt.Fprintln(output, numberReplacement(arr, s))
	}
}

func solution_2(input io.Reader, output io.Writer) {
	in := bufio.NewReader(input)
	var n int
	_, err := fmt.Fscanf(in, "%d\n", &n)
	if err != nil {
		panic(err)
	}

	for i := 0; i < n; i++ {
		var narr, q int
		_, err := fmt.Fscanf(in, "%d %d\n", &narr, &q)
		if err != nil {
			panic(err)
		}

		arr := make([]int, 0, narr)
		for j := 0; j < narr; j++ {
			var v int
			_, err := fmt.Fscanf(in, "%d", &v)
			if err != nil {
				panic(err)
			}
			arr = append(arr, v)
		}
		fmt.Fscanf(in, "\n")

		nOdd := 0
		nEven := 0
		sum := 0

		for _, n := range arr {
			sum += n
			if n%2 == 0 {
				nEven++
			} else {
				nOdd++
			}
		}
		for j := 0; j < q; j++ {
			var a, b int
			fmt.Fscanf(in, "%d %d\n", &a, &b)
			if a == 0 {
				sum += nEven * b
				if b%2 != 0 {
					nOdd += nEven
					nEven = 0
				}
			}

			if a == 1 {
				sum += nOdd * b
				if b%2 != 0 {
					nEven += nOdd
					nOdd = 0
				}
			}
			fmt.Fprintln(output, sum)
		}

	}
}

func numberReplacement(arr []int, s string) string {
	if len(arr) != len(s) {
		return "NO"
	}

	someMap := make(map[int]byte)
	for i, n := range arr {
		if v, ok := someMap[n]; !ok {
			someMap[n] = s[i]
		} else {
			if v != s[i] {
				return "NO"
			}
		}
	}

	return "YES"
}

func solution_3(input io.Reader, output io.Writer) {
	in := bufio.NewReader(input)
	var n int
	_, err := fmt.Fscanf(in, "%d\n", &n)
	if err != nil {
		panic(err)
	}

	for i := 0; i < n; i++ {
		var narr int
		var colour string
		_, err := fmt.Fscanf(in, "%d %s\n", &narr, &colour)
		if err != nil {
			panic(err)
		}

		var s string
		_, err = fmt.Fscanf(in, "%s\n", &s)
		if err != nil {
			panic(err)
		}
		fmt.Fprintln(output, waitTime(s, colour))
	}
}

func waitTime(s string, c string) int {
	if c == "g" {
		return 0
	}
	maxRes := 0
	firstGreenIndex := -1
	i := len(s) - 1
	curDist := 1

	for {
		if s[i] == 'g' {
			firstGreenIndex = i
			break
		}
		i--
	}

	i--
	if i < 0 {
		i = len(s) - 1
	}
	distances := make([]int, len(s))

	for i != firstGreenIndex {
		if s[i] == 'g' {
			curDist = 0
		}
		distances[i] = curDist
		curDist++
		i--
		if i < 0 {
			i = len(s) - 1
		}
	}

	for i := 0; i < len(s); i++ {
		if s[i] == c[0] {
			curDist := distances[i]
			if curDist > maxRes {
				maxRes = curDist
			}
		}
	}

	return maxRes
}

func solution_4(input io.Reader, output io.Writer) {
	in := bufio.NewReader(input)
	var n int
	_, err := fmt.Fscanf(in, "%d\n", &n)
	if err != nil {
		panic(err)
	}

	for i := 0; i < n; i++ {
		var narr int
		_, err := fmt.Fscanf(in, "%d\n", &narr)
		if err != nil {
			panic(err)
		}

		arr := make([]int, 0, narr)
		for j := 0; j < narr; j++ {
			var v int
			_, err := fmt.Fscanf(in, "%d", &v)
			if err != nil {
				panic(err)
			}
			arr = append(arr, v)
		}
		fmt.Fscanf(in, "\n")
		fmt.Fprintln(output, numOpertions(arr))
	}
}

func numOpertions(arr []int) int {
	N := len(arr)
	l := 0
	r := 0
	pows := make([]int, 0)

	for ii, n := range arr {
		l = 0
		i := ii + 1
		l = getPowerOf2(i)
		r = 0
		for n >= 2 && n%2 == 0 {
			n /= 2
			r++
		}
		if r >= N {
			return 0
		}
		N -= r
		if l > 0 {
			pows = append(pows, l)
		}
	}

	res := 0
	sort.Slice(pows, func(i, j int) bool {
		return pows[i] > pows[j]
	})
	// fmt.Println(N, pows)
	i := 0
	for N > 0 {
		if i >= len(pows) {
			break
		}
		N -= pows[i]
		i++
		res++
	}
	if N > 0 {
		return -1
	}
	return res
}

func getPowerOf2(n int) int {
	power := 0
	for n >= 2 && n%2 == 0 {
		n /= 2
		power++
	}
	return power
}

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

package main

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

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

func main() {
	var t int
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		var n int
		fmt.Fscan(in, &n)
		arr := make([]int, n)
		for j := 0; j < n; j++ {
			fmt.Fscan(in, &arr[j])
		}
		sum2Pos := 0
		indexes := make([]int, n)

		for i, x := range arr {
			sum2Pos += countPow2(x)
			indexes[i] = countPow2(i + 1)
		}
		sort.Ints(indexes)
		diff := int(math.Pow(2, float64(n-sum2Pos)))
		diff = n - sum2Pos
		count := 0
		for k := len(indexes) - 1; k >= 0; k-- {
			if diff == 0 {
				break
			}
			x := indexes[k]
			if x > 0 {
				count++
				diff = diff - x
			}
		}
		if diff == 0 {
			fmt.Println(count)
		} else {
			fmt.Println(-1)
		}
	}
}

func countPow2(x int) int {
	temp := x
	count := 0
	for temp%2 == 0 {
		temp = temp / 2
		count++
	}
	return count
}

func GCD(a, b int) int {
	for a != b {
		if a > b {
			a -= b
		} else {
			b -= a
		}
	}

	return a
}

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

type Ordered interface {
	int | float64 | ~string
}

package main

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

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

func main() {
	var t int
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		var n int
		fmt.Fscan(in, &n)
		arr := make([]int, n)
		for j := 0; j < n; j++ {
			fmt.Fscan(in, &arr[j])
		}
		mul := 1
		for _, x := range arr {
			mul *= x
		}
		if mul%int(math.Pow(2, float64(n))) != 0 {
			kx := 0
			for newMul := mul; newMul%2 == 0; newMul /= 2 {
				kx++
			}
			x := int(math.Pow(2, float64(n-kx)))
			found := false
			s := 0
			for k := len(arr); k > 0; k-- {
				if x == 1 {
					break
				}
				if k == x {
					found = true
					s++
					mul = mul * k
					break
				} else {
					if k < x && x%k == 0 {
						s++
						mul = mul * k
						x = x / k
					}
				}
			}
			st := math.Pow(2, float64(n))
			res := math.Mod(float64(mul), st)
			if found && res == 0.0 {
				fmt.Println(s)
			} else {
				fmt.Println(-1)
			}
		} else {
			fmt.Println(0)
		}
	}
}

func GCD(a, b int) int {
	for a != b {
		if a > b {
			a -= b
		} else {
			b -= a
		}
	}

	return a
}

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

type Ordered interface {
	int | float64 | ~string
}

package main

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

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

func main() {
	var t int
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		var n int
		fmt.Fscan(in, &n)
		arr := make([]int, n)
		for j := 0; j < n; j++ {
			fmt.Fscan(in, &arr[j])
		}
		mul := 1
		for _, x := range arr {
			mul *= x
		}
		if mul%int(math.Pow(2, float64(n))) != 0 {
			kx := 0
			for newMul := mul; newMul%2 == 0; newMul /= 2 {
				kx++
			}
			x := int(math.Pow(2, float64(n-kx)))
			found := false
			s := 0
			for k := len(arr); k > 0; k-- {
				if x == 1 {
					break
				}
				if k == x {
					found = true
					s++
					mul = mul * k
					break
				} else {
					if k < x && GCD(x, k) != 1 {
						s++
						mul = mul * k
						x = x / k
					}
				}
			}
			st := math.Pow(2, float64(n))
			res := math.Mod(float64(mul), st)
			if found && res == 0.0 {
				fmt.Println(s)
			} else {
				fmt.Println(-1)
			}
		} else {
			fmt.Println(0)
		}
	}
}

func GCD(a, b int) int {
	for a != b {
		if a > b {
			a -= b
		} else {
			b -= a
		}
	}

	return a
}

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

type Ordered interface {
	int | float64 | ~string
}

package main

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

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

func main() {
	var t int
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		var n int
		fmt.Fscan(in, &n)
		arr := make([]int, n)
		for j := 0; j < n; j++ {
			fmt.Fscan(in, &arr[j])
		}
		mul := 1
		for _, x := range arr {
			mul *= x
		}
		if mul%int(math.Pow(2, float64(n))) != 0 {
			kx := 0
			for newMul := mul; newMul%2 == 0; newMul /= 2 {
				kx++
			}
			x := int(math.Pow(2, float64(n-kx)))
			found := false
			s := 0
			for k := len(arr); k > 0; k-- {
				if x == 1 {
					break
				}
				if k == x {
					found = true
					s++
					mul = mul * k
					break
				} else {
					if k < x && GCD(x, k) != 1 {
						s++
						mul = mul * k
						x = x / k
					}
				}
			}
			if found && mul%int(math.Pow(2, float64(n))) == 0 {
				fmt.Println(s)
			} else {
				fmt.Println(-1)
			}
		} else {
			fmt.Println(0)
		}
	}
}

func GCD(a, b int) int {
	for a != b {
		if a > b {
			a -= b
		} else {
			b -= a
		}
	}

	return a
}

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

type Ordered interface {
	int | float64 | ~string
}

package main

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

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

func main() {
	var t int
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		var n int
		fmt.Fscan(in, &n)
		arr := make([]int, n)
		for j := 0; j < n; j++ {
			fmt.Fscan(in, &arr[j])
		}
		mul := 1
		for _, x := range arr {
			mul *= x
		}
		if mul%int(math.Pow(2, float64(n))) != 0 {
			kx := 0
			for newMul := mul; newMul%2 == 0; newMul /= 2 {
				kx++
			}
			x := int(math.Pow(2, float64(n-kx)))
			found := false
			s := 0
			for k := len(arr); k > 0; k-- {
				if x == 1 {
					break
				}
				if k == x {
					found = true
					s++
					break
				} else {
					if k < x && GCD(x, k) != 1 {
						s++
						x = x / k
					}
				}
			}
			if found {
				fmt.Println(s)
			} else {
				fmt.Println(-1)
			}
		} else {
			fmt.Println(0)
		}
	}
}

func GCD(a, b int) int {
	for a != b {
		if a > b {
			a -= b
		} else {
			b -= a
		}
	}

	return a
}

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

type Ordered interface {
	int | float64 | ~string
}

package main

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

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)

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

			aa[i] += bits.TrailingZeros(v)
			z += aa[i]
			if bits.TrailingZeros(uint(i+1)) > 0 {
				if aa[i] == 0 {
					bb = append(bb, bits.TrailingZeros(uint(i+1)))
				} else {
					bb = append(bb, aa[i]*bits.TrailingZeros(uint(i+1)))
				}
			}
		}

		sort.Slice(bb, func(i, j int) bool {
			return bb[i] > bb[j]
		})

		var c int
		for _, v := range bb {
			if z >= n {
				break
			}
			z += v
			c++
		}
		if z < n {
			fmt.Fprintln(out, -1)
		} else {
			fmt.Fprintln(out, c)
		}
	}
}

package main

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

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)

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

			aa[i] += bits.TrailingZeros(v)
			z += aa[i]
			if bits.TrailingZeros(uint(i+1)) > 0 {
				if aa[i] == 0 {
					bb = append(bb, bits.TrailingZeros(uint(i+1)))
				} else {
					bb = append(bb, aa[i]*bits.TrailingZeros(uint(i+1)))
				}
			}
		}

		sort.Slice(bb, func(i, j int) bool {
			return bb[i] > bb[j]
		})

		if z == 0 {
			fmt.Fprintln(out, -1)
		} else {
			var c int
			for _, v := range bb {
				if z >= n {
					break
				}
				z += v
				c++
			}
			fmt.Fprintln(out, c)
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"math/bits"
	"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)

		var z int
		for i := 0; i < n; i++ {
			var v uint
			fmt.Fscan(in, &v)

			z += bits.TrailingZeros(v)
		}

		if z == 0 {
			fmt.Fprintln(out, -1)
		} else {
			var c int
			for z < n {
				z *= 2
				c++
			}
			fmt.Fprintln(out, c)
		}
	}
}

package main

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

var reader = bufio.NewReader(os.Stdin)

var n, m int

func main() {
	var t int

	fmt.Fscan(reader, &t)

	for t > 0 {
		t--
		var n int
		fmt.Fscan(reader, &n)
		var num int
		needed := n
		have := 0
		divs := make([]int, 0, n/4)
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &num)
			for num%2 == 0 {
				have++
				num /= 2
			}
			cur := i + 1
			val := 0
			// 2 4 6 // the middle 4 gives most, we will first multiple by the 4
			for cur%2 == 0 { // cur_index % 2 = 0
				val++    //
				cur /= 2 // cur_index /= 2
			}
			if val > 0 {
				divs = append(divs, val) // index will contribute val,
			}
		}

		if have >= needed { // already satisfied
			fmt.Println(0)
			continue
		}
		fmt.Println(divs)
		sort.Ints(divs)

		ans := 0
		fmt.Println(divs)
		// count the contribution
		for i := len(divs) - 1; i >= 0; i-- {
			have += divs[i]
			ans++
			if have >= needed {
				break
			}
		}
		if have < needed {
			fmt.Println(-1)
			continue
		}
		fmt.Println(ans)

	}
}

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

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

package main

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

func solution_1(input io.Reader, output io.Writer) {
	in := bufio.NewReader(input)
	var n int
	_, err := fmt.Fscanf(in, "%d\n", &n)
	if err != nil {
		panic(err)
	}

	for i := 0; i < n; i++ {
		var narr int
		_, err := fmt.Fscanf(in, "%d\n", &narr)
		if err != nil {
			panic(err)
		}

		arr := make([]int, 0, narr)
		for j := 0; j < narr; j++ {
			var v int
			_, err := fmt.Fscanf(in, "%d", &v)
			if err != nil {
				panic(err)
			}
			arr = append(arr, v)
		}
		fmt.Fscanf(in, "\n")
		var s string
		_, err = fmt.Fscanf(in, "%s\n", &s)
		fmt.Fprintln(output, numberReplacement(arr, s))
	}
}

func solution_2(input io.Reader, output io.Writer) {
	in := bufio.NewReader(input)
	var n int
	_, err := fmt.Fscanf(in, "%d\n", &n)
	if err != nil {
		panic(err)
	}

	for i := 0; i < n; i++ {
		var narr, q int
		_, err := fmt.Fscanf(in, "%d %d\n", &narr, &q)
		if err != nil {
			panic(err)
		}

		arr := make([]int, 0, narr)
		for j := 0; j < narr; j++ {
			var v int
			_, err := fmt.Fscanf(in, "%d", &v)
			if err != nil {
				panic(err)
			}
			arr = append(arr, v)
		}
		fmt.Fscanf(in, "\n")

		nOdd := 0
		nEven := 0
		sum := 0

		for _, n := range arr {
			sum += n
			if n%2 == 0 {
				nEven++
			} else {
				nOdd++
			}
		}
		for j := 0; j < q; j++ {
			var a, b int
			fmt.Fscanf(in, "%d %d\n", &a, &b)
			if a == 0 {
				sum += nEven * b
				if b%2 != 0 {
					nOdd += nEven
					nEven = 0
				}
			}

			if a == 1 {
				sum += nOdd * b
				if b%2 != 0 {
					nEven += nOdd
					nOdd = 0
				}
			}
			fmt.Fprintln(output, sum)
		}

	}
}

func numberReplacement(arr []int, s string) string {
	if len(arr) != len(s) {
		return "NO"
	}

	someMap := make(map[int]byte)
	for i, n := range arr {
		if v, ok := someMap[n]; !ok {
			someMap[n] = s[i]
		} else {
			if v != s[i] {
				return "NO"
			}
		}
	}

	return "YES"
}

func solution_3(input io.Reader, output io.Writer) {
	in := bufio.NewReader(input)
	var n int
	_, err := fmt.Fscanf(in, "%d\n", &n)
	if err != nil {
		panic(err)
	}

	for i := 0; i < n; i++ {
		var narr int
		var colour string
		_, err := fmt.Fscanf(in, "%d %s\n", &narr, &colour)
		if err != nil {
			panic(err)
		}

		var s string
		_, err = fmt.Fscanf(in, "%s\n", &s)
		if err != nil {
			panic(err)
		}
		fmt.Fprintln(output, waitTime(s, colour))
	}
}

func waitTime(s string, c string) int {
	if c == "g" {
		return 0
	}
	maxRes := 0
	firstGreenIndex := -1
	i := len(s) - 1
	curDist := 1

	for {
		if s[i] == 'g' {
			firstGreenIndex = i
			break
		}
		i--
	}

	i--
	if i < 0 {
		i = len(s) - 1
	}
	distances := make([]int, len(s))

	for i != firstGreenIndex {
		if s[i] == 'g' {
			curDist = 0
		}
		distances[i] = curDist
		curDist++
		i--
		if i < 0 {
			i = len(s) - 1
		}
	}

	for i := 0; i < len(s); i++ {
		if s[i] == c[0] {
			curDist := distances[i]
			if curDist > maxRes {
				maxRes = curDist
			}
		}
	}

	return maxRes
}

func solution_4(input io.Reader, output io.Writer) {
	in := bufio.NewReader(input)
	var n int
	_, err := fmt.Fscanf(in, "%d\n", &n)
	if err != nil {
		panic(err)
	}

	for i := 0; i < n; i++ {
		var narr int
		_, err := fmt.Fscanf(in, "%d\n", &narr)
		if err != nil {
			panic(err)
		}

		arr := make([]int, 0, narr)
		for j := 0; j < narr; j++ {
			var v int
			_, err := fmt.Fscanf(in, "%d", &v)
			if err != nil {
				panic(err)
			}
			arr = append(arr, v)
		}
		fmt.Fscanf(in, "\n")
		fmt.Fprintln(output, numOpertions(arr))
	}
}

func numOpertions(arr []int) int {
	N := len(arr)
	power := 1
	l := 0
	r := 0
	pows := make([]int, 0)

	for ii, n := range arr {
		l = 0
		i := ii + 1
		if i >= 2 && i%2 == 0 {
			l = power
			power++
		}
		r = 0
		for n >= 2 && n%2 == 0 {
			n /= 2
			r++
		}
		if r >= N {
			return 0
		}
		N -= r
		if l > 0 {
			pows = append(pows, l)
		}
	}

	res := 0
	sort.Slice(pows, func(i, j int) bool {
		return pows[i] > pows[j]
	})
	// fmt.Println(N, pows)
	i := 0
	for N > 0 {
		if i >= len(pows) {
			break
		}
		N -= pows[i]
		i++
		res++
	}
	if N > 0 {
		return -1
	}
	return res
}

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


// https://codeforces.com/problemset/problem/1648/A

// 1648A - Weird Sum

// Egor has a table of size n×m, with lines numbered from 1 to n and columns numbered from 1 to m. Each cell has a color that can be presented as an integer from 1 to 105.
// Let us denote the cell that lies in the intersection of the r-th row and the c-th column as (r,c). We define the manhattan distance between two cells (r1,c1) and (r2,c2) as the length of a shortest path between them where each consecutive cells in the path must have a common side. The path can go through cells of any color. For example, in the table 3×4 the manhattan distance between (1,2) and (3,3) is 3, one of the shortest paths is the following: (1,2)→(2,2)→(2,3)→(3,3).
// Egor decided to calculate the sum of manhattan distances between each pair of cells of the same color. Help him to calculate this sum.

// Input
// The first line contains two integers n and m (1≤n≤m, n⋅m≤100000) — number of rows and columns in the table.
// Each of next n lines describes a row of the table. The i-th line contains m integers ci1,ci2,…,cim (1≤cij≤100000) — colors of cells in the i-th row.

// Output
// Print one integer — the the sum of manhattan distances between each pair of cells of the same color.

// Examples

// input
// 2 3
// 1 2 3
// 3 2 1
// output
// 7

// input
// 3 4
// 1 1 2 2
// 2 1 1 2
// 2 2 1 1
// output
// 76

// input
// 4 4
// 1 1 2 3
// 2 1 1 2
// 3 1 2 1
// 1 1 2 1
// output
// 129

// Note
// In the first sample there are three pairs of cells of same color: in cells (1,1) and (2,3), in cells (1,2) and (2,2), in cells (1,3) and (2,1). The manhattan distances between them are 3, 1 and 3, the sum is 7.

package main

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

func main() {
	var reader = bufio.NewReader(os.Stdin)
	var writer = bufio.NewWriter(os.Stdout)
	var t int
	var n, m, i, j, num_colors, ans int64
	var color, row, col [][]int64

	for t, _ = fmt.Fscan(reader, &n, &m); t == 2; t, _ = fmt.Fscan(reader, &n, &m) {
		color = make([][]int64, n)
		num_colors = 0
		ans = 0

		for i = 0; i < n; i++ {
			color[i] = make([]int64, m)

			for j = 0; j < m; j++ {
				fmt.Fscan(reader, &color[i][j])

				if num_colors < color[i][j] {
					num_colors = color[i][j]
				}
			}
		}

		row = make([][]int64, num_colors)
		col = make([][]int64, num_colors)

		for i = 0; i < n; i++ {
			for j = 0; j < m; j++ {
				row[color[i][j]-1] = append(row[color[i][j]-1], i)
				col[color[i][j]-1] = append(col[color[i][j]-1], j)
			}
		}

		for i = 0; i < num_colors; i++ {
			sort.Slice(row[i], func(a, b int) bool {
				return row[i][a] < row[i][b]
			})

			for j = 0; j < int64(len(row[i])); j++ {
				ans += (2*j + 1 - int64(len(row[i]))) * row[i][j]
			}

			sort.Slice(col[i], func(a, b int) bool {
				return col[i][a] < col[i][b]
			})

			for j = 0; j < int64(len(col[i])); j++ {
				ans += (2*j + 1 - int64(len(col[i]))) * col[i][j]
			}
		}

		fmt.Fprintln(writer, ans)
	}

	writer.Flush()
}

package main

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

/*
3
1 1
1
2 2
3 2
1 2
2 3
2 3 2
1 1 2
---
0
1
7
=======

colorToRow
1 -> 2
2 -> 1, 2

colorToCol
1 -> 1, 2
2 -> 1, 3

  2 3 2
  1 1 2

2+2 2+1 +1

*/

//goland:noinspection GoUnhandledErrorResult
func solve() {
	var n, m int
	scanf("%d %d\n", &n, &m)

	clrToRows := make(map[int][]int)
	clrToCols := make(map[int][]int)

	for r := 0; r < n; r++ {
		for c := 0; c < m; c++ {
			var clr int
			scanf("%d", &clr)

			clrToRows[clr] = append(clrToRows[clr], r)
			clrToCols[clr] = append(clrToCols[clr], c)
		}
		scanf("\n")
	}

	distSum := int64(0)

	for _, nums := range clrToRows {
		for i, num := range nums {
			if i == 0 {
				continue
			}

			dist := num - nums[i-1]
			cnt := int64(i) * int64(len(nums)-i)

			distSum += int64(dist) * cnt
		}
	}

	for _, nums := range clrToCols {
		sort.Ints(nums)

		for i, num := range nums {
			if i == 0 {
				continue
			}

			dist := num - nums[i-1]
			cnt := int64(i) * int64(len(nums)-i)

			distSum += int64(dist) * cnt
		}
	}

	fmt.Fprintln(writer, distSum)
}

//goland:noinspection GoUnhandledErrorResult
func main() {
	defer writer.Flush()

	t := 1
	//scanf("%d\n", &t)

	for ; t > 0; t-- {
		solve()
	}
}

const maxUint = ^uint(0)

//goland:noinspection GoUnusedConst
const minUint = 0
const maxInt = int(maxUint >> 1)

//goland:noinspection GoUnusedConst
const minInt = -maxInt - 1

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

func scanf(f string, a ...interface{}) {
	if _, err := fmt.Fscanf(reader, f, a...); err != nil {
		panic(err)
	}
}

//goland:noinspection GoUnusedFunction
func reversed(s string) string {
	input := []rune(s)
	li := len(input)
	ret := make([]rune, li)
	for i, r := range input {
		ret[li-i-1] = r
	}
	return string(ret)
}

//goland:noinspection GoUnusedFunction
func divCeil(num, div int) int {
	quot := num / div
	if num%div != 0 {
		quot++
	}
	return quot
}

//goland:noinspection GoUnusedFunction
func min(nums ...int64) int64 {
	minimum := nums[0]
	for _, num := range nums {
		if num < minimum {
			minimum = num
		}
	}
	return minimum
}

//goland:noinspection GoUnusedFunction
func max(nums ...int64) (max, idx int64) {
	for i, num := range nums {
		if num > nums[idx] {
			idx = int64(i)
		}
	}

	return nums[idx], idx
}

//goland:noinspection GoUnusedFunction
func abs(num int) int {
	if num < 0 {
		return -num
	}
	return num
}

//goland:noinspection GoUnusedFunction
func isPrime(num int) bool {
	if num == 2 || num == 3 {
		return true
	}

	if num <= 1 || num%2 == 0 || num%3 == 0 {
		return false
	}

	for i := 5; i*i <= num; i += 6 {
		if num%i == 0 || num%(i+2) == 0 {
			return false
		}
	}

	return true
}

//goland:noinspection GoUnusedFunction
func gcd(a, b int) int {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

//goland:noinspection GoUnusedFunction
func lcm(a, b int) int {
	return abs(a) / gcd(a, b) * abs(b)
}

// Examples:
//	digitAt(1234, -1) -> panic!
//	digitAt(1234, 0) -> 4
//	digitAt(1234, 1) -> 3
//	digitAt(1234, 2) -> 2
//	digitAt(1234, 3) -> 1
//	digitAt(1234, 4) -> panic!
//goland:noinspection GoUnusedFunction
func digitAt(num, pos int) int {
	if num == 0 {
		return 0
	}

	num = abs(num)

	lenNum := int(math.Log10(float64(num))) + 1
	if pos < 0 || pos >= lenNum {
		err := fmt.Sprintf("Out of bound access for digit. Position passed %d, number %d", pos, num)
		panic(err)
	}

	rem := num % int(math.Pow10(pos+1))
	return rem / int(math.Pow10(pos))
}

package main

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

/*
3
1 1
1
2 2
3 2
1 2
2 3
2 3 2
1 1 2
---
0
1
7
=======

colorToRow
1 -> 2
2 -> 1, 2

colorToCol
1 -> 1, 2
2 -> 1, 3

  2 3 2
  1 1 2

2+2 2+1 +1

*/

//goland:noinspection GoUnhandledErrorResult
func solve() {
	var n, m int
	scanf("%d %d\n", &n, &m)

	clrToRows := make(map[int][]int)
	clrToCols := make(map[int][]int)

	for r := 0; r < n; r++ {
		for c := 0; c < m; c++ {
			var clr int
			scanf("%d", &clr)

			clrToRows[clr] = append(clrToRows[clr], r)
			clrToCols[clr] = append(clrToCols[clr], c)
		}
		scanf("\n")
	}

	distSum := int64(0)

	for _, nums := range clrToRows {
		for i, num := range nums {
			if i == 0 {
				continue
			}

			dist := num - nums[i-1]
			cnt := i * (len(nums) - i)

			distSum += int64(dist) * int64(cnt)
		}
	}

	for _, nums := range clrToCols {
		sort.Ints(nums)

		for i, num := range nums {
			if i == 0 {
				continue
			}

			dist := num - nums[i-1]
			cnt := i * (len(nums) - i)

			distSum += int64(dist) * int64(cnt)
		}
	}

	fmt.Fprintln(writer, distSum)
}

//goland:noinspection GoUnhandledErrorResult
func main() {
	defer writer.Flush()

	t := 1
	//scanf("%d\n", &t)

	for ; t > 0; t-- {
		solve()
	}
}

const maxUint = ^uint(0)

//goland:noinspection GoUnusedConst
const minUint = 0
const maxInt = int(maxUint >> 1)

//goland:noinspection GoUnusedConst
const minInt = -maxInt - 1

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

func scanf(f string, a ...interface{}) {
	if _, err := fmt.Fscanf(reader, f, a...); err != nil {
		panic(err)
	}
}

//goland:noinspection GoUnusedFunction
func reversed(s string) string {
	input := []rune(s)
	li := len(input)
	ret := make([]rune, li)
	for i, r := range input {
		ret[li-i-1] = r
	}
	return string(ret)
}

//goland:noinspection GoUnusedFunction
func divCeil(num, div int) int {
	quot := num / div
	if num%div != 0 {
		quot++
	}
	return quot
}

//goland:noinspection GoUnusedFunction
func min(nums ...int64) int64 {
	minimum := nums[0]
	for _, num := range nums {
		if num < minimum {
			minimum = num
		}
	}
	return minimum
}

//goland:noinspection GoUnusedFunction
func max(nums ...int64) (max, idx int64) {
	for i, num := range nums {
		if num > nums[idx] {
			idx = int64(i)
		}
	}

	return nums[idx], idx
}

//goland:noinspection GoUnusedFunction
func abs(num int) int {
	if num < 0 {
		return -num
	}
	return num
}

//goland:noinspection GoUnusedFunction
func isPrime(num int) bool {
	if num == 2 || num == 3 {
		return true
	}

	if num <= 1 || num%2 == 0 || num%3 == 0 {
		return false
	}

	for i := 5; i*i <= num; i += 6 {
		if num%i == 0 || num%(i+2) == 0 {
			return false
		}
	}

	return true
}

//goland:noinspection GoUnusedFunction
func gcd(a, b int) int {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

//goland:noinspection GoUnusedFunction
func lcm(a, b int) int {
	return abs(a) / gcd(a, b) * abs(b)
}

// Examples:
//	digitAt(1234, -1) -> panic!
//	digitAt(1234, 0) -> 4
//	digitAt(1234, 1) -> 3
//	digitAt(1234, 2) -> 2
//	digitAt(1234, 3) -> 1
//	digitAt(1234, 4) -> panic!
//goland:noinspection GoUnusedFunction
func digitAt(num, pos int) int {
	if num == 0 {
		return 0
	}

	num = abs(num)

	lenNum := int(math.Log10(float64(num))) + 1
	if pos < 0 || pos >= lenNum {
		err := fmt.Sprintf("Out of bound access for digit. Position passed %d, number %d", pos, num)
		panic(err)
	}

	rem := num % int(math.Pow10(pos+1))
	return rem / int(math.Pow10(pos))
}

// https://codeforces.com/problemset/problem/1648/A

// 1648A - Weird Sum

// Egor has a table of size n×m, with lines numbered from 1 to n and columns numbered from 1 to m. Each cell has a color that can be presented as an integer from 1 to 105.
// Let us denote the cell that lies in the intersection of the r-th row and the c-th column as (r,c). We define the manhattan distance between two cells (r1,c1) and (r2,c2) as the length of a shortest path between them where each consecutive cells in the path must have a common side. The path can go through cells of any color. For example, in the table 3×4 the manhattan distance between (1,2) and (3,3) is 3, one of the shortest paths is the following: (1,2)→(2,2)→(2,3)→(3,3).
// Egor decided to calculate the sum of manhattan distances between each pair of cells of the same color. Help him to calculate this sum.

// Input
// The first line contains two integers n and m (1≤n≤m, n⋅m≤100000) — number of rows and columns in the table.
// Each of next n lines describes a row of the table. The i-th line contains m integers ci1,ci2,…,cim (1≤cij≤100000) — colors of cells in the i-th row.

// Output
// Print one integer — the the sum of manhattan distances between each pair of cells of the same color.

// Examples

// input
// 2 3
// 1 2 3
// 3 2 1
// output
// 7

// input
// 3 4
// 1 1 2 2
// 2 1 1 2
// 2 2 1 1
// output
// 76

// input
// 4 4
// 1 1 2 3
// 2 1 1 2
// 3 1 2 1
// 1 1 2 1
// output
// 129

// Note
// In the first sample there are three pairs of cells of same color: in cells (1,1) and (2,3), in cells (1,2) and (2,2), in cells (1,3) and (2,1). The manhattan distances between them are 3, 1 and 3, the sum is 7.

package main

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

func main() {
	var reader = bufio.NewReader(os.Stdin)
	var writer = bufio.NewWriter(os.Stdout)
	var t, n, m, i, j, num_colors, ans int
	var color, row, col [][]int

	for t, _ = fmt.Fscan(reader, &n, &m); t == 2; t, _ = fmt.Fscan(reader, &n, &m) {
		color = make([][]int, n)
		num_colors = 0
		ans = 0

		for i = 0; i < n; i++ {
			color[i] = make([]int, m)

			for j = 0; j < m; j++ {
				fmt.Fscan(reader, &color[i][j])

				if num_colors < color[i][j] {
					num_colors = color[i][j]
				}
			}
		}

		row = make([][]int, num_colors)
		col = make([][]int, num_colors)

		for i = 0; i < n; i++ {
			for j = 0; j < m; j++ {
				row[color[i][j]-1] = append(row[color[i][j]-1], i)
				col[color[i][j]-1] = append(col[color[i][j]-1], j)
			}
		}

		for i = 0; i < num_colors; i++ {
			sort.Ints(row[i])

			for j = 0; j < len(row[i]); j++ {
				ans += (2*j + 1 - len(row[i])) * row[i][j]
			}

			sort.Ints(col[i])

			for j = 0; j < len(col[i]); j++ {
				ans += (2*j + 1 - len(col[i])) * col[i][j]
			}
		}

		fmt.Fprintln(writer, ans)
	}

	writer.Flush()
}