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


package main

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

const N = 1e5 + 10

var p, hap, tot, good [N]int
var tree [N][]int
var ans bool
var scanner *bufio.Scanner

func solve() {
	ans = true

	n := readInt()
	m := readInt()
	_ = m

	for i := 0; i < n; i++ {
		p[i] = readInt()
		good[i] = 0
		tot[i] = 0
	}
	for i := 0; i < n; i++ {
		hap[i] = readInt()
	}
	for i := 0; i < n; i++ {
		tree[i] = make([]int, 0)
	}
	for i := 0; i < n-1; i++ {
		x := readInt()
		y := readInt()
		x--
		y--
		tree[x] = append(tree[x], y)
		tree[y] = append(tree[y], x)
	}

	dfs(0, -1)

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

func dfs(id, parent int) {
	tot[id] = p[id]

	for _, child := range tree[id] {
		if child == parent {
			continue
		}

		dfs(child, id)
		tot[id] += tot[child]
		good[id] += good[child]
	}

	if (hap[id]+tot[id])%2 != 0 {
		ans = false
	}

	goodMood := (hap[id] + tot[id]) / 2
	if goodMood < 0 || goodMood > tot[id] {
		ans = false
	}

	if goodMood < good[id] {
		ans = false
	}

	good[id] = goodMood
}

func main() {

	f, err := os.Open("input.txt")
	if err == nil {
		os.Stdin = f
	}

	scanner = bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)

	t := readInt()

	for i := 0; i < t; i++ {
		solve()
	}
}

func readInt() int {
	if !scanner.Scan() {
		die(io.ErrUnexpectedEOF)
	}
	i, err := strconv.Atoi(scanner.Text())
	if err != nil {
		die(err)
	}
	return i

}
func die(v ...interface{}) {
	fmt.Fprintln(os.Stderr, v...)
	os.Exit(1)
}

package main

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

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

	T:= 0
	for Fscan(in, &T); T > 0; T-- {
		var n,m int
		Fscan(in, &n,&m)
		p,h:=make([]int,n+1),make([]int,n+1)
		for i:=1;i<=n;i++{
			Fscan(in,&p[i])
		}
		h[0]=2*m
		for i:=1;i<=n;i++{
			Fscan(in,&h[i])
		}
		e:=make([][]int,n+1)
		for i:=0;i<n-1;i++{
			var u,v int
			Fscan(in,&u,&v)
			e[u]=append(e[u],v)
			e[v]=append(e[v],u)
		}

		fl:=true
		var dfs func(int,int) (int,int)
		dfs=func(u,par int) (int,int) {
			gg,nn:=0,p[u]
			for _,v := range(e[u]){
				if v==par{
					continue
				}
				aa,bb:=dfs(v,u)
				gg+=aa
				nn+=bb
			}
			if (nn+h[u])%2!=0{
				fl=false
			}
			// Fprintln(out,nn,gg,h[u])
			if h[u]>nn || h[u]<2*gg-nn{
				fl=false
			}

			return (h[u]+nn)/2,nn
		}
		dfs(1,0)
		// Fprintln(out,m,aa,bb)
		if fl{
			Fprintln(out,"YES")
		} else {
			Fprintln(out,"NO")
		}
	}
}

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

package main

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

const N = 1e5 + 10

var p, hap, tot, good [N]int
var tree [N][]int
var ans bool
var scanner *bufio.Scanner

func solve() {
	ans = true

	n := readInt()
	m := readInt()
	_ = m

	for i := 0; i < n; i++ {
		p[i] = readInt()
		good[i] = 0
		tot[i] = 0
	}
	for i := 0; i < n; i++ {
		hap[i] = readInt()
	}
	for i := 0; i < n; i++ {
		tree[i] = make([]int, 0)
	}
	for i := 0; i < n-1; i++ {
		x := readInt()
		y := readInt()
		x--
		y--
		tree[x] = append(tree[x], y)
		tree[y] = append(tree[y], x)
	}

	dfs(0, -1)

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

func dfs(id, parent int) {
	tot[id] = p[id]

	for _, child := range tree[id] {
		if child == parent {
			continue
		}

		dfs(child, id)
		tot[id] += tot[child]
		good[id] += good[child]
	}

	if (hap[id]+tot[id])%2 != 0 {
		ans = false
	}

	goodMood := (hap[id] + tot[id]) / 2
	if goodMood < 0 || goodMood > tot[id] {
		ans = false
	}

	if goodMood < good[id] {
		ans = false
	}

	good[id] = goodMood
}

func main() {

	f, err := os.Open("input.txt")
	if err == nil {
		os.Stdin = f
	}

	scanner = bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)

	t := readInt()

	for i := 0; i < t; i++ {
		solve()
	}
}

func readInt() int {
	if !scanner.Scan() {
		die(io.ErrUnexpectedEOF)
	}

	i, err := atoi(scanner.Bytes())
	if err != nil {
		die(err)
	}
	return i
}

func atoi(b []byte) (int, error) {
	neg := false
	if b[0] == '+' {
		b = b[1:]
	} else if b[0] == '-' {
		neg = true
		b = b[1:]
	}
	n := 0
	for _, v := range b {
		if v < '0' || v > '9' {
			return 0, strconv.ErrSyntax
		}
		n = n*10 + int(v-'0')
	}
	if neg {
		return -n, nil
	}
	return n, nil
}
func die(v ...interface{}) {
	fmt.Fprintln(os.Stderr, v...)
	os.Exit(1)
}

package main

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

const N = 1e5 + 10

var p, hap, tot, good [N]int
var tree [N][]int
var ans bool
var scanner *bufio.Scanner

