Polycarp often uses his smartphone. He has already installed $$$n$$$ applications on it. Application with number $$$i$$$ takes up $$$a_i$$$ units of memory.Polycarp wants to free at least $$$m$$$ units of memory (by removing some applications).Of course, some applications are more important to Polycarp than others. He came up with the following scoring system — he assigned an integer $$$b_i$$$ to each application:   $$$b_i = 1$$$ — regular application;  $$$b_i = 2$$$ — important application. According to this rating system, his phone has $$$b_1 + b_2 + \ldots + b_n$$$ convenience points.Polycarp believes that if he removes applications with numbers $$$i_1, i_2, \ldots, i_k$$$, then he will free $$$a_{i_1} + a_{i_2} + \ldots + a_{i_k}$$$ units of memory and lose $$$b_{i_1} + b_{i_2} + \ldots + b_{i_k}$$$ convenience points.For example, if $$$n=5$$$, $$$m=7$$$, $$$a=[5, 3, 2, 1, 4]$$$, $$$b=[2, 1, 1, 2, 1]$$$, then Polycarp can uninstall the following application sets (not all options are listed below):   applications with numbers $$$1, 4$$$ and $$$5$$$. In this case, it will free $$$a_1+a_4+a_5=10$$$ units of memory and lose $$$b_1+b_4+b_5=5$$$ convenience points;  applications with numbers $$$1$$$ and $$$3$$$. In this case, it will free $$$a_1+a_3=7$$$ units of memory and lose $$$b_1+b_3=3$$$ convenience points.  applications with numbers $$$2$$$ and $$$5$$$. In this case, it will free $$$a_2+a_5=7$$$ memory units and lose $$$b_2+b_5=2$$$ convenience points. Help Polycarp, choose a set of applications, such that if removing them will free at least $$$m$$$ units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$1 \le m \le 10^9$$$) — the number of applications on Polycarp's phone and the number of memory units to be freed. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the number of memory units used by applications. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \le b_i \le 2$$$) — the convenience points of each application. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case, output on a separate line:    -1, if there is no set of applications, removing which will free at least $$$m$$$ units of memory;  the minimum number of convenience points that Polycarp will lose if such a set exists.

NoteIn the first test case, it is optimal to remove applications with numbers $$$2$$$ and $$$5$$$, freeing $$$7$$$ units of memory. $$$b_2+b_5=2$$$.In the second test case, by removing the only application, Polycarp will be able to clear only $$$2$$$ of memory units out of the $$$3$$$ needed.In the third test case, it is optimal to remove applications with numbers $$$1$$$, $$$2$$$, $$$3$$$ and $$$4$$$, freeing $$$10$$$ units of memory. $$$b_1+b_2+b_3+b_4=6$$$.In the fourth test case, it is optimal to remove applications with numbers $$$1$$$, $$$3$$$ and $$$4$$$, freeing $$$12$$$ units of memory. $$$b_1+b_3+b_4=4$$$.In the fifth test case, it is optimal to remove applications with numbers $$$1$$$ and $$$2$$$, freeing $$$5$$$ units of memory. $$$b_1+b_2=3$$$.

package main

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

type app struct {
	w, p int64
}

func solve(apps []app, m int64) {
	a1, a2 := []app{}, []app{}
	for i := range apps {
		if apps[i].p == 1 {
			a1 = append(a1, apps[i])
		} else {
			a2 = append(a2, apps[i])
		}
	}
	sort.Slice(a1, func(i, j int) bool { return a1[i].w > a1[j].w })
	sort.Slice(a2, func(i, j int) bool { return a2[i].w > a2[j].w })
	for i := 1; i < len(a2); i++ {
		a2[i].w += a2[i-1].w
	}

	var (
		i   int64 = -1
		sum int64 = 0
		res int64 = math.MaxInt64
	)
	bs := func(x int64) int64 {
		if x <= 0 {
			return -1
		}
		l, r := -1, len(a2)
		for r-l > 1 {
			m := l + (r-l)/2
			if a2[m].w >= x {
				r = m
			} else {
				l = m
			}
		}
		return int64(r)
	}
	for ; i < int64(len(a1)); i++ {
		if i > -1 {
			sum += a1[i].w
		}
		j := bs(m - sum)
		if j < int64(len(a2)) {
			res = IntMin(res, i+1+2*(j+1))
		}
	}
	if res == math.MaxInt64 {
		res = -1
	}
	printf("%d\n", res)
}

func main() {
	//RedirectInputToFile(reader, "input.txt")

	t := 1
	scan(&t)

	for T := 0; T < t; T++ {
		var n, m int64
		scan(&n)
		scan(&m)

		a := make([]app, n)
		for i := range a {
			scan(&a[i].w)
		}
		for i := range a {
			scan(&a[i].p)
		}

		solve(a, m)
	}

	writer.Flush()
}

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

// IntMax returns maximum of received int numbers
func IntMax(nums ...int64) int64 {
	cand := nums[0]
	for _, num := range nums {
		if cand < num {
			cand = num
		}
	}
	return cand
}

// IntMin returns minimum of received int numbers
func IntMin(nums ...int64) int64 {
	cand := nums[0]
	for _, num := range nums {
		if cand > num {
			cand = num
		}
	}
	return cand
}

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

func scan(a ...interface{})             { fmt.Fscan(reader, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }

func RedirectInputToFile(r *bufio.Reader, fp string) {
	f, err := os.Open(fp)
	if err != nil {
		panic(err)
	}
	r.Reset(f)
}

package main

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

type app struct {
	w, p int64
}

func solve(apps []app, m int64) {
	a1, a2 := []app{}, []app{}
	for i := range apps {
		if apps[i].p == 1 {
			a1 = append(a1, apps[i])
		} else {
			a2 = append(a2, apps[i])
		}
	}
	sort.Slice(a1, func(i, j int) bool { return a1[i].w > a1[j].w })
	sort.Slice(a2, func(i, j int) bool { return a2[i].w > a2[j].w })

	var (
		res      int64 = math.MaxInt64
		p1, sum1 int64 = int64(len(a1)) - 1, 0
		p2, sum2 int64 = -1, 0
	)
	update := func() bool {
		if sum1+sum2 >= m {
			res = IntMin(res, p1+1+2*(p2+1))
			return true
		}
		return false
	}
	for i := range a1 {
		sum1 += a1[i].w
		if sum1 >= m {
			res = IntMin(res, int64(i)+1)
		}
	}

	update()
	for p2 = 0; p2 < int64(len(a2)); p2++ {
		sum2 += a2[p2].w
		for ; update() && p1 >= 0; p1-- {
			sum1 -= a1[p1].w
		}
	}

	if res == math.MaxInt64 {
		res = -1
	}
	printf("%d\n", res)
}

func main() {
	//RedirectInputToFile(reader, "input.txt")

	t := 1
	scan(&t)

	for T := 0; T < t; T++ {
		var n, m int64
		scan(&n)
		scan(&m)

		a := make([]app, n)
		for i := range a {
			scan(&a[i].w)
		}
		for i := range a {
			scan(&a[i].p)
		}

		solve(a, m)
	}

	writer.Flush()
}

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

// IntMax returns maximum of received int numbers
func IntMax(nums ...int64) int64 {
	cand := nums[0]
	for _, num := range nums {
		if cand < num {
			cand = num
		}
	}
	return cand
}

// IntMin returns minimum of received int numbers
func IntMin(nums ...int64) int64 {
	cand := nums[0]
	for _, num := range nums {
		if cand > num {
			cand = num
		}
	}
	return cand
}

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

func scan(a ...interface{})             { fmt.Fscan(reader, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }

func RedirectInputToFile(r *bufio.Reader, fp string) {
	f, err := os.Open(fp)
	if err != nil {
		panic(err)
	}
	r.Reset(f)
}

package main

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

type app struct {
	w, p int64
}

type heap []app

func (h heap) Len() int           { return len(h) }
func (h heap) Less(i, j int) bool { return h[i].p > h[j].p }
func (h heap) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }

func (h *heap) Push(x interface{}) {
	// Push and Pop use pointer receivers because they modify the slice's length,
	// not just its contents.
	*h = append(*h, x.(app))
}

func (h *heap) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}

func solve(apps []app, m int64) {
	a1, a2 := []app{}, []app{}
	for i := range apps {
		if apps[i].p == 1 {
			a1 = append(a1, apps[i])
		} else {
			a2 = append(a2, apps[i])
		}
	}
	sort.Slice(a1, func(i, j int) bool { return a1[i].w > a1[j].w })
	sort.Slice(a2, func(i, j int) bool { return a2[i].w > a2[j].w })

	var (
		res      int64 = math.MaxInt64
		p1, sum1 int64 = int64(len(a1)) - 1, 0
		p2, sum2 int64 = -1, 0
	)
	update := func() bool {
		if sum1+sum2 >= m {
			res = IntMin(res, p1+1+2*(p2+1))
			return true
		}
		return false
	}
	for i := range a1 {
		sum1 += a1[i].w
		if sum1 >= m {
			res = IntMin(res, int64(i)+1)
		}
	}

	update()
	for p2 = 0; p2 < int64(len(a2)); p2++ {
		sum2 += a2[p2].w
		for ; update() && p1 >= 0; p1-- {
			sum1 -= a1[p1].w
		}
	}

	if res == math.MaxInt64 {
		res = -1
	}
	printf("%d\n", res)
}

func main() {
	//RedirectInputToFile(reader, "input.txt")

	t := 1
	scan(&t)

	for T := 0; T < t; T++ {
		var n, m int64
		scan(&n)
		scan(&m)

		a := make([]app, n)
		for i := range a {
			scan(&a[i].w)
		}
		for i := range a {
			scan(&a[i].p)
		}

		solve(a, m)
	}

	writer.Flush()
}

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

// IntMax returns maximum of received int numbers
func IntMax(nums ...int64) int64 {
	cand := nums[0]
	for _, num := range nums {
		if cand < num {
			cand = num
		}
	}
	return cand
}

// IntMin returns minimum of received int numbers
func IntMin(nums ...int64) int64 {
	cand := nums[0]
	for _, num := range nums {
		if cand > num {
			cand = num
		}
	}
	return cand
}

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

func scan(a ...interface{})             { fmt.Fscan(reader, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }

func RedirectInputToFile(r *bufio.Reader, fp string) {
	f, err := os.Open(fp)
	if err != nil {
		panic(err)
	}
	r.Reset(f)
}

package main

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

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

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

	n, m int
	a, b [200000]int
	l [2]int
	ab [2][200000]int
	dp [400001]q
	res int
)

type q struct {
	l1, l2, s int
}

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() {
	l[0] = 0
	l[1] = 0
	res = -1
}

func input() {
	n = getInt()
	m = getInt()
	for i := 0; i < n; i++ {
		a[i] = getInt()
	}
	for i := 0; i < n; i++ {
		b[i] = getInt() - 1
		ab[b[i]][l[b[i]]] = a[i]
		l[b[i]]++
	}
}

func count() {
	for k := 0; k < 2; k++ {
		sort.Slice(ab[k][:l[k]], func(i, j int) bool {
			return ab[k][i] > ab[k][j]
		})
	}
	if l[0] > 0 {
		dp[1] = q{1, 0, ab[0][0]}
		if dp[1].s >= m {
			res = 1
			return
		}
	} else {
		dp[1] = q{0, 0, 0}
	}
	for i := 2; i <= l[0] + 2 * l[1]; i++ {
		dp[i] = q{0, 0, 0}
		if dp[i-2].l2 < l[1] {
			dp[i] = q{dp[i-2].l1, dp[i-2].l2 + 1, dp[i-2].s + ab[1][dp[i-2].l2]}
		}
		if dp[i-1].l1 < l[0] && (dp[i-1].s + ab[0][dp[i-1].l1] > dp[i].s) {
			dp[i] = q{dp[i-1].l1 + 1, dp[i-1].l2, dp[i-1].s + ab[0][dp[i-1].l1]}
		}
		if dp[i].s >= m {
			res = i
			return
		}
	}
}

func printIt() {
	fmt.Println(ab[0][:l[0]])
	fmt.Println(ab[1][:l[1]])
}

func output() {
	out.Write(intToBytes(res))
	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"
	"os"
	"sort"
)

