package main

import (
	"bufio"
	. "fmt"
	"io"
	"io/ioutil"
	"os"
	"sort"
	"strings"
)

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

	solve := func(curCase int) {
		var n,q,v int
		Fscan(in, &n,&q)
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		sum := make([]int64, len(a)+1)
		for i, v := range a {
			sum[i+1] = sum[i] + int64(v)
		}
		mx := make([]int, n)
		mx[0] = a[0]
		for i := 1; i < n; i++ {
			mx[i] = max(mx[i-1], a[i])
		}
		for qi := 0; qi < q; qi++ {
			Fscan(in, &v)
			j := sort.SearchInts(mx, v+1)
			Fprint(out, sum[j], " ")
		}
		Fprintln(out)
	}

	cases := 1
	Fscan(in, &cases)
	for curCase := 1; curCase <= cases; curCase++ {
		solve(curCase)
	}

	// 如果是多组数据，请务必加上这段保险 —— 已经无法统计，有多少位竞赛选手在漏读数据上损失大把分数（包括我）
	// 此外，如果不小心写成了 scan(v) 而不是 scan(&v)，由于未传入指针，不会读入任何数据，这样本地运行一次就能立马发现错误
	leftData, _ := ioutil.ReadAll(in)
	if s := strings.TrimSpace(string(leftData)); s != "" {
		panic("有未读入的数据：\n" + s)
	}

}

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

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

package main

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

const cf = true

func solve() {
	var n, q int
	fmt.Fscan(in, &n, &q)
	mx := make([]int, n)
	sum := make([]int, n)
	var num int
	for i := range mx {
		fmt.Fscan(in, &num)
		mx[i] = num
		if i > 0 {
			mx[i] = max(mx[i], mx[i-1])
		}
		sum[i] = num
		if i > 0 {
			sum[i] += sum[i-1]
		}
	}
	for i := 0; i < q; i++ {
		fmt.Fscan(in, &num)
		l, r := -1, n
		for l < r-1 {
			mid := (l + r) / 2
			if num >= mx[mid] {
				l = mid
			} else {
				r = mid
			}
		}
		if l == -1 {
			fmt.Fprint(out, "0 ")
		} else {
			fmt.Fprint(out, sum[l], " ")
		}
	}
	fmt.Fprintln(out)
}

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

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

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

	var T int
	var n int
	var m int
	var x int

	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		Fscan(in, &m)
		r := make([]int, n)
		q := make([]int, m)
		st := []int{}
		stv := []int{}
		s := make([]int64, n)
		for i := 0; i < n; i++ {
			Fscan(in, &x)
			r[i] = x
			if len(st) == 0 || stv[len(stv)-1] < x {
				st = append(st, i)
				stv = append(stv, x)
			}

			s[i] += int64(x)
			if i > 0 {
				s[i] += s[i-1]
			}
		}
		for i := 0; i < m; i++ {
			Fscan(in, &x)
			q[i] = x
		}

		ans := make([]int64, m)
		for i, v := range q {
			t := sort.SearchInts(stv, v+1)
			if t >= len(stv) {
				ans[i] = s[n-1]
			} else if t == 0 {
				ans[i] = 0
			} else {
				ans[i] = s[st[t]-1]
			}
		}

		for _, v := range ans {
			Fprintf(out, "%d ", v)
		}
		Fprintln(out, "")
	}
}

package main

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

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

func bs(arr []int, l, r, v int) int {
	if l == r {
		return l
	}
	m := (l + r + 1) >> 1
	if arr[m] > v {
		return bs(arr, l, m-1, v)
	}
	return bs(arr, m, r, v)
}

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, q int
		fmt.Fscan(r, &n, &q)
		sArr := make([]int, n+1)
		hArr := make([]int, n+1)
		for i := range sArr {
			if i == 0 {
				continue
			}
			fmt.Fscan(r, &sArr[i])
			hArr[i] = hArr[i-1] + sArr[i]
			sArr[i] = max(sArr[i], sArr[i-1])
		}

		for i := 0; i < q; i++ {
			var qq int
			fmt.Fscan(r, &qq)
			fmt.Fprintf(w, "%d ", hArr[bs(sArr, 0, n, qq)])
		}

		fmt.Fprintln(w, "")
	}

}

package main

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

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

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

func readString() string {
	var s string
	fmt.Fscan(reader, &s)
	return s
}

func main(){
	defer writer.Flush()
	T := readInt()
	for i := 1; i <= T; i++{
		n, m := readInt(), readInt()
		steps := make([]int64, n)
		ans := make([]int64, m)
		pairs := make([][]int64, m)
		for i := 0; i < n; i++ {
			steps[i] = int64(readInt())
		}
		for i := 0; i < m; i++ {
			q := readInt()
			pairs[i] = []int64{int64(q), int64(i)}
		}
		sort.Slice(pairs, func(i,  j int) bool {
			return pairs[i][0] < pairs[j][0]
		})
		cur, p1 := int64(0), 0
		for p2 := 0; p2 < m; p2++ {
			for ; p1 < n; {
				if pairs[p2][0] >= steps[p1] {
					cur += steps[p1]
					p1++
				} else{
					break
				}
			}
			ans[pairs[p2][1]] = cur
		}
		for i := 0; i < m; i++ {
			fmt.Fprint(writer, ans[i], " ")
		}
		fmt.Fprintln(writer)
	}
}

package main

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

const mod = 998244353
const mx = int64(1e9 + 7)

func solve(in *bufio.Reader, out *bufio.Writer) {
	defer out.Flush()
	var n, q int
	Fscan(in, &n, &q)
	arr := make([]int64, n+1)
	for i := 1; i <= n; i++ {
		Fscan(in, &arr[i])
	}
	height := make([]int, n+1)
	for i := 1; i <= n; i++ {
		height[i] = max(height[i-1], int(arr[i]))
		arr[i] += arr[i-1]
	}
	var ans []int64
	for x := 0; x < q; x++ {
		var hg int
		Fscan(in, &hg)
		l, r := 0, n-1
		for l <= r {
			mid := (l + r) / 2
			if height[mid] > hg {
				r = mid - 1
			} else {
				l = mid + 1
			}
		}
		if height[l] > hg {
			l--
		}
		ans = append(ans, arr[l])
	}
	for _, v := range ans {
		Fprintf(out, "%d ", v)
	}
	Fprintln(out)
}

func abs(a int64) int64 {
	if a > 0 {
		return a
	}
	return -a
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}
func min(a, b int) int {
	if a > b {
		return b
	}
	return a
}
func gcd(a, b int) int {
	if a%b == 0 {
		return b
	}
	return gcd(b, a%b)
}

func main() {
	stdin := os.Stdin
	stdout := os.Stdout
	in := bufio.NewReader(stdin)
	out := bufio.NewWriter(stdout)
	n := 1
	Fscan(in, &n)
	for i := 0; i < n; i++ {
		solve(in, out)
	}
}

