//nolint
package main

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

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

func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{})  { fmt.Fscanf(reader, f, a...) }
func print(a ...interface{})            { fmt.Fprint(writer, a...) }
func println(a ...interface{})          { fmt.Fprintln(writer, a...) }
func scan(a ...interface{})             { fmt.Fscan(reader, a...) }

func main() {
	defer writer.Flush()

	Main()
}

func Main() {
	var t int
	scan(&t)
	for ; t > 0; t-- {
		Solve()
	}
}

//}

func Solve() {
	var n, k int
	scan(&n, &k)
	if k == 1 {
		println("YES")
		for i := 0; i < n; i++ {
			println(i + 1)
		}
		return
	}
	if n%2 == 1 {
		println("NO")
	} else {
		println("YES")
		for i := 1; i <= n; i++ {
			for j := 0; j < k; j++ {
				print(i+j*n, " ")
			}
			println()
		}
	}
}

/*
https://codeforces.com/contest/1634/problem/C

*/

package main

import (
	"bytes"
	"bufio"
	"fmt"
	//"math"
	"os"
	"strconv"
	"strings"
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	line, _ := reader.ReadString('\n')
	line_arr := strings.Fields(line)
	T, _ := strconv.Atoi(line_arr[0])
	for t := 0; t<T; t++ {
		line, _ = reader.ReadString('\n')
		line_arr = strings.Fields(line)
		N, _ := strconv.Atoi(line_arr[0])
		K, _ := strconv.Atoi(line_arr[1])
		//fmt.Println(N, K)

		var buffer bytes.Buffer
		if K == 1{
			fmt.Println("YES")
			for i:=0; i<N; i++ {
				buffer.WriteString(strconv.Itoa(i+1) + "\n")
			}
			fmt.Println(buffer.String())
		} else if N%2 == 0 {
			fmt.Println("YES")
			kmul := 0
			for i:=0; i<N; i++ {
				var offset int
				if i%2 == 0{
					kmul = i*K
					offset = kmul + 1
				} else {
					offset = kmul + 2
				}
				arr := make([]int, K)
				for j:=0; j<K; j++ {
					arr[j] = offset + j*2
					if j > 0 {
						buffer.WriteString(" ")
					}
					buffer.WriteString(strconv.Itoa(arr[j]))
				}
				buffer.WriteString("\n")
			}
			fmt.Println(buffer.String())
		} else {
			fmt.Println("NO")
		}
	}

}

package main

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

/*
7
1 7
5 1
4 2
3 3
4 3
3 4
5 6
---
NO
YES
YES
NO
YES
*/

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

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

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

		if n%2 != 0 && k > 1 {
			fmt.Fprintln(writer, "NO")
			continue
		}

		oddNum := 1
		evnNum := 2

		fmt.Fprintln(writer, "YES")
		for i := 0; i < n; i++ {
			if i%2 == 0 {
				for j := 0; j < k; j++ {
					fmt.Fprintf(writer, "%d ", oddNum)
					oddNum += 2
				}
			} else {
				for j := 0; j < k; j++ {
					fmt.Fprintf(writer, "%d ", evnNum)
					evnNum += 2
				}
			}
			fmt.Fprintln(writer)
		}
	}
}

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 divCeil(num, div int64) int64 {
	quot := num / div
	if num%div != 0 {
		quot++
	}
	return quot
}

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

//goland:noinspection GoUnusedFunction
func max(nums ...int) int {
	maximum := nums[0]
	for _, num := range nums {
		if num > maximum {
			maximum = num
		}
	}
	return maximum
}

//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"
	"bytes"
	"fmt"
	"os"
)

const (
	MOD int64 = 1e9+7
	NL byte = byte('\n')
	SP byte = byte(' ')
)

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

	n, k int
	res bool
)

func main() {
	initBuffer()
	var t int = 1
	t = getInt()
	for i := 0; i < t; i++ {
		solve()
	}
	// test()
	out.Flush()
}

func solve() {
	initialize()
	input()
	count()
	output()
}

func initialize() {

}

func input() {
	n = getInt()
	k = getInt()
}

func count() {
	res = k == 1 || n % 2 == 0
}

func output() {
	if yes(res, true) {
		for i := 0; i < n; i++ {
			for j := 0; j < k; j++ {
				out.Write(intToBytes(j * n + i + 1))
				out.WriteByte(SP)
			}
			out.WriteByte(NL)
		}
	}
}

func test() {
	fmt.Print()
}

func initBuffer() {
	in = bufio.NewScanner(os.Stdin)
	in.Buffer([]byte{}, 4 * 1024 * 1024)
	in.Split(scanWords)
	out = bufio.NewWriter(os.Stdout)
}

func scanWords(data []byte, atEOF bool) (advance int, token []byte, err error) {
	if atEOF && len(data) == 0 {
		return 0, nil, nil
	}
	i := bytes.IndexByte(data, '\n')
	j := bytes.IndexByte(data, ' ')
	ind := min(i, j)
	if ind < 0 {
		ind = max(i, j)
	}
	if ind >= 0 {
		return ind + 1, dropCR(data[0:ind]), nil
	}
	if atEOF {
		return len(data), dropCR(data), nil
	}
	return 0, nil, nil
}

func dropCR(data []byte) []byte {
	if len(data) > 0 && data[len(data)-1] == '\r' {
		return data[:len(data)-1]
	}
	return data
}

func getFloat() float64 {
	in.Scan()
	b := in.Bytes()
	var isNeg bool
	if b[0] == '-' {
		isNeg = true
		b = b[1:]
	}
	var res1 int64
	for i := 0; i < len(b); i++ {
		if b[i] == '.' {
			b = b[i+1:]
			break
		}
		res1 *= 10
		res1 += int64(b[i]) - 48
		if i == len(b) - 1 {
			b = b[:0]
		}
	}
	var res2 float64
	for i := len(b); i > 0; i-- {
		res2 += float64(b[i-1]) - 48
		res2 /= 10
	}
	if isNeg {
		res1 *= -1
		res2 *= -1
	}
	return float64(res1) + res2
}

func getInt() int {
	in.Scan()
	b := in.Bytes()
	var isNeg bool
	if b[0] == '-' {
		isNeg = true
		b = b[1:]
	}
	var res int
	for i := 0; i < len(b); i++ {
		res *= 10
		res += int(b[i]) - 48
	}
	if isNeg {
		res *= -1
	}
	return res
}

func getInt64() int64 {
	in.Scan()
	b := in.Bytes()
	var isNeg bool
	if b[0] == '-' {
		isNeg = true
		b = b[1:]
	}
	var res int64
	for i := 0; i < len(b); i++ {
		res *= 10
		res += int64(b[i]) - 48
	}
	if isNeg {
		res *= -1
	}
	return res
}

func getString() string {
	in.Scan()
	return in.Text()
}

func intToBytes(n int) []byte {
	var res [33]byte
	var isNeg bool
	if n < 0 {
		isNeg = true
		n = -n
	}
	var l int = 33
	for n > 0 {
		l--
		res[l] = byte(n % 10 + 48)
		n /= 10
	}
	if l == 33 {
		l--
		res[l] = byte(48)
	}
	if isNeg {
		l--
		res[l] = byte(45)
	}
	return res[l:]
}

func int64ToBytes(n int64) []byte {
	var res [65]byte
	var isNeg bool
	if n < 0 {
		isNeg = true
		n = -n
	}
	var l int = 65
	for n > 0 {
		l--
		res[l] = byte(n % 10 + 48)
		n /= 10
	}
	if l == 65 {
		l--
		res[l] = byte(48)
	}
	if isNeg {
		l--
		res[l] = byte(45)
	}
	return res[l:]
}

func bsLower(arr []int, n int) int {
	var minN, midN, maxN int
	maxN = len(arr)
	for maxN - 1 > minN {
		midN = (minN + maxN) / 2
		if arr[midN] < n {
			minN = midN
		} else {
			maxN = midN
		}
	}
	if arr[minN] >= n {
		return minN
	}
	return maxN
}

func bsLower64(arr []int64, n int64) int {
	var minN, midN, maxN int
	maxN = len(arr)
	for maxN - 1 > minN {
		midN = (minN + maxN) / 2
		if arr[midN] < n {
			minN = midN
		} else {
			maxN = midN
		}
	}
	if arr[minN] >= n {
		return minN
	}
	return maxN
}

func bsUpper(arr []int, n int) int {
	var minN, midN, maxN int
	maxN = len(arr)
	for maxN - 1 > minN {
		midN = (minN + maxN) / 2
		if arr[midN] <= n {
			minN = midN
		} else {
			maxN = midN
		}
	}
	if arr[minN] > n {
		return minN
	}
	return maxN
}

func bsUpper64(arr []int64, n int64) int {
	var minN, midN, maxN int
	maxN = len(arr)
	for maxN - 1 > minN {
		midN = (minN + maxN) / 2
		if arr[midN] <= n {
			minN = midN
		} else {
			maxN = midN
		}
	}
	if arr[minN] > n {
		return minN
	}
	return maxN
}

func max(n1, n2 int) int {
	if n1 > n2 {
		return n1
	}
	return n2
}

func max64(n1, n2 int64) int64 {
	if n1 > n2 {
		return n1
	}
	return n2
}

func min(n1, n2 int) int {
	if n1 < n2 {
		return n1
	}
	return n2
}

func min64(n1, n2 int64) int64 {
	if n1 < n2 {
		return n1
	}
	return n2
}

func yes(res, isUpperCase bool) bool {
	var arr [4]string = [4]string{"No", "Yes", "NO", "YES"}
	var ind int
	if res == true {
		ind = 1
	}
	if isUpperCase == true {
		ind += 2
	}
	out.WriteString(arr[ind])
	out.WriteByte(NL)
	return res
}

package main

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

// github.com/EndlessCheng/codeforces-go
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, m int
		Fscan(in, &n, &m)
		if m == 1 {
			Fprintln(out, "YES")
			for i := 1; i <= n; i++ {
				Fprintln(out, i)
			}
			return
		}
		if n&1 != 0 {
			Fprintln(out, "NO")
			return
		}
		Fprintln(out, "YES")
		ans := make([][]int, n)
		for i := range ans {
			ans[i] = make([]int, m)
		}
		k := 1
		for i := 0; i < n; i+=2 {
			for j := 0; j < m; j++ {
				ans[i][j] = k
				k++
				ans[i+1][j] = k
				k++
			}
		}

		for _, r := range ans {
			for _, v := range r {
				Fprint(out, v, " ")
			}
			Fprintln(out)
		}
	}

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

	leftData, _ := ioutil.ReadAll(in)
	if s := strings.TrimSpace(string(leftData)); s != "" {
		panic("有未读入的数据：\n" + s)
	}

}

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

// 00
package main

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

func write(f *bufio.Writer, a ...interface{}) {
	f.Write([]byte(fmt.Sprint(a...)))
}