func run(r io.Reader, w io.Writer) {
	in := bufio.NewReader(r)
	out := bufio.NewWriter(w)
	defer out.Flush()

	type pair struct {
		x, y int
	}
	var t int
	for Fscan(in, &t); t > 0; t-- {
		var n, m int
		Fscan(in, &n, &m)
		pairs := make([]pair, n)

		var sum int64
		for i := range pairs {
			Fscan(in, &pairs[i].x)
			sum += int64(pairs[i].x)
		}
		for i := range pairs {
			Fscan(in, &pairs[i].y)
		}
		if sum < int64(m) {
			Fprintln(out, -1)
			continue
		}

		var knapsack1, knapsack2 []int
		for i := 0; i < n; i++ {
			if pairs[i].y == 1 {
				knapsack1 = append(knapsack1, pairs[i].x)
			} else {
				knapsack2 = append(knapsack2, pairs[i].x)
			}
		}

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

		sum = 0
		for _, val := range knapsack1 {
			sum += int64(val)
		}

		ans := int(1e9)
		for i, j := len(knapsack1), 0; i >= 0; i-- {
			for j < len(knapsack2) && sum < int64(m) {
				sum += int64(knapsack2[j])
				j++
			}
			if sum >= int64(m) {
				ans = min(ans, 2 * j + i)
			}
			if i > 0 {
				sum -= int64(knapsack1[i-1])
			}
		}
		Fprintln(out, ans)
	}
}

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

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

package main

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

func run(r io.Reader, w io.Writer) {
	in := bufio.NewReader(r)
	out := bufio.NewWriter(w)
	defer out.Flush()

	type pair struct {
		x, y int
	}
	var t int
	for Fscan(in, &t); t > 0; t-- {
		var n, m int
		Fscan(in, &n, &m)
		pairs := make([]pair, n)

		var sum int64
		for i := range pairs {
			Fscan(in, &pairs[i].x)
			sum += int64(pairs[i].x)
		}
		for i := range pairs {
			Fscan(in, &pairs[i].y)
		}

		if sum < int64(m) {
			Fprintln(out, -1)
			continue
		}

		var k1, k2 []int
		for i := 0; i < n; i++ {
			if pairs[i].y == 1 {
				k1 = append(k1, pairs[i].x)
			} else {
				k2 = append(k2, pairs[i].x)
			}
		}
		sort.Slice(k1, func(i, j int) bool {
			return k1[i] > k1[j]
		})
		sort.Slice(k2, func(i, j int) bool {
			return k2[i] > k2[j]
		})

		sum = 0
		for i := 0; i < len(k2); i++ {
			sum += int64(k2[i])
		}
		j, ans := 0, int(1e9)
		for i := len(k2); i >= 0; i-- {
			for j < len(k1) && sum < int64(m) {
				sum += int64(k1[j])
				j++
			}
			if sum >= int64(m) {
				ans = min(ans, 2 * i + j)
			}
			if i > 0 {
				sum -= int64(k2[i-1])
			}
		}
		Fprintln(out, ans)
	}
}

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

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

package main

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

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

func init() {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 4096), 1e9)
}

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

func sint64() int64 {
	s := sstring()
	i := int64(0)
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int64(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int64(r - '0')
	}
	return i
}

func sint64s(n int64) []int64 {
	a := make([]int64, n)
	for i := range a {
		a[i] = sint64()
	}
	return a
}

func sstring() string {
	if !scanner.Scan() {
		panic(scanner.Err())
	}
	return scanner.Text()
}

func main() {
	defer writer.Flush()
	for t := sint64(); t > 0; t-- {
		n := sint64()
		m := sint64()
		a := sint64s(n)
		b := sint64s(n)
		printf("%d\n", sol(n, m, a, b))
	}
}

func sol(n, m int64, a, b []int64) int64 {
	twos := []int64{}
	ones := []int64{}
	for i := n - 1; i >= 0; i-- {
		switch b[i] {
		case 2:
			twos = append(twos, a[i])
		case 1:
			ones = append(ones, a[i])
		}
	}
	sort.Slice(twos, func(il, ir int) bool {
		return twos[il] > twos[ir]
	})
	sort.Slice(ones, func(il, ir int) bool {
		return ones[il] > ones[ir]
	})
	p1 := 0
	p2 := 0
	ret := int64(0)
	for p1 < len(ones) && p2 < len(twos) {
		if ones[p1] >= m {
			ret++
			return ret
		}
		if twos[p2] >= m {
			ret += 2
			return ret
		}
		if p1 == len(ones)-1 { // the last "one" element doesn't finish the job
			m -= twos[p2]
			ret += 2
			p2++
			continue
		}
		sum1 := ones[p1] + ones[p1+1]
		sum2 := twos[p2]
		if sum1 >= sum2 {
			m -= ones[p1]
			ret++
			p1++
			continue
		}
		m -= twos[p2]
		ret += 2
		p2++
		continue
	}
	for m > 0 && p1 < len(ones) {
		m -= ones[p1]
		ret++
		p1++
	}
	for m > 0 && p2 < len(twos) {
		m -= twos[p2]
		ret += 2
		p2++
	}
	if m > 0 {
		return -1
	}
	return ret
}

package main

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

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

func init() {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 4096), 1e9)
}

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

func sint64() int64 {
	s := sstring()
	i := int64(0)
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int64(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int64(r - '0')
	}
	return i
}

func sint64s(n int64) []int64 {
	a := make([]int64, n)
	for i := range a {
		a[i] = sint64()
	}
	return a
}

func sstring() string {
	if !scanner.Scan() {
		panic(scanner.Err())
	}
	return scanner.Text()
}

func main() {
	defer writer.Flush()
	for t := sint64(); t > 0; t-- {
		n := sint64()
		m := sint64()
		a := sint64s(n)
		b := sint64s(n)
		printf("%d\n", sol(n, m, a, b))
	}
}

func sol(n, m int64, a, b []int64) int64 {
	twos := []int64{}
	ones := []int64{}
	for i := n - 1; i >= 0; i-- {
		switch b[i] {
		case 2:
			twos = append(twos, a[i])
		case 1:
			ones = append(ones, a[i])
		}
	}
	sort.Slice(twos, func(il, ir int) bool {
		return twos[il] > twos[ir]
	})
	sort.Slice(ones, func(il, ir int) bool {
		return ones[il] > ones[ir]
	})
	p1 := 0
	p2 := 0
	ret := int64(0)
	for p1 < len(ones) && p2 < len(twos) {
		if ones[p1] >= m {
			ret++
			return ret
		}
		if twos[p2] >= m {
			ret += 2
			return ret
		}
		if p1 == len(ones)-1 { // the last "one" element doesn't finish the job
			m -= twos[p2]
			ret += 2
			p2++
			continue
		}
		sum1 := ones[p1] + ones[p1+1]
		sum2 := twos[p2]
		if sum1 > sum2 { // >= is also fine?
			m -= ones[p1]
			ret++
			p1++
			continue
		}
		m -= twos[p2]
		ret += 2
		p2++
		continue
	}
	for m > 0 && p1 < len(ones) {
		m -= ones[p1]
		ret++
		p1++
	}
	for m > 0 && p2 < len(twos) {
		m -= twos[p2]
		ret += 2
		p2++
	}
	if m > 0 {
		return -1
	}
	return ret
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1475D(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var T, n, tp int
	var low int64
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &low)
		a := make([]int64, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		b := [3][]int64{}
		for _, v := range a {
			Fscan(in, &tp)
			b[tp] = append(b[tp], v)
		}
		for _, a := range b {
			sort.Slice(a, func(i, j int) bool { return a[i] > a[j] })
		}
		n2 := len(b[2])
		s2 := make([]int64, n2+1)
		for i, v := range b[2] {
			s2[i+1] = s2[i] + v
		}

		ans := int(1e9)
		b[1] = append(b[1], 0)
		for i, v := range b[1] {
			if j := sort.Search(n2+1, func(j int) bool { return s2[j] >= low }); j <= n2 && i+2*j < ans {
				ans = i + 2*j
			}
			low -= v
		}

		if ans == 1e9 {
			ans = -1
		}
		Fprintln(out, ans)
	}
}

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

package main

import (
	"bufio"
	. "fmt"
	"io"
	"io/ioutil"
	"os"
	"sort"
	"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(Case int) {
		var n, m, w int64
		Fscan(in, &n, &m)
		a := make([]int64, n)
		for i := range a {
			Fscan(in, &a[i])
		}

		var a1, a2 []int64
		for _, v := range a {
			Fscan(in, &w)
			if w == 1 {
				a1 = append(a1, v)
			} else {
				a2 = append(a2, v)
			}
		}
		sort.Slice(a1, func(i, j int) bool { return a1[i] > a1[j] })
		sort.Slice(a2, func(i, j int) bool { return a2[i] > a2[j] })

		sum2 := make([]int64, len(a2)+1)
		for i, v := range a2 {
			sum2[i+1] = sum2[i] + v
		}

		ans := int(1e9)
		j := sort.Search(len(sum2), func(i int) bool { return sum2[i] >= m })
		if j < len(sum2) {
			ans = min(ans, 2*j)
		}

		for i, v := range a1 {
			m -= v
			if m <= 0 {
				ans = min(ans, i+1)
				break
			}
			j := sort.Search(len(sum2), func(i int) bool { return sum2[i] >= m })
			if j < len(sum2) {
				ans = min(ans, i+1+2*j)
			}
		}

		if ans == 1e9 {
			ans = -1
		}
		Fprintln(out, ans)
	}

	var t int
	Fscan(in, &t)
	for Case := 1; Case <= t; Case++ {
		solve(Case)
	}

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

func main() { run(os.Stdin, os.Stdout) }
func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

package main

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

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

	tc := readNum(reader)

	var buf bytes.Buffer

	for tc > 0 {
		tc--
		n, m := readTwoNums(reader)
		A := readNNums(reader, n)
		B := readNNums(reader, n)
		res := solve(n, m, A, 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
}

func solve(n int, m int, A []int, B []int) int {
	M := int64(m)

	var all int64

	for i := 0; i < n; i++ {
		all += int64(A[i])
	}

	if all < M {
		return -1
	}

	que := make([][]int, 2)
	for i := 0; i < 2; i++ {
		que[i] = make([]int, 0, n)
	}

	for i := 0; i < n; i++ {
		b := B[i] - 1
		que[b] = append(que[b], A[i])
	}

	for i := 0; i < 2; i++ {
		sort.Ints(que[i])
		reverse(que[i])
	}
	preSum := make([]int64, len(que[1])+1)
	for i := 0; i < len(que[1]); i++ {
		preSum[i+1] = preSum[i] + int64(que[1][i])
	}
	// if use all type 2
	j := len(preSum) - 1
	var best = 2 * n
	for j >= 0 && preSum[j] >= M {
		best = j * 2
		if j == 0 || preSum[j-1] < M {
			break
		}
		j--
	}
	var sum int64
	for i := 1; i <= len(que[0]); i++ {
		sum += int64(que[0][i-1])
		if sum+preSum[j] < M {
			// not a valid solution
			continue
		}
		tmp := M - sum
		// need tmp
		for j > 0 && preSum[j-1] >= tmp {
			j--
		}
		if best > i+j*2 {
			best = i + j*2
		}
	}

	return best
}

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

func search(n int, fn func(int) bool) int {
	left, right := 0, n
	for left < right {
		mid := (left + right) / 2
		if fn(mid) {
			right = mid
		} else {
			left = mid + 1
		}
	}
	return right
}

package main

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

type app struct {
	w, p int64
}

type heap []app

func (h heap) Len() int           { return len(h) }
func (h heap) Less(i, j int) bool { return h[i].p > h[j].p }
func (h heap) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }

func (h *heap) Push(x interface{}) {
	// Push and Pop use pointer receivers because they modify the slice's length,
	// not just its contents.
	*h = append(*h, x.(app))
}

func (h *heap) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}