func solve() {
	ans = true

	n := fs()
	m := fs()
	_ = m

	for i := 0; i < n; i++ {
		p[i] = fs()
		good[i] = 0
		tot[i] = 0
	}
	for i := 0; i < n; i++ {
		hap[i] = fs()
	}
	for i := 0; i < n; i++ {
		tree[i] = make([]int, 0)
	}
	for i := 0; i < n-1; i++ {
		x := fs()
		y := fs()
		x--
		y--
		tree[x] = append(tree[x], y)
		tree[y] = append(tree[y], x)
	}

	dfs(0, -1)

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

func dfs(id, parent int) {
	tot[id] = p[id]

	for _, child := range tree[id] {
		if child == parent {
			continue
		}

		dfs(child, id)
		tot[id] += tot[child]
		good[id] += good[child]
	}

	if (hap[id]+tot[id])%2 != 0 {
		ans = false
	}

	goodMood := (hap[id] + tot[id]) / 2
	if goodMood < 0 || goodMood > tot[id] {
		ans = false
	}

	if goodMood < good[id] {
		ans = false
	}

	good[id] = goodMood
}

func main() {

	f, err := os.Open("input.txt")
	if err == nil {
		os.Stdin = f
	}
	defer f.Close()

	scanner = bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)

	t := fs()

	for i := 0; i < t; i++ {
		solve()
	}
}

func fs() int {
	if !scanner.Scan() {
		os.Exit(1)
	}
	i, _ := strconv.Atoi(scanner.Text())
	return i
}

package main

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

const N = 1e5 + 10

var p, hap, tot, good [N]int
var tree [N][]int
var ans bool

func solve(in *bufio.Reader) {
	var n, m int
	ans = true

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

	for i := 0; i < n; i++ {
		fmt.Fscan(in, &p[i])
		good[i] = 0
		tot[i] = 0
	}
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &hap[i])
	}
	for i := 0; i < n; i++ {
		tree[i] = make([]int, 0)
	}
	for i := 0; i < n-1; i++ {
		var x, y int
		fmt.Fscan(in, &x, &y)
		x--
		y--
		tree[x] = append(tree[x], y)
		tree[y] = append(tree[y], x)
	}

	dfs(0, -1)

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

func dfs(id, parent int) {
	tot[id] = p[id]

	for _, child := range tree[id] {
		if child == parent {
			continue
		}

		dfs(child, id)
		tot[id] += tot[child]
		good[id] += good[child]
	}

	if (hap[id]+tot[id])%2 != 0 {
		ans = false
	}

	goodMood := (hap[id] + tot[id]) / 2
	if goodMood < 0 || goodMood > tot[id] {
		ans = false
	}

	if goodMood < good[id] {
		ans = false
	}

	good[id] = goodMood
}

func main() {

	f, err := os.Open("input.txt")
	if err == nil {
		os.Stdin = f
	}

	in := bufio.NewReader(os.Stdin)

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

	for i := 0; i < t; i++ {
		solve(in)
	}
}

package main

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

const N = 1e5 + 100

var p, hap, tot, good []int
var tree [][]int
var ans bool

func init() {
	p = make([]int, N)
	hap = make([]int, N)
	tot = make([]int, N)
	good = make([]int, N)
	tree = make([][]int, N)
}

func solve(in *bufio.Reader) {
	var n, m int
	ans = true

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

	for i := 0; i < n; i++ {
		fmt.Fscan(in, &p[i])
		good[i] = 0
		tot[i] = 0
	}
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &hap[i])
	}
	for i := 0; i < n; i++ {
		tree[i] = make([]int, 0)
	}
	for i := 0; i < n-1; i++ {
		var x, y int
		fmt.Fscan(in, &x, &y)
		x--
		y--
		tree[x] = append(tree[x], y)
		tree[y] = append(tree[y], x)
	}

	dfs(0, -1)

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

func dfs(id, parent int) {
	tot[id] = p[id]

	for _, child := range tree[id] {
		if child == parent {
			continue
		}

		dfs(child, id)
		tot[id] += tot[child]
		good[id] += good[child]
	}

	if (hap[id]+tot[id])%2 != 0 {
		ans = false
	}

	goodMood := (hap[id] + tot[id]) / 2
	if goodMood < 0 || goodMood > tot[id] {
		ans = false
	}

	if goodMood < good[id] {
		ans = false
	}

	good[id] = goodMood
}

func main() {

	f, err := os.Open("input.txt")
	if err == nil {
		os.Stdin = f
	}

	in := bufio.NewReader(os.Stdin)

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

	for i := 0; i < t; i++ {
		solve(in)
	}
}

package main

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

func main() {
	io := newFastIO(os.Stdin, os.Stdout)
	defer io.Flush()

	for t := io.nextInt(); t > 0; t-- {
		n, _ := io.nextInt(), io.nextInt()
		x, h := io.nextInts(n), io.nextInts(n)
		g := make([][]int, n+1)
		for i := 0; i < n-1; i++ {
			u, v := io.nextInt(), io.nextInt()
			g[u] = append(g[u], v)
			g[v] = append(g[v], u)
		}

		ok := true
		var dfs func(u, p int) int
		dfs = func(u, p int) int {
			hh, xx := h[u-1], x[u-1]
			xsum, hsum := xx, 0
			for _, v := range g[u] {
				if v != p {
					xsum += dfs(v, u)
					hsum += h[v-1]
				}
			}
			if hh > xsum || hh < hsum-xx || hh&1 != xsum&1 {
				ok = false
			}
			return xsum
		}
		dfs(1, 1)

		if ok {
			io.println("YES")
		} else {
			io.println("NO")
		}
	}
}

type fastIO struct {
	*bufio.Reader
	*bufio.Writer
	p [1]byte
}

func newFastIO(r io.Reader, w io.Writer) *fastIO {
	return &fastIO{Reader: bufio.NewReader(r), Writer: bufio.NewWriter(w)}
}

func (f *fastIO) nextInt() int {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	neg := false
	if f.p[0] == '-' {
		neg = true
		f.Read(f.p[:])
	}
	n := 0
	for '0' <= f.p[0] && f.p[0] <= '9' {
		n = n*10 + int(f.p[0]) - '0'
		f.Read(f.p[:])
	}
	if neg {
		return -n
	}
	return n
}