package main

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

func max(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, q int
		fmt.Fscan(in, &n, &q)

		aa := make([]int64, n)
		for i := range aa {
			fmt.Fscan(in, &aa[i])
		}

		pf := make([]int64, n+1)
		for i, v := range aa {
			pf[i+1] = v + pf[i]
		}

		bb := [][2]int{}
		for i, v := range aa {
			if len(bb) == 0 || int(v) > bb[len(bb)-1][0] {
				bb = append(bb, [2]int{int(v), i})
			}
			bb[len(bb)-1][1] = i
		}

		qq := make([]int, q)
		for i := range qq {
			fmt.Fscan(in, &qq[i])
		}

		for _, v := range qq {
			idx := sort.Search(len(bb), func(i int) bool {
				return bb[i][0] >= v
			})
			var d int64
			if idx >= len(bb) {
				d = pf[n]
			} else {
				if v < bb[idx][0] {
					idx--
				}
				if idx < 0 {
					d = 0
				} else {
					d = pf[bb[idx][1]+1]
				}
			}
			fmt.Fprintf(out, "%d ", d)
		}
		fmt.Fprintln(out)
	}
}

package main

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

type IO struct {
	in  io.Reader
	out io.Writer
}

func NewIO(in io.Reader, out io.Writer) *IO {
	return &IO{in: bufio.NewReader(in), out: out}
}

func (io *IO) ReadInt() int {
	var res int
	_, _ = fmt.Fscanf(io.in, "%d\n", &res)
	return res
}

func (io *IO) ReadPairInt() (int, int) {
	var a, b int
	_, _ = fmt.Fscanf(io.in, "%d %d\n", &a, &b)
	return a, b
}

func (io *IO) ReadString() string {
	var res string
	_, _ = fmt.Fscanf(io.in, "%s\n", &res)
	return res
}

func (io *IO) ReadIntSlice(n int) sort.IntSlice {
	var t int
	res := make(sort.IntSlice, 0, n)
	for i := 0; i < n; i++ {
		_, _ = fmt.Fscanf(io.in, "%d", &t)
		res = append(res, t)
	}
	_, _ = fmt.Fscanf(io.in, "\n")
	return res
}

func (io *IO) Println(v ...interface{}) {
	_, _ = fmt.Fprintln(io.out, v...)
}

func (io *IO) Print(v ...interface{}) {
	_, _ = fmt.Fprint(io.out, v...)
}

func (io *IO) Printf(format string, v ...interface{}) {
	_, _ = fmt.Fprintf(io.out, format, v...)
}

func (io *IO) PrintIntSlice(s sort.IntSlice) {
	for _, v := range s {
		io.Printf("%d ", v)
	}
}

func (io *IO) PrintlnIntSlice(s sort.IntSlice) {
	io.PrintIntSlice(s)
	io.Printf("\n")
}

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

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

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

func gcd(a, b int) int {
	for b != 0 {
		a %= b
		a, b = b, a
	}
	return a
}

func main() {
	io := NewIO(os.Stdin, os.Stdout)

	t := io.ReadInt()
	for ; t > 0; t-- {
		a := io.ReadIntSlice(2)
		n, q := a[0], a[1]

		stairs := io.ReadIntSlice(n)
		queries := io.ReadIntSlice(q)

		heightOnPosition := make([]int64, len(stairs))
		heightOnPosition[0] = int64(stairs[0])
		for i := 1; i < len(stairs); i++ {
			heightOnPosition[i] = heightOnPosition[i-1] + int64(stairs[i])
		}

		mxOnPrefix := make([]int, len(stairs))
		mxOnPrefix[0] = stairs[0]
		for i := 1; i < len(stairs); i++ {
			mxOnPrefix[i] = max(mxOnPrefix[i-1], stairs[i])
		}

		lastPositionLessEqualThanK := func(k int) int {
			if mxOnPrefix[0] > k {
				return -1
			}

			l, r := 0, len(mxOnPrefix)-1
			for r-l > 1 {
				m := (r + l) / 2
				if mxOnPrefix[m] > k {
					r = m
				} else {
					l = m
				}
			}

			for i := r; i >= l; i-- {
				if mxOnPrefix[i] > k {
					continue
				}
				return i
			}

			return -2
		}

		for i := 0; i < len(queries); i++ {
			k := queries[i]
			pos := lastPositionLessEqualThanK(k)
			if pos == -1 {
				io.Print(0, " ")
			} else {
				io.Print(heightOnPosition[pos], " ")
			}
		}

		io.Println()
	}
}

package main

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

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

func bsearch(a []int, l, r, v int) int {
	if (l == r) { return l }
	m := (l + r + 1) >> 1
	if a[m] > v { return bsearch(a, l, m-1, v) }
	return bsearch(a, m, r, v)
}

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, q int
		fmt.Fscan(r, &n, &q)
		a := make([]int, n+1)
		h := make([]int, n+1)
		for i := range a {
			if i == 0 { continue }
			fmt.Fscan(r, &a[i])
			h[i] = h[i-1] + a[i]
			a[i] = max(a[i], a[i-1])
		}

		for i := 0; i < q; i++ {
			var qq int
			fmt.Fscan(r, &qq)
			fmt.Fprintf(w, "%d ", h[bsearch(a, 0, n, qq)])
		}

		fmt.Fprintln(w, "")

	}

}

package main

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

func trim(s string) string {
	return s[:len(s)-2]
}

func checkNumberPowerOfTwo(n int) int {
	return n & (n - 1)
}

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 sliceStr2Int(arr []string) []int {
	var arrInt []int
	for _, i := range arr {
		num, _ := strconv.Atoi(i)
		arrInt = append(arrInt, num)
	}

	return arrInt
}

func sliceIntFromConsole() []int {
	arrLine, _ := reader.ReadString('\n')
	return sliceStr2Int(strings.Split(trim(arrLine), " "))
}

func check(i int) int {
	if i == 0 {
		return 0
	}
	return i
}

var reader = bufio.NewReader(os.Stdin)

func main() {
	testLine, _ := reader.ReadString('\n')
	test, _ := strconv.Atoi(trim(testLine))

	for t := 0; t < test; t++ {
		nq := sliceIntFromConsole()
		n, q := nq[0], nq[1]
		a := sliceIntFromConsole()
		kRaw := sliceIntFromConsole()

		var k [][]int
		for i, v := range kRaw {
			k = append(k, []int{i, v})
		}
		sort.Slice(k, func(i, j int) bool {
			return k[i][1] < k[j][1]
		})

		answer := make([]int, q)
		var p1, p2, height int
		// p1 -- points at array a
		// p2 -- points at array k
		for {
			if p2 == q {
				break
			}
			if p1 == n {
				for i := check(p2); i < q; i++ {
					answer[k[i][0]] = height
				}
				break
			}

			if k[p2][1] >= a[p1] {
				height += a[p1]
				p1++
			} else {
				answer[k[p2][0]] = height
				p2++
			}
		}

		for _, v := range answer {
			fmt.Print(v, " ")
		}
		fmt.Println()
	}
}