func solve(apps []app, m int64) {
	a1, a2 := []app{}, []app{}
	for i := range apps {
		if apps[i].p == 1 {
			a1 = append(a1, apps[i])
		} else {
			a2 = append(a2, apps[i])
		}
	}
	sort.Slice(a1, func(i, j int) bool { return a1[i].w > a1[j].w })
	sort.Slice(a2, func(i, j int) bool { return a2[i].w > a2[j].w })

	var (
		res      int64 = math.MaxInt64
		p1, sum1 int64 = int64(len(a1)) - 1, 0
		p2, sum2 int64 = -1, 0
	)
	update := func() bool {
		if sum1+sum2 >= m {
			res = IntMin(res, p1+1+2*(p2+1))
			return true
		}
		return false
	}
	for i := range a1 {
		sum1 += a1[i].w
		if sum1 >= m {
			res = IntMin(res, int64(i)+1)
		}
	}

	update()
	for p2 = 0; p2 < int64(len(a2)); p2++ {
		sum2 += a2[p2].w
		for ; p1 >= 0 && update(); p1-- {
			sum1 -= a1[p1].w
		}
	}

	if res == math.MaxInt64 {
		res = -1
	}
	printf("%d\n", res)
}

func main() {
	//RedirectInputToFile(reader, "input.txt")

	t := 1
	scan(&t)

	for T := 0; T < t; T++ {
		var n, m int64
		scan(&n)
		scan(&m)

		a := make([]app, n)
		for i := range a {
			scan(&a[i].w)
		}
		for i := range a {
			scan(&a[i].p)
		}

		solve(a, m)
	}

	writer.Flush()
}

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

// IntMax returns maximum of received int numbers
func IntMax(nums ...int64) int64 {
	cand := nums[0]
	for _, num := range nums {
		if cand < num {
			cand = num
		}
	}
	return cand
}

// IntMin returns minimum of received int numbers
func IntMin(nums ...int64) int64 {
	cand := nums[0]
	for _, num := range nums {
		if cand > num {
			cand = num
		}
	}
	return cand
}

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