func (f *fastIO) nextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.nextInt()
	}
	return a
}

func (f *fastIO) next() string {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	var s []byte
	for f.p[0] > ' ' {
		s = append(s, f.p[0])
		f.Read(f.p[:])
	}
	return string(s)
}

func (f *fastIO) println(a ...interface{}) {
	fmt.Fprintln(f.Writer, a...)
}

func (f *fastIO) print(a ...interface{}) {
	fmt.Fprint(f.Writer, a...)
}

func (f *fastIO) printInts(a ...int) {
	if len(a) > 0 {
		f.print(a[0])
		for _, x := range a[1:] {
			f.print(" ", x)
		}
	}
}

func (f *fastIO) printlnInts(a ...int) {
	f.printInts(a...)
	f.println()
}

/*
URL:
https://codeforces.com/contest/1388/problem/C
*/

package main

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

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

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

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

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

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

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

const (
	// General purpose
	MOD = 1000000000 + 7
	// MOD          = 998244353
	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 (
	t int

	n, m int
	P, H []int
	G    [][]int
)

func main() {
	t = ReadInt()

	for tc := 0; tc < t; tc++ {
		n, m = ReadInt2()
		P = ReadIntSlice(n)
		H = ReadIntSlice(n)
		G = make([][]int, n)
		for i := 0; i < n-1; i++ {
			s, t := ReadInt2()
			s--
			t--

			G[s] = append(G[s], t)
			G[t] = append(G[t], s)
		}

		solve()
	}
}

func solve() {
	_, _, ok := dfs(0, -1)
	if ok {
		fmt.Println("YES")
	} else {
		fmt.Println("NO")
	}
	// PrintfDebug("\n")
}

// 正, 負, isOK
func dfs(cid, pid int) (int, int, bool) {
	tp, tn := 0, 0

	for _, nid := range G[cid] {
		if nid == pid {
			continue
		}

		pos, neg, ok := dfs(nid, cid)
		if !ok {
			return 0, 0, false
		}

		tp += pos
		tn += neg
	}

	// 考えられる幸福度を計算する
	target := H[cid] - tp
	free := P[cid] + tn
	total := P[cid] + tn + tp
	if -free <= target && target <= free && AbsInt(target)%2 == AbsInt(free)%2 {
		// 最大はfree, tmpは新たに生じるpositive
		newPosi := (free + target) / 2
		// newNega := free - newPosi
		ppp := tp + newPosi
		nnn := total - ppp

		// PrintfDebug("cid: %d, posi: %d, nega: %d\n", cid, ppp, nnn)
		// return tp + newPosi, tn + newNega, true
		return ppp, nnn, true
	} else {
		return 0, 0, false
	}
}

// AbsInt is integer version of math.Abs
func AbsInt(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

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

/*********** 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/ioutil"
	"os"
)

const N = 1e5 + 10

var p, hap, tot, good [N]int

var ans bool

func solve() {
	ans = true
	n := fs()
	m := fs()
	_ = m

	for i := 0; i < n; i++ {
		p[i] = fs()
		good[i] = 0
	}
	for i := 0; i < n; i++ {
		hap[i] = fs()
	}
	tree := make([][]int, n)

	for i := 0; i < n-1; i++ {
		x := fs() - 1
		y := fs() - 1
		tree[x] = append(tree[x], y)
		tree[y] = append(tree[y], x)
	}

	dfs(0, -1, tree)

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

func dfs(id, parent int, tree [][]int) {
	tot[id] = p[id]

	for _, child := range tree[id] {
		if child == parent {
			continue
		}

		dfs(child, id, tree)
		tot[id] += tot[child]
		good[id] += good[child]
	}

	if (hap[id]+tot[id])%2 != 0 {
		ans = false
	}

	goodMood := (hap[id] + tot[id]) / 2
	if goodMood < 0 || goodMood > tot[id] {
		ans = false
	}

	if goodMood < good[id] {
		ans = false
	}

	good[id] = goodMood
}

func main() {

	in, err := os.Open("input.txt")
	if err == nil {
		os.Stdin = in
	}

	bytes, _ = ioutil.ReadAll(bufio.NewReader(os.Stdin))
	maxV = len(bytes) - 1

	t := fs()

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

var bytes []byte
var l, maxV int

func fs() int {
	b := bytes[l]

	for b < 48 || b > 57 {
		l++
		b = bytes[l]
	}

	result := 0
	for 47 < b && b < 58 {
		result = (result << 3) + (result << 1) + int(b-48)

		l++
		if l > maxV {
			return result
		}
		b = bytes[l]
	}
	return result
}

package main

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

const N = 1e5 + 100

var p, hap, tot, good []int
var tree [][]int
var ans bool

func init() {
	p = make([]int, N)
	hap = make([]int, N)
	tot = make([]int, N)
	good = make([]int, N)
	tree = make([][]int, N)
}

func solve() {
	var n, m int
	ans = true
	n = fs()
	m = fs()
	_ = m

	for i := 0; i < n; i++ {
		p[i] = fs()
		good[i] = 0
		tot[i] = 0
	}
	for i := 0; i < n; i++ {
		hap[i] = fs()
	}
	for i := 0; i < n; i++ {
		tree[i] = make([]int, 0)
	}
	for i := 0; i < n-1; i++ {
		var x, y int
		x = fs()
		y = fs()
		x--
		y--
		tree[x] = append(tree[x], y)
		tree[y] = append(tree[y], x)
	}

	dfs(0, -1)

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

func dfs(id, parent int) {
	tot[id] = p[id]

	for _, child := range tree[id] {
		if child == parent {
			continue
		}

		dfs(child, id)
		tot[id] += tot[child]
		good[id] += good[child]
	}

	if (hap[id]+tot[id])%2 != 0 {
		ans = false
	}

	goodMood := (hap[id] + tot[id]) / 2
	if goodMood < 0 || goodMood > tot[id] {
		ans = false
	}

	if goodMood < good[id] {
		ans = false
	}

	good[id] = goodMood
}
func main() {

	in, err := os.Open("input.txt")
	if err == nil {
		os.Stdin = in
	}
	bytes, _ = ioutil.ReadAll(bufio.NewReader(os.Stdin))
	maxV = len(bytes) - 1

	t := fs()

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

var bytes []byte
var l, maxV int

func fs() int {
	b := bytes[l]

	for b < 48 || b > 57 {
		l++
		b = bytes[l]
	}

	result := 0
	for 47 < b && b < 58 {
		result = (result << 3) + (result << 1) + int(b-48)

		l++
		if l > maxV {
			return result
		}
		b = bytes[l]
	}
	return result
}

package main

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

const N = 1e5 + 10

var p, hap, tot, good [N]int

var ans bool

func solve() {
	ans = true
	n := fs()
	m := fs()
	_ = m

	for i := 0; i < n; i++ {
		p[i] = fs()
		good[i] = 0
	}
	for i := 0; i < n; i++ {
		hap[i] = fs()
	}
	tree := make([][]int, n)

	for i := 0; i < n-1; i++ {
		x := fs()
		y := fs()
		x--
		y--
		tree[x] = append(tree[x], y)
		tree[y] = append(tree[y], x)
	}

	dfs(0, -1, tree)

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

func dfs(id, parent int, tree [][]int) {
	if !ans {
		return
	}
	tot[id] = p[id]

	for _, child := range tree[id] {
		if child == parent {
			continue
		}

		dfs(child, id, tree)
		tot[id] += tot[child]
		good[id] += good[child]
	}

	if (hap[id]+tot[id])%2 != 0 {
		ans = false
	}

	goodMood := (hap[id] + tot[id]) / 2
	if goodMood < 0 || goodMood > tot[id] {
		ans = false
	}

	if goodMood < good[id] {
		ans = false
	}

	good[id] = goodMood
}

func main() {

	in, err := os.Open("input.txt")
	if err == nil {
		os.Stdin = in
	}
	defer in.Close()

	bytes, _ = ioutil.ReadAll(bufio.NewReader(os.Stdin))
	maxV = len(bytes) - 1

	t := fs()

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

var bytes []byte
var l, maxV int

func fs() int {
	b := bytes[l]

	for b < 48 || b > 57 {
		l++
		b = bytes[l]
	}

	result := 0
	for 47 < b && b < 58 {
		result = (result << 3) + (result << 1) + int(b-48)

		l++
		if l > maxV {
			return result
		}
		b = bytes[l]
	}
	return result
}

package main

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

const N = 1e5 + 10

var p, hap, tot, good [N]int

var ans bool

func solve() {
	ans = true
	n := fs()
	m := fs()
	_ = m

	for i := 0; i < n; i++ {
		p[i] = fs()
		good[i] = 0
	}
	for i := 0; i < n; i++ {
		hap[i] = fs()
	}
	tree := make([][]int, n)

	for i := 0; i < n-1; i++ {
		x := fs()
		y := fs()
		x--
		y--
		tree[x] = append(tree[x], y)
		tree[y] = append(tree[y], x)
	}

	dfs(0, -1, tree)

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

func dfs(id, parent int, tree [][]int) {
	if !ans {
		return
	}
	tot[id] = p[id]

	for _, child := range tree[id] {
		if child == parent {
			continue
		}

		dfs(child, id, tree)
		tot[id] += tot[child]
		good[id] += good[child]
	}

	if (hap[id]+tot[id])%2 != 0 {
		ans = false
	}

	goodMood := (hap[id] + tot[id]) / 2
	if goodMood < 0 || goodMood > tot[id] {
		ans = false
	}

	if goodMood < good[id] {
		ans = false
	}

	good[id] = goodMood
}

func main() {

	in, err := os.Open("input.txt")
	if err == nil {
		os.Stdin = in
	}
	defer in.Close()

	bytes, _ = ioutil.ReadAll(bufio.NewReader(os.Stdin))
	maxV = len(bytes) - 1

	t := fs()

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

var bytes []byte
var l, maxV int

func fs() int {
	b := bytes[l]

	for b < 48 || b > 57 {
		l++
		b = bytes[l]
	}

	result := 0
	for 47 < b && b < 58 {
		result = (result << 3) + (result << 1) + int(b-48)

		l++
		if l > maxV {
			return result
		}
		b = bytes[l]
	}
	return result
}

package main

import (
	"fmt"
	"os"
)

const N = 1e5 + 10

var p, hap, tot, good []int
var tree[][]int
var ans bool

func init(){
	ans = true
	p = make([]int, N)
	hap = make([]int, N)
	tot = make([]int, N)
	good = make([]int, N)
	tree = make([][]int, N)
}

func main() {
	var t int

	f, err := os.Open("input.txt")

	if err == nil {
		os.Stdin = f
	}
	defer f.Close()

	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		solve()
	}
}

func solve() {
	var n, m int
	fmt.Scan(&n, &m)

	for i := 0; i < n; i++ {
		fmt.Scan(&p[i])
		good[i] = 0
	}
	for i := 0; i < n; i++ {
		fmt.Scan(&hap[i])
	}
	for i := 0; i < n; i++ {
		tree[i] = make([]int, 0)
	}
	for i := 0; i < n - 1; i++ {
		var x, y int
		fmt.Scan(&x)
		fmt.Scan(&y)
		x--
		y--
		tree[x] = append(tree[x], y)
		tree[y] = append(tree[y], x)
	}

	dfs(0, -1)

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

func dfs(id, parent int) {
	tot[id] = p[id]

	for _, child := range(tree[id]) {
		if child == parent {
			continue
		}

		dfs(child, id)
		tot[id] += tot[child]
		good[id] += good[child]
	}

	if ((hap[id] + tot[id]) % 2 != 0) {
		ans = false
	}

	goodMood := (hap[id] + tot[id]) / 2
	if goodMood < 0 || goodMood > tot[id] {
		ans = false
	}

	if goodMood < good[id] {
		ans = false
	}

	good[id] = goodMood
}

package main

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

const N = 1e5 + 10

var p, hap, tot, good []int
var tree[][]int
var ans bool

func init(){
	p = make([]int, N)
	hap = make([]int, N)
	tot = make([]int, N)
	good = make([]int, N)
	tree = make([][]int, N)
}

func solve() {
	var n, m int
	ans = true
	n = fs()
	m = fs()
	_ = m

	for i := 0; i < n; i++ {
		p[i] = fs()
		good[i] = 0
	}
	for i := 0; i < n; i++ {
		hap[i] = fs()
	}
	for i := 0; i < n; i++ {
		tree[i] = make([]int, 0)
	}
	for i := 0; i < n - 1; i++ {
		var x, y int
		x = fs()
		y = fs()
		x--
		y--
		tree[x] = append(tree[x], y)
		tree[y] = append(tree[y], x)
	}

	dfs(0, -1)

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

func dfs(id, parent int) {
	tot[id] = p[id]

	for _, child := range(tree[id]) {
		if child == parent {
			continue
		}

		dfs(child, id)
		tot[id] += tot[child]
		good[id] += good[child]
	}

	if ((hap[id] + tot[id]) % 2 != 0) {
		ans = false
	}

	goodMood := (hap[id] + tot[id]) / 2
	if goodMood < 0 || goodMood > tot[id] {
		ans = false
	}

	if goodMood < good[id] {
		ans = false
	}

	good[id] = goodMood
}
func main() {

	in, err := os.Open("input.txt")
	if err == nil {
		os.Stdin = in
	}
	defer in.Close()

	bytes, _ = ioutil.ReadAll(bufio.NewReader(os.Stdin))
	maxV = len(bytes) - 1

	t := fs()

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

var bytes []byte
var l, maxV int

func fs() int {
	b := bytes[l]

	for b < 48 || b > 57 {
		l++
		b = bytes[l]
	}

	result := 0
	for 47 < b && b < 58 {
		result = (result << 3) + (result << 1) + int(b-48)

		l++
		if l > maxV {
			return result
		}
		b = bytes[l]
	}
	return result
}

package main

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

func main() {
	io := newFastIO(os.Stdin, os.Stdout)
	defer io.Flush()

	n := io.nextInt()
	a, b := io.nextInts(n), io.nextInts(n)

	g := make([][]int, n+1)
	for i := 1; i <= n; i++ {
		g[i] = []int{}
	}
	for i := 0; i < n; i++ {
		if b[i] != -1 {
			g[b[i]] = append(g[b[i]], i+1)
		}
	}

	ans := int64(0)
	later := [][]int{}
	var dfs func(u, p int) (int64, []int)
	dfs = func(u, p int) (int64, []int) {
		now, t := []int{}, int64(0)
		for _, v := range g[u] {
			s, y := dfs(v, u)
			t += s
			if len(now) < len(y) {
				now, y = y, now
			}
			now = append(now, y...)
		}
		cur := int64(a[u-1])
		ans += t + cur
		now = append(now, u)
		if t+cur <= 0 {
			later = append(later, now)
			return 0, []int{}
		}
		return t + cur, now
	}

	for i := 0; i < n; i++ {
		if b[i] == -1 {
			_, y := dfs(i+1, i+1)
			later = append(later, y)
		}
	}

	out := []int{}
	for i := len(later) - 1; i >= 0; i-- {
		out = append(out, later[i]...)
	}

	io.println(ans)
	io.printlnInts(out...)
}

type fastIO struct {
	*bufio.Reader
	*bufio.Writer
	p [1]byte
}

func newFastIO(r io.Reader, w io.Writer) *fastIO {
	return &fastIO{Reader: bufio.NewReader(r), Writer: bufio.NewWriter(w)}
}

func (f *fastIO) nextInt() int {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	neg := false
	if f.p[0] == '-' {
		neg = true
		f.Read(f.p[:])
	}
	n := 0
	for '0' <= f.p[0] && f.p[0] <= '9' {
		n = n*10 + int(f.p[0]) - '0'
		f.Read(f.p[:])
	}
	if neg {
		return -n
	}
	return n
}

func (f *fastIO) nextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.nextInt()
	}
	return a
}

func (f *fastIO) next() string {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	var s []byte
	for f.p[0] > ' ' {
		s = append(s, f.p[0])
		f.Read(f.p[:])
	}
	return string(s)
}

func (f *fastIO) println(a ...interface{}) {
	fmt.Fprintln(f.Writer, a...)
}

func (f *fastIO) print(a ...interface{}) {
	fmt.Fprint(f.Writer, a...)
}

func (f *fastIO) printInts(a ...int) {
	if len(a) > 0 {
		f.print(a[0])
		for _, x := range a[1:] {
			f.print(" ", x)
		}
	}
}

func (f *fastIO) printlnInts(a ...int) {
	f.printInts(a...)
	f.println()
}


package main

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

// https://space.bilibili.com/206214
func CF1579G(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	const inf int = 1e9
	const mx = 2001
	pre := make([]int, mx)
	f := make([]int, mx)

	var T, n, v int
	for Fscan(in, &T); T > 0; T-- {
		for i := range pre {
			pre[i] = inf
		}
		pre[0] = 0
		for Fscan(in, &n); n > 0; n-- {
			Fscan(in, &v)
			for i := range f {
				f[i] = inf
			}
			for j, dis := range pre {
				if pre[j] == inf {
					continue
				}
				if j+v < mx {
					f[j+v] = min(f[j+v], max(dis, j+v))
				}
				if j >= v {
					f[j-v] = min(f[j-v], dis)
				} else {
					f[0] = min(f[0], dis-j+v)
				}
			}
			pre, f = f, pre
		}
		ans := inf
		for _, x := range pre {
			ans = min(ans, x)
		}
		Fprintln(out, ans)
	}
}

func main() { CF1579G(os.Stdin, os.Stdout) }
func min(a, b int) int {
	if a > b {
		return b
	}
	return a
}
func max(a, b int) int {
	if b > a {
		return b
	}
	return a
}

package main

import (
	"fmt"
)

func max(a int, b int) int {
	if a > b {
		return a
	}
	return b
}
func min(a int, b int) int {
	if a < b {
		return a
	}
	return b
}
func solve() {
	var n int
	fmt.Scan(&n)
	maxl := 3000
	a := make([]int, n+1)
	for i := 0; i < n; i++ {
		fmt.Scan(&a[i])
	}
	dp := make([][]int, n+1)
	for i := 0; i < n+1; i++ {
		dp[i] = make([]int, maxl)
	}
	for i := n - 1; i >= 0; i-- {
		for j := 0; j < maxl; j++ {
			o1 := maxl
			if j+a[i] < maxl {
				o1 = a[i] + dp[i+1][j+a[i]]
			}
			o2 := maxl
			if j-a[i] >= 0 {
				o2 = max(0, dp[i+1][j-a[i]]-a[i])
			}
			dp[i][j] = min(o1, o2)
		}
	}

	best := maxl
	for i := 0; i < maxl; i++ {
		best = min(best, dp[0][i]+i)
	}
	fmt.Println(best)
}

func main() {
	var t int
	fmt.Scan(&t)
	for tt := 0; tt < t; tt++ {
		solve()
	}
}

package main

import (
	"fmt"
)

func max(a int, b int) int {
	if a > b {
		return a
	}
	return b
}
func min(a int, b int) int {
	if a < b {
		return a
	}
	return b
}
func solve() {
	var n int
	fmt.Scan(&n)
	maxl := 3*n + 10
	a := make([]int, n+1)
	for i := 0; i < n; i++ {
		fmt.Scan(&a[i])
	}
	dp := make([][]int, n+1)
	for i := 0; i < n+1; i++ {
		dp[i] = make([]int, maxl)
	}
	for i := n - 1; i >= 0; i-- {
		for j := 0; j < maxl; j++ {
			o1 := maxl
			if j+a[i] < maxl {
				o1 = a[i] + dp[i+1][j+a[i]]
			}
			o2 := maxl
			if j-a[i] >= 0 {
				o2 = max(0, dp[i+1][j-a[i]]-a[i])
			}
			dp[i][j] = min(o1, o2)
		}
	}

	best := maxl
	for i := 0; i < maxl; i++ {
		best = min(best, dp[0][i]+i)
	}
	fmt.Println(best)
}

func main() {
	var t int
	fmt.Scan(&t)
	for tt := 0; tt < t; tt++ {
		solve()
	}
}

package main

import (
	"fmt"
)

func max(a int, b int) int {
	if a > b {
		return a
	}
	return b
}
func min(a int, b int) int {
	if a < b {
		return a
	}
	return b
}
func solve() {
	var n int
	fmt.Scanf("%d", &n)
	maxl := 3*n + 10
	a := make([]int, n+1)
	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &a[i])
	}
	dp := make([][]int, n+1)
	for i := 0; i < n+1; i++ {
		dp[i] = make([]int, maxl)
	}
	for i := n - 1; i >= 0; i-- {
		for j := 0; j < maxl; j++ {
			o1 := maxl
			if j+a[i] < maxl {
				o1 = a[i] + dp[i+1][j+a[i]]
			}
			o2 := maxl
			if j-a[i] >= 0 {
				o2 = max(0, dp[i+1][j-a[i]]-a[i])
			}
			dp[i][j] = min(o1, o2)
		}
	}

	best := maxl
	for i := 0; i < maxl; i++ {
		best = min(best, dp[0][i]+i)
	}
	fmt.Println(best)
}

func main() {
	var t int
	fmt.Scanf("%d", &t)
	for tt := 0; tt < t; tt++ {
		solve()
	}
}


package main

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

/*
* lets try some fancy go :)
we need to binary search the maximum possible min of joy.

So, for given joy x check if we can buy a gift foreach friend.
How to implement?
Since we can buy multiple presents in each shop,
we need to find a set of n-1 shops so that
all friends are satisfied.

Most likely there are more shops than n-1, since else we would
simply buy in all shops.

We could solve with max flow algo, but that maybe TLE?

Can we choose one shop per friend?
Ok, we need to find one shop that works for two (or more) friends.
If such shop exists, then we are done since we can
choose any shop for any other friend.
*/

func main() {
    re, w, e := bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout), bufio.NewWriter(os.Stderr)
    defer w.Flush()
    defer e.Flush()

    var t int=0
    Fscan(re, &t)

    for i:=0; i<t; i++ {
        var m,n int64
        Fscan(re, &m, &n)

        var ma=make([]int64,n)    // max joy friend i can get in any shop
        var p=make([][]int64, m)

        var i int64=0
        var j int64=0
        for ; i<m; i++ {
            p[i]=make([]int64, n)
        }

        // max two joys of any friends in shop i
        var ma2=make([][]int64, m)

        for i=0; i<m; i++ {
            ma2[i]=make([]int64, 2)
        }

        for i=0; i<m; i++ {
            for j=0; j<n; j++  {
                Fscan(re, &p[i][j])
                if p[i][j]>ma[j] {
                    ma[j]=p[i][j]
                }
                if ma2[i][0]<p[i][j] {
                    ma2[i][1]=ma2[i][0]
                    ma2[i][0]=p[i][j]
                } else if ma2[i][1]<p[i][j] {
                    ma2[i][1]=p[i][j]
                }
            }
        }

        var mi int64=1000000000  // min max joy of any friend, upper bound for ans
        for i=0; i<n; i++ {
            if mi>ma[i] {
                mi=ma[i]
            }
        }

        var l int64=0
        var r int64=mi+1

        for ; l+1<r ; {
            mid:=(l+r)/2
            ok:=false
            for i=0; !ok && i<m; i++ {
                if ma2[i][1]>=mid  {
                    ok=true
                }
            }
            if ok {
                l=mid
            } else {
                r=mid
            }
        }

        Fprintf(w, "%d\n", l)
    }
}

package main

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

var o [][]int

func check(x, n, m int) bool {
	c, pair := make([]bool, m), false
	for i := 0; i < n; i++ {
		cnt := 0
		for j := 0; j < m; j++ {
			if o[i][j] >= x {
				c[j] = true
				cnt++
			}
		}
		if cnt > 1 {
			pair = true
		}
	}
	if !pair {
		return false
	}
	ans := true
	for _, v := range c {
		ans = ans && v
	}
	return ans
}

func solve() {
	f := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	N, n, m := 0, 0, 0
	fmt.Fscan(f, &N)
	for i := 0; i < N; i++ {
		fmt.Fscan(f, &n, &m)
		o = make([][]int, n)
		for j := 0; j < n; j++ {
			o[j] = make([]int, m)
		}
		for j := 0; j < n; j++ {
			for k := 0; k < m; k++ {
				fmt.Fscan(f, &o[j][k])
			}
		}
		l, r := 1, 2
		for check(r, n, m) {
			r *= 2
		}
		for r-l > 1 {
			mid := (l + r) >> 1
			if check(mid, n, m) {
				l = mid
			} else {
				r = mid
			}
		}
		fmt.Fprintln(w, l)
	}
	w.Flush()
}

func main() {
	solve()
}

package main

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

var o [][]int

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

func check(x, n, m int) bool {
	c, pair := make([]bool, m), false
	for i := 0; i < n; i++ {
		cnt := 0
		for j := 0; j < m; j++ {
			if o[i][j] >= x {
				c[j] = true
				cnt++
			}
		}
		if cnt > 1 {
			pair = true
		}
	}
	if !pair {
		return false
	}
	ans := true
	for _, v := range c {
		ans = ans && v
	}
	return ans
}

func solve() {
	f := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	N, n, m, MAX := 0, 0, 0, -1000
	fmt.Fscan(f, &N)
	for i := 0; i < N; i++ {
		fmt.Fscan(f, &n, &m)
		o = make([][]int, n)
		for j := 0; j < n; j++ {
			o[j] = make([]int, m)
		}
		for j := 0; j < n; j++ {
			for k := 0; k < m; k++ {
				fmt.Fscan(f, &o[j][k])
				MAX = max(MAX, o[j][k])
			}
		}
		l, r := 1, MAX+1
		for r-l > 1 {
			mid := (l + r) >> 1
			if check(mid, n, m) {
				l = mid
			} else {
				r = mid
			}
		}
		fmt.Fprintln(w, l)
	}
	w.Flush()
}

func main() {
	solve()
}

package main

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

var o [][]int

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

func check(x, n, m int) bool {
	c, pair := make([]bool, m), false
	for i := 0; i < n; i++ {
		cnt := 0
		for j := 0; j < m; j++ {
			if o[i][j] >= x {
				c[j] = true
				cnt++
			}
		}
		if cnt > 1 {
			pair = true
		}
	}
	if !pair {
		return false
	}
	ans := true
	for _, v := range c {
		ans = ans && v
	}
	return ans
}

func solve() {
	f := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	N, n, m := 0, 0, 0
	fmt.Fscan(f, &N)
	for i := 0; i < N; i++ {
		fmt.Fscan(f, &n, &m)
		o = make([][]int, n)
		for j := 0; j < n; j++ {
			o[j] = make([]int, m)
		}
		for j := 0; j < n; j++ {
			for k := 0; k < m; k++ {
				fmt.Fscan(f, &o[j][k])
			}
		}
		l, r := 1, 2
		for check(r, n, m) {
			r *= 2
		}
		for r-l > 1 {
			mid := (l + r) >> 1
			if check(mid, n, m) {
				l = mid
			} else {
				r = mid
			}
		}
		fmt.Fprintln(w, l)
	}
	w.Flush()
}

func main() {
	solve()
}

package main

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

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

func main() {

	var t int64
	fmt.Fscan(rr, &t)

	for i := int64(0); i < t; i++ {

		var m int64
		fmt.Fscan(rr, &m)

		var n int64
		fmt.Fscan(rr, &n)

		var ar [][]int64

		var min int64
		var max int64

		for j := int64(0); j < m; j++ {
			var arr []int64

			for k := int64(0); k < n; k++ {
				var tmp int64
				fmt.Fscan(rr, &tmp)

				arr = append(arr, tmp)
			}

			ar = append(ar, arr)
		}

		min = ar[0][0]
		max = ar[0][0]

		for j := int64(0); j < m; j++ {

			for k := int64(0); k < n; k++ {
				if ar[j][k] > max {
					max = ar[j][k]
				}

				if ar[j][k] < min {
					min = ar[j][k]
				}
			}

		}
		ans := int64(-1)

		for min <= max {
			mid := min + (max-min)/2
			// fmt.Println(mid)

			if check(ar, mid, n, m) {
				ans = mid
				min = mid + 1
			} else {
				max = mid - 1
			}
		}

		// ans := min

		fmt.Fprintln(ww, ans)
		ww.Flush()

	}
	ww.Flush()
}

func check(ar [][]int64, mid int64, n int64, m int64) bool {

	a := int64(-1)
	b := int64(-1)
	counter := int64(0)

	for j := int64(0); j < m; j++ {
		counter = 0
		a = -1
		b = -1

		for k := int64(0); k < n; k++ {
			if ar[j][k] >= mid {
				counter++
				// fmt.Println(a, b)
				if a == -1 {
					a = k
					// fmt.Println(a, b)

				} else if b == -1 {
					b = k
					break
				}
			}
		}
		if counter >= 2 {
			break
		}

	}

	if counter < 2 {
		return false
	}
	// fmt.Println(counter)

	// fmt.Println(a, b)

	for j := int64(0); j < n; j++ {
		isValid := false

		if j == a || j == b {
			continue
		}

		for k := int64(0); k < m; k++ {
			// fmt.Println(ar[k][j])
			if ar[k][j] >= mid {
				isValid = true
			}
		}

		if isValid == false {
			return false
		}

	}
	return true

	// if toUse <= (n-1) && toUse > 0 {
	// 	return true
	// }
	// return false
}

package main

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

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

	var T, n, m int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &m)
		a := make([][]int, n)
		for i := range a {
			a[i] = make([]int, m)
			for j := range a[i] {
				Fscan(in, &a[i][j])
			}
		}
		Fprintln(out, sort.Search(1e9, func(low int) bool {
		o:
			for j := range a[0] {
				for _, r := range a {
					if r[j] > low {
						continue o
					}
				}
				return true
			}
			for _, r := range a {
				ok := false
				for _, v := range r {
					if v > low {
						if ok {
							return false
						}
						ok = true
					}
				}
			}
			return true
		}))
	}
}

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