type Number interface {
	int64 | float64 | int
}

func min[K Number](a K, b K) K {
	if a < b {
		return a
	}
	return b
}
func max[K Number](a K, b K) K {
	if a > b {
		return a
	}
	return b
}
func main() {
	var T int
	reader := bufio.NewReader(os.Stdin)
	f := bufio.NewWriter(os.Stdout)
	defer f.Flush()
	fmt.Fscan(reader, &T)
	for t := 1; t <= T; t++ {
		var n, k int
		fmt.Fscan(reader, &n, &k)
		if n%2 == 1 && k != 1 {
			write(f, "NO\n")
		} else {
			write(f, "YES\n")
			odd_start := 1
			even_start := 2
			for i := 0; i < n; i++ {
				for j := 0; j < k; j++ {
					if i%2 == 0 {
						write(f, odd_start)
						odd_start += 2
					} else {
						write(f, even_start)
						even_start += 2
					}
					if j == k-1 {
						write(f, "\n")
					} else {
						write(f, " ")
					}
				}
			}
		}
	}
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	var tests int
	fmt.Fscan(in, &tests)
	for t := 0; t < tests; t++ {
		var n, k int
		fmt.Fscan(in, &n, &k)
		if k == 1 {
			fmt.Println("YES")
			for i := 1; i <= n; i++ {
				fmt.Println(i)
			}
			continue
		}
		if n%2 == 1 {
			fmt.Println("NO")
			continue
		}
		fmt.Println("YES")
		for i := 1; i <= n; i++ {
			for j := i; j <= n*k; j += n {
				fmt.Print(j, " ")
			}
			fmt.Println()
		}
	}
}

package main

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

const MOD int64 = 1000000007

var n, k int

// ByAge implements sort.Interface based on the Age field.
type ByV []*pair

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

func main() {

	read := bufio.NewReader(os.Stdin)

	var t int
	fmt.Fscan(read, &t)

	for i := 0; i < t; i++ {

		fmt.Fscan(read, &n, &k)
		solve()
	}
}

type pair struct {
	v int
	idx []int
}

func solve()  {
	if n % 2 == 1 {

		if k > 1 {
			fmt.Println("NO")
			return
		}
		fmt.Println("YES")

		var cur int = 1

		for i := 0; i < n; i++ {
			for j := 0; j < 1; j++ {
				fmt.Println(cur)
			}
			cur++
		}
		return

	} else {
		fmt.Println("YES")

		odd := 1
		ev := 2

		for i := 0; i < n/2; i++ {
			for j := 0; j < k; j++ {
				fmt.Print(odd, " ")
				odd += 2
			}
			fmt.Println()
		}
		for i := 0; i < n/2; i++ {
			for j := 0; j < k; j++ {
				fmt.Print(ev, " ")
				ev += 2
			}
			fmt.Println()
		}

		return
	}
}

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

package main

import (
	"bufio"
	"crypto/rand"
	"fmt"
	"math"
	"math/big"
	"os"
	"sort"
	"strconv"
)

func main() {

	reader := bufio.NewReader(os.Stdin)
	writter := bufio.NewWriter(os.Stdout)
	defer writter.Flush()

	t := 0
	fmt.Fscan(reader, &t)

	for t > 0 {
		n, k := 0, 0
		fmt.Fscan(reader, &n, &k)

		result, array := solve(n, k)

		fmt.Fprintln(writter, result)
		for i := range array {
			for j := range array[i] {
				fmt.Fprintf(writter, "%d ", array[i][j])
			}
			fmt.Fprintln(writter)
		}

		t--
	}

	// n, k := 4, 3
	// result, array := solve(n, k)
	// fmt.Println(result)
	// printMatrix(array)

	//getPower()

}

func solve(n, k int) (result string, array [][]int) {

	array = make([][]int, n)
	for i := range array {
		array[i] = make([]int, k)
	}

	if k == 1 {
		result = "YES"

		now := 1
		for i := range array {
			for j := range array[i] {
				array[i][j] = now
				now++
			}
		}
	} else {
		if n%2 == 0 {
			result = "YES"
			one := 1
			now := one
			for i := range array {
				for j := range array[i] {
					array[i][j] = now
					now += 2
				}

				if i%2 == 0 {
					now = one + 1
				} else {
					one = now - 1
					now = one
				}
			}
		} else {
			result = "NO"
			array = [][]int{}
		}
	}

	return
}

func palindrome(str string) bool {

	size := len(str)

	for i := 0; i < size/2; i++ {
		if str[i] != str[size-1-i] {
			return false
		}
	}

	return true

}

func reverseString(str string) string {
	result := ""

	for i := len(str) - 1; i >= 0; i-- {

		result += string(str[i])

	}

	return result
}

func arraySum(array []int) int {
	sum := 0

	for i := range array {
		sum += array[i]
	}

	return sum
}

func numSum(num int) int {
	sum := 0

	for i := num; i > 0; i-- {
		sum += i
	}

	return sum
}

func strToint(str string) int {
	num, _ := strconv.Atoi(str)
	return num
}

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

	return b
}

func getFactor(num int) []int {
	result := []int{}

	for i := 1; i <= int(math.Sqrt(float64(num))); i++ {
		if num%i == 0 {
			result = append(result, i)
			result = append(result, num/i)
		}
	}

	return result
}

func gcd(a, b int) int {
	if a == 0 {
		return b
	}

	return gcd(b%a, a)
}

func printMatrix(matrix [][]int) {
	for i := range matrix {

		for j := range matrix[i] {
			fmt.Printf("%d ", matrix[i][j])
		}

		fmt.Println()

	}
}

func checkPrime(num int) bool {

	if num < 2 {
		return false
	}

	for i := 2; i <= int(math.Sqrt(float64(num))); i++ {
		if num%i == 0 {
			return false
		}
	}

	return true
}

func getPower() {
	A := []int{}

	for i := 1; i <= 38; i++ {
		A = append(A, i)
	}

	for j := 0; j < 10000; j++ {
		for i := range A {
			target, _ := rand.Int(rand.Reader, big.NewInt(38))
			A[i], A[target.Int64()] = A[target.Int64()], A[i]
		}
	}

	B, _ := rand.Int(rand.Reader, big.NewInt(8))

	result := A[:6]
	sort.Ints(result)

	fmt.Println(result)
	fmt.Println(int(B.Int64()) + 1)
}

func xor(a, b int) int {
	aBit := strconv.FormatInt(int64(a), 2)
	bBit := strconv.FormatInt(int64(b), 2)

	size := max(len(aBit), len(bBit))

	for i := size - len(aBit); i > 0; i-- {
		aBit = "0" + aBit
	}
	for i := size - len(bBit); i > 0; i-- {
		bBit = "0" + bBit
	}

	result := ""
	for i := range aBit {
		if aBit[i] == bBit[i] {
			result += "0"
		} else {
			result += "1"
		}
	}

	//fmt.Println(result)
	num, _ := strconv.ParseInt(result, 2, 64)

	return int(num)
}

package main

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

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

func flush()                            { out.Flush() }
func scan(a ...interface{})             { fmt.Fscan(in, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(out, f, a...) }
func assert(f bool) {
	if !f {
		panic("gg")
	}
}

// ----------------------------- TEMPLATE END ----------------------------------

const TAM = 501

var ans [TAM][TAM]int

func main() {
	defer flush()

	var ntc int
	scan(&ntc)
	for t := 0; t < ntc; t++ {
		var n, k int
		scan(&n, &k)
		if n*k == 1 {
			printf("YES\n1\n")
		} else {
			var even = 2
			var odd = 1
			for i := 0; i < n; i++ {
				if even <= n*k {
					for j := 0; j < k; j++ {
						ans[i][j] = even
						even += 2
					}
				} else {
					for j := 0; j < k; j++ {
						ans[i][j] = odd
						odd += 2
					}
				}
			}
			if even-2 > n*k || odd-2 > n*k {
				printf("NO\n")
			} else {
				printf("YES\n")
				for i := 0; i < n; i++ {
					for j := 0; j < k; j++ {
						printf("%d ", ans[i][j])
					}
					printf("\n")
				}
			}
		}
	}
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in, &t)

	for ; t > 0; t-- {
		var n, k int
		fmt.Fscan(in, &n, &k)

		if n == 1 && k == 1 {
			fmt.Fprintln(out, "YES")
			fmt.Fprintln(out, "1")
		} else {
			if n%2 != 0 && k != 1 {
				fmt.Fprintln(out, "NO")
			} else if k == 1 {
				fmt.Fprintln(out, "YES")
				for i, x := 0, 1; i < n; i++ {
					fmt.Fprintln(out, x)
					x++
				}
			} else {
				fmt.Fprintln(out, "YES")
				for i, x := 0, 1; i < n/2; i++ {
					for j := 0; j < k; j++ {
						if j > 0 {
							fmt.Fprint(out, " ")
						}
						fmt.Fprint(out, x)
						x += 2
					}
					fmt.Fprintln(out)
				}
				for i, x := 0, 2; i < n/2; i++ {
					for j := 0; j < k; j++ {
						if j > 0 {
							fmt.Fprint(out, " ")
						}
						fmt.Fprint(out, x)
						x += 2
					}
					fmt.Fprintln(out)
				}
			}
		}

	}
}

package main

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

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

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

		isValid := true
		o := 1
		e := 2
		m := make([][]int, n)
		for i := 0; i < n; i++ {
			row := make([]int, k)
			if i%2 == 0 {
				row[0] = o
				o += k * 2
			} else {
				row[0] = e
				e += k * 2
			}

			for j := 1; j < k; j++ {
				row[j] = row[j-1] + 2
			}

			if row[k-1] > n*k {
				isValid = false
				break
			}

			m[i] = row
		}

		if !isValid {
			fmt.Fprintln(out, "NO")
		} else {
			fmt.Fprintln(out, "YES")
			for _, row := range m {
				for _, v := range row {
					fmt.Fprintf(out, "%d ", v)
				}
				fmt.Fprintln(out)
			}
		}
	}
}

/*
https://codeforces.com/contest/1634/problem/C

*/

package main

import (
	"bytes"
	"bufio"
	"fmt"
	//"math"
	"os"
	"strconv"
	"strings"
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	line, _ := reader.ReadString('\n')
	line_arr := strings.Fields(line)
	T, _ := strconv.Atoi(line_arr[0])
	for t := 0; t<T; t++ {
		line, _ = reader.ReadString('\n')
		line_arr = strings.Fields(line)
		N, _ := strconv.Atoi(line_arr[0])
		K, _ := strconv.Atoi(line_arr[1])
		//fmt.Println(N, K)

		var buffer bytes.Buffer
		if K == 1{
			fmt.Println("YES")
			for i:=0; i<N; i++ {
				buffer.WriteString(strconv.Itoa(i+1))
			}
			fmt.Println(buffer.String())
		} else if N%2 == 0 {
			fmt.Println("YES")
			kmul := 0
			for i:=0; i<N; i++ {
				var offset int
				if i%2 == 0{
					kmul = i*K
					offset = kmul + 1
				} else {
					offset = kmul + 2
				}
				arr := make([]int, K)
				for j:=0; j<K; j++ {
					arr[j] = offset + j*2
					if j > 0 {
						buffer.WriteString(" ")
					}
					buffer.WriteString(strconv.Itoa(arr[j]))
				}
				buffer.WriteString("\n")
			}
			fmt.Println(buffer.String())
		} else {
			fmt.Println("NO")
		}
	}

}