//1
//15 10
//11 14 3 10 6 11 3 14 3 7 14 2 10 5 4
//100 46 29 82 70 8 28 28 119 100

package main

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

func trim(s string) string {
	return s[:len(s)-2]
}

func checkNumberPowerOfTwo(n int) int {
	return n & (n - 1)
}

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 sliceStr2Int(arr []string) []int {
	var arrInt []int
	for _, i := range arr {
		num, _ := strconv.Atoi(i)
		arrInt = append(arrInt, num)
	}

	return arrInt
}

func sliceIntFromConsole() []int {
	arrLine, _ := reader.ReadString('\n')
	return sliceStr2Int(strings.Split(trim(arrLine), " "))
}

func check(i int) int {
	if i == 0 {
		return 0
	}
	return i
}

var reader = bufio.NewReader(os.Stdin)

func main() {
	testLine, _ := reader.ReadString('\n')
	test, _ := strconv.Atoi(trim(testLine))

	for t := 0; t < test; t++ {
		nq := sliceIntFromConsole()
		n, q := nq[0], nq[1]
		a := sliceIntFromConsole()
		kRaw := sliceIntFromConsole()

		var k [][]int
		for i, v := range kRaw {
			k = append(k, []int{i, v})
		}
		sort.Slice(k, func(i, j int) bool {
			return k[i][1] < k[j][1]
		})
		fmt.Println(k)

		answer := make([]int, q)
		var p1, p2, height int
		// p1 -- points at array a
		// p2 -- points at array k
		for {
			if p2 == q {
				break
			}
			if p1 == n {
				for i := check(p2); i < q; i++ {
					answer[k[i][0]] = height
				}
				break
			}

			if k[p2][1] >= a[p1] {
				height += a[p1]
				p1++
			} else {
				fmt.Println(k[p2][0])
				answer[k[p2][0]] = height
				p2++
			}
		}

		for _, v := range answer {
			fmt.Print(v, " ")
		}
		fmt.Println()
	}
}

//1
//15 10
//11 14 3 10 6 11 3 14 3 7 14 2 10 5 4
//100 46 29 82 70 8 28 28 119 100

package main

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

func trim(s string) string {
	return s[:len(s)-2]
}

func checkNumberPowerOfTwo(n int) int {
	return n & (n - 1)
}

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 sliceStr2Int(arr []string) []int {
	var arrInt []int
	for _, i := range arr {
		num, _ := strconv.Atoi(i)
		arrInt = append(arrInt, num)
	}

	return arrInt
}

func sliceIntFromConsole() []int {
	arrLine, _ := reader.ReadString('\n')
	return sliceStr2Int(strings.Split(trim(arrLine), " "))
}

func check(i int) int {
	if i == 0 {
		return 0
	}
	return i
}

var reader = bufio.NewReader(os.Stdin)

func main() {
	testLine, _ := reader.ReadString('\n')
	test, _ := strconv.Atoi(trim(testLine))

	for t := 0; t < test; t++ {
		nq := sliceIntFromConsole()
		n, q := nq[0], nq[1]
		a := sliceIntFromConsole()
		kRaw := sliceIntFromConsole()

		var k [][]int
		for i, v := range kRaw {
			k = append(k, []int{i, v})
		}
		sort.Slice(k, func(i, j int) bool {
			return k[i][1] < k[j][1]
		})

		answer := make([]int, q)
		var p1, p2, height int
		// p1 -- points at array a
		// p2 -- points at array k
		for {
			if p2 == q {
				break
			}
			if p1 == n {
				for i := check(p2); i < q; i++ {
					answer[i] = height
				}
				break
			}

			if k[p2][1] >= a[p1] {
				height += a[p1]
				p1++
			} else {
				answer[p2] = height
				p2++
			}
		}

		for _, v := range answer {
			fmt.Print(v, " ")
		}
		fmt.Println()
	}
}

package main

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

const cf = true

func solve() {
	var n, q int
	fmt.Fscan(in, &n, &q)
	mx := make([]int, n)
	sum := make([]int, n)
	var num int
	for i := range mx {
		fmt.Fscan(in, &num)
		mx[i] = max(mx[i], num)
		sum[i] = num
		if i > 0 {
			sum[i] += sum[i-1]
		}
	}
	for i := 0; i < q; i++ {
		fmt.Fscan(in, &num)
		l, r := -1, n
		for l < r-1 {
			mid := (l + r) / 2
			if num >= mx[mid] {
				l = mid
			} else {
				r = mid
			}
		}
		if l == -1 {
			fmt.Fprint(out, "0 ")
		} else {
			fmt.Fprint(out, sum[l], " ")
		}
	}
	fmt.Fprintln(out)
}

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

const cf = true

func solve() {
	var n, q int
	fmt.Fscan(in, &n, &q)
	mx := make([]int, n)
	sum := make([]int, n)
	var num int
	for i := range mx {
		fmt.Fscan(in, &num)
		mx[i] = max(mx[i], num)
		sum[i] = num
		if i > 0 {
			sum[i] += sum[i-1]
		}
	}
	for i := 0; i < q; i++ {
		fmt.Fscan(in, &num)
		l, r := 0, n-1
		for l <= r {
			mid := (l + r) / 2
			if mx[mid] > num {
				r = mid - 1
			} else {
				l = mid + 1
			}
		}
		if l == 0 {
			fmt.Fprint(out, "0 ")
		} else {
			fmt.Fprint(out, sum[l-1], " ")
		}
	}
	fmt.Fprintln(out)
}

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

const cf = true

func solve() {
	var n, q int
	fmt.Fscan(in, &n, &q)
	mx := make([]int, n)
	sum := make([]int, n)
	var num int
	for i := range mx {
		fmt.Fscan(in, &num)
		mx[i] = max(mx[i], num)
		sum[i] = num
		if i > 0 {
			sum[i] += sum[i-1]
		}
	}
	for i := 0; i < q; i++ {
		fmt.Fscan(in, &num)
		l, r := -1, n
		for l < r-1 {
			mid := (l + r) / 2
			if mx[mid] > num {
				r = mid
			} else {
				l = mid
			}
		}
		if l == -1 {
			fmt.Fprint(out, "0 ")
		} else {
			fmt.Fprint(out, sum[l], " ")
		}
	}
	fmt.Fprintln(out)
}

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

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

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

	var T int
	var n int
	var m int
	var x int

	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		Fscan(in, &m)
		r := make([]int, n)
		q := make([]int, m)
		st := []int{}
		stv := []int{}
		s := make([]int, n)
		for i := 0; i < n; i++ {
			Fscan(in, &x)
			r[i] = x
			if len(st) == 0 || stv[len(stv)-1] < x {
				st = append(st, i)
				stv = append(stv, x)
			}

			s[i] += x
			if i > 0 {
				s[i] += s[i-1]
			}
		}
		for i := 0; i < m; i++ {
			Fscan(in, &x)
			q[i] = x
		}

		ans := make([]int, m)
		for i, v := range q {
			t := sort.SearchInts(stv, v+1)
			if t >= len(stv) {
				ans[i] = s[n-1]
			} else if t == 0 {
				ans[i] = 0
			} else {
				ans[i] = s[st[t]-1]
			}
		}

		Fprintln(out, ans)
	}
}