func scan(a ...interface{})             { fmt.Fscan(reader, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }

func RedirectInputToFile(r *bufio.Reader, fp string) {
	f, err := os.Open(fp)
	if err != nil {
		panic(err)
	}
	r.Reset(f)
}

package main

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

type app struct {
	w, p int64
}

func solve(apps []app, m int64) {
	sort.Slice(apps, func(i, j int) bool {
		if apps[i].p == apps[j].p {
			return apps[i].w > apps[j].w
		}
		return apps[i].p < apps[j].p
	})

	last := -1
	for i := 0; i < len(apps); i++ {
		if apps[i].p == 1 {
			last = i
		}
	}

	i, j := 0, last+1
	res := int64(0)
	for m > 0 && j < len(apps) && i < last {
		if apps[i].w+apps[i+1].w >= apps[j].w {
			m -= apps[i].w
			i++
			res++
		} else {
			m -= apps[j].w
			j++
			res += 2
		}
	}

	if m <= 0 {
		printf("%d\n", res)
		return
	}

	if j == len(apps) { // i < last and j == len(apps)
		for ; m > 0 && i <= last; i++ {
			m -= apps[i].w
			res++
		}
	} else { // i == last and j < len(apps)
		w := apps[i].w
		for ; j < len(apps); j++ {
			if m <= w {
				printf("%d\n", res+1)
				return
			}
			m -= apps[j].w
			res += 2
			if m <= 0 {
				printf("%d\n", res)
				return
			}
		}
		if m <= w {
			printf("%d\n", res+1)
			return
		}
	}

	if m > 0 {
		res = -1
	}
	printf("%d\n", res)
}

func main() {
	//RedirectInputToFile(reader, "input.txt")

	t := 1
	scan(&t)

	for T := 0; T < t; T++ {
		var n, m int64
		scan(&n)
		scan(&m)

		a := make([]app, n)
		for i := range a {
			scan(&a[i].w)
		}
		for i := range a {
			scan(&a[i].p)
		}

		solve(a, m)
	}

	writer.Flush()
}

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

// IntMax returns maximum of received int numbers
func IntMax(nums ...int64) int64 {
	cand := nums[0]
	for _, num := range nums {
		if cand < num {
			cand = num
		}
	}
	return cand
}

// IntMin returns minimum of received int numbers
func IntMin(nums ...int64) int64 {
	cand := nums[0]
	for _, num := range nums {
		if cand > num {
			cand = num
		}
	}
	return cand
}

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

func scan(a ...interface{})             { fmt.Fscan(reader, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }

func RedirectInputToFile(r *bufio.Reader, fp string) {
	f, err := os.Open(fp)
	if err != nil {
		panic(err)
	}
	r.Reset(f)
}

package main

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

type app struct {
	w, p int64
}

func solve(apps []app, m int64) {
	sort.Slice(apps, func(i, j int) bool {
		if apps[i].p == apps[j].p {
			return apps[i].w > apps[j].w
		}
		return apps[i].p < apps[j].p
	})

	last := -1
	for i := 0; i < len(apps); i++ {
		if apps[i].p == 1 {
			last = i
		}
	}

	i, j := 0, last+1
	res := int64(0)
	for i+1 <= last && j < len(apps) {
		if m <= 0 {
			break
		}

		if apps[i].w+apps[i+1].w >= apps[j].w {
			m -= apps[i].w
			i++
			res++
		} else {
			m -= apps[j].w
			j++
			res += 2
		}
	}

	if m <= 0 {
		printf("%d\n", res)
		return
	}

	for ; j < len(apps) && m > 0; j++ {
		if m <= apps[i].w {
			printf("%d\n", res+1)
			return
		}
		m -= apps[j].w
		res += 2
	}

	if m <= 0 {
		printf("%d\n", res)
	} else if m <= apps[i].w {
		printf("%d\n", res+1)
	} else {
		printf("-1\n")
	}
}

func main() {
	//RedirectInputToFile(reader, "input.txt")

	t := 1
	scan(&t)

	for T := 0; T < t; T++ {
		var n, m int64
		scan(&n)
		scan(&m)

		a := make([]app, n)
		for i := range a {
			scan(&a[i].w)
		}
		for i := range a {
			scan(&a[i].p)
		}

		solve(a, m)
	}

	writer.Flush()
}

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

// IntMax returns maximum of received int numbers
func IntMax(nums ...int64) int64 {
	cand := nums[0]
	for _, num := range nums {
		if cand < num {
			cand = num
		}
	}
	return cand
}

// IntMin returns minimum of received int numbers
func IntMin(nums ...int64) int64 {
	cand := nums[0]
	for _, num := range nums {
		if cand > num {
			cand = num
		}
	}
	return cand
}

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

func scan(a ...interface{})             { fmt.Fscan(reader, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }

func RedirectInputToFile(r *bufio.Reader, fp string) {
	f, err := os.Open(fp)
	if err != nil {
		panic(err)
	}
	r.Reset(f)
}

package main

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

type app struct {
	w, p int64
}

func solve(apps []app, m int64) {
	sort.Slice(apps, func(i, j int) bool {
		if apps[i].p == apps[j].p {
			return apps[i].w > apps[j].w
		}
		return apps[i].p < apps[j].p
	})

	last := -1
	for i := 0; i < len(apps); i++ {
		if apps[i].p == 1 {
			last = i
		}
	}

	i, j := 0, last+1
	res := int64(0)
	for i+1 <= last && j < len(apps) {
		if m <= 0 {
			break
		}

		if apps[i].w+apps[i+1].w >= apps[j].w {
			m -= apps[i].w
			i++
			res++
		} else {
			m -= apps[j].w
			j++
			res += 2
		}
	}

	if m <= 0 {
		printf("%d\n", res)
		return
	}

	if i+1 <= last && j < len(apps) {
		panic("can't be")
	}

	for ; m > 0 && j < len(apps); j++ {
		if m-apps[i].w <= 0 {
			m -= apps[i].w
			res++
			i++
			break
		}
		m -= apps[j].w
		res += 2
	}

	if m > 0 && i == last {
		m -= apps[i].w
		res++
	}

	ans := int64(-1)
	if m <= 0 {
		ans = res
	}
	printf("%d\n", ans)
}

func main() {
	//RedirectInputToFile(reader, "input.txt")

	t := 1
	scan(&t)

	for T := 0; T < t; T++ {
		var n, m int64
		scan(&n)
		scan(&m)

		a := make([]app, n)
		for i := range a {
			scan(&a[i].w)
		}
		for i := range a {
			scan(&a[i].p)
		}

		solve(a, m)
	}

	writer.Flush()
}

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

// IntMax returns maximum of received int numbers
func IntMax(nums ...int64) int64 {
	cand := nums[0]
	for _, num := range nums {
		if cand < num {
			cand = num
		}
	}
	return cand
}

// IntMin returns minimum of received int numbers
func IntMin(nums ...int64) int64 {
	cand := nums[0]
	for _, num := range nums {
		if cand > num {
			cand = num
		}
	}
	return cand
}

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

func scan(a ...interface{})             { fmt.Fscan(reader, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }

func RedirectInputToFile(r *bufio.Reader, fp string) {
	f, err := os.Open(fp)
	if err != nil {
		panic(err)
	}
	r.Reset(f)
}

package main

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

type app struct {
	w, p int64
}

func solve(apps []app, m int64) {
	sort.Slice(apps, func(i, j int) bool {
		if apps[i].p == apps[j].p {
			return apps[i].w > apps[j].w
		}
		return apps[i].p < apps[j].p
	})

	last := -1
	for i := 0; i < len(apps); i++ {
		if apps[i].p == 1 {
			last = i
		}
	}

	i, j := 0, last+1
	res := int64(0)
	for i <= last && j < len(apps) {
		if m <= 0 {
			break
		}

		var next int64
		if i+1 <= last {
			next = apps[i+1].w
		}

		if apps[i].w+next >= apps[j].w {
			m -= apps[i].w
			i++
			res++
		} else {
			m -= apps[j].w
			j++
			res += 2
		}
	}

	if m <= 0 {
		printf("%d\n", res)
		return
	}

	if i <= last && j < len(apps) {
		panic("can't be")
	}

	for k := i; k <= last; k++ {
		if m <= 0 {
			break
		}
		m -= apps[k].w
		res++
	}

	for k := j; k < len(apps); k++ {
		if m <= 0 {
			break
		}
		m -= apps[k].w
		res += 2
	}

	ans := int64(-1)
	if m <= 0 {
		ans = res
	}
	printf("%d\n", ans)
}

func main() {
	//RedirectInputToFile(reader, "input.txt")

	t := 1
	scan(&t)

	for T := 0; T < t; T++ {
		var n, m int64
		scan(&n)
		scan(&m)

		a := make([]app, n)
		for i := range a {
			scan(&a[i].w)
		}
		for i := range a {
			scan(&a[i].p)
		}

		solve(a, m)
	}

	writer.Flush()
}

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

// IntMax returns maximum of received int numbers
func IntMax(nums ...int64) int64 {
	cand := nums[0]
	for _, num := range nums {
		if cand < num {
			cand = num
		}
	}
	return cand
}

// IntMin returns minimum of received int numbers
func IntMin(nums ...int64) int64 {
	cand := nums[0]
	for _, num := range nums {
		if cand > num {
			cand = num
		}
	}
	return cand
}

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

func scan(a ...interface{})             { fmt.Fscan(reader, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }

func RedirectInputToFile(r *bufio.Reader, fp string) {
	f, err := os.Open(fp)
	if err != nil {
		panic(err)
	}
	r.Reset(f)
}

package main

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

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

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

	n, m int
	a, b [200000]int
	l [2]int
	ab [2][200000]int
	dp [400001]q
	res int
)

type q struct {
	l1, l2, s int
}

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() {
	l[0] = 0
	l[1] = 0
	res = -1
}

func input() {
	n = getInt()
	m = getInt()
	for i := 0; i < n; i++ {
		a[i] = getInt()
	}
	for i := 0; i < n; i++ {
		b[i] = getInt() - 1
		ab[b[i]][l[b[i]]] = a[i]
		l[b[i]]++
	}
}

func count() {
	for k := 0; k < 2; k++ {
		sort.Slice(ab[k][:l[k]], func(i, j int) bool {
			return ab[k][i] > ab[k][j]
		})
	}
	if l[0] > 0 {
		dp[1] = q{1, 0, ab[0][0]}
		if dp[1].s >= m {
			res = 1
			return
		}
	} else {
		dp[1] = q{0, 0, 0}
	}
	for i := 2; i <= l[0] + 2 * l[1]; i++ {
		dp[i] = q{0, 0, 0}
		if dp[i-2].l2 < l[1] {
			dp[i] = q{dp[i-2].l1, dp[i-2].l2 + 1, dp[i-2].s + ab[1][dp[i-2].l2]}
		}
		if dp[i-1].l1 < l[0] && (dp[i-1].s + ab[0][dp[i-1].l1] > dp[i].s) {
			dp[i] = q{dp[i-1].l1 + 1, dp[i-1].l2, dp[i-1].s + ab[0][dp[i-1].l1]}
		}
		if dp[i].s >= m {
			res = i
			return
		}
	}
	printIt()
}

func printIt() {
	fmt.Println(ab[0][:l[0]])
	fmt.Println(ab[1][:l[1]])
}

func output() {
	out.Write(intToBytes(res))
	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
}


Little Petya learns how to write. The teacher gave pupils the task to write the letter A on the sheet of paper. It is required to check whether Petya really had written the letter A.You are given three segments on the plane. They form the letter A if the following conditions hold:  Two segments have common endpoint (lets call these segments first and second), while the third segment connects two points on the different segments.  The angle between the first and the second segments is greater than 0 and do not exceed 90 degrees.  The third segment divides each of the first two segments in proportion not less than 1 / 4 (i.e. the ratio of the length of the shortest part to the length of the longest part is not less than 1 / 4).

The first line contains one integer t (1 ≤ t ≤ 10000) — the number of test cases to solve. Each case consists of three lines. Each of these three lines contains four space-separated integers — coordinates of the endpoints of one of the segments. All coordinates do not exceed 108 by absolute value. All segments have positive length.

Output one line for each test case. Print «YES» (without quotes), if the segments form the letter A and «NO» otherwise.

package main

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

var scanner *bufio.Scanner

func getI64() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}
func getI() int {
  return int(getI64())
}

type Point struct {
  x, y int64
}
func (p Point) String() string {
  return fmt.Sprintf("(%d, %d)", p.x, p.y)
}
type Segment struct {
  p, q Point
}
func (s Segment) String() string {
  return fmt.Sprintf("[%s, %s]", s.p, s.q)
}

func ok(p Point, s Segment) bool {
  if p == s.p || p == s.q {
    return false  // p lies on an endpoint of s
  }
  if (p.x < s.p.x && p.x < s.q.x) || (p.x > s.p.x && p.x > s.q.x) {
    return false  // p lies outside s
  }
  if (p.y < s.p.y && p.y < s.q.y) || (p.y > s.p.y && p.y > s.q.y) {
    return false  // p lies outside s
  }
  x0, y0 := s.q.x - s.p.x, s.q.y - s.p.y
  x1, y1 := p.x - s.p.x, p.y - s.p.y
  x2, y2 := s.q.x - p.x, s.q.y - p.y
  if y0*x1 != x0*y1 {
    return false  // p does not fall on s
  }
  dd1 := x1*x1 + y1*y1
  dd2 := x2*x2 + y2*y2
  return (dd1 <= dd2 && 16*dd1 >= dd2) || (dd2 <= dd1 && 16*dd2 >= dd1)
}

func goodAngle(p, a, b Point) bool {
  ax, ay := a.x - p.x, a.y - p.y
  bx, by := b.x - p.x, b.y - p.y
  return ax*bx + ay*by >= 0
}

func isA(a, b, c Segment) bool {
  okay := false
  w, x, y, z := a.p, a.q, b.p, b.q
  if (w == y && goodAngle(w, x, z)) || (w == z && goodAngle(w, x, y)) {
    okay = true
  }
  if (x == y && goodAngle(x, w, z)) || (x == z && goodAngle(x, w, y)) {
    okay = true
  }
  return okay && ((ok(c.p, a) && ok(c.q, b)) || (ok(c.p, b) && ok(c.q, a)))
}

func main() {
  scanner = bufio.NewScanner(os.Stdin)
  scanner.Split(bufio.ScanWords)
  writer := bufio.NewWriter(os.Stdout)
  defer writer.Flush()

  t := getI()
  for ti := 0; ti < t; ti++ {
    a := Segment{ Point{ getI64(), getI64() }, Point{ getI64(), getI64() } }
    b := Segment{ Point{ getI64(), getI64() }, Point{ getI64(), getI64() } }
    c := Segment{ Point{ getI64(), getI64() }, Point{ getI64(), getI64() } }
    if isA(a, b, c) || isA(a, c, b) || isA(b, c, a) {
      writer.WriteString("YES\n")
    } else {
      writer.WriteString("NO\n")
    }
  }
}

package main

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

var scanner *bufio.Scanner

func getI64() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}
func getI() int {
  return int(getI64())
}

type Point struct {
  x, y int64
}
func (p Point) String() string {
  return fmt.Sprintf("(%d, %d)", p.x, p.y)
}
type Segment struct {
  p, q Point
}
func (s Segment) String() string {
  return fmt.Sprintf("[%s, %s]", s.p, s.q)
}

func ok(p Point, s Segment) bool {
  if p == s.p || p == s.q {
    return false  // p lies on an endpoint of s
  }
  if (p.x < s.p.x && p.x < s.q.x) || (p.x > s.p.x && p.x > s.q.x) {
    return false  // p lies outside s
  }
  if (p.y < s.p.y && p.y < s.q.y) || (p.y > s.p.y && p.y > s.q.y) {
    return false  // p lies outside s
  }
  x0, y0 := s.q.x - s.p.x, s.q.y - s.p.y
  x1, y1 := p.x - s.p.x, p.y - s.p.y
  x2, y2 := s.q.x - p.x, s.q.y - p.y
  if y0*x1 != x0*y1 {
    return false  // p does not fall on s
  }
  fmt.Fprintf(os.Stderr, "%s falls on %s\n", p, s)
  dd1 := x1*x1 + y1*y1
  dd2 := x2*x2 + y2*y2
  return (dd1 <= dd2 && 16*dd1 >= dd2) || (dd2 <= dd1 && 16*dd2 >= dd1)
}

func isA(a, b, c Segment) bool {
  okay := false
  w, x, y, z := a.p, a.q, b.p, b.q
  if w == y && goodAngle(w, x, z) {
    okay = true
  }
  if w == z && goodAngle(w, x, y) {
    okay = true
  }
  if x == y && goodAngle(x, w, z) {
    okay = true
  }
  if x == z && goodAngle(x, w, y) {
    okay = true
  }
  if !okay {
    return false
  }
  return (ok(c.p, a) && ok(c.q, b)) || (ok(c.p, b) && ok(c.q, a))
}

func goodAngle(p, a, b Point) bool {
  ax, ay := a.x - p.x, a.y - p.y
  bx, by := b.x - p.x, b.y - p.y
  return (ax*bx + ay*by) >= 0
  dotProduct := float64(ax*bx + ay*by)
  lengthA := math.Sqrt(float64(ax*ax + ay*ay))
  lengthB := math.Sqrt(float64(bx*bx + by*by))
  angle := math.Acos(dotProduct/lengthA/lengthB)
  if angle < 0 {
    angle += 2*math.Pi
  }
  if angle > math.Pi {
    angle -= math.Pi
  }
  return angle > 0 && angle <= math.Pi/2
}

func main() {
  scanner = bufio.NewScanner(os.Stdin)
  scanner.Split(bufio.ScanWords)
  writer := bufio.NewWriter(os.Stdout)
  defer writer.Flush()

  t := getI()
  for ti := 0; ti < t; ti++ {
    a := Segment{ Point{ getI64(), getI64() }, Point{ getI64(), getI64() } }
    b := Segment{ Point{ getI64(), getI64() }, Point{ getI64(), getI64() } }
    c := Segment{ Point{ getI64(), getI64() }, Point{ getI64(), getI64() } }
    if isA(a, b, c) || isA(a, c, b) || isA(b, c, a) {
      writer.WriteString("YES\n")
    } else {
      writer.WriteString("NO\n")
    }
  }
}

package main

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

var scanner *bufio.Scanner

func getI64() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}
func getI() int {
  return int(getI64())
}
func getF() float64 {
  scanner.Scan()
  x, _ := strconv.ParseFloat(scanner.Text(), 64)
  return x
}
func getS() string {
  scanner.Scan()
  return scanner.Text()
}

type Point struct {
  x, y int64
}
func (p Point) String() string {
  return fmt.Sprintf("(%d, %d)", p.x, p.y)
}
type Segment struct {
  p, q Point
}
func (s Segment) String() string {
  return fmt.Sprintf("[%s, %s]", s.p, s.q)
}

func ok(p Point, s Segment) bool {
  if p == s.p || p == s.q {
    fmt.Fprintf(os.Stderr, "%s lies on an endpoint of %s\n", p, s)
    return false
  }
  if (p.x < s.p.x && p.x < s.q.x) || (p.x > s.p.x && p.x > s.q.x) {
    fmt.Fprintf(os.Stderr, "%s lies outside %s\n", p, s)
    return false
  }
  if (p.y < s.p.y && p.y < s.q.y) || (p.y > s.p.y && p.y > s.q.y) {
    fmt.Fprintf(os.Stderr, "%s lies outside %s\n", p, s)
    return false
  }
  x0, y0 := s.q.x - s.p.x, s.q.y - s.p.y
  x1, y1 := p.x - s.p.x, p.y - s.p.y
  x2, y2 := s.q.x - p.x, s.q.y - p.y
  if y0*x1 != x0*y1 {
    fmt.Fprintf(os.Stderr, "%s does not fall on %s\n", p, s)
    return false
  }
  fmt.Fprintf(os.Stderr, "%s falls on %s\n", p, s)
  dd1 := x1*x1 + y1*y1
  dd2 := x2*x2 + y2*y2
  fmt.Fprintf(os.Stderr, "dd1 = %d, dd2 = %d\n", dd1, dd2)
  return (dd1 <= dd2 && 16*dd1 >= dd2) || (dd2 <= dd1 && 16*dd2 >= dd1)
}

func isA(a, b, c Segment) bool {
  okay := false
  w, x, y, z := a.p, a.q, b.p, b.q
  if w == y && goodAngle(w, x, z) {
    okay = true
  }
  if w == z && goodAngle(w, x, y) {
    okay = true
  }
  if x == y && goodAngle(x, w, z) {
    okay = true
  }
  if x == z && goodAngle(x, w, y) {
    okay = true
  }
  if !okay {
    return false
  }
  fmt.Fprintf(os.Stderr, "%s meets %s at a good angle\n", a, b)
  return (ok(c.p, a) && ok(c.q, b)) || (ok(c.p, b) && ok(c.q, a))
}

func goodAngle(p, a, b Point) bool {
  ax, ay := a.x - p.x, a.y - p.y
  bx, by := b.x - p.x, b.y - p.y
  dotProduct := float64(ax*bx + ay*by)
  lengthA := math.Sqrt(float64(ax*ax + ay*ay))
  lengthB := math.Sqrt(float64(bx*bx + by*by))
  angle := math.Acos(dotProduct/lengthA/lengthB)
  if angle < 0 {
    angle += 2*math.Pi
  }
  if angle > math.Pi {
    angle -= math.Pi
  }
  fmt.Fprintf(os.Stderr, "angle = %.6f\n", angle)
  return angle > 0 && angle <= math.Pi/2
}

func main() {
  scanner = bufio.NewScanner(os.Stdin)
  scanner.Split(bufio.ScanWords)
  writer := bufio.NewWriter(os.Stdout)
  defer writer.Flush()

  t := getI()
  for ti := 0; ti < t; ti++ {
    a := Segment{ Point{ getI64(), getI64() }, Point{ getI64(), getI64() } }
    b := Segment{ Point{ getI64(), getI64() }, Point{ getI64(), getI64() } }
    c := Segment{ Point{ getI64(), getI64() }, Point{ getI64(), getI64() } }
    if isA(a, b, c) || isA(a, c, b) || isA(b, c, a) {
      fmt.Fprintf(os.Stderr, "yes\n\n")
      writer.WriteString("YES\n")
    } else {
      fmt.Fprintf(os.Stderr, "no\n\n")
      writer.WriteString("NO\n")
    }
  }
}

package main

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

var scanner *bufio.Scanner

func getI64() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}
func getI() int {
  return int(getI64())
}