/*
https://codeforces.com/contest/1634/problem/C

*/

package main

import (
	//"bytes"
	"bufio"
	"fmt"
	//"math"
	"os"
	"strconv"
	"strings"
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	line, _ := reader.ReadString('\n')
	line_arr := strings.Fields(line)
	T, _ := strconv.Atoi(line_arr[0])
	for t := 0; t<T; t++ {
		line, _ = reader.ReadString('\n')
		line_arr = strings.Fields(line)
		N, _ := strconv.Atoi(line_arr[0])
		K, _ := strconv.Atoi(line_arr[1])
		//fmt.Println(N, K)

		if K == 1{
			fmt.Println("YES")
			for i:=0; i<N; i++ {
				fmt.Println(i+1)
			}
			continue
		} else if N%2 == 0 {
			fmt.Println("YES")
			kmul := 0
			for i:=0; i<N; i++ {
				var offset int
				if i%2 == 0{
					kmul = i*K
					offset = kmul + 1
				} else {
					offset = kmul + 2
				}
				arr := make([]int, K)
				for j:=0; j<K; j++ {
					arr[j] = offset + j*2
				}
				fmt.Println(arr)
			}
		} else {
			fmt.Println("NO")
		}
	}

}

package main

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

/*
7
1 7
5 1
4 2
3 3
4 3
3 4
5 6
---
NO
YES
YES
NO
YES
*/

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

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

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

		nk := n * k

		if (nk%2 != 0 && k > 1) || k > 4 {
			fmt.Fprintln(writer, "NO")
			continue
		}

		oddNum := 1
		evnNum := 2

		fmt.Fprintln(writer, "YES")
		for i := 0; i < n; i++ {
			if i%2 == 0 {
				for j := 0; j < k; j++ {
					fmt.Fprintf(writer, "%d ", oddNum)
					oddNum += 2
				}
			} else {
				for j := 0; j < k; j++ {
					fmt.Fprintf(writer, "%d ", evnNum)
					evnNum += 2
				}
			}
			fmt.Fprintln(writer)
		}
	}
}

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 divCeil(num, div int64) int64 {
	quot := num / div
	if num%div != 0 {
		quot++
	}
	return quot
}

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

//goland:noinspection GoUnusedFunction
func max(nums ...int) int {
	maximum := nums[0]
	for _, num := range nums {
		if num > maximum {
			maximum = num
		}
	}
	return maximum
}

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

/*
6
1 7
5 1
4 2
3 3
4 3
3 4
---
NO
YES
YES
NO
YES
*/

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

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

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

		nk := n * k

		if nk%2 != 0 && k > 1 {
			fmt.Fprintln(writer, "NO")
			continue
		}

		oddNum := 1
		evnNum := 2

		fmt.Fprintln(writer, "YES")
		for i := 0; i < n; i++ {
			if i%2 == 0 {
				for j := 0; j < k; j++ {
					fmt.Fprintf(writer, "%d ", oddNum)
					oddNum += 2
				}
			} else {
				for j := 0; j < k; j++ {
					fmt.Fprintf(writer, "%d ", evnNum)
					evnNum += 2
				}
			}
			fmt.Fprintln(writer)
		}
	}
}

func helper(i, d, y int64, a []int64, cache map[int64]bool) bool {
	if i == int64(len(a)) {
		return false
	}

	sum := d + a[i]
	xor := d ^ a[i]

	fmt.Fprintf(writer, "i=%d: %d %d\n", i, sum, xor)

	if i == int64(len(a))-1 && (sum == y || xor == y) {
		return true
	}

	res := false

	if !cache[sum] {
		//cache[sum] = true
		res = helper(i+1, sum, y, a, cache)
	}

	if !res && !cache[xor] {
		//cache[xor] = true
		res = helper(i+1, xor, y, a, cache)
	}

	return res
}

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 divCeil(num, div int64) int64 {
	quot := num / div
	if num%div != 0 {
		quot++
	}
	return quot
}

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

//goland:noinspection GoUnusedFunction
func max(nums ...int) int {
	maximum := nums[0]
	for _, num := range nums {
		if num > maximum {
			maximum = num
		}
	}
	return maximum
}

//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"
	"bytes"
	"fmt"
	"os"
)

const (
	MOD int64 = 1e9+7
	NL byte = byte('\n')
	SP byte = byte(' ')
)

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

	n, k int
	res bool
)

func main() {
	initBuffer()
	var t int = 1
	t = getInt()
	for i := 0; i < t; i++ {
		solve()
	}
	// test()
	out.Flush()
}

func solve() {
	initialize()
	input()
	count()
	output()
}

func initialize() {

}

func input() {
	n = getInt()
	k = getInt()
}

func count() {
	res = k == 1 || n % 2 == 0
}

func output() {
	if yes(res, true) {
		for i := 0; i < n; i++ {
			for j := 0; j < k; j++ {
				out.Write(intToBytes(i * k + j + 1))
				out.WriteByte(SP)
			}
			out.WriteByte(NL)
		}
	}
}

func test() {
	fmt.Print()
}

func initBuffer() {
	in = bufio.NewScanner(os.Stdin)
	in.Buffer([]byte{}, 4 * 1024 * 1024)
	in.Split(scanWords)
	out = bufio.NewWriter(os.Stdout)
}

func scanWords(data []byte, atEOF bool) (advance int, token []byte, err error) {
	if atEOF && len(data) == 0 {
		return 0, nil, nil
	}
	i := bytes.IndexByte(data, '\n')
	j := bytes.IndexByte(data, ' ')
	ind := min(i, j)
	if ind < 0 {
		ind = max(i, j)
	}
	if ind >= 0 {
		return ind + 1, dropCR(data[0:ind]), nil
	}
	if atEOF {
		return len(data), dropCR(data), nil
	}
	return 0, nil, nil
}

func dropCR(data []byte) []byte {
	if len(data) > 0 && data[len(data)-1] == '\r' {
		return data[:len(data)-1]
	}
	return data
}

func getFloat() float64 {
	in.Scan()
	b := in.Bytes()
	var isNeg bool
	if b[0] == '-' {
		isNeg = true
		b = b[1:]
	}
	var res1 int64
	for i := 0; i < len(b); i++ {
		if b[i] == '.' {
			b = b[i+1:]
			break
		}
		res1 *= 10
		res1 += int64(b[i]) - 48
		if i == len(b) - 1 {
			b = b[:0]
		}
	}
	var res2 float64
	for i := len(b); i > 0; i-- {
		res2 += float64(b[i-1]) - 48
		res2 /= 10
	}
	if isNeg {
		res1 *= -1
		res2 *= -1
	}
	return float64(res1) + res2
}

func getInt() int {
	in.Scan()
	b := in.Bytes()
	var isNeg bool
	if b[0] == '-' {
		isNeg = true
		b = b[1:]
	}
	var res int
	for i := 0; i < len(b); i++ {
		res *= 10
		res += int(b[i]) - 48
	}
	if isNeg {
		res *= -1
	}
	return res
}

func getInt64() int64 {
	in.Scan()
	b := in.Bytes()
	var isNeg bool
	if b[0] == '-' {
		isNeg = true
		b = b[1:]
	}
	var res int64
	for i := 0; i < len(b); i++ {
		res *= 10
		res += int64(b[i]) - 48
	}
	if isNeg {
		res *= -1
	}
	return res
}

func getString() string {
	in.Scan()
	return in.Text()
}

func intToBytes(n int) []byte {
	var res [33]byte
	var isNeg bool
	if n < 0 {
		isNeg = true
		n = -n
	}
	var l int = 33
	for n > 0 {
		l--
		res[l] = byte(n % 10 + 48)
		n /= 10
	}
	if l == 33 {
		l--
		res[l] = byte(48)
	}
	if isNeg {
		l--
		res[l] = byte(45)
	}
	return res[l:]
}

func int64ToBytes(n int64) []byte {
	var res [65]byte
	var isNeg bool
	if n < 0 {
		isNeg = true
		n = -n
	}
	var l int = 65
	for n > 0 {
		l--
		res[l] = byte(n % 10 + 48)
		n /= 10
	}
	if l == 65 {
		l--
		res[l] = byte(48)
	}
	if isNeg {
		l--
		res[l] = byte(45)
	}
	return res[l:]
}

func bsLower(arr []int, n int) int {
	var minN, midN, maxN int
	maxN = len(arr)
	for maxN - 1 > minN {
		midN = (minN + maxN) / 2
		if arr[midN] < n {
			minN = midN
		} else {
			maxN = midN
		}
	}
	if arr[minN] >= n {
		return minN
	}
	return maxN
}

func bsLower64(arr []int64, n int64) int {
	var minN, midN, maxN int
	maxN = len(arr)
	for maxN - 1 > minN {
		midN = (minN + maxN) / 2
		if arr[midN] < n {
			minN = midN
		} else {
			maxN = midN
		}
	}
	if arr[minN] >= n {
		return minN
	}
	return maxN
}

func bsUpper(arr []int, n int) int {
	var minN, midN, maxN int
	maxN = len(arr)
	for maxN - 1 > minN {
		midN = (minN + maxN) / 2
		if arr[midN] <= n {
			minN = midN
		} else {
			maxN = midN
		}
	}
	if arr[minN] > n {
		return minN
	}
	return maxN
}

func bsUpper64(arr []int64, n int64) int {
	var minN, midN, maxN int
	maxN = len(arr)
	for maxN - 1 > minN {
		midN = (minN + maxN) / 2
		if arr[midN] <= n {
			minN = midN
		} else {
			maxN = midN
		}
	}
	if arr[minN] > n {
		return minN
	}
	return maxN
}

func max(n1, n2 int) int {
	if n1 > n2 {
		return n1
	}
	return n2
}

func max64(n1, n2 int64) int64 {
	if n1 > n2 {
		return n1
	}
	return n2
}

func min(n1, n2 int) int {
	if n1 < n2 {
		return n1
	}
	return n2
}

func min64(n1, n2 int64) int64 {
	if n1 < n2 {
		return n1
	}
	return n2
}

func yes(res, isUpperCase bool) bool {
	var arr [4]string = [4]string{"No", "Yes", "NO", "YES"}
	var ind int
	if res == true {
		ind = 1
	}
	if isUpperCase == true {
		ind += 2
	}
	out.WriteString(arr[ind])
	out.WriteByte(NL)
	return res
}