package main

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

var n, k int

var x []int

func main() {

	r := bufio.NewReader(os.Stdin)

	var t int

	fmt.Fscan(r, &t)

	for i := 0; i < t; i++ {
		fmt.Fscan(r, &n, &k)

		x = make([]int, k)

		for j := 0; j < k; j++ {
			fmt.Fscan(r, &x[j])
		}

		ans := solve()

		fmt.Println(ans)
	}
}

func solve() int {
	sort.Ints(x)

	cat := 0

	saved := 0

	for  j := k - 1; j >= 0; j-- {
		pos := x[j]

		if pos <= cat {
			return saved
		}

		moves := n - pos

		cat += moves

		saved++
	}

	return k
}

package main

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

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

	var t int

	for fmt.Fscan(in, &t); t > 0; t-- {
		var n, k int
		fmt.Fscan(in, &n, &k)
		arr := make([]int, k)
		for i := 0; i < k; i++ {
			fmt.Fscan(in, &arr[i])
		}
		sort.Slice(arr, func(i, j int) bool {
			return arr[i] > arr[j]
		})

		ans, cat := 0, 0
		for cnt := 0; cnt < k && cat < arr[cnt]; cnt++ {
			cat += n - arr[cnt]
			ans++
		}

		fmt.Fprintln(out, ans)
	}
}

package main

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

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

	var t int

	for fmt.Fscan(in, &t); t > 0; t-- {
		var n, k int
		fmt.Fscan(in, &n, &k)
		arr := make([]int, k)
		for i := 0; i < k; i++ {
			fmt.Fscan(in, &arr[i])
		}
		sort.Slice(arr, func(i, j int) bool {
			return arr[i] > arr[j]
		})

		var cnt int
		cat := 0
		for cnt = 0; cnt < k && cat < arr[cnt]; cnt++ {
			cat += n - arr[cnt]
		}

		fmt.Fprintln(out, cnt)
	}
}

package main

import (
	"bufio"
	"fmt"
	"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, k int
		fmt.Fscan(in, &n, &k)

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

		sort.Ints(arr)
		var total int
		var cat int
		for i := len(arr) - 1; i >= 0; i-- {
			if arr[i] > cat {
				total++
				cat += n - arr[i]
			}
		}
		fmt.Fprintln(out, total)
	}
}

package main

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

func main() {
	r := bufio.NewReaderSize(os.Stdin, 1024*1024)

	var T int
	fmt.Fscanf(r, "%d\n", &T)
	for t := 0; t < T; t++ {
		var n, k int
		fmt.Fscanf(r, "%d %d\n", &n, &k)

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

		sort.Ints(arr)

		res := sort.Search(len(arr), func(i int) bool {
			m := int64(0)
			for ; i < len(arr); i++ {
				m += int64(n - arr[i])
			}

			return m < int64(n)
		})

		fmt.Println(k - res)
	}
}

package main

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

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

var pos [400000]int

func main() {
	var ntc, len, mice int
	fmt.Fscan(in, &ntc)
	for t := 0; t < ntc; t++ {
		fmt.Fscan(in, &len, &mice)
		for i := 0; i < mice; i++ {
			fmt.Fscan(in, &pos[i])
		}

		sort.Slice(pos[0:mice], func(i, j int) bool {
			return pos[i] < pos[j]
		})

		var miceTime, ans = 0, 0
		for i := mice - 1; i >= 0; i-- {
			miceTime += len - pos[i]
			if miceTime < len {
				ans++
			} else {
				break
			}
		}

		fmt.Fprintf(out, "%d\n", ans)
	}
	out.Flush()
}

package main

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

const Inf = 1000

var (
	rr = bufio.NewReader(os.Stdin)
	ww = bufio.NewWriter(os.Stdout)

	T int

	n, k int
	x []int
)

func main() {
	fmt.Fscan(rr, &T)
	for ; T > 0; T-- {
		fmt.Fscan(rr, &n, &k)
		x = make([]int, k)
		for i := range x {
			fmt.Fscan(rr, &x[i])
		}
		sort.Sort(sort.Reverse(sort.IntSlice(x)))

		fmt.Fprintln(ww, sort.Search(k + 1, func(k int) bool {
			if k == 0 {
				return false
			}
			res := 0
			for i := k - 1; i >= 0; i-- {
				if x[i] <= res {
					return true
				}
				res += n - x[i]
			}
			return false
		}) - 1)
	}
	_ = ww.Flush()
}

package main

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

func solve() {
	n := get()
	k := get()
	a := geta(k)
	sort.Ints(a)
	for i := 0; 2*i < k; i++ {
		a[i], a[k-i-1] = a[k-i-1], a[i]
	}
	cat := 0
	count := 0
	for _, v := range a {
		if cat >= v {
			break
		}
		count++
		cat += n - v
	}
	putln(count)
}

const (
	once       = false
	maxBufSize = 10000000
)

var (
	splitFunc = bufio.ScanWords
)

func main() {
	in = bufio.NewScanner(bufio.NewReader(os.Stdin))
	in.Split(splitFunc)
	in.Buffer(make([]byte, maxBufSize), maxBufSize)

	out = bufio.NewWriter(os.Stdout)
	defer out.Flush()

	if once {
		solve()
		return
	}

	t := get()
	for t > 0 {
		solve()
		t--
	}
}

var (
	in  *bufio.Scanner
	out *bufio.Writer
)

func get() int {
	x, _ := strconv.Atoi(gets())
	return x
}

func geta(n int) []int {
	a := make([]int, n)
	for i := range a {
		a[i] = get()
	}
	return a
}

func geta1(n int) []int {
	a := make([]int, n)
	for i := range a {
		a[i] = get() - 1
	}
	return a
}

func get64() int64 {
	x, _ := strconv.ParseInt(gets(), 10, 64)
	return x
}

func get64a(n int) []int64 {
	a := make([]int64, n)
	for i := range a {
		a[i] = get64()
	}
	return a
}

func get64a1(n int) []int64 {
	a := make([]int64, n)
	for i := range a {
		a[i] = get64() - 1
	}
	return a
}

func getf() float64 {
	x, _ := strconv.ParseFloat(gets(), 64)
	return x
}

func getfa(n int) []float64 {
	a := make([]float64, n)
	for i := range a {
		a[i] = getf()
	}
	return a
}