type Point struct {
  x, y int64
}
func (p Point) String() string {
  return fmt.Sprintf("(%d, %d)", p.x, p.y)
}
type Segment struct {
  p, q Point
}
func (s Segment) String() string {
  return fmt.Sprintf("[%s, %s]", s.p, s.q)
}

func ok(p Point, s Segment) bool {
  if p == s.p || p == s.q {
    return false  // p lies on an endpoint of s
  }
  if (p.x < s.p.x && p.x < s.q.x) || (p.x > s.p.x && p.x > s.q.x) {
    return false  // p lies outside s
  }
  if (p.y < s.p.y && p.y < s.q.y) || (p.y > s.p.y && p.y > s.q.y) {
    return false  // p lies outside s
  }
  x0, y0 := s.q.x - s.p.x, s.q.y - s.p.y
  x1, y1 := p.x - s.p.x, p.y - s.p.y
  x2, y2 := s.q.x - p.x, s.q.y - p.y
  if y0*x1 != x0*y1 {
    return false  // p does not fall on s
  }
  dd1 := x1*x1 + y1*y1
  dd2 := x2*x2 + y2*y2
  return (dd1 <= dd2 && 16*dd1 >= dd2) || (dd2 <= dd1 && 16*dd2 >= dd1)
}

func goodAngle(p, a, b Point) bool {
  ax, ay := a.x - p.x, a.y - p.y
  bx, by := b.x - p.x, b.y - p.y
  return ax*by != bx*ay && ax*bx + ay*by >= 0
}

func isA(a, b, c Segment) bool {
  okay := false
  w, x, y, z := a.p, a.q, b.p, b.q
  if (w == y && goodAngle(w, x, z)) || (w == z && goodAngle(w, x, y)) {
    okay = true
  }
  if (x == y && goodAngle(x, w, z)) || (x == z && goodAngle(x, w, y)) {
    okay = true
  }
  return okay && ((ok(c.p, a) && ok(c.q, b)) || (ok(c.p, b) && ok(c.q, a)))
}

func main() {
  scanner = bufio.NewScanner(os.Stdin)
  scanner.Split(bufio.ScanWords)
  writer := bufio.NewWriter(os.Stdout)
  defer writer.Flush()

  t := getI()
  for ti := 0; ti < t; ti++ {
    a := Segment{ Point{ getI64(), getI64() }, Point{ getI64(), getI64() } }
    b := Segment{ Point{ getI64(), getI64() }, Point{ getI64(), getI64() } }
    c := Segment{ Point{ getI64(), getI64() }, Point{ getI64(), getI64() } }
    if isA(a, b, c) || isA(a, c, b) || isA(b, c, a) {
      writer.WriteString("YES\n")
    } else {
      writer.WriteString("NO\n")
    }
  }
}

package main

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

var scanner *bufio.Scanner

func getI64() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}
func getI() int {
  return int(getI64())
}

type Point struct {
  x, y int64
}
func (p Point) String() string {
  return fmt.Sprintf("(%d, %d)", p.x, p.y)
}
type Segment struct {
  p, q Point
}
func (s Segment) String() string {
  return fmt.Sprintf("[%s, %s]", s.p, s.q)
}

func ok(p Point, s Segment) bool {
  if p == s.p || p == s.q {
    return false  // p lies on an endpoint of s
  }
  if (p.x < s.p.x && p.x < s.q.x) || (p.x > s.p.x && p.x > s.q.x) {
    return false  // p lies outside s
  }
  if (p.y < s.p.y && p.y < s.q.y) || (p.y > s.p.y && p.y > s.q.y) {
    return false  // p lies outside s
  }
  x0, y0 := s.q.x - s.p.x, s.q.y - s.p.y
  x1, y1 := p.x - s.p.x, p.y - s.p.y
  x2, y2 := s.q.x - p.x, s.q.y - p.y
  if y0*x1 != x0*y1 {
    return false  // p does not fall on s
  }
  fmt.Fprintf(os.Stderr, "%s falls on %s\n", p, s)
  dd1 := x1*x1 + y1*y1
  dd2 := x2*x2 + y2*y2
  return (dd1 <= dd2 && 16*dd1 >= dd2) || (dd2 <= dd1 && 16*dd2 >= dd1)
}

func isA(a, b, c Segment) bool {
  okay := false
  w, x, y, z := a.p, a.q, b.p, b.q
  if w == y && goodAngle(w, x, z) {
    okay = true
  }
  if w == z && goodAngle(w, x, y) {
    okay = true
  }
  if x == y && goodAngle(x, w, z) {
    okay = true
  }
  if x == z && goodAngle(x, w, y) {
    okay = true
  }
  if !okay {
    return false
  }
  return (ok(c.p, a) && ok(c.q, b)) || (ok(c.p, b) && ok(c.q, a))
}

func goodAngle(p, a, b Point) bool {
  ax, ay := a.x - p.x, a.y - p.y
  bx, by := b.x - p.x, b.y - p.y
  dotProduct := float64(ax*bx + ay*by)
  lengthA := math.Sqrt(float64(ax*ax + ay*ay))
  lengthB := math.Sqrt(float64(bx*bx + by*by))
  angle := math.Acos(dotProduct/lengthA/lengthB)
  if angle < 0 {
    angle += 2*math.Pi
  }
  if angle > math.Pi {
    angle -= math.Pi
  }
  return angle > 0 && angle <= math.Pi/2
}

func main() {
  scanner = bufio.NewScanner(os.Stdin)
  scanner.Split(bufio.ScanWords)
  writer := bufio.NewWriter(os.Stdout)
  defer writer.Flush()

  t := getI()
  for ti := 0; ti < t; ti++ {
    a := Segment{ Point{ getI64(), getI64() }, Point{ getI64(), getI64() } }
    b := Segment{ Point{ getI64(), getI64() }, Point{ getI64(), getI64() } }
    c := Segment{ Point{ getI64(), getI64() }, Point{ getI64(), getI64() } }
    if isA(a, b, c) || isA(a, c, b) || isA(b, c, a) {
      writer.WriteString("YES\n")
    } else {
      writer.WriteString("NO\n")
    }
  }
}

package main

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

var scanner *bufio.Scanner

func getI64() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}
func getI() int {
  return int(getI64())
}
func getF() float64 {
  scanner.Scan()
  x, _ := strconv.ParseFloat(scanner.Text(), 64)
  return x
}
func getS() string {
  scanner.Scan()
  return scanner.Text()
}

type Point struct {
  x, y int64
}
func (p Point) String() string {
  return fmt.Sprintf("(%d, %d)", p.x, p.y)
}
type Segment struct {
  p, q Point
}
func (s Segment) String() string {
  return fmt.Sprintf("[%s, %s]", s.p, s.q)
}

func ok(p Point, s Segment) bool {
  if p == s.p || p == s.q {
    //fmt.Fprintf(os.Stderr, "%s lies on an endpoint of %s\n", p, s)
    return false
  }
  if (p.x < s.p.x && p.x < s.q.x) || (p.x > s.p.x && p.x > s.q.x) {
    //fmt.Fprintf(os.Stderr, "%s lies outside %s\n", p, s)
    return false
  }
  if (p.y < s.p.y && p.y < s.q.y) || (p.y > s.p.y && p.y > s.q.y) {
    //fmt.Fprintf(os.Stderr, "%s lies outside %s\n", p, s)
    return false
  }
  x0, y0 := s.q.x - s.p.x, s.q.y - s.p.y
  x1, y1 := p.x - s.p.x, p.y - s.p.y
  x2, y2 := s.q.x - p.x, s.q.y - p.y
  if y0*x1 != x0*y1 {
    //fmt.Fprintf(os.Stderr, "%s does not fall on %s\n", p, s)
    return false
  }
  //fmt.Fprintf(os.Stderr, "%s falls on %s\n", p, s)
  dd1 := x1*x1 + y1*y1
  dd2 := x2*x2 + y2*y2
  //fmt.Fprintf(os.Stderr, "dd1 = %d, dd2 = %d\n", dd1, dd2)
  return (dd1 <= dd2 && 16*dd1 >= dd2) || (dd2 <= dd1 && 16*dd2 >= dd1)
}

func isA(a, b, c Segment) bool {
  if a.p != b.p && a.p != b.q && a.q != b.p && a.q != b.q {
    return false
  }
  //fmt.Fprintf(os.Stderr, "%s meets %s\n", a, b)
  return (ok(c.p, a) && ok(c.q, b)) || (ok(c.p, b) && ok(c.q, a))
}

func main() {
  scanner = bufio.NewScanner(os.Stdin)
  scanner.Split(bufio.ScanWords)
  writer := bufio.NewWriter(os.Stdout)
  defer writer.Flush()

  t := getI()
  for ti := 0; ti < t; ti++ {
    a := Segment{ Point{ getI64(), getI64() }, Point{ getI64(), getI64() } }
    b := Segment{ Point{ getI64(), getI64() }, Point{ getI64(), getI64() } }
    c := Segment{ Point{ getI64(), getI64() }, Point{ getI64(), getI64() } }
    if isA(a, b, c) || isA(a, c, b) || isA(b, c, a) {
      //fmt.Fprintf(os.Stderr, "yes\n\n")
      if t == 10000 {
        writer.WriteString(fmt.Sprintf("YES %s %s %s\n", a, b, c))
      } else {
        writer.WriteString("YES\n")
      }
    } else {
      //fmt.Fprintf(os.Stderr, "no\n\n")
      if t == 10000 {
        writer.WriteString(fmt.Sprintf("NO %s %s %s\n", a, b, c))
      } else {
        writer.WriteString("NO\n")
      }
    }
  }
}

package main

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

var scanner *bufio.Scanner

func getI64() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}
func getI() int {
  return int(getI64())
}
func getF() float64 {
  scanner.Scan()
  x, _ := strconv.ParseFloat(scanner.Text(), 64)
  return x
}
func getS() string {
  scanner.Scan()
  return scanner.Text()
}

type Point struct {
  x, y int64
}
func (p Point) String() string {
  return fmt.Sprintf("(%d, %d)", p.x, p.y)
}
type Segment struct {
  p, q Point
}
func (s Segment) String() string {
  return fmt.Sprintf("[%s, %s]", s.p, s.q)
}

func ok(p Point, s Segment) bool {
  if p == s.p || p == s.q {
    //fmt.Fprintf(os.Stderr, "%s lies on an endpoint of %s\n", p, s)
    return false
  }
  if (p.x < s.p.x && p.x < s.q.x) || (p.x > s.p.x && p.x > s.q.x) {
    //fmt.Fprintf(os.Stderr, "%s lies outside %s\n", p, s)
    return false
  }
  if (p.y < s.p.y && p.y < s.q.y) || (p.y > s.p.y && p.y > s.q.y) {
    //fmt.Fprintf(os.Stderr, "%s lies outside %s\n", p, s)
    return false
  }
  x0, y0 := s.q.x - s.p.x, s.q.y - s.p.y
  x1, y1 := p.x - s.p.x, p.y - s.p.y
  x2, y2 := s.q.x - p.x, s.q.y - p.y
  if y0*x1 != x0*y1 {
    //fmt.Fprintf(os.Stderr, "%s does not fall on %s\n", p, s)
    return false
  }
  //fmt.Fprintf(os.Stderr, "%s falls on %s\n", p, s)
  dd1 := x1*x1 + y1*y1
  dd2 := x2*x2 + y2*y2
  //fmt.Fprintf(os.Stderr, "dd1 = %d, dd2 = %d\n", dd1, dd2)
  return 16*dd1 >= dd2 && 16*dd2 >= dd1
}

func isA(a, b, c Segment) bool {
  if a.p != b.p && a.p != b.q && a.q != b.p && a.q != b.q {
    return false
  }
  //fmt.Fprintf(os.Stderr, "%s meets %s\n", a, b)
  return (ok(c.p, a) && ok(c.q, b)) || (ok(c.p, b) && ok(c.q, a))
}