package main

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

// github.com/EndlessCheng/codeforces-go
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 ,m int
		Fscan(in, &n,&m)
		if m == 1 {
			Fprintln(out, "YES")
			for i := 1; i <= n; i++ {
				Fprintln(out, i)
			}
			return
		}
		if n != 2 {
			Fprintln(out, "NO")
			return
		}
		Fprintln(out, "YES")
		for i := 1; i <= m*2; i+=2 {
			Fprint(out, i, " ")
		}
		Fprintln(out)
		for i := 2; i <= m*2; i+=2 {
			Fprint(out, i, " ")
		}
		Fprintln(out)
	}

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

	leftData, _ := ioutil.ReadAll(in)
	if s := strings.TrimSpace(string(leftData)); s != "" {
		panic("有未读入的数据：\n" + s)
	}

}

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


package main

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

func main() {
	noOfTopics := scanInt64()
	teacherArray := scanInt64Slice(int(noOfTopics))
	studentArray := scanInt64Slice(int(noOfTopics))

	for i := int64(0); i < noOfTopics; i++ {
		teacherArray[i] -= studentArray[i]
	}

	sortInt64Slice(teacherArray)
	left, right, count := int64(0), noOfTopics-1, int64(0)
	for left < right{
		if teacherArray[left] + teacherArray[right] > 0{
			count += right - left
			right--
		}else{
			left++
		}
	}

	printFmt(count)

	close(channelType)
	oWg.Wait()
}

func getMax(n, m int) string {
	var max string
	for m > 9 {
		max = max + "9"
		m -= 9
		n--
	}

	max = max + strconv.Itoa(m)
	n--
	for i := 0; i < n; i++ {
		max = max + "0"
	}
	return max
}

func getMin(n, m int) string {
	var min string
	for m > 9 {
		min = "9" + min
		m -= 9
		n--
	}

	if n == 1 {
		min = strconv.Itoa(m) + min
	} else {
		if m > 1 {
			min = strconv.Itoa(m-1) + min
			n--
		}

		for i := 0; i < n-1; i++ {
			min = "0" + min
		}
		min = "1" + min
	}
	return min
}

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

var channelType chan string
var in *bufio.Scanner
var oWg *sync.WaitGroup
var printFmt func(a ...interface{}) (n int, err error)

func init() {
	printFmt = fmt.Println

	in = bufio.NewScanner(os.Stdin)
	in.Buffer(make([]byte, 1024), int(1e+9))
	in.Split(bufio.ScanWords)

	channelType = make(chan string, 16)
	oWg = &sync.WaitGroup{}
	oWg.Add(1)

	writer := bufio.NewWriter(os.Stdout)
	go func() {
		defer oWg.Done()
		defer writer.Flush()

		for line := range channelType {
			writer.WriteString(line + "\n")
		}
	}()
}

func scanString() string { in.Scan(); return in.Text() }
func scanInt() int       { i, _ := strconv.Atoi(scanString()); return i }
func scanInt64() int64   { i, _ := strconv.ParseInt(scanString(), 10, 64); return i }
func scanStringSlice(n int) []string {
	s := make([]string, n);
	for i := 0; i < n; i++ {
		s[i] = scanString()
	};
	return s
}
func scanIntSlice(n int) []int {
	arr := make([]int, n);
	for i := 0; i < n; i++ {
		arr[i] = scanInt()
	};
	return arr
}
func scanInt64Slice(n int) []int64 {
	arr := make([]int64, n);
	for i := 0; i < n; i++ {
		arr[i] = scanInt64()
	};
	return arr
}

/*********************** Utils ***********************/

func stringEquivalent(v interface{}) string { return fmt.Sprintf("%v", v) }
func spaceSeperatedStringFromArray(a []int) string {
	sb := strings.Builder{};
	sb.Grow(len(a) * 9);
	for i := range a {
		sb.WriteString(strconv.Itoa(a[i]) + " ")
	};
	return sb.String()
}
func spaceSeperatedStringFromArray64(a []int64) string {
	sb := strings.Builder{};
	sb.Grow(len(a) * 9);
	for i := range a {
		sb.WriteString(strconv.FormatInt(a[i], 10) + " ")
	};
	return sb.String()
}
func minMaxFromArray(args ...int64) (int64, int64) {
	minVal, maxVal := int64(math.MaxInt64), int64(math.MinInt64);
	for _, v := range args {
		if v < minVal {
			minVal = v
		};
		if v > maxVal {
			maxVal = v
		}
	};
	return minVal, maxVal
}
func minElementFromArray(args ...int64) int64 { mn, _ := minMaxFromArray(args...); return mn }
func maxElementFromArray(args ...int64) int64 { _, mx := minMaxFromArray(args...); return mx }
func lcmCalculation(a, b int64) int64         { return a / gcdCalculation(a, b) * b }
func gcdCalculation(a, b int64) int64 {
	if (a == 0) {
		return b
	};
	return gcdCalculation(b%a, a)
}
func sortInt64Slice(a []int64) { sort.Slice(a, func(i, j int) bool { return a[i] < a[j] }) }
func absoluteValue(x int64) int64 {
	if x < 0 {
		return -x
	};
	return x
}
func min(args ...int) int { mn, _ := minMaxFinder(args...); return mn }
func max(args ...int) int { _, mx := minMaxFinder(args...); return mx }
func minMaxFinder(args ...int) (int, int) {
	minVal, maxVal := int(math.MaxInt32), int(math.MinInt32);
	for _, v := range args {
		if v < minVal {
			minVal = v
		};
		if v > maxVal {
			maxVal = v
		}
	};
	return minVal, maxVal
}

package main

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

func main() {
	r := bufio.NewReader(os.Stdin)
	var n int
	fmt.Fscan(r, &n)
	topics := make([][]int, n)
	for i := 0; i < n; i++ {
		topics[i] = make([]int, 2)
	}
	for i := 0; i < n; i++ {
		fmt.Fscan(r, &topics[i][0])
	}
	for i := 0; i < n; i++ {
		fmt.Fscan(r, &topics[i][1])
	}
	rls := solve(topics)
	fmt.Println(rls)
}

func solve(topics [][]int) int64 {
	n := len(topics)
	sort.Slice(topics, func(i, j int) bool {
		v1 := topics[i][0] - topics[i][1]
		v2 := topics[j][0] - topics[j][1]
		return v1 < v2
	})
	rls := int64(0)
	for i := n - 1; i > 0; i-- {
		idx := sort.Search(i, func(j int) bool {
			return topics[j][0]-topics[j][1] > topics[i][1]-topics[i][0]
		})
		rls += int64(i - idx)
	}
	return rls
}

package main

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

func morethan(a []int64, v int64) int64 {
	n := int64(len(a))

	if v < a[0] {
		return 0
	}

	if v >= a[n-1] {
		return n
	}

	s := int64(0)
	e := n-1

	for s < e {
		m := (s+e)/2

		if a[m] > v {
			e = m
		} else {
			s = m+1
		}
	}

	return s
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	bf := make([]byte, 1024*1024)
	sc.Buffer(bf, 10*1024*1024)
	sc.Split(bufio.ScanWords)

	in := func() int64 {
		sc.Scan()
		i, _ := strconv.ParseInt(sc.Text(), 0, 64)
		return i
	}

	n := in()
	a := make([]int64, n)
	b := make([]int64, n)

	for i := range a {
		a[i] = in()
	}

	for i := range b {
		b[i] = in()
	}

	for i := range a {
		a[i] = a[i] - b[i]
	}

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

	var sum int64 = 0
	var i int64
	for i = 0; i < n-1; i++ {
		sum += n-(i+1) - morethan(a[i+1:], -a[i])
	}

	fmt.Println(sum)
}

package main

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

func main() {
	var n int64
	fmt.Scanf("%d\n", &n)
	reader := bufio.NewReader(os.Stdin)
	line := readLine(reader)
	ans := strings.Split(string(line), " ")
	line = readLine(reader)
	bns := strings.Split(string(line), " ")
	cn := make([]int64, n)
	for i := int64(0); i < n; i++ {
		a, _ := strconv.ParseInt(ans[i], 10, 64)
		b, _ := strconv.ParseInt(bns[i], 10, 64)
		cn[i] = a - b
	}
	sort.Slice(cn, func(i, j int) bool {
		return cn[i] < cn[j]
	})
	result := int64(0)
	for i:=n-1;i>=0;i-- {
		if cn[i] <=0 {
			break
		}
		k := search(-cn[i], cn, 0, i-1)
		if cn[k] > -cn[i] {
			result += i - k
		}
	}
	fmt.Println(result)
}

func search(k int64, arr []int64, start, end int64) int64 {
	if start >= end {
		return start
	}
	loc := (start + end) / 2
	if arr[loc] > k {
		return search(k, arr, start, loc)
	} else {
		return search(k, arr, loc+1, end)
	}
}

func readLine(reader *bufio.Reader) []byte {
	result := make([]byte, 0)
	for {
		s, isPrefix, err := reader.ReadLine()
		if err != nil {
			fmt.Printf("error=%+v\n", err)
			break
		}
		result = append(result, s...)
		if !isPrefix {
			break
		}
	}
	return result
}

/*
URL:
https://codeforces.com/problemset/problem/1324/D
*/

package main

import (
	"bufio"
	"errors"
	"fmt"
	"io"
	"math"
	"os"
	"sort"
	"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    int64
	A, B []int64
)

func main() {
	n = ReadInt64()
	A, B = ReadInt64Slice(int(n)), ReadInt64Slice(int(n))

	C := make([]int64, n)
	for i := int64(0); i < n; i++ {
		C[i] = -A[i] + B[i]
	}
	sort.Slice(C, func(i, j int) bool {
		return C[i] < C[j]
	})

	ans := int64(0)
	for i := int64(0); i < n; i++ {
		key := A[i] - B[i]
		ok := BinarySearch(-1, n, func(mid int64) bool {
			return key > C[mid]
		})
		ok++

		if key > -key {
			ok--
		}

		ans += ok
	}

	ans /= 2
	fmt.Println(ans)
}

func BinarySearch(initOK, initNG int64, isOK func(mid int64) bool) (ok int64) {
	ng := initNG
	ok = initOK
	for int(math.Abs(float64(ok-ng))) > 1 {
		mid := (ok + ng) / 2
		if isOK(mid) {
			ok = mid
		} else {
			ng = mid
		}
	}

	return ok
}

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