package main

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

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

func main() {
	defer writer.Flush()
	// redirectInputToFile(reader, "input.txt")

	T := 1
	scan(&T)
	for t := 0; t < T; t++ {
		solve()
	}
}

func solve() {
	var n, m int
	scan(&m, &n)
	p := make([][]int, m)
	for i := range p {
		p[i] = make([]int, n)
		for j := range p[i] {
			scan(&p[i][j])
		}
	}

	chk1 := func(x int) bool {
		set := map[int]int{}
		for i := 0; i < m; i++ {
			for j := range p[i] {
				if p[i][j] >= x {
					set[j]++
				}
			}
		}
		return len(set) == n
	}

	chk2 := func(x int) bool {
		cntByFriends := make([]int, n)
		cntByShops := make([]int, m)
		for i := range p {
			for j := range p[i] {
				if p[i][j] >= x {
					cntByFriends[j]++
					cntByShops[i]++
				}
			}
		}
		for i := range p {
			if cntByShops[i] >= 2 {
				ok := true
				for j := range p[i] {
					if p[i][j] < x && cntByFriends[j] == 0 {
						ok = false
					}
				}
				if ok {
					return true
				}
			}
		}
		return false
	}

	check := func(x int) bool {
		if n > m {
			return chk1(x)
		}
		return chk2(x)
	}

	l, r := 1, int(1e9+1)
	for r-l > 1 {
		x := l + (r-l)/2
		if check(x) {
			l = x
		} else {
			r = x
		}
	}

	printf("%v\n", l)
}