func gets() string {
	scan()
	return in.Text()
}

func scanN(n int) {
	for n > 0 {
		scan()
		n--
	}
}

func scan() {
	in.Scan()
}

func putc(c rune) {
	out.WriteRune(c)
}

func line() {
	putc('\n')
}

func space() {
	putc(' ')
}

func put(x int) {
	out.WriteString(strconv.Itoa(x))
}

func putln(i int) {
	put(i)
	line()
}

func puta(a []int) {
	for _, v := range a {
		put(v)
		space()
	}
	line()
}

func put64(x int64) {
	out.WriteString(strconv.FormatInt(x, 10))
}

func put64ln(x int64) {
	put64(x)
	line()
}

func put64a(a []int64) {
	for _, v := range a {
		put64(v)
		space()
	}
	line()
}

func puts(s string) {
	out.WriteString(s)
}

func putsln(s string) {
	puts(s)
	line()
}

func yn(y bool) {
	if y {
		yes()
		return
	}
	no()
}

func yes() {
	puts("YES\n")
}

func no() {
	puts("NO\n")
}

func putf(x float64) {
	out.WriteString(strconv.FormatFloat(x, 'f', -1, 64))
}

func putfln(x float64) {
	putf(x)
	line()
}

func putfa(a []float64) {
	for _, v := range a {
		putf(v)
		space()
	}
	line()
}

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

func abs64(x int64) int64 {
	if x < 0 {
		return -x
	}
	return x
}

func absf(x float64) float64 {
	return math.Abs(x)
}

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

func max64(x int64, y int64) int64 {
	if x > y {
		return x
	}
	return y
}

func maxf(x float64, y float64) float64 {
	return math.Max(x, y)
}

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

func min64(x int64, y int64) int64 {
	if x < y {
		return x
	}
	return y
}

func minf(x float64, y float64) float64 {
	return math.Min(x, y)
}

func L(i int) int64 {
	return int64(i)
}

func ints(n int, val int) (a []int) {
	a = make([]int, n)
	if val == 0 {
		return
	}
	for i := range a {
		a[i] = val
	}
	return
}

func int64s(n int, val int64) (a []int64) {
	a = make([]int64, n)
	if val == 0 {
		return
	}
	for i := range a {
		a[i] = val
	}
	return
}

func floats(n int, val float64) (a []float64) {
	a = make([]float64, n)
	if val == 0 {
		return
	}
	for i := range a {
		a[i] = val
	}
	return
}

package main

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

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

  var t int
  for fmt.Fscan(reader, &t); t > 0; t-- {
    var n, k int
    fmt.Fscan(reader, &n, &k)
    arr := make([]int, k)
    for i := 0; i < k; i++ {
      fmt.Fscan(reader, &arr[i])
    }
    sort.Ints(arr)
    d := 0
    ans := 0
    for i := k-1; i >= 0; i-- {
      if arr[i] <= d {
        break
      }
      ans++
      d += (n-arr[i])
    }
    fmt.Fprint(writer, ans, "\n")
  }
}

package main

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

/*
4
4 2
2 3
4 2
2 2
2 1
1
2 1
2
---
2
1
1
1
=====
*/

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

	bld := strings.Builder{}

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

	for ; t > 0; t-- {
		var n, k int
		scanf("%d %d\n", &n, &k)

		a := make([]int, k)

		for i := range a {
			scanf("%d", &a[i])
		}
		scanf("\n")

		sort.Ints(a)

		catPos := 0
		miceCnt := 0

		for i := len(a) - 1; i >= 0; i-- {
			num := a[i]
			if num > catPos {
				miceCnt++
				catPos += n - num
			} else {
				break
			}
		}

		fmt.Fprintln(&bld, miceCnt)
	}

	printf("%s", bld.String())
}

const maxUint = ^uint(0)

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

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

//goland:noinspection GoUnusedType
type pair struct {
	x int64
	y int64
}

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

//goland:noinspection GoUnusedFunction
func printf(f string, a ...interface{}) {
	if _, err := fmt.Fprintf(writer, f, a...); err != nil {
		panic(err)
	}
}

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

//goland:noinspection GoUnusedFunction
func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