/*********** 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 (
	"bufio"
	. "fmt"
	"io"
	"os"
	"sort"
)

// github.com/EndlessCheng/codeforces-go
func CF1324D(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	var n, v int
	Fscan(in, &n)
	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	for i := range a {
		Fscan(in, &v)
		a[i] -= v
	}
	sort.Ints(a)
	ans := int64(0)
	for i, v := range a {
		if j := sort.SearchInts(a[:i], 1-v); j < i {
			ans += int64(i - j)
		}
	}
	Fprint(_w, ans)
}

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

package main

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

func morethan(a []int, v int) int {
	n := len(a)

	if v < a[0] {
		return 0
	}

	if v >= a[n-1] {
		return n
	}

	s := 0
	e := n-1

	for s < e {
		m := (s+e)/2

		if a[m] > v {
			e = m
		} else {
			s = m+1
		}
	}

	return s
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	bf := make([]byte, 1024*1024)
	sc.Buffer(bf, 10*1024*1024)
	sc.Split(bufio.ScanWords)

	in := func() int {
		sc.Scan()
		i, _ := strconv.Atoi(sc.Text())
		return i
	}

	n := in()
	a := make([]int, n)
	b := make([]int, n)

	for i := range a {
		a[i] = in()
	}

	for i := range b {
		b[i] = in()
	}

	for i := range a {
		a[i] = a[i] - b[i]
	}

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

	sum := 0
	for i := 0; i < n-1; i++ {
		sum += n-(i+1) - morethan(a[i+1:], -a[i])
	}

	fmt.Println(sum)
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1324D(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	var n, v, ans int
	Fscan(in, &n)
	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	for i := range a {
		Fscan(in, &v)
		a[i] -= v
	}
	sort.Ints(a)
	for i, v := range a {
		if j := sort.SearchInts(a[:i], 1-v); j < i {
			ans += i - j
		}
	}
	Fprint(_w, ans)
}

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

package main

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

func main() {
	r := bufio.NewReader(os.Stdin)
	var n int
	fmt.Fscan(r, &n)
	topics := make([][]int, n)
	for i := 0; i < n; i++ {
		topics[i] = make([]int, 2)
	}
	for i := 0; i < n; i++ {
		fmt.Fscan(r, &topics[i][0])
	}
	for i := 0; i < n; i++ {
		fmt.Fscan(r, &topics[i][1])
	}
	rls := solve(topics)
	fmt.Println(rls)
}

func solve(topics [][]int) int {
	n := len(topics)
	sort.Slice(topics, func(i, j int) bool {
		v1 := topics[i][0] - topics[i][1]
		v2 := topics[j][0] - topics[j][1]
		return v1 < v2
	})
	rls := 0
	for i := n - 1; i > 0; i-- {
		idx := sort.Search(i, func(j int) bool {
			return topics[j][0]-topics[j][1] > topics[i][1]-topics[i][0]
		})
		rls += i - idx
	}
	return rls
}


package main

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

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

	var n int
	var w int

	fmt.Fscan(in, &n, &w)

	m, M := 0, 0
	s := 0
	for i := 0; i < n; i++ {
		var x int
		fmt.Fscan(in, &x)

		s += x
		if s < m {
			m = s
		}
		if s > M {
			M = s
		}
	}

	res := w - M - (-m) + 1
	if res < 0 {
		res = 0
	}
	fmt.Fprintln(out, res)
}

package main

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

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

func ReadInt64() int64 {
    scanner.Scan()
    ans, _ := strconv.ParseInt(scanner.Text(),10,64)
    return ans
}

func ReadInt32() int {
    scanner.Scan()
    ans, _ := strconv.Atoi(scanner.Text())
    return ans
}

func PrintInts(ints ...int64) {
    for _, value := range ints {
        writer.WriteString(strconv.FormatInt(value, 10))
        writer.WriteByte(' ')
    }
}

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

	n, w := ReadInt32(), ReadInt32()

	i := 0
	j := w

	menor := 0
	maior := 0

	offset := 0

	for ; n > 0; n-- {
		val := ReadInt32()

		offset += val

		if offset > maior {
			j -= (offset - maior)
			maior = offset
		}
		if offset < menor {
			i += (menor - offset)
			menor = offset
		}
	}

	if i > j {
		writer.WriteString(strconv.Itoa(0))
	} else {
		writer.WriteString(strconv.Itoa(j - i + 1))
	}

}

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer(make([]byte, 2048*2048), 2048*2048)
	scanner.Split(bufio.ScanWords)

	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())

	scanner.Scan()
	w, _ := strconv.Atoi(scanner.Text())

	a := make([]int, n)
	for i := 0; i < n; i++ {
		scanner.Scan()
		a[i], _ = strconv.Atoi(scanner.Text())
	}

	pred_high := func(val int) bool {
		for i := 0; i < n; i++ {
			val += a[i]
			if val > w {
				return true
			}
		}
		return false
	}

	pred_low := func(val int) bool {
		for i := 0; i < n; i++ {
			val += a[i]
			if val < 0 {
				return false
			}
		}
		return true
	}

	l := 0
	r := w
	for l < r {
		m := l + (r-l)/2
		if pred_low(m) {
			r = m
		} else {
			l = m + 1
		}
	}

	ll := 0
	rr := w
	for ll < rr {
		m := ll + (rr-ll+1)/2
		if pred_high(m) {
			rr = m - 1
		} else {
			ll = m
		}
	}

	if !pred_low(l) {
		fmt.Println(0)
		return
	}
	if pred_high(ll) {
		fmt.Println(0)
		return
	}
	if l > ll {
		fmt.Println(0)
		return
	}

	fmt.Println(ll - l + 1)
}

package main

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

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

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

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

	var n int
	var w int64
	fmt.Fscan(in, &n, &w)
	var t, tmp int64
	fmt.Fscan(in, &tmp)
	mn, mx := tmp, tmp
	for i := 1; i < n; i++ {
		fmt.Fscan(in, &t)
		tmp += t
		mx = max(mx, tmp)
		mn = min(mn, tmp)
	}
	l, r := int64(0), w
	if mn < 0 {
		l =  -mn
	} else {
		r = w - mn
	}
	if mx > 0 {
		r = min(r, w-mx)
	} else {
		l = max(l, -mx)
	}
	fmt.Fprintln(out, max(r-l+1, 0))
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF978E(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var n, w, min, max, s, v, ans int
	Fscan(in, &n, &w)
	for ; n > 0; n-- {
		Fscan(in, &v)
		s += v
		if s < min {
			min = s
		} else if s > max {
			max = s
		}
	}
	if max-min <= w {
		ans = w - max + min + 1
	}
	Fprint(out, ans)
}

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

package main

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

const (
	MaxN = 1024
)

var a, pre [MaxN]int
var n, w int

func main() {
	buf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(buf, "%d %d\n", &n, &w)
	for i := 0; i < n; i++ {
		fmt.Fscanf(buf, "%d", &a[i])
	}
	pre[0] = a[0]
	for i := 1; i < n; i++ {
		pre[i] = pre[i - 1] + a[i]
	}
	l, r := 0, 0
	for i := 0; i < n; i++ {
		if pre[i] < 0 && l > pre[i] {
			l = pre[i]
		}
		if pre[i] > 0 && r < pre[i] {
			r = pre[i]
		}
	}
	l = -l
	if l > w - r + 1 {
		fmt.Println(0)
	} else {
		fmt.Println(w - r - l + 1)
	}
}

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer(make([]byte, 2048*2048), 2048*2048)
	scanner.Split(bufio.ScanWords)

	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())

	scanner.Scan()
	w, _ := strconv.Atoi(scanner.Text())

	a := make([]int, n)
	for i := 0; i < n; i++ {
		scanner.Scan()
		a[i], _ = strconv.Atoi(scanner.Text())
	}

	pred_high := func(val int) bool {
		for i := 0; i < n; i++ {
			val += a[i]
			if val > w {
				return true
			}
		}
		return false
	}

	pred_low := func(val int) bool {
		for i := 0; i < n; i++ {
			val += a[i]
			if val < 0 {
				return false
			}
		}
		return true
	}

	l := 0
	r := w
	for l < r {
		m := l + (r-l)/2
		if pred_low(m) {
			r = m
		} else {
			l = m + 1
		}
	}

	ll := 0
	rr := w
	for ll < rr {
		m := ll + (rr-ll+1)/2
		if pred_high(m) {
			rr = m - 1
		} else {
			ll = m
		}
	}

	if !pred_low(l) {
		fmt.Println(0)
		return
	}
	if pred_high(ll) {
		fmt.Println(0)
		return
	}
	if l >= ll {
		fmt.Println(0)
		return
	}

	fmt.Println(ll - l + 1)
}

package main

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

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

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

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

	var n int
	var w int64
	fmt.Fscan(in, &n, &w)
	var t, tmp int64
	fmt.Fscan(in, &tmp)
	mn, mx := tmp, tmp
	for i := 1; i < n; i++ {
		fmt.Fscan(in, &t)
		tmp += t
		mx = max(mx, tmp)
		mn = min(mn, tmp)
	}
	l, r := int64(0), w
	if mn < 0 {
		l =  -mn
	} else {
		r = w - mn
	}
	if mx > 0 {
		r = min(r, w-mx)
	} else {
		l = max(l, -mx)
	}
	fmt.Fprintln(out, r-l+1)
}


package main

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

// github.com/EndlessCheng/codeforces-go
func CF864E(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	type pair struct{ w, mxW, v, i int }

	var n int
	Fscan(in, &n)
	a := make([]pair, n)
	for i := range a {
		Fscan(in, &a[i].w, &a[i].mxW, &a[i].v)
		a[i].i = i + 1
	}
	sort.Slice(a, func(i, j int) bool { return a[i].mxW < a[j].mxW })

	m := a[n-1].mxW
	dp := make([]int, m)
	fa := make([][]int, n)
	for i := range fa {
		fa[i] = make([]int, m)
		for j := range fa[i] {
			fa[i][j] = -1
		}
	}
	for i, p := range a {
		w := p.w
		for j := p.mxW - 1; j >= w; j-- {
			if d := dp[j-w] + p.v; d > dp[j] {
				dp[j] = d
				fa[i][j] = j - w
			}
		}
	}
	mxJ := 0
	for j, v := range dp {
		if v > dp[mxJ] {
			mxJ = j
		}
	}
	Fprintln(out, dp[mxJ])
	ans := []int{}
	for i, j := n-1, mxJ; i >= 0; i-- {
		if fa[i][j] != -1 {
			ans = append(ans, a[i].i)
			j = fa[i][j]
		}
	}
	Fprintln(out, len(ans))
	for i := len(ans) - 1; i >= 0; i-- {
		Fprint(out, ans[i], " ")
	}
}

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

package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"os"
	"sort"
)

type Item struct {
	Time, DestroyTime, Value, Index int
}
type Items []*Item

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

var dp [101][2001]int
var route [101][2001]int

func main() {
	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewBuffer(bs)

	var n int

	fmt.Fscanf(reader, "%d\n", &n)
	var items Items
	items = make([]*Item, n)

	for i := 0; i < n; i++ {
		item := Item{}
		fmt.Fscanf(reader, "%d %d %d\n", &item.Time, &item.DestroyTime, &item.Value)
		item.Index = i + 1
		items[i] = &item
	}
	sort.Sort(items)

	for i := 1; i <= n; i++ {
		item := items[i-1]
		for j := 0; j <= 2000; j++ {
			if j != 0 {
				dp[i][j] = dp[i][j-1]
				route[i][j] = 1
			}
			if dp[i][j] < dp[i-1][j] {
				dp[i][j] = dp[i-1][j]
				route[i][j] = 2
			}
			if j < item.DestroyTime && j >= item.Time && dp[i][j] < dp[i-1][j-item.Time]+item.Value {
				dp[i][j] = dp[i-1][j-item.Time] + item.Value
				route[i][j] = 3
			}
		}
	}
	ans := make([]int, 0, n)
	itemPos, timePos := n, 2000
	for itemPos != 0 && timePos != 0 {
		if route[itemPos][timePos] == 3 {
			ans = append(ans, items[itemPos-1].Index)
			timePos = timePos - items[itemPos-1].Time
			itemPos--
		} else if route[itemPos][timePos] == 2 {
			itemPos--
		} else {
			timePos--
		}
	}
	fmt.Printf("%d\n%d\n", dp[n][2000], len(ans))

	for i := len(ans) - 1; i >= 0; i-- {
		fmt.Printf("%d ", ans[i])
	}
}

package main

import (
	"fmt"
	"sort"
)

type item struct {
	t, d, p, idx int
}

type byD []item

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

const maxD = 2000

func main() {
	var n int
	fmt.Scan(&n)
	items := make([]item, n)
	for i := range items {
		fmt.Scan(&items[i].t, &items[i].d, &items[i].p)
		items[i].idx = i + 1
	}
	sort.Sort(byD(items))

	value := make([][]int, n+1)
	for i := range value {
		value[i] = make([]int, maxD+1)
		for j := range value[i] {
			value[i][j] = -1
		}
	}
	value[0][0] = 0
	take := make([][]int, n+1)
	for i := range take {
		take[i] = make([]int, maxD+1)
	}

	bestT := 0
	for i, it := range items {
		for t := maxD - 1; t >= 0; t-- {
			value[i+1][t] = value[i][t]
			take[i+1][t] = take[i][t]
		}
		for t := it.d - 1; t >= it.t; t-- {
			cur := value[i][t-it.t]
			if cur == -1 {
				continue
			}
			cur += it.p
			if value[i+1][t] < cur {
				value[i+1][t] = cur
				take[i+1][t] = i
				if value[i+1][bestT] < cur {
					bestT = t
				}
			}
		}
	}

	fmt.Println(value[n][bestT])
	var taken []int
	for t, s := bestT, n; t > 0; {
		it := items[take[s][t]]
		s = take[s][t]
		taken = append(taken, it.idx)
		t -= it.t
	}
	fmt.Println(len(taken))
	for i := len(taken) - 1; i >= 0; i-- {
		if i == 0 {
			fmt.Println(taken[i])
		} else {
			fmt.Print(taken[i], " ")
		}
	}
}

package main

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

type Node struct {
	num, p, d, t int
}

type Nodes []Node

func (nodes Nodes) Len() int {
	return len(nodes)
}

func (nodes Nodes) Less(i, j int) bool {
	return nodes[i].d < nodes[j].d
}

func (nodes Nodes) Swap(i, j int) {
	nodes[i], nodes[j] = nodes[j], nodes[i]
}

func solve() {
	n := readInt()
	a := make([]Node, n)
	for i := 0; i < n; i++ {
		a[i].t, a[i].d, a[i].p = readInt(), readInt(), readInt()
		a[i].num = i + 1
	}
	sort.Sort(Nodes(a))
	dp := make([][]int, n + 1)
	MAXD := 2001
	dp[0] = make([]int, MAXD)
	for i := 1; i <= n; i++ {
		dp[i] = make([]int, MAXD)
		for j := MAXD - 1; j >= 0; j-- {
			dp[i][j] = dp[i-1][j]
			if j < a[i-1].d && j - a[i-1].t >= 0 {
				dp[i][j] = max(dp[i-1][j-a[i-1].t] + a[i-1].p, dp[i][j])
			}
		}
	}
	pos := 0
	for i := 1; i < MAXD-1; i++ {
		if dp[n][i] > dp[n][pos] {
			pos = i
		}
	}
	ansValue := dp[n][pos]
	ansArray := make([]int, 0)
	for curRow := n; curRow > 0; curRow-- {
		if pos < a[curRow-1].d && pos - a[curRow-1].t >= 0 && dp[curRow][pos] == dp[curRow-1][pos-a[curRow-1].t] + a[curRow-1].p {
			pos -= a[curRow-1].t
			ansArray = append(ansArray, a[curRow-1].num)
		}
	}
	//sort.Reverse(sort.IntSlice(ansArray))

	var buf bytes.Buffer
	buf.WriteString(strconv.Itoa(ansValue))
	buf.WriteString("\n")
	buf.WriteString(strconv.Itoa(len(ansArray)))
	buf.WriteString("\n")
	if len(ansArray) > 0 {
		for i := range ansArray {
			buf.WriteString(strconv.Itoa(ansArray[len(ansArray)-i-1]))
			buf.WriteString(" ")
		}
	}
	fmt.Println(buf.String())
}

var scanner *bufio.Scanner

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

// IO

func readString() string {
	scanner.Scan()
	return scanner.Text()
}

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

func readInt64() int64 {
	v, _ := strconv.ParseInt(readString(), 10, 64)
	return v
}

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

func readInt64Array(n int) []int64 {
	res := make([]int64, n)
	for i := 0; i < n; i++ {
		res[i] = readInt64()
	}
	return res
}

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
}

package main

import (
	"fmt"
	"sort"
)

type item struct {
	t, d, p, idx int
}

type byD []item

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

const maxD = 2000

func main() {
	var n int
	fmt.Scan(&n)
	items := make([]item, n)
	for i := range items {
		fmt.Scan(&items[i].t, &items[i].d, &items[i].p)
		items[i].idx = i + 1
	}
	sort.Sort(byD(items))

	value := make([][]int, n+1)
	for i := range value {
		value[i] = make([]int, maxD+1)
		for j := range value[i] {
			value[i][j] = -1
		}
	}
	value[0][0] = 0
	take := make([][]int, n+1)
	for i := range take {
		take[i] = make([]int, maxD+1)
	}

	bestT := 0
	for i, it := range items {
		for t := maxD - 1; t >= 0; t-- {
			value[i+1][t] = value[i][t]
			take[i+1][t] = take[i][t]
			if t < it.t || t >= it.d {
				continue
			}
			cur := value[i][t-it.t]
			if cur == -1 {
				continue
			}
			cur += it.p
			if value[i+1][t] < cur {
				value[i+1][t] = cur
				take[i+1][t] = i
				if value[i][bestT] < cur {
					bestT = t
				}
			}
		}
	}

	fmt.Println(value[n][bestT])
	var taken []int
	for t, s := bestT, n; t > 0; {
		it := items[take[s][t]]
		s = take[s][t] - 1
		taken = append(taken, it.idx)
		t -= it.t
	}
	fmt.Println(len(taken))
	for i := len(taken) - 1; i >= 0; i-- {
		if i == 0 {
			fmt.Println(taken[i])
		} else {
			fmt.Print(taken[i], " ")
		}
	}
}

package main

import (
	"fmt"
	"sort"
)

type item struct {
	t, d, p, idx int
}

type byD []item

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

const maxD = 2000

func main() {
	var n int
	fmt.Scan(&n)
	items := make([]item, n)
	for i := range items {
		fmt.Scan(&items[i].t, &items[i].d, &items[i].p)
		items[i].idx = i + 1
	}
	sort.Sort(byD(items))

	value := make([][]int, n+1)
	for i := range value {
		value[i] = make([]int, maxD+1)
	}
	take := make([][]int, n+1)
	for i := range take {
		take[i] = make([]int, maxD+1)
	}
	bestT := 0
	for i, it := range items {
		for t := it.d - 1; t >= it.t; t-- {
			value[i+1][t] = value[i][t]
			take[i+1][t] = take[i][t]
			if cur := value[i][t-it.t] + it.p; value[i+1][t] < cur {
				value[i+1][t] = cur
				take[i+1][t] = i
				if value[i+1][bestT] < cur {
					bestT = t
				}
			}
		}
	}

	fmt.Println(value[n][bestT])
	var taken []int
	for t, s := bestT, n; t > 0 && s > 0; s-- {
		it := items[take[s][t]]
		if len(taken) > 0 && it.idx == taken[len(taken)-1] {
			continue
		}
		taken = append(taken, it.idx)
		t -= it.t
	}
	fmt.Println(len(taken))
	for i := len(taken) - 1; i >= 0; i-- {
		if i == 0 {
			fmt.Println(taken[i])
		} else {
			fmt.Print(taken[i], " ")
		}
	}
}

package main

import (
	"fmt"
	"sort"
)

type item struct {
	t, d, p, idx int
}

type byD []item

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

const maxD = 2000

func main() {
	var n int
	fmt.Scan(&n)
	items := make([]item, n)
	for i := range items {
		fmt.Scan(&items[i].t, &items[i].d, &items[i].p)
		items[i].idx = i + 1
	}
	sort.Sort(byD(items))

	value := make([][]int, n+1)
	for i := range value {
		value[i] = make([]int, maxD+1)
		for j := range value[i] {
			value[i][j] = -1
		}
	}
	value[0][0] = 0
	take := make([][]int, n+1)
	for i := range take {
		take[i] = make([]int, maxD+1)
	}

	bestT := 0
	for i, it := range items {
		for t := maxD - 1; t >= 0; t-- {
			value[i+1][t] = value[i][t]
			take[i+1][t] = take[i][t]
			if t < it.t || t >= it.d {
				continue
			}
			cur := value[i][t-it.t]
			if cur == -1 {
				continue
			}
			cur += it.p
			if value[i+1][t] < cur {
				value[i+1][t] = cur
				take[i+1][t] = i
				if value[i][bestT] < cur {
					bestT = t
				}
			}
		}
	}

	fmt.Println(value[n][bestT])
	var taken []int
	for t, s := bestT, n; t > 0; {
		it := items[take[s][t]]
		s = take[s][t]
		taken = append(taken, it.idx)
		t -= it.t
	}
	fmt.Println(len(taken))
	for i := len(taken) - 1; i >= 0; i-- {
		if i == 0 {
			fmt.Println(taken[i])
		} else {
			fmt.Print(taken[i], " ")
		}
	}
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF864E(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	type pair struct{ w, mxW, v, i int }

	var n int
	Fscan(in, &n)
	a := make([]pair, n)
	for i := range a {
		Fscan(in, &a[i].w, &a[i].mxW, &a[i].v)
		a[i].i = i + 1
	}
	sort.Slice(a, func(i, j int) bool { return a[i].mxW < a[j].mxW })

	m := a[n-1].mxW
	dp := make([]int, m)
	fa := make([][]int, n)
	for i := range fa {
		fa[i] = make([]int, m)
		for j := range fa[i] {
			fa[i][j] = -1
		}
	}
	for i, p := range a {
		w := p.w
		for j := p.mxW - 1; j >= w; j-- {
			if d := dp[j-w] + p.v; d > dp[j] {
				dp[j] = d
				fa[i][j] = j - w
			}
		}
	}
	Fprintln(out, dp[m-1])
	ans := []int{}
	for i, j := n-1, m-1; i >= 0; i-- {
		if fa[i][j] != -1 {
			ans = append(ans, a[i].i)
			j = fa[i][j]
		}
	}
	Fprintln(out, len(ans))
	for i := len(ans) - 1; i >= 0; i-- {
		Fprint(out, ans[i], " ")
	}
}

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

package main

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

type Node struct {
	num, p, d, t int
}

type Nodes []Node

func (nodes Nodes) Len() int {
	return len(nodes)
}

func (nodes Nodes) Less(i, j int) bool {
	return nodes[i].d < nodes[j].d
}

func (nodes Nodes) Swap(i, j int) {
	nodes[i], nodes[j] = nodes[j], nodes[i]
}

func solve() {
	n := readInt()
	a := make([]Node, n)
	for i := 0; i < n; i++ {
		a[i].t, a[i].d, a[i].p = readInt(), readInt(), readInt()
		a[i].num = i + 1
	}
	sort.Sort(Nodes(a))
	dp := make([][]int, n + 1)
	MAXD := 2001
	dp[0] = make([]int, MAXD)
	for i := 1; i <= n; i++ {
		dp[i] = make([]int, MAXD)
		for j := MAXD - 1; j >= 0; j-- {
			dp[i][j] = dp[i-1][j]
			if j < a[i-1].d && j - a[i-1].t >= 0 {
				dp[i][j] = max(dp[i-1][j-a[i-1].t] + a[i-1].p, dp[i][j])
			}
		}
	}
	pos := 0
	for i := 1; i < MAXD-1; i++ {
		if dp[n][i] > dp[n][pos] {
			pos = i
		}
	}
	ansValue := dp[n][pos]
	ansArray := make([]int, 0)
	for curRow := n; curRow > 0; curRow-- {
		if pos < a[curRow-1].d && pos - a[curRow-1].t >= 0 && dp[curRow][pos] == dp[curRow-1][pos-a[curRow-1].t] + a[curRow-1].p {
			pos -= a[curRow-1].t
			ansArray = append(ansArray, a[curRow-1].num)
		}
	}
	sort.Reverse(sort.IntSlice(ansArray))

	fmt.Println(n)
	var buf bytes.Buffer
	buf.WriteString(strconv.Itoa(ansValue))
	buf.WriteString("\n")
	buf.WriteString(strconv.Itoa(len(ansArray)))
	buf.WriteString("\n")
	if len(ansArray) > 0 {
		for i := range ansArray {
			buf.WriteString(strconv.Itoa(ansArray[len(ansArray)-i-1]))
			buf.WriteString(" ")
		}
	}
	fmt.Println(buf.String())
}

var scanner *bufio.Scanner

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

// IO

func readString() string {
	scanner.Scan()
	return scanner.Text()
}

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

func readInt64() int64 {
	v, _ := strconv.ParseInt(readString(), 10, 64)
	return v
}

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

func readInt64Array(n int) []int64 {
	res := make([]int64, n)
	for i := 0; i < n; i++ {
		res[i] = readInt64()
	}
	return res
}

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
}


package main

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

var _ = sort.Slice
var _ = math.Sqrt
var _ = strconv.Itoa

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var q, x, y int
	fmt.Fscan(in, &q, &x)
	var ans []string
	mape := make([]int, x)
	ansi := 0
	for i := 0; i < q; i++ {
		fmt.Fscan(in, &y);
		mape[y%x]++
		for mape[ansi%x] > 0 {
			mape[ansi%x]--
			ansi++
		}
		ans = append(ans, fmt.Sprint(ansi))
	}
	fmt.Fprintln(out, strings.Join(ans, "\n"));
}

package main

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

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

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

func main() {
	defer writer.Flush()

	var x, q int

	scanf("%d %d\n", &q, &x)

	occCount := make([]int, x)
	queries := make([]int, q)

	for i := 0; i < q; i++ {
		var y int
		scanf("%d\n", &y)
		y %= x
		queries[i] = y
		occCount[y]++
	}

	minX := 0
	for i := 1; i < x; i++ {
		if occCount[i] < occCount[minX] {
			minX = i
		}
	}
	result := make([]int, q)
	for i := q - 1; i >= 0; i-- {
		result[i] = occCount[minX]*x + minX
		occCount[queries[i]]--
		if occCount[queries[i]] < occCount[minX] || (occCount[queries[i]] == occCount[minX] && queries[i] < minX) {
			minX = queries[i]
		}
	}

	for _, res := range result {
		printf("%d\n", res)
	}

}

package main

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

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

// 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
}

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

// Read next line as []byte
func readbln() []byte {
	b, err := nextLine()
	if err != nil {
		panic(err)
	}
	return 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...)
}

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

// 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 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 abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

func absll(a int64) int64 {
	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)
}

func main() {
	defer Flush()

	q := readi()
	x := readi()
	m := make([]int, x)
	k := 0
	l := 1
	for i := 0; i < q; i++ {
		y := readi()
		z := y % x
		m[z]++
		for l <= m[k] {
			k = (k + 1) % x
			if k == 0 {
				l++
			}
		}
		println((l-1)*x + k)
	}
}

// Author: sighduck
// URL: https://codeforces.com/contest/1294/problem/D

package main

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

func Solve(q int, x int, y []int) []int {
	mods := make([]int, x)
	m := 0
	answer := []int{}
	for _, elem := range y {
		mods[elem%x] += 1
		for mods[m%x] > 0 {
			mods[m%x] -= 1
			m += 1
		}
		answer = append(answer, m)
	}
	return answer
}

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

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

	answer := Solve(q, x, y)
	for _, elem := range answer {
		fmt.Fprintf(writer, "%d\n", elem)
	}
}

package main

import "fmt"

func main() {
	var x, q int

	_, _ = fmt.Scanf("%d %d", &q, &x)

	occCount := make([]int, x)
	queries := make([]int, q)

	for i := 0; i < q; i++ {
		var y int
		_, _ = fmt.Scanf("%d", &y)
		y %= x
		queries[i] = y
		occCount[y]++
	}

	minX := 0
	for i := 1; i < x; i++ {
		if occCount[i] < occCount[minX] {
			minX = i
		}
	}
	result := make([]int, q)
	for i := q - 1; i >= 0; i-- {
		result[i] = occCount[minX]*x + minX
		occCount[queries[i]]--
		if occCount[queries[i]] < occCount[minX] || (occCount[queries[i]] == occCount[minX] && queries[i] < minX) {
			minX = queries[i]
		}
	}

	for _, res := range result {
		fmt.Println(res)
	}

}


package main

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

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

	var buf bytes.Buffer

	tc := readNum(reader)

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

	fmt.Print(buf.String())
}

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

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

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

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

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

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

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

	return i
}

const MOD = 1000000007

func solve(n int, B []int) int {
	dp := make(map[int64]int64)
	dp[0] = 1
	var tt int64 = 1
	var k int64

	for i := 0; i < n; i++ {
		a := dp[k]
		dp[k] = tt
		k -= int64(B[i])
		tt = (tt*2 + MOD - a) % MOD
	}
	return int(tt)
}

package main

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

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

data[i] = read data
acc[i] = acc[i-1] + data[i]
f[i] = sum( f[j] if sum(j, i-1) != 0 ) = sum( f[j] if acc[i-1] - acc[j-1] != 0 )
     = sum( f[j] if acc[i-1] != acc[j-1] )
let k = max{ j, for j in 1..i-2, acc[j-1] = acc[i-1] }
f[k] = sum( f[j] if acc[j-1] != acc[k-1], for j < k ) , as acc[k-1] == acc[j-1]
f[i] = f[k] + f[k+1] + ... + f[i-1]
let accf[i] = accf[i-1] + f[i]
f[i] = accf[i-1] - accf[k-1]

issue: f[0] = ? f[1] = ?

// 1 -1 1

f[0] = 1
data[1] = 1
acc[1] = 1
acc[0] = 0
k = last[acc[1-1]] + 1 = 1 // how to maintain last?
k == i -> f[1] = 0

data[2] = -1
acc[2] = 0
k=last[acc[i-1]] + 1 = last[1] +1 = not found
f[2] =1

data[3] = 1
acc[3] = 1
k=last[acc[i-1]] + 1 = last[0] + 1 = 1
f[3] = f[1] + f[2] = 1

ans = f[0] + f[1] + f[2] + f[3] = 1 + 0 + 1 + 1 = 3

// 0 0 0 1

f[0] = 1
data[1] = 0
acc[1] = 0
acc[0] = 0
k = 1, k== i. f[1] = 0
data[2] = 0
acc[2] = 0
k = last[acc[i-1]] + 1 = last[0] + 1 = 1
f[2] = f[1] = 0
data[3] = 0
acc[3] = 0
k = last[acc[i-1]] + 1 = 2
f[3] = f[2] = 0

data[4] = 1
acc[4] = 1
k = last[acc[i-1]] + 1 = 3
f[4] = f[3] = 0

ans = f[0] + f[1] + f[2] + f[3] + f[4] = 1

// consider last[x] = last occurence of acc[?] = x

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

type bigint struct {
	b    [128]byte
	sign int
}

const MODULAR int64 = 1e9 + 7

func main() {
	input := bufio.NewReader(os.Stdin)
	var t int
	fmt.Fscanln(input, &t)
	for ; t > 0; t-- {
		var n int
		_, err := fmt.Fscanln(input, &n)
		if err != nil {
			panic(err)
		}
		var acc int64
		accf := make([]int64, n+1)
		accf[0] = 1
		last := map[int64]int{0: 0}
		for i := 1; i <= n; i++ {
			var data, f int64
			_, err := fmt.Fscan(input, &data)
			if err != nil {
				panic(err)
			}

			k, found := last[acc]
			last[acc] = i - 1
			k++
			switch {
			case !found:
				f = accf[i-1]
			case k >= i:
			default:
				f = (accf[i-1] - accf[k-1]) % MODULAR
			}
			accf[i] = (accf[i-1] + f) % MODULAR
			acc += data
		}
		fmt.Fscanf(input, "\n")
		ans := accf[n]
		if ans < 0 {
			ans = ans + MODULAR
		}
		fmt.Println(ans)
	}
}

package main

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

const N = 2e5 + 5
const mod = 1e9 + 7

var b [N]int64
var preB [N]int64
var dp [N][2]int

var in = bufio.NewReader(os.Stdin)

func solve(t int) {
	var n int
	fmt.Fscan(in, &n)
	sum := int64(0)
	for i := 1; i <= n; i++ {
		dp[i][0] = 0
		dp[i][1] = 0
		fmt.Fscan(in, &b[i])
		sum += b[i]
		preB[i] = sum
	}
	dp0v := make(map[int64]int)
	dp0vTot := 1
	dp1vTot := 0
	dp0v[0] = 1
	dp[0][0] = 1
	ans := 1
	for i := 1; i < n; i++ {
		ans *= 2
		if ans >= mod {
			ans -= mod
		}
	}
	for i := 1; i < n; i++ {
		dp[i][1] = dp0v[preB[i]] + dp1vTot
		dp[i][1] %= mod
		dp1vTot += dp[i][1]
		dp1vTot %= mod
		dp[i][0] = dp0vTot - dp0v[preB[i]]
		dp[i][0] %= mod
		if dp[i][0] < 0 {
			dp[i][0] += mod
		}
		dp0vTot += dp[i][0]
		if dp0vTot >= mod {
			dp0vTot -= mod
		}
		dp0v[preB[i]] += dp[i][0]
		if dp0v[preB[i]] >= mod {
			dp0v[preB[i]] -= mod
		}
		ans -= dp[i][1]
		if ans < 0 {
			ans += mod
		}
		// fmt.Printf("dp[%d][1]: %d\n", i, dp[i][1])
	}
	fmt.Println(ans)
}

func main() {
	var T int
	fmt.Fscan(in, &T)
	for t := 1; t <= T; t++ {
		solve(t)
	}
}

package main

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

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

data[i] = read data
acc[i] = acc[i-1] + data[i]
f[i] = sum( f[j] if sum(j, i-1) != 0 ) = sum( f[j] if acc[i-1] - acc[j-1] != 0 )
     = sum( f[j] if acc[i-1] != acc[j-1] )
let k = max{ j, for j in 1..i-2, acc[j-1] = acc[i-1] }
f[k] = sum( f[j] if acc[j-1] != acc[k-1], for j < k ) , as acc[k-1] == acc[j-1]
f[i] = f[k] + f[k+1] + ... + f[i-1]
let accf[i] = accf[i-1] + f[i]
f[i] = accf[i-1] - accf[k-1]

issue: f[0] = ? f[1] = ?

// 1 -1 1

f[0] = 1
data[1] = 1
acc[1] = 1
acc[0] = 0
k = last[acc[1-1]] + 1 = 1 // how to maintain last?
k == i -> f[1] = 0

data[2] = -1
acc[2] = 0
k=last[acc[i-1]] + 1 = last[1] +1 = not found
f[2] =1

data[3] = 1
acc[3] = 1
k=last[acc[i-1]] + 1 = last[0] + 1 = 1
f[3] = f[1] + f[2] = 1

ans = f[0] + f[1] + f[2] + f[3] = 1 + 0 + 1 + 1 = 3

// 0 0 0 1

f[0] = 1
data[1] = 0
acc[1] = 0
acc[0] = 0
k = 1, k== i. f[1] = 0
data[2] = 0
acc[2] = 0
k = last[acc[i-1]] + 1 = last[0] + 1 = 1
f[2] = f[1] = 0
data[3] = 0
acc[3] = 0
k = last[acc[i-1]] + 1 = 2
f[3] = f[2] = 0

data[4] = 1
acc[4] = 1
k = last[acc[i-1]] + 1 = 3
f[4] = f[3] = 0

ans = f[0] + f[1] + f[2] + f[3] + f[4] = 1

// consider last[x] = last occurence of acc[?] = x

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

type bigint struct {
	b    [128]byte
	sign int
}

const MODULAR int64 = 1e9 + 7

func main() {
	input := bufio.NewReader(os.Stdin)
	var t int
	fmt.Fscanln(input, &t)
	for ; t > 0; t-- {
		var n int
		_, err := fmt.Fscanln(input, &n)
		if err != nil {
			panic(err)
		}
		var acc int64
		accf := make([]int64, n+1)
		accf[0] = 1
		last := map[int64]int{0: 0}
		for i := 1; i <= n; i++ {
			var data, f int64
			_, err := fmt.Fscan(input, &data)
			if err != nil {
				panic(err)
			}

			k, found := last[acc]
			last[acc] = i - 1
			k++
			switch {
			case !found:
				f = accf[i-1]
			case k >= i:
			default:
				f = (accf[i-1] - accf[k-1]) % MODULAR
			}
			accf[i] = (accf[i-1] + f) % MODULAR
			acc += data
		}
		fmt.Fscanf(input, "\n")
		fmt.Println(accf[n])
	}
}

package main

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

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

data[i] = read data
acc[i] = acc[i-1] + data[i]
f[i] = sum( f[j] if sum(j, i-1) != 0 ) = sum( f[j] if acc[i-1] - acc[j-1] != 0 )
     = sum( f[j] if acc[i-1] != acc[j-1] )
let k = max{ j, for j in 1..i-2, acc[j-1] = acc[i-1] }
f[k] = sum( f[j] if acc[j-1] != acc[k-1], for j < k ) , as acc[k-1] == acc[j-1]
f[i] = f[k] + f[k+1] + ... + f[i-1]
let accf[i] = accf[i-1] + f[i]
f[i] = accf[i-1] - accf[k-1]

issue: f[0] = ? f[1] = ?

// 1 -1 1

f[0] = 1
data[1] = 1
acc[1] = 1
acc[0] = 0
k = last[acc[1-1]] + 1 = 1 // how to maintain last?
k == i -> f[1] = 0

data[2] = -1
acc[2] = 0
k=last[acc[i-1]] + 1 = last[1] +1 = not found
f[2] =1

data[3] = 1
acc[3] = 1
k=last[acc[i-1]] + 1 = last[0] + 1 = 1
f[3] = f[1] + f[2] = 1

ans = f[0] + f[1] + f[2] + f[3] = 1 + 0 + 1 + 1 = 3

// 0 0 0 1

f[0] = 1
data[1] = 0
acc[1] = 0
acc[0] = 0
k = 1, k== i. f[1] = 0
data[2] = 0
acc[2] = 0
k = last[acc[i-1]] + 1 = last[0] + 1 = 1
f[2] = f[1] = 0
data[3] = 0
acc[3] = 0
k = last[acc[i-1]] + 1 = 2
f[3] = f[2] = 0

data[4] = 1
acc[4] = 1
k = last[acc[i-1]] + 1 = 3
f[4] = f[3] = 0

ans = f[0] + f[1] + f[2] + f[3] + f[4] = 1

// consider last[x] = last occurence of acc[?] = x

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

type bigint struct {
	b    [128]byte
	sign int
}

const MODULAR int64 = 1e9 + 7

func (b *bigint) set(i *big.Int) {
	i.FillBytes(b.b[:])
	b.sign = i.Sign()
}

func main() {
	input := bufio.NewReader(os.Stdin)
	var t int
	fmt.Fscanln(input, &t)
	for ; t > 0; t-- {
		var n int
		_, err := fmt.Fscanln(input, &n)
		if err != nil {
			panic(err)
		}
		acc, acb := big.NewInt(0), new(bigint)
		accf := make([]int64, n+1)
		accf[0] = 1
		last := map[bigint]int{*acb: 0}
		for i := 1; i <= n; i++ {
			var data, f int64
			fmt.Fscan(input, &data)

			k, found := last[*acb]
			last[*acb] = i - 1
			k++
			switch {
			case !found:
				f = accf[i-1]
			case k >= i:
			default:
				f = (accf[i-1] - accf[k-1]) % MODULAR
			}
			accf[i] = (accf[i-1] + f) % MODULAR
			acc.Add(acc, big.NewInt(data))
			acb.set(acc)
		}
		fmt.Fscanf(input, "\n")
		fmt.Println(accf[n])
	}
}

package main

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

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

data[i] = read data
acc[i] = acc[i-1] + data[i]
f[i] = sum( f[j] if sum(j, i-1) != 0 ) = sum( f[j] if acc[i-1] - acc[j-1] != 0 )
     = sum( f[j] if acc[i-1] != acc[j-1] )
let k = max{ j, for j in 1..i-2, acc[j-1] = acc[i-1] }
f[k] = sum( f[j] if acc[j-1] != acc[k-1], for j < k ) , as acc[k-1] == acc[j-1]
f[i] = f[k] + f[k+1] + ... + f[i-1]
let accf[i] = accf[i-1] + f[i]
f[i] = accf[i-1] - accf[k-1]

issue: f[0] = ? f[1] = ?

// 1 -1 1

f[0] = 1
data[1] = 1
acc[1] = 1
acc[0] = 0
k = last[acc[1-1]] + 1 = 1 // how to maintain last?
k == i -> f[1] = 0

data[2] = -1
acc[2] = 0
k=last[acc[i-1]] + 1 = last[1] +1 = not found
f[2] =1

data[3] = 1
acc[3] = 1
k=last[acc[i-1]] + 1 = last[0] + 1 = 1
f[3] = f[1] + f[2] = 1

ans = f[0] + f[1] + f[2] + f[3] = 1 + 0 + 1 + 1 = 3

// 0 0 0 1

f[0] = 1
data[1] = 0
acc[1] = 0
acc[0] = 0
k = 1, k== i. f[1] = 0
data[2] = 0
acc[2] = 0
k = last[acc[i-1]] + 1 = last[0] + 1 = 1
f[2] = f[1] = 0
data[3] = 0
acc[3] = 0
k = last[acc[i-1]] + 1 = 2
f[3] = f[2] = 0

data[4] = 1
acc[4] = 1
k = last[acc[i-1]] + 1 = 3
f[4] = f[3] = 0

ans = f[0] + f[1] + f[2] + f[3] + f[4] = 1

// consider last[x] = last occurence of acc[?] = x

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

type bigint struct {
	b    [128]byte
	sign int
}

const MODULAR int64 = 1e9 + 7

func (b *bigint) set(i *big.Int) {
	i.FillBytes(b.b[:])
	b.sign = i.Sign()
}

func main() {
	input := bufio.NewReader(os.Stdin)
	var t int
	fmt.Fscanln(input, &t)
	for ; t > 0; t-- {
		var n int
		_, err := fmt.Fscanln(input, &n)
		if err != nil {
			panic(err)
		}
		acc, acb := big.NewInt(0), new(bigint)
		accf := make([]int64, n+1)
		accf[0] = 1
		last := map[bigint]int{*acb: 0}
		for i := 1; i <= n; i++ {
			var data, f int64
			fmt.Fscan(input, &data)

			k, found := last[*acb]
			last[*acb] = i - 1
			k++
			switch {
			case !found:
				f = accf[i-1]
			case k >= i:
			default:
				f = (accf[i-1] - accf[k-1]) % MODULAR
			}
			accf[i] = accf[i-1] + f
			acc.Add(acc, big.NewInt(data))
			acb.set(acc)
		}
		fmt.Fscanf(input, "\n")
		fmt.Println(accf[n])
	}
}