package main

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

var o [][]int

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

func check(x, n, m int) bool {
	c, pair := make([]bool, m), false
	for i := 0; i < n; i++ {
		cnt := 0
		for j := 0; j < m; j++ {
			if o[i][j] >= x {
				c[j] = true
				cnt++
			}
		}
		if cnt > 1 {
			pair = true
		}
	}
	if !pair {
		return false
	}
	ans := true
	for _, v := range c {
		ans = ans && v
	}
	return ans
}

func solve() {
	f := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	N, n, m, MAX := 0, 0, 0, -1000
	fmt.Fscan(f, &N)
	for i := 0; i < N; i++ {
		fmt.Fscan(f, &n, &m)
		o = make([][]int, n)
		for j := 0; j < n; j++ {
			o[j] = make([]int, m)
		}
		for j := 0; j < n; j++ {
			for k := 0; k < m; k++ {
				fmt.Fscan(f, &o[j][k])
				MAX = max(MAX, o[j][k])
			}
		}
		l, r := 1, MAX
		for r-l > 1 {
			mid := (l + r) >> 1
			if check(mid, n, m) {
				l = mid
			} else {
				r = mid
			}
		}
		fmt.Fprintln(w, l)
	}
	w.Flush()
}

func main() {
	solve()
}

package main

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

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