func main() {
  scanner = bufio.NewScanner(os.Stdin)
  scanner.Split(bufio.ScanWords)
  writer := bufio.NewWriter(os.Stdout)
  defer writer.Flush()

  t := getI()
  for ti := 0; ti < t; ti++ {
    a := Segment{ Point{ getI64(), getI64() }, Point{ getI64(), getI64() } }
    b := Segment{ Point{ getI64(), getI64() }, Point{ getI64(), getI64() } }
    c := Segment{ Point{ getI64(), getI64() }, Point{ getI64(), getI64() } }
    if isA(a, b, c) || isA(a, c, b) || isA(b, c, a) {
      //fmt.Fprintf(os.Stderr, "yes\n\n")
      writer.WriteString("YES\n")
    } else {
      //fmt.Fprintf(os.Stderr, "no\n\n")
      writer.WriteString("NO\n")
    }
  }
}

package main

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

var scanner *bufio.Scanner

func getI64() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}
func getI() int {
  return int(getI64())
}

type Point struct {
  x, y int64
}
func (p Point) String() string {
  return fmt.Sprintf("(%d, %d)", p.x, p.y)
}
type Segment struct {
  p, q Point
}
func (s Segment) String() string {
  return fmt.Sprintf("[%s, %s]", s.p, s.q)
}

func ok(p Point, s Segment) bool {
  if p == s.p || p == s.q {
    return false  // p lies on an endpoint of s
  }
  if (p.x < s.p.x && p.x < s.q.x) || (p.x > s.p.x && p.x > s.q.x) {
    return false  // p lies outside s
  }
  if (p.y < s.p.y && p.y < s.q.y) || (p.y > s.p.y && p.y > s.q.y) {
    return false  // p lies outside s
  }
  x0, y0 := s.q.x - s.p.x, s.q.y - s.p.y
  x1, y1 := p.x - s.p.x, p.y - s.p.y
  x2, y2 := s.q.x - p.x, s.q.y - p.y
  if y0*x1 != x0*y1 {
    return false  // p does not fall on s
  }
  fmt.Fprintf(os.Stderr, "%s falls on %s\n", p, s)
  dd1 := x1*x1 + y1*y1
  dd2 := x2*x2 + y2*y2
  return (dd1 <= dd2 && 16*dd1 >= dd2) || (dd2 <= dd1 && 16*dd2 >= dd1)
}

func isA(a, b, c Segment) bool {
  okay := false
  w, x, y, z := a.p, a.q, b.p, b.q
  if w == y && goodAngle(w, x, z) {
    okay = true
  }
  if w == z && goodAngle(w, x, y) {
    okay = true
  }
  if x == y && goodAngle(x, w, z) {
    okay = true
  }
  if x == z && goodAngle(x, w, y) {
    okay = true
  }
  if !okay {
    return false
  }
  return (ok(c.p, a) && ok(c.q, b)) || (ok(c.p, b) && ok(c.q, a))
}

func goodAngle(p, a, b Point) bool {
  ax, ay := a.x - p.x, a.y - p.y
  bx, by := b.x - p.x, b.y - p.y
  return (ax*bx + ay*by) > 0
  dotProduct := float64(ax*bx + ay*by)
  lengthA := math.Sqrt(float64(ax*ax + ay*ay))
  lengthB := math.Sqrt(float64(bx*bx + by*by))
  angle := math.Acos(dotProduct/lengthA/lengthB)
  if angle < 0 {
    angle += 2*math.Pi
  }
  if angle > math.Pi {
    angle -= math.Pi
  }
  return angle > 0 && angle <= math.Pi/2
}

func main() {
  scanner = bufio.NewScanner(os.Stdin)
  scanner.Split(bufio.ScanWords)
  writer := bufio.NewWriter(os.Stdout)
  defer writer.Flush()

  t := getI()
  for ti := 0; ti < t; ti++ {
    a := Segment{ Point{ getI64(), getI64() }, Point{ getI64(), getI64() } }
    b := Segment{ Point{ getI64(), getI64() }, Point{ getI64(), getI64() } }
    c := Segment{ Point{ getI64(), getI64() }, Point{ getI64(), getI64() } }
    if isA(a, b, c) || isA(a, c, b) || isA(b, c, a) {
      writer.WriteString("YES\n")
    } else {
      writer.WriteString("NO\n")
    }
  }
}

package main

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

var scanner *bufio.Scanner

func getI64() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}
func getI() int {
  return int(getI64())
}

type Point struct {
  x, y int64
}
func (p Point) String() string {
  return fmt.Sprintf("(%d, %d)", p.x, p.y)
}
type Segment struct {
  p, q Point
}
func (s Segment) String() string {
  return fmt.Sprintf("[%s, %s]", s.p, s.q)
}

func ok(p Point, s Segment) bool {
  if p == s.p || p == s.q {
    return false  // p lies on an endpoint of s
  }
  if (p.x < s.p.x && p.x < s.q.x) || (p.x > s.p.x && p.x > s.q.x) {
    return false  // p lies outside s
  }
  if (p.y < s.p.y && p.y < s.q.y) || (p.y > s.p.y && p.y > s.q.y) {
    return false  // p lies outside s
  }
  x0, y0 := s.q.x - s.p.x, s.q.y - s.p.y
  x1, y1 := p.x - s.p.x, p.y - s.p.y
  x2, y2 := s.q.x - p.x, s.q.y - p.y
  if y0*x1 != x0*y1 {
    return false  // p does not fall on s
  }
  dd1 := x1*x1 + y1*y1
  dd2 := x2*x2 + y2*y2
  return (dd1 <= dd2 && 16*dd1 >= dd2) || (dd2 <= dd1 && 16*dd2 >= dd1)
}

func isA(a, b, c Segment) bool {
  okay := false
  w, x, y, z := a.p, a.q, b.p, b.q
  if w == y && goodAngle(w, x, z) {
    okay = true
  }
  if w == z && goodAngle(w, x, y) {
    okay = true
  }
  if x == y && goodAngle(x, w, z) {
    okay = true
  }
  if x == z && goodAngle(x, w, y) {
    okay = true
  }
  if !okay {
    return false
  }
  return (ok(c.p, a) && ok(c.q, b)) || (ok(c.p, b) && ok(c.q, a))
}

func goodAngle(p, a, b Point) bool {
  ax, ay := a.x - p.x, a.y - p.y
  bx, by := b.x - p.x, b.y - p.y
  return ax*bx + ay*by > 0
}

func main() {
  scanner = bufio.NewScanner(os.Stdin)
  scanner.Split(bufio.ScanWords)
  writer := bufio.NewWriter(os.Stdout)
  defer writer.Flush()

  t := getI()
  for ti := 0; ti < t; ti++ {
    a := Segment{ Point{ getI64(), getI64() }, Point{ getI64(), getI64() } }
    b := Segment{ Point{ getI64(), getI64() }, Point{ getI64(), getI64() } }
    c := Segment{ Point{ getI64(), getI64() }, Point{ getI64(), getI64() } }
    if isA(a, b, c) || isA(a, c, b) || isA(b, c, a) {
      writer.WriteString("YES\n")
    } else {
      writer.WriteString("NO\n")
    }
  }
}

package main

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

var scanner *bufio.Scanner

func getI64() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}
func getI() int {
  return int(getI64())
}
func getF() float64 {
  scanner.Scan()
  x, _ := strconv.ParseFloat(scanner.Text(), 64)
  return x
}
func getS() string {
  scanner.Scan()
  return scanner.Text()
}

type Point struct {
  x, y int64
}
func (p Point) String() string {
  return fmt.Sprintf("(%d, %d)", p.x, p.y)
}
type Segment struct {
  p, q Point
}
func (s Segment) String() string {
  return fmt.Sprintf("[%s, %s]", s.p, s.q)
}

func ok(p Point, s Segment) bool {
  if p == s.p || p == s.q {
    //fmt.Fprintf(os.Stderr, "%s lies on an endpoint of %s\n", p, s)
    return false
  }
  if (p.x < s.p.x && p.x < s.q.x) || (p.x > s.p.x && p.x > s.q.x) {
    //fmt.Fprintf(os.Stderr, "%s lies outside %s\n", p, s)
    return false
  }
  if (p.y < s.p.y && p.y < s.q.y) || (p.y > s.p.y && p.y > s.q.y) {
    //fmt.Fprintf(os.Stderr, "%s lies outside %s\n", p, s)
    return false
  }
  x0, y0 := s.q.x - s.p.x, s.q.y - s.p.y
  x1, y1 := p.x - s.p.x, p.y - s.p.y
  x2, y2 := s.q.x - p.x, s.q.y - p.y
  if y0*x1 != x0*y1 {
    //fmt.Fprintf(os.Stderr, "%s does not fall on %s\n", p, s)
    return false
  }
  //fmt.Fprintf(os.Stderr, "%s falls on %s\n", p, s)
  dd1 := x1*x1 + y1*y1
  dd2 := x2*x2 + y2*y2
  //fmt.Fprintf(os.Stderr, "dd1 = %d, dd2 = %d\n", dd1, dd2)
  return (dd1 <= dd2 && 16*dd1 >= dd2) || (dd2 <= dd1 && 16*dd2 >= dd1)
}

func isA(a, b, c Segment) bool {
  if a.p != b.p && a.p != b.q && a.q != b.p && a.q != b.q {
    return false
  }
  //fmt.Fprintf(os.Stderr, "%s meets %s\n", a, b)
  return (ok(c.p, a) && ok(c.q, b)) || (ok(c.p, b) && ok(c.q, a))
}

func main() {
  scanner = bufio.NewScanner(os.Stdin)
  scanner.Split(bufio.ScanWords)
  writer := bufio.NewWriter(os.Stdout)
  defer writer.Flush()

  t := getI()
  for ti := 0; ti < t; ti++ {
    a := Segment{ Point{ getI64(), getI64() }, Point{ getI64(), getI64() } }
    b := Segment{ Point{ getI64(), getI64() }, Point{ getI64(), getI64() } }
    c := Segment{ Point{ getI64(), getI64() }, Point{ getI64(), getI64() } }
    if isA(a, b, c) || isA(a, c, b) || isA(b, c, a) {
      //fmt.Fprintf(os.Stderr, "yes\n\n")
      writer.WriteString("YES\n")
    } else {
      //fmt.Fprintf(os.Stderr, "no\n\n")
      writer.WriteString("NO\n")
    }
  }
}

package main

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

var scanner *bufio.Scanner

func getI64() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}
func getI() int {
  return int(getI64())
}

type Point struct {
  x, y int64
}
func (p Point) String() string {
  return fmt.Sprintf("(%d, %d)", p.x, p.y)
}
type Segment struct {
  p, q Point
}
func (s Segment) String() string {
  return fmt.Sprintf("[%s, %s]", s.p, s.q)
}

func ok(p Point, s Segment) bool {
  if p == s.p || p == s.q {
    return false  // p lies on an endpoint of s
  }
  if (p.x < s.p.x && p.x < s.q.x) || (p.x > s.p.x && p.x > s.q.x) {
    return false  // p lies outside s
  }
  if (p.y < s.p.y && p.y < s.q.y) || (p.y > s.p.y && p.y > s.q.y) {
    return false  // p lies outside s
  }
  x0, y0 := s.q.x - s.p.x, s.q.y - s.p.y
  x1, y1 := p.x - s.p.x, p.y - s.p.y
  x2, y2 := s.q.x - p.x, s.q.y - p.y
  if y0*x1 != x0*y1 {
    return false  // p does not fall on s
  }
  fmt.Fprintf(os.Stderr, "%s falls on %s\n", p, s)
  dd1 := x1*x1 + y1*y1
  dd2 := x2*x2 + y2*y2
  return (dd1 <= dd2 && 16*dd1 >= dd2) || (dd2 <= dd1 && 16*dd2 >= dd1)
}

func isA(a, b, c Segment) bool {
  okay := false
  w, x, y, z := a.p, a.q, b.p, b.q
  if w == y && goodAngle(w, x, z) {
    okay = true
  }
  if w == z && goodAngle(w, x, y) {
    okay = true
  }
  if x == y && goodAngle(x, w, z) {
    okay = true
  }
  if x == z && goodAngle(x, w, y) {
    okay = true
  }
  if !okay {
    return false
  }
  return (ok(c.p, a) && ok(c.q, b)) || (ok(c.p, b) && ok(c.q, a))
}