//goland:noinspection GoUnusedFunction
func max(x, y int) int {
	if x > y {
		return x
	}
	return y
}

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

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

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

	for i := 5; i*i <= n; i += 6 {
		if n%i == 0 || n%(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 {
	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"
	"os"
	"sort"
)

//problem C

type problem struct {
	n, k int
	mice []int
}

func (p *problem) SaveMices() int {
	remainSecond := p.n - 1
	saved := 0
	sort.Ints(p.mice)
	for i := p.k - 1; i >= 0; i-- {
		needSecond := p.n - p.mice[i]
		if remainSecond >= needSecond {
			saved++
			remainSecond -= needSecond
		} else {
			break
		}
	}

	return saved
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	var t int
	var p problem

	fmt.Fscanf(reader, "%d\n", &t)
	for ; t > 0; t-- {
		fmt.Fscanf(reader, "%d  %d\n", &p.n, &p.k)
		p.mice = make([]int, p.k)

		for i := 0; i < p.k; i++ {
			if i < p.k-1 {
				fmt.Fscanf(reader, "%d ", &p.mice[i])
			} else {
				fmt.Fscanf(reader, "%d\n", &p.mice[i])
			}
		}

		fmt.Printf("%d\n", p.SaveMices())
	}
}

package main

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

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

	return b
}

func min(nums ...int) int {
	result := nums[0]
	for _, num := range nums {
		result = min2(result, num)
	}

	return result
}

func main() {
	r := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	defer w.Flush()
	var t, n, k, mice int

	_, _ = fmt.Fscan(r, &t)

	for ; t > 0; t-- {
		_, _ = fmt.Fscan(r, &n, &k)
		var x []int
		for i := 1; i <= k; i++ {
			_, _ = fmt.Fscan(r, &mice)
			x = append(x, mice)
		}

		sort.Ints(x)

		catpos := 0
		result := 0
		for i := k - 1; i >= 0; i-- {
			if catpos >= x[i] {
				break
			}

			catpos += n - x[i]
			result++
		}

		fmt.Println(result)
	}
}

package main

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

// var m []int
// err = json.Unmarshal([]byte(r.FormValue("example")), &m)

// for i := 0; i < len(m); i++ {
// 	fmt.Println(m[i])
// }

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

func MIN(x, y int64) int64 {
	if x > y {
		return y
	} else {
		return x
	}
}

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

func MKO(x, y int64) int64 {
	if x < y {
		return 0
	} else {
		return x - y + 1
	}
}

func MAX(x, y int64) int64 {
	if x > y {
		return x
	} else {
		return y
	}
}

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

func LCM(x, y int) int {
	if x >= y {
		initial := x
		for i := 1; i < y+2; i++ {
			if initial%y == 0 {
				break
			} else {
				initial = initial + x
			}

		}
		return initial
	} else {
		initial := y
		for i := 1; i < x+2; i++ {
			if initial%x == 0 {
				break
			} else {
				initial = initial + y
			}

		}
		return initial
	}
}

func GCD(x, y int64) int64 {
	if x*y == 0 {
		return MAX(x, y)
	} else {
		var l, q int64
		l = MAX(x, y)
		q = MIN(x, y)
		var m int64
		for l*q > 0 {
			a := l % q
			b := l - a
			cc := int64(b / q)
			remain := l - cc*q
			l = MAX(remain, q)
			q = MIN(remain, q)
			if l*q == 0 {
				m = l + q
				break
			}

		}
		return m
	}
}

func IsItPrime(x int) bool {
	if x < 10 {
		if x == 2 || x == 3 || x == 5 || x == 7 {
			return true
		} else {
			return false
		}
	} else {
		j := 0
		k := math.Sqrt(float64(x))
		for m := 3; m <= int(k); {
			if x%m == 0 {
				j = 1
				break
			}
			m = m + 2
		}
		if j == 1 {
			return false
		} else {
			return true
		}
	}
}

func PrimeFactorization(n int) (pfs map[int]int) {
	pfs = make(map[int]int)

	// Get the number of 2s that divide n
	for n%2 == 0 {
		if _, ok := pfs[2]; ok {
			pfs[2] += 1
		} else {
			pfs[2] = 1
		}
		n = n / 2
	}

	// n must be odd at this point. so we can skip one element
	// (note i = i + 2)
	for i := 3; i*i <= n; i = i + 2 {
		// while i divides n, append i and divide n
		for n%i == 0 {
			if _, ok := pfs[i]; ok {
				pfs[i] += 1
			} else {
				pfs[i] = 1
			}
			n = n / i
		}
	}

	// This condition is to handle the case when n is a prime number
	// greater than 2
	if n > 2 {
		pfs[n] = 1
	}

	return
}

func NumberOfDivisors(n int) int {
	pfs := PrimeFactorization(n)

	num := 1
	for _, exponents := range pfs {
		num *= (exponents + 1)
	}

	return num
}

func KthPrime(x int) (y int) {
	if x == 1 {
		y = 2
	}
	if x == 2 {
		y = 3
	}
	if x > 2 {
		var sum int = 2
		begin := 5
		for sum < x+1 {
			if IsItPrime(begin) == true {
				sum++
			}
			if sum == x {
				y = begin
				break
			} else {
				if begin%6 == 1 {
					begin = begin + 4
				} else {
					begin = begin + 2
				}
			}
		}
	}
	return
}

func MODULE(x, y int) int {
	if x > y {
		return x - y
	} else {
		return y - x
	}
}

func BITAND(x, y int64) int64 {
	var ans int64
	begin := x
	sec := y
	var count int64 = 1
	for 1 == 1 {
		if (begin%2)*(sec%2) == 1 {
			ans = ans + count
			count = count * 2
			begin = int64(begin / 2)
			sec = int64(sec / 2)
			if begin*sec == 0 {
				break
			}

		} else {
			count = count * 2
			begin = int64(begin / 2)
			sec = int64(sec / 2)
			if begin*sec == 0 {
				break
			}
		}
	}
	return ans
}

func Sum(n, m int) int {
	var ans int

	if n == 1 {
		return 0
	} else {
		for i := 2; i < n; i++ {
			ans = (ans + Sum(i, m)) % m
		}
		for j := 2; j < n+1; j++ {
			ans = (ans + Sum(n/j, m)) % m
		}
		return ans
	}
}

func ChangeStringToDate(x string) (t time.Time, err error) {
	t, err = time.Parse("2006-01-02", x)
	return
}

func DiffrenceBetweenTwoDates(first, second time.Time) int64 {
	date := first
	date2 := second
	k := date.Unix() - date2.Unix()
	return k
}

func Union(a, b []int) []int {
	m := make(map[int]bool)

	for _, item := range a {
		m[item] = true
	}

	for _, item := range b {
		if _, ok := m[item]; !ok {
			a = append(a, item)
		}
	}
	return a
}

var t int

func main() {
	var input io.Reader = bufio.NewReader(os.Stdin)
	fmt.Fscan(input, &t)

	for o := 0; o < t; o++ {
		var n, k int
		fmt.Fscan(input, &n, &k)

		ARR := make([]int, k)

		for i := 0; i < k; i++ {
			var p int
			fmt.Fscan(input, &p)

			ARR[i] = n - p

		}

		sort.Ints(ARR)
		init := 0
		num := 0
		for i := 0; i < k; i++ {
			init += ARR[i]
			if init <= n-1 {
				num++
			} else {
				break
			}

		}

		fmt.Println(num)

	}

}

package main

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

var (
	reader  = bufio.NewReader(os.Stdin)
	writer  = bufio.NewWriter(os.Stdout)
	t, n, k int
	x       []int
)

func main() {
	fmt.Fscan(reader, &t)
	for ; t > 0; t-- {
		fmt.Fscan(reader, &n, &k)
		x = make([]int, k)
		for i := 0; i < k; i++ {
			fmt.Fscan(reader, &x[i])
		}
		sort.Ints(x)
		var i, c int
		for i = len(x) - 1; i >= 0 && c-x[i] < 0; i-- {
			c += n - x[i]
		}
		fmt.Println(len(x) - i - 1)
	}
}

package main

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

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

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	var t int
	fmt.Fscan(reader, &t)
	for c := 0; c < t; c++ {
		var n, k int
		fmt.Fscan(reader, &n, &k)
		mice := make([]int, k)
		for i := 0; i < k; i++ {
			fmt.Fscan(reader, &mice[i])
		}
		sort.Slice(mice, func(i, j int) bool { return mice[i] > mice[j] })
		cat := 0
		result := 0
		for _, x := range mice {
			if cat >= n || cat >= x {
				break
			}
			cat += n - x
			result++
		}
		fmt.Fprintln(writer, result)
	}
}

package main

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

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

	var t int

	for fmt.Fscan(in, &t); t > 0; t-- {
		var n, k int
		fmt.Fscan(in, &n, &k)
		arr := make([]int, k)
		for i := 0; i < k; i++ {
			fmt.Fscan(in, &arr[i])
		}
		sort.Sort(sort.Reverse(sort.IntSlice(arr)))
		/*sort.Slice(arr, func(i, j int) bool {
			return arr[i] > arr[j]
		})*/

		ans, cat := 0, 0
		for i := 0; i < k && cat < arr[i]; i++ {
			cat += n - arr[i]
			ans++
		}

		fmt.Fprintln(out, ans)
	}
}