func main() {

	var t int64
	fmt.Fscan(rr, &t)

	for i := int64(0); i < t; i++ {

		var m int64
		fmt.Fscan(rr, &m)

		var n int64
		fmt.Fscan(rr, &n)

		var ar [][]int64

		var min int64
		var max int64

		for j := int64(0); j < m; j++ {
			var arr []int64

			for k := int64(0); k < n; k++ {
				var tmp int64
				fmt.Fscan(rr, &tmp)

				arr = append(arr, tmp)
			}

			ar = append(ar, arr)
		}

		min = ar[0][0]
		max = ar[0][0]

		for j := int64(0); j < m; j++ {

			for k := int64(0); k < n; k++ {
				if ar[j][k] > max {
					max = ar[j][k]
				}

				if ar[j][k] < min {
					min = ar[j][k]
				}
			}

		}
		ans := int64(-1)

		for min <= max {
			mid := min + (max-min)/2
			// fmt.Println(mid)

			if check(ar, mid, n, m) {
				ans = mid
				min = mid + 1
			} else {
				max = mid - 1
			}
		}

		// ans := min

		fmt.Fprintln(ww, ans)
		ww.Flush()

	}
	ww.Flush()
}

func check(ar [][]int64, mid int64, n int64, m int64) bool {

	a := int64(-1)
	b := int64(-1)
	counter := int64(0)

	for j := int64(0); j < m; j++ {
		counter = 0
		a = 0
		b = 0

		for k := int64(0); k < n; k++ {
			if ar[j][k] >= mid {
				counter++
				if a == 0 {
					a = j
				} else if b == 0 {
					b = j
				}
			}
		}
		if counter >= 2 {
			break
		}

	}

	if counter < 2 {
		return false
	}

	for j := int64(0); j < n; j++ {
		isValid := false

		if j == a || j == b {
			continue
		}

		for k := int64(0); k < m; k++ {
			if ar[k][j] >= mid {
				isValid = true
			}
		}

		if isValid == false {
			return false
		}

	}
	return true

	// if toUse <= (n-1) && toUse > 0 {
	// 	return true
	// }
	// return false
}