func goodAngle(p, a, b Point) bool {
  ax, ay := a.x - p.x, a.y - p.y
  bx, by := b.x - p.x, b.y - p.y
  return ax*bx + ay*by > 0
}

func main() {
  scanner = bufio.NewScanner(os.Stdin)
  scanner.Split(bufio.ScanWords)
  writer := bufio.NewWriter(os.Stdout)
  defer writer.Flush()

  t := getI()
  for ti := 0; ti < t; ti++ {
    a := Segment{ Point{ getI64(), getI64() }, Point{ getI64(), getI64() } }
    b := Segment{ Point{ getI64(), getI64() }, Point{ getI64(), getI64() } }
    c := Segment{ Point{ getI64(), getI64() }, Point{ getI64(), getI64() } }
    if isA(a, b, c) || isA(a, c, b) || isA(b, c, a) {
      writer.WriteString("YES\n")
    } else {
      writer.WriteString("NO\n")
    }
  }
}

package main

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

var scanner *bufio.Scanner

func getI64() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}
func getI() int {
  return int(getI64())
}

type Point struct {
  x, y int64
}
func (p Point) String() string {
  return fmt.Sprintf("(%d, %d)", p.x, p.y)
}
type Segment struct {
  p, q Point
}
func (s Segment) String() string {
  return fmt.Sprintf("[%s, %s]", s.p, s.q)
}

func ok(p Point, s Segment) bool {
  if p == s.p || p == s.q {
    return false  // p lies on an endpoint of s
  }
  if (p.x < s.p.x && p.x < s.q.x) || (p.x > s.p.x && p.x > s.q.x) {
    return false  // p lies outside s
  }
  if (p.y < s.p.y && p.y < s.q.y) || (p.y > s.p.y && p.y > s.q.y) {
    return false  // p lies outside s
  }
  x0, y0 := s.q.x - s.p.x, s.q.y - s.p.y
  x1, y1 := p.x - s.p.x, p.y - s.p.y
  x2, y2 := s.q.x - p.x, s.q.y - p.y
  if y0*x1 != x0*y1 {
    return false  // p does not fall on s
  }
  dd1 := x1*x1 + y1*y1
  dd2 := x2*x2 + y2*y2
  return (dd1 <= dd2 && 16*dd1 >= dd2) || (dd2 <= dd1 && 16*dd2 >= dd1)
}

func goodAngle(p, a, b Point) bool {
  ax, ay := a.x - p.x, a.y - p.y
  bx, by := b.x - p.x, b.y - p.y
  return ax*bx + ay*by >= 0
}

func isA(a, b, c Segment) bool {
  okay := false
  w, x, y, z := a.p, a.q, b.p, b.q
  if (w == y && goodAngle(w, x, z)) || (w == z && goodAngle(w, x, y)) {
    okay = true
  }
  if (x == y && goodAngle(x, w, z)) || (x == z && goodAngle(x, w, y)) {
    okay = true
  }
  return okay && (ok(c.p, a) && ok(c.q, b)) || (ok(c.p, b) && ok(c.q, a))
}

func main() {
  scanner = bufio.NewScanner(os.Stdin)
  scanner.Split(bufio.ScanWords)
  writer := bufio.NewWriter(os.Stdout)
  defer writer.Flush()

  t := getI()
  for ti := 0; ti < t; ti++ {
    a := Segment{ Point{ getI64(), getI64() }, Point{ getI64(), getI64() } }
    b := Segment{ Point{ getI64(), getI64() }, Point{ getI64(), getI64() } }
    c := Segment{ Point{ getI64(), getI64() }, Point{ getI64(), getI64() } }
    if isA(a, b, c) || isA(a, c, b) || isA(b, c, a) {
      writer.WriteString("YES\n")
    } else {
      writer.WriteString("NO\n")
    }
  }
}

package main

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

var scanner *bufio.Scanner

func getI64() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}
func getI() int {
  return int(getI64())
}
func getF() float64 {
  scanner.Scan()
  x, _ := strconv.ParseFloat(scanner.Text(), 64)
  return x
}
func getS() string {
  scanner.Scan()
  return scanner.Text()
}

type Point struct {
  x, y int64
}
func (p Point) String() string {
  return fmt.Sprintf("(%d, %d)", p.x, p.y)
}
type Segment struct {
  p, q Point
}
func (s Segment) String() string {
  return fmt.Sprintf("[%s, %s]", s.p, s.q)
}

func ok(p Point, s Segment) bool {
  if p == s.p || p == s.q {
    fmt.Fprintf(os.Stderr, "%s lies on an endpoint of %s\n", p, s)
    return false
  }
  if (p.x < s.p.x && p.x < s.q.x) || (p.x > s.p.x && p.x > s.q.x) {
    fmt.Fprintf(os.Stderr, "%s lies outside %s\n", p, s)
    return false
  }
  if (p.y < s.p.y && p.y < s.q.y) || (p.y > s.p.y && p.y > s.q.y) {
    fmt.Fprintf(os.Stderr, "%s lies outside %s\n", p, s)
    return false
  }
  x0, y0 := s.q.x - s.p.x, s.q.y - s.p.y
  x1, y1 := p.x - s.p.x, p.y - s.p.y
  x2, y2 := s.q.x - p.x, s.q.y - p.y
  if y0*x1 != x0*y1 {
    fmt.Fprintf(os.Stderr, "%s does not fall on %s\n", p, s)
    return false
  }
  fmt.Fprintf(os.Stderr, "%s falls on %s\n", p, s)
  dd1 := x1*x1 + y1*y1
  dd2 := x2*x2 + y2*y2
  fmt.Fprintf(os.Stderr, "dd1 = %d, dd2 = %d\n", dd1, dd2)
  return (dd1 <= dd2 && 16*dd1 >= dd2) || (dd2 <= dd1 && 16*dd2 >= dd1)
}

func isA(a, b, c Segment) bool {
  okay := false
  w, x, y, z := a.p, a.q, b.p, b.q
  if w == y && goodAngle(w, x, z) {
    okay = true
  }
  if w == z && goodAngle(w, x, y) {
    okay = true
  }
  if x == y && goodAngle(x, w, z) {
    okay = true
  }
  if x == z && goodAngle(x, w, y) {
    okay = true
  }
  if !okay {
    return false
  }
  fmt.Fprintf(os.Stderr, "%s meets %s at a good angle\n", a, b)
  return (ok(c.p, a) && ok(c.q, b)) || (ok(c.p, b) && ok(c.q, a))
}

func goodAngle(p, a, b Point) bool {
  ax, ay := a.x - p.x, a.y - p.y
  bx, by := b.x - p.x, b.y - p.y
  dotProduct := float64(ax*bx + ay*by)
  lengthA := math.Sqrt(float64(ax*ax + ay*ay))
  lengthB := math.Sqrt(float64(bx*bx + by*by))
  angle := math.Acos(dotProduct/lengthA/lengthB)
  if angle < 0 {
    angle += 2*math.Pi
  }
  if angle > math.Pi {
    angle -= math.Pi
  }
  fmt.Fprintf(os.Stderr, "angle = %.6f\n", angle)
  return angle > 0 && angle <= math.Pi/2
}

func main() {
  scanner = bufio.NewScanner(os.Stdin)
  scanner.Split(bufio.ScanWords)
  writer := bufio.NewWriter(os.Stdout)
  defer writer.Flush()

  t := getI()
  for ti := 0; ti < t; ti++ {
    a := Segment{ Point{ getI64(), getI64() }, Point{ getI64(), getI64() } }
    b := Segment{ Point{ getI64(), getI64() }, Point{ getI64(), getI64() } }
    c := Segment{ Point{ getI64(), getI64() }, Point{ getI64(), getI64() } }
    if isA(a, b, c) || isA(a, c, b) || isA(b, c, a) {
      fmt.Fprintf(os.Stderr, "yes\n\n")
      writer.WriteString("YES\n")
    } else {
      fmt.Fprintf(os.Stderr, "no\n\n")
      if t == 10000 {
        writer.WriteString(fmt.Sprintf("NO %s %s %s\n", a, b, c))
      } else {
        writer.WriteString("NO\n")
      }
    }
  }
}

package main

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

var scanner *bufio.Scanner

func getI64() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}
func getI() int {
  return int(getI64())
}
func getF() float64 {
  scanner.Scan()
  x, _ := strconv.ParseFloat(scanner.Text(), 64)
  return x
}
func getS() string {
  scanner.Scan()
  return scanner.Text()
}

type Point struct {
  x, y int
}
func (p Point) String() string {
  return fmt.Sprintf("(%d, %d)", p.x, p.y)
}
type Segment struct {
  p, q Point
}
func (s Segment) String() string {
  return fmt.Sprintf("[%s, %s]", s.p, s.q)
}

func ok(p Point, s Segment) bool {
  if p == s.p || p == s.q {
    //fmt.Fprintf(os.Stderr, "%s lies on an endpoint of %s\n", p, s)
    return false
  }
  if (p.x < s.p.x && p.x < s.q.x) || (p.x > s.p.x && p.x > s.q.x) {
    //fmt.Fprintf(os.Stderr, "%s lies outside %s\n", p, s)
    return false
  }
  if (p.y < s.p.y && p.y < s.q.y) || (p.y > s.p.y && p.y > s.q.y) {
    //fmt.Fprintf(os.Stderr, "%s lies outside %s\n", p, s)
    return false
  }
  x0, y0 := s.q.x - s.p.x, s.q.y - s.p.y
  x1, y1 := p.x - s.p.x, p.y - s.p.y
  x2, y2 := s.q.x - p.x, s.q.y - p.y
  if y0*x1 != x0*y1 {
    //fmt.Fprintf(os.Stderr, "%s does not fall on %s\n", p, s)
    return false
  }
  //fmt.Fprintf(os.Stderr, "%s falls on %s\n", p, s)
  dd1 := x1*x1 + y1*y1
  dd2 := x2*x2 + y2*y2
  //fmt.Fprintf(os.Stderr, "dd1 = %d, dd2 = %d\n", dd1, dd2)
  return 16*dd1 >= dd2 && 16*dd2 >= dd1
}

func isA(a, b, c Segment) bool {
  if a.p != b.p && a.p != b.q && a.q != b.p && a.q != b.q {
    return false
  }
  //fmt.Fprintf(os.Stderr, "%s meets %s\n", a, b)
  return (ok(c.p, a) && ok(c.q, b)) || (ok(c.p, b) && ok(c.q, a))
}

func main() {
  scanner = bufio.NewScanner(os.Stdin)
  scanner.Split(bufio.ScanWords)
  writer := bufio.NewWriter(os.Stdout)
  defer writer.Flush()

  t := getI()
  for ti := 0; ti < t; ti++ {
    a := Segment{ Point{ getI(), getI() }, Point{ getI(), getI() } }
    b := Segment{ Point{ getI(), getI() }, Point{ getI(), getI() } }
    c := Segment{ Point{ getI(), getI() }, Point{ getI(), getI() } }
    if isA(a, b, c) || isA(a, c, b) || isA(b, c, a) {
      //fmt.Fprintf(os.Stderr, "yes\n\n")
      writer.WriteString("YES\n")
    } else {
      //fmt.Fprintf(os.Stderr, "no\n\n")
      writer.WriteString("NO\n")
    }
  }
}

package main

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

var scanner *bufio.Scanner

func getI64() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}
func getI() int {
  return int(getI64())
}
func getF() float64 {
  scanner.Scan()
  x, _ := strconv.ParseFloat(scanner.Text(), 64)
  return x
}
func getS() string {
  scanner.Scan()
  return scanner.Text()
}

type Point struct {
  x, y int64
}
func (p Point) String() string {
  return fmt.Sprintf("(%d, %d)", p.x, p.y)
}
type Segment struct {
  p, q Point
}
func (s Segment) String() string {
  return fmt.Sprintf("[%s, %s]", s.p, s.q)
}