package main

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

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

	var t int

	for fmt.Fscan(in, &t); t > 0; t-- {
		var n, k int
		fmt.Fscan(in, &n, &k)
		arr := make([]int, k)
		for i := 0; i < k; i++ {
			fmt.Fscan(in, &arr[i])
		}
		/*sort.Sort(sort.Reverse(sort.IntSlice(arr)))*/
		sort.Slice(arr, func(i, j int) bool {
			return arr[i] > arr[j]
		})

		ans, cat := 0, 0
		for i := 0; i < k && cat < arr[i]; i++ {
			cat += n - arr[i]
			ans++
		}

		fmt.Fprintln(out, ans)
	}
}

package main

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

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

	var t int

	for fmt.Fscan(in, &t); t > 0; t-- {
		var n, k int
		fmt.Fscan(in, &n, &k)
		arr := make([]int, k)
		for i := 0; i < k; i++ {
			fmt.Fscan(in, &arr[i])
		}
		sort.Sort(sort.Reverse(sort.IntSlice(arr)))
		/*sort.Slice(arr, func(x, y int) bool {
			return arr[x] > arr[y]
		})*/

		ans, cat := 0, 0
		for i := 0; i < k && cat < arr[i]; i++ {
			cat += n - arr[i]
			ans++
		}

		fmt.Fprintln(out, ans)
	}
}

package main

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

func main() {
	r := bufio.NewReaderSize(os.Stdin, 1024*1024)

	var T int
	fmt.Fscanf(r, "%d\n", &T)
	for t := 0; t < T; t++ {
		var n, k int
		fmt.Fscanf(r, "%d %d\n", &n, &k)

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

		sort.Ints(arr)

		res := sort.Search(len(arr), func(i int) bool {
			m := 0
			for ; i < len(arr); i++ {
				m += n - arr[i]
			}

			return m < n
		})

		fmt.Println(k - res)
	}
}

package main

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

func main() {
	r := bufio.NewReaderSize(os.Stdin, 1024*1024)

	var T int
	fmt.Fscanf(r, "%d\n", &T)
	for t := 0; t < T; t++ {
		var n, k int
		fmt.Fscanf(r, "%d %d\n", &n, &k)

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

		sort.Ints(arr)

		res := sort.Search(len(arr), func(i int) bool {
			m := 0
			for ; i < len(arr); i++ {
				m += n - arr[i]
				if m > arr[i] {
					return false
				}
			}

			return true
		})

		fmt.Println(k - res)
	}
}

package main

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

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

var pos [400000]int

func main() {
	var ntc, len, mice int
	fmt.Fscanf(in, "%d\n", &ntc)
	for t := 0; t < ntc; t++ {
		fmt.Fscanf(in, "%d %d\n", &len, &mice)
		for i := 0; i < mice; i++ {
			fmt.Fscanf(in, "%d ", &pos[i])
		}

		sort.Slice(pos[0:mice], func(i, j int) bool {
			return pos[i] < pos[j]
		})

		var miceTime, ans = 0, 0
		for i := mice - 1; i >= 0; i-- {
			miceTime += len - pos[i]
			if miceTime < len {
				ans++
			} else {
				break
			}
		}

		fmt.Fprintf(out, "%d\n", ans)
	}
	out.Flush()
}

package main

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

const Inf = 1000

var (
	rr = bufio.NewReader(os.Stdin)
	ww = bufio.NewWriter(os.Stdout)

	T int

	n, k int
	x []int
)

func main() {
	fmt.Fscan(rr, &T)
	for ; T > 0; T-- {
		fmt.Fscan(rr, &n, &k)
		x = make([]int, k)
		for i := range x {
			fmt.Fscan(rr, &x[i])
		}
		sort.Sort(sort.Reverse(sort.IntSlice(x)))

		fmt.Fprintln(ww, sort.Search(k, func(k int) bool {
			if k == 0 {
				return false
			}
			res := 0
			for i := k - 1; i >= 0; i-- {
				if x[i] <= res {
					return true
				}
				res += n - x[i]
			}
			return false
		}) - 1)
	}
	_ = ww.Flush()
}


package main

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

const MODULUS = 1000000007

type Solver struct {
	prefixOfOnceCount []int
	powerOfTwo [] int64
}

func (s *Solver) Init(input string) {

	s.prefixOfOnceCount = make([]int, len(input)+1)
	for index, char := range input {
		incrementBy := 0
		if char == '1' {
			incrementBy = 1
		}
		s.prefixOfOnceCount[index+1] = s.prefixOfOnceCount[index] + incrementBy
	}

	maxPowersOfTwoNeeded := len(input) + 7
	s.powerOfTwo = make([]int64, maxPowersOfTwoNeeded)
	s.powerOfTwo[0] = 1
	for i := 1; i < maxPowersOfTwoNeeded; i++ {
		s.powerOfTwo[i] = 2*s.powerOfTwo[i-1] % MODULUS
	}
}

func (s *Solver) Query(from, to int) int64 {
	ones := s.getOnceOnInterval(from, to)
	zeros :=  s.getZerosOnInterval(from, to)

	sumOfOnes := s.powerOf2(ones) - 1
	return (sumOfOnes * s.powerOf2(zeros)) % MODULUS
}

func (s *Solver) powerOf2(n int) int64 {
	return s.powerOfTwo[n]
}

func (s *Solver) getOnceOnInterval(from, to int) int {
	return s.prefixOfOnceCount[to] - s.prefixOfOnceCount[from]
}

func (s *Solver) getZerosOnInterval(from, to int) int {
	return (to - from) - s.getOnceOnInterval(from, to)
}

func main()  {
	var n, q int
	var input string
	inBuf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(inBuf,"%d %d\n", &n, &q)
	fmt.Fscanf(inBuf,"%s\n", &input)

	solver := Solver{}
	solver.Init(input)

	outBuf := bufio.NewWriter(os.Stdout)
	defer func() { outBuf.Flush() }()

	for i := 0; i < q; i++ {
		var from, to int
		fmt.Fscanf(inBuf, "%d %d\n", &from, &to)
		fmt.Fprintf(outBuf, "%d\n", solver.Query(from-1, to))
	}
}

// Giving a 1 inside the input with length N, it can contribute {1, 1, 2, 4, 8 ...}
package main

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

const m = 1000000007

func main() {
	reader := bufio.NewReaderSize(os.Stdin, 1000000)
	n, q, line := 0, 0, ""
	fmt.Fscanf(reader, "%d %d\n%s\n", &n, &q, &line)

	cMap, oneMap := map[int]int{0: 1, 1: 1}, map[int]int{0: 0}
	for i := 1; i <= n; i++ {
		oneMap[i] = oneMap[i-1]
		if line[i-1:i] == "1" {
			oneMap[i]++
		}
	}
	for i := 2; i <= (n + 1); i++ {
		cMap[i] = cMap[i-1] * 2 % m
	}

	for i := 0; i < q; i++ {
		l, r := 0, 0
		fmt.Fscanf(reader, "%d %d\n", &l, &r)

		width, oneCount := r-l+1, oneMap[r]-oneMap[l-1]
		sum := 0
		if oneCount > 0 {
			sum = (cMap[width+1] + m - cMap[width-oneCount+1]) % m
		}

		fmt.Println(sum)
	}
}