func ok(p Point, s Segment) bool {
  if p == s.p || p == s.q {
    //fmt.Fprintf(os.Stderr, "%s lies on an endpoint of %s\n", p, s)
    return false
  }
  if (p.x < s.p.x && p.x < s.q.x) || (p.x > s.p.x && p.x > s.q.x) {
    //fmt.Fprintf(os.Stderr, "%s lies outside %s\n", p, s)
    return false
  }
  if (p.y < s.p.y && p.y < s.q.y) || (p.y > s.p.y && p.y > s.q.y) {
    //fmt.Fprintf(os.Stderr, "%s lies outside %s\n", p, s)
    return false
  }
  x0, y0 := s.q.x - s.p.x, s.q.y - s.p.y
  x1, y1 := p.x - s.p.x, p.y - s.p.y
  x2, y2 := s.q.x - p.x, s.q.y - p.y
  if y0*x1 != x0*y1 {
    //fmt.Fprintf(os.Stderr, "%s does not fall on %s\n", p, s)
    return false
  }
  //fmt.Fprintf(os.Stderr, "%s falls on %s\n", p, s)
  dd1 := x1*x1 + y1*y1
  dd2 := x2*x2 + y2*y2
  //fmt.Fprintf(os.Stderr, "dd1 = %d, dd2 = %d\n", dd1, dd2)
  return (dd1 <= dd2 && 16*dd1 >= dd2) || (dd2 <= dd1 && 16*dd2 >= dd1)
}

func isA(a, b, c Segment) bool {
  if a.p != b.p && a.p != b.q && a.q != b.p && a.q != b.q {
    return false
  }
  //fmt.Fprintf(os.Stderr, "%s meets %s\n", a, b)
  return (ok(c.p, a) && ok(c.q, b)) || (ok(c.p, b) && ok(c.q, a))
}

func main() {
  scanner = bufio.NewScanner(os.Stdin)
  scanner.Split(bufio.ScanWords)
  writer := bufio.NewWriter(os.Stdout)
  defer writer.Flush()

  t := getI()
  for ti := 0; ti < t; ti++ {
    a := Segment{ Point{ getI64(), getI64() }, Point{ getI64(), getI64() } }
    b := Segment{ Point{ getI64(), getI64() }, Point{ getI64(), getI64() } }
    c := Segment{ Point{ getI64(), getI64() }, Point{ getI64(), getI64() } }
    if isA(a, b, c) || isA(a, c, b) || isA(b, c, a) {
      //fmt.Fprintf(os.Stderr, "yes\n\n")
      if t == 10000 {
        writer.WriteString(fmt.Sprintf("%s %s %s YES\n", a, b, c))
      } else {
        writer.WriteString("YES\n")
      }
    } else {
      //fmt.Fprintf(os.Stderr, "no\n\n")
      writer.WriteString("NO\n")
    }
  }
}

package main

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

var scanner *bufio.Scanner

func getI64() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}
func getI() int {
  return int(getI64())
}
func getF() float64 {
  scanner.Scan()
  x, _ := strconv.ParseFloat(scanner.Text(), 64)
  return x
}
func getS() string {
  scanner.Scan()
  return scanner.Text()
}

type Point struct {
  x, y int64
}
func (p Point) String() string {
  return fmt.Sprintf("(%d, %d)", p.x, p.y)
}
type Segment struct {
  p, q Point
}
func (s Segment) String() string {
  return fmt.Sprintf("[%s, %s]", s.p, s.q)
}

func ok(p Point, s Segment) bool {
  if p == s.p || p == s.q {
    //fmt.Fprintf(os.Stderr, "%s lies on an endpoint of %s\n", p, s)
    return false
  }
  if (p.x < s.p.x && p.x < s.q.x) || (p.x > s.p.x && p.x > s.q.x) {
    //fmt.Fprintf(os.Stderr, "%s lies outside %s\n", p, s)
    return false
  }
  if (p.y < s.p.y && p.y < s.q.y) || (p.y > s.p.y && p.y > s.q.y) {
    //fmt.Fprintf(os.Stderr, "%s lies outside %s\n", p, s)
    return false
  }
  x0, y0 := s.q.x - s.p.x, s.q.y - s.p.y
  x1, y1 := p.x - s.p.x, p.y - s.p.y
  x2, y2 := s.q.x - p.x, s.q.y - p.y
  if y0*x1 != x0*y1 {
    //fmt.Fprintf(os.Stderr, "%s does not fall on %s\n", p, s)
    return false
  }
  //fmt.Fprintf(os.Stderr, "%s falls on %s\n", p, s)
  dd1 := x1*x1 + y1*y1
  dd2 := x2*x2 + y2*y2
  //fmt.Fprintf(os.Stderr, "dd1 = %d, dd2 = %d\n", dd1, dd2)
  return (dd1 <= dd2 && 16*dd1 >= dd2) || (dd2 <= dd1 && 16*dd2 >= dd1)
}

func isA(a, b, c Segment) bool {
  if a.p != b.p && a.p != b.q && a.q != b.p && a.q != b.q {
    return false
  }
  //fmt.Fprintf(os.Stderr, "%s meets %s\n", a, b)
  return (ok(c.p, a) && ok(c.q, b)) || (ok(c.p, b) && ok(c.q, a))
}

func main() {
  scanner = bufio.NewScanner(os.Stdin)
  scanner.Split(bufio.ScanWords)
  writer := bufio.NewWriter(os.Stdout)
  defer writer.Flush()

  t := getI()
  for ti := 0; ti < t; ti++ {
    a := Segment{ Point{ getI64(), getI64() }, Point{ getI64(), getI64() } }
    b := Segment{ Point{ getI64(), getI64() }, Point{ getI64(), getI64() } }
    c := Segment{ Point{ getI64(), getI64() }, Point{ getI64(), getI64() } }
    if isA(a, b, c) || isA(a, c, b) || isA(b, c, a) {
      //fmt.Fprintf(os.Stderr, "yes\n\n")
      writer.WriteString(fmt.Sprintf("%s %s %s YES\n", a, b, c))
    } else {
      //fmt.Fprintf(os.Stderr, "no\n\n")
      writer.WriteString("NO\n")
    }
  }
}

package main

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

var scanner *bufio.Scanner

func getI64() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}
func getI() int {
  return int(getI64())
}
func getF() float64 {
  scanner.Scan()
  x, _ := strconv.ParseFloat(scanner.Text(), 64)
  return x
}
func getS() string {
  scanner.Scan()
  return scanner.Text()
}

type Point struct {
  x, y int64
}
func (p Point) String() string {
  return fmt.Sprintf("(%d, %d)", p.x, p.y)
}
type Segment struct {
  p, q Point
}
func (s Segment) String() string {
  return fmt.Sprintf("[%s, %s]", s.p, s.q)
}

func ok(p Point, s Segment) bool {
  if p == s.p || p == s.q {
    //fmt.Fprintf(os.Stderr, "%s lies on an endpoint of %s\n", p, s)
    return false
  }
  if (p.x < s.p.x && p.x < s.q.x) || (p.x > s.p.x && p.x > s.q.x) {
    //fmt.Fprintf(os.Stderr, "%s lies outside %s\n", p, s)
    return false
  }
  if (p.y < s.p.y && p.y < s.q.y) || (p.y > s.p.y && p.y > s.q.y) {
    //fmt.Fprintf(os.Stderr, "%s lies outside %s\n", p, s)
    return false
  }
  x0, y0 := s.q.x - s.p.x, s.q.y - s.p.y
  x1, y1 := p.x - s.p.x, p.y - s.p.y
  x2, y2 := s.q.x - p.x, s.q.y - p.y
  if y0*x1 != x0*y1 {
    //fmt.Fprintf(os.Stderr, "%s does not fall on %s\n", p, s)
    return false
  }
  //fmt.Fprintf(os.Stderr, "%s falls on %s\n", p, s)
  dd1 := x1*x1 + y1*y1
  dd2 := x2*x2 + y2*y2
  //fmt.Fprintf(os.Stderr, "dd1 = %d, dd2 = %d\n", dd1, dd2)
  return (dd1 <= dd2 && 16*dd1 >= dd2) || (dd2 <= dd1 && 16*dd2 >= dd1)
}

func isA(a, b, c Segment) bool {
  if a.p != b.p && a.p != b.q && a.q != b.p && a.q != b.q {
    return false
  }
  //fmt.Fprintf(os.Stderr, "%s meets %s\n", a, b)
  return (ok(c.p, a) && ok(c.q, b)) || (ok(c.p, b) && ok(c.q, a))
}

func main() {
  scanner = bufio.NewScanner(os.Stdin)
  scanner.Split(bufio.ScanWords)
  writer := bufio.NewWriter(os.Stdout)
  defer writer.Flush()

  t := getI()
  for ti := 0; ti < t; ti++ {
    a := Segment{ Point{ getI64(), getI64() }, Point{ getI64(), getI64() } }
    b := Segment{ Point{ getI64(), getI64() }, Point{ getI64(), getI64() } }
    c := Segment{ Point{ getI64(), getI64() }, Point{ getI64(), getI64() } }
    if isA(a, b, c) || isA(a, c, b) || isA(b, c, a) {
      //fmt.Fprintf(os.Stderr, "yes\n\n")
      writer.WriteString("YES\n")
    } else {
      //fmt.Fprintf(os.Stderr, "no\n\n")
      if t == 10000 {
        writer.WriteString(fmt.Sprintf("NO %s %s %s\n", a, b, c))
      } else {
        writer.WriteString("NO\n")
      }
    }
  }
}


Fox Ciel saw a large field while she was on a bus. The field was a n × m rectangle divided into 1 × 1 cells. Some cells were wasteland, and other each cell contained crop plants: either carrots or kiwis or grapes. After seeing the field carefully, Ciel found that the crop plants of each cell were planted in following procedure:  Assume that the rows are numbered 1 to n from top to bottom and the columns are numbered 1 to m from left to right, and a cell in row i and column j is represented as (i, j).  First, each field is either cultivated or waste. Crop plants will be planted in the cultivated cells in the order of (1, 1) → ... → (1, m) → (2, 1) → ... → (2, m) → ... → (n, 1) → ... → (n, m). Waste cells will be ignored.  Crop plants (either carrots or kiwis or grapes) will be planted in each cell one after another cyclically. Carrots will be planted in the first cell, then kiwis in the second one, grapes in the third one, carrots in the forth one, kiwis in the fifth one, and so on. The following figure will show you the example of this procedure. Here, a white square represents a cultivated cell, and a black square represents a waste cell.  Now she is wondering how to determine the crop plants in some certain cells.

In the first line there are four positive integers n, m, k, t (1 ≤ n ≤ 4·104, 1 ≤ m ≤ 4·104, 1 ≤ k ≤ 103, 1 ≤ t ≤ 103), each of which represents the height of the field, the width of the field, the number of waste cells and the number of queries that ask the kind of crop plants in a certain cell. Following each k lines contains two integers a, b (1 ≤ a ≤ n, 1 ≤ b ≤ m), which denotes a cell (a, b) is waste. It is guaranteed that the same cell will not appear twice in this section. Following each t lines contains two integers i, j (1 ≤ i ≤ n, 1 ≤ j ≤ m), which is a query that asks you the kind of crop plants of a cell (i, j).

For each query, if the cell is waste, print Waste. Otherwise, print the name of crop plants in the cell: either Carrots or Kiwis or Grapes.

Waste
Grapes
Carrots
Kiwis
Carrots
Carrots

NoteThe sample corresponds to the figure in the statement.

package main

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

var scanner = bufio.NewScanner(os.Stdin)
var plants = [3]string{"Carrots", "Kiwis", "Grapes"}

func scanNum() int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num
}
func main() {
	scanner.Split(bufio.ScanWords)
	_, m, k, t := scanNum(), scanNum(), scanNum(), scanNum()
	W, M := make([]int, k), map[int]bool{}
	for i := 0; i < k; i++ {
		W[i] = (scanNum()-1)*m + scanNum()
		M[W[i]] = true
	}
	sort.Ints(W)

	for i := 0; i < t; i++ {
		q := (scanNum()-1)*m + scanNum()
		if M[q] {
			fmt.Println("Waste")
			continue
		}

		wSum := 0
		for j := 0; j < k; j++ {
			if W[j] > q {
				break
			}
			wSum++
		}

		fmt.Println(plants[(q-wSum-1)%3])
	}
}