package main

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

const MODULUS = 1000000007

type Solver struct {
	prefixOfOnceCount []int
	powerOfTwo [] int64
}

func (s *Solver) Init(input string) {

	s.prefixOfOnceCount = make([]int, len(input)+1)
	for index, char := range input {
		incrementBy := 0
		if char == '1' {
			incrementBy = 1
		}
		s.prefixOfOnceCount[index+1] = s.prefixOfOnceCount[index] + incrementBy
	}

	maxPowersOfTwoNeeded := len(input) + 7
	s.powerOfTwo = make([]int64, maxPowersOfTwoNeeded)
	s.powerOfTwo[0] = 1
	for i := 1; i < maxPowersOfTwoNeeded; i++ {
		s.powerOfTwo[i] = 2*s.powerOfTwo[i-1] % MODULUS
	}
}

func (s *Solver) Query(from, to int) int64 {
	ones := s.getOnceOnInterval(from, to)
	zeros :=  s.getZerosOnInterval(from, to)

	sumOfOnes := s.powerOf2(ones) - 1
	return (sumOfOnes * s.powerOf2(zeros)) % MODULUS
}

func (s *Solver) powerOf2(n int) int64 {
	return s.powerOfTwo[n]
}

func (s *Solver) getOnceOnInterval(from, to int) int {
	return s.prefixOfOnceCount[to] - s.prefixOfOnceCount[from]
}

func (s *Solver) getZerosOnInterval(from, to int) int {
	return (to - from) - s.getOnceOnInterval(from, to)
}

func main()  {

	var n, q int
	var input string
	inBuf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(inBuf,"%d %d\n", &n, &q)
	fmt.Fscanf(inBuf,"%s\n", &input)

	solver := Solver{}
	solver.Init(input)

	for i := 0; i < q; i++ {
		var from, to int
		fmt.Fscanf(inBuf, "%d %d\n", &from, &to)
		fmt.Println(solver.Query(from-1, to))
	}
}

package main

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

const (
	Mod  = 1000000007
	MaxN = 100005
)

var n, q int
var input string
var pre, s [MaxN+1]int64

func init() {
	pre[0] = 1
	for i := 1; i < MaxN; i++ {
		pre[i] = (pre[i - 1] * 2) % Mod
	}
}

func main() {
	inBuf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(inBuf,"%d %d\n", &n, &q)
	fmt.Fscanf(inBuf,"%s\n", &input)

	for i := 0; i < n; i++ {
		c := int64(0)
		if input[i] == '1' {
			c++
		}
		s[i + 1] = s[i] + c
	}

	outBuf := bufio.NewWriter(os.Stdout)
	defer func() { outBuf.Flush() }()
	for i := 0; i < q; i++ {
		var l, r int64
		fmt.Fscanf(inBuf, "%d %d\n", &l, &r)
		ones := s[r] - s[l - 1]
		zeros := r - l + 1 - ones
		fmt.Fprintf(outBuf, "%d\n", (pre[ones] - 1 + Mod) * pre[zeros] % Mod)
	}

}

package main

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

const MODULUS = 1000000007

type Solver struct {
	prefixOfOnceCount []int
	powerOfTwo [] int64
}

func (s *Solver) Init(input string) {

	s.prefixOfOnceCount = make([]int, len(input)+1)
	for index, char := range input {
		incrementBy := 0
		if char == '1' {
			incrementBy = 1
		}
		s.prefixOfOnceCount[index+1] = s.prefixOfOnceCount[index] + incrementBy
	}

	maxPowersOfTwoNeeded := len(input) + 7
	s.powerOfTwo = make([]int64, maxPowersOfTwoNeeded)
	s.powerOfTwo[0] = 1
	for i := 1; i < maxPowersOfTwoNeeded; i++ {
		s.powerOfTwo[i] = 2*s.powerOfTwo[i-1] % MODULUS
	}
}

func (s *Solver) Query(from, to int) int64 {
	ones := s.getOnceOnInterval(from, to)
	zeros :=  s.getZerosOnInterval(from, to)

	sumOfOnes := s.powerOf2(ones) - 1
	return (sumOfOnes * s.powerOf2(zeros)) % MODULUS
}

func (s *Solver) powerOf2(n int) int64 {
	return s.powerOfTwo[n]
}

func (s *Solver) getOnceOnInterval(from, to int) int {
	return s.prefixOfOnceCount[to] - s.prefixOfOnceCount[from]
}

func (s *Solver) getZerosOnInterval(from, to int) int {
	return (to - from) - s.getOnceOnInterval(from, to)
}

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

	line1, _ := reader.ReadString('\n')
	line1Elems := strings.Split(strings.Trim(line1, "\n"), " ")
	q, _ := strconv.ParseInt(line1Elems[1], 10, 32)

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

	solver := Solver{}
	solver.Init(line)

	for i := 0; i < int(q); i++ {
		ln, _ := reader.ReadString('\n')
		lnElems := strings.Split(strings.Trim(ln, "\n"), " ")
		from, _ := strconv.ParseInt(lnElems[0], 10, 32)
		to, _ := strconv.ParseInt(lnElems[1], 10, 32)

		fmt.Println(solver.Query(int(from)-1, int(to)))
	}
}

// Giving a 1 inside the input with length N, it can contribute {1, 1, 2, 4, 8 ...}
package main

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

const m = 1000000007

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())
	scanner.Scan()
	q, _ := strconv.Atoi(scanner.Text())
	scanner.Scan()
	line := scanner.Text()

	cMap, oneMap := map[int]int{0: 0, 1: 1, 2: 1}, map[int]int{0: 0}
	for i := 1; i <= n; i++ {
		oneMap[i] = oneMap[i-1]
		if line[i-1:i] == "1" {
			oneMap[i]++
		}
	}
	for i := 3; i <= (n + 2); i++ {
		cMap[i] = cMap[i-1] * 2 % m
	}

	for i := 0; i < q; i++ {
		scanner.Scan()
		l, _ := strconv.Atoi(scanner.Text())
		scanner.Scan()
		r, _ := strconv.Atoi(scanner.Text())

		// width, sum, oneCount := r-l+1, 0, oneMap[r]-oneMap[l-1]
		// for j := 0; j < oneCount; j++ {
		// 	sum = (sum + cMap[width-j]) % m
		// }
		width, oneCount := r-l+1, oneMap[r]-oneMap[l-1]
		sum := 0
		if oneCount > 0 {
			// fmt.Println("cMap[width+2], cMap[width-oneCount+1]", cMap[width+2], cMap[width-oneCount+1])
			sum = cMap[width+2] - cMap[width-oneCount+2]
		}

		fmt.Println(sum)
	}
}