package main

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

var in *bufio.Reader

type seg struct {
	l, r int
	c    int64
}

type lr struct {
	l, r int
}

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

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

	segs := make([]seg, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &segs[i].l)
		fmt.Fscan(in, &segs[i].r)
		fmt.Fscan(in, &segs[i].c)
	}

	left, right := segs[0].l, segs[0].r
	lMinCost, rMinCost := make(map[int]int64), make(map[int]int64)
	lrMinCost := make(map[lr]int64)
	for i := 0; i < n; i++ {
		si := segs[i]
		if si.l < left {
			left = si.l
		}
		if right < si.r {
			right = si.r
		}

		v, ok := lMinCost[si.l]
		if !ok {
			lMinCost[si.l] = si.c
		} else {
			lMinCost[si.l] = min(v, si.c)
		}
		v, ok = rMinCost[si.r]
		if !ok {
			rMinCost[si.r] = si.c
		} else {
			rMinCost[si.r] = min(v, si.c)
		}
		v, ok = lrMinCost[lr{si.l, si.r}]
		if !ok {
			lrMinCost[lr{si.l, si.r}] = si.c
		} else {
			lrMinCost[lr{si.l, si.r}] = min(v, si.c)
		}

		cost := lMinCost[left] + rMinCost[right]
		v, ok = lrMinCost[lr{left, right}]
		if ok {
			cost = min(cost, v)
		}
		fmt.Println(cost)
	}
}

func main() {
	in = bufio.NewReader(os.Stdin)

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

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

// generated by 1e9y at 06-01-2022
// template version: 2022-01

package main

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

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

var inf uint64 = math.MaxUint64

// 1621.B
func solve(shop [][]uint64) []uint64 {
	result := make([]uint64, len(shop))
	var lmin, rmin, lrmin uint64 = inf, inf, inf
	var l, r uint64 = 1e9 + 1, 0
	for i, s := range shop {
		if s[0] < l {
			lmin = inf
			lrmin = inf
			l = s[0]
		}
		if s[1] > r {
			rmin = inf
			lrmin = inf
			r = s[1]
		}
		if s[0] == l {
			lmin = min(lmin, s[2])
		}
		if s[1] == r {
			rmin = min(rmin, s[2])
		}
		if s[0] == l && s[1] == r {
			lrmin = min(lrmin, s[2])
		}
		result[i] = min(lmin+rmin, lrmin)
	}
	return result
}

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

	t := ReadInt(in)
	for i := 0; i < t; i++ {
		n := ReadInt(in)
		shop := make([][]uint64, n)
		for j := 0; j < n; j++ {
			shop[j] = ReadUint64Slice(in)
		}
		for _, line := range solve(shop) {
			Println(line)
		}
	}
}

// helpers for convenient input reading

func ReadInt(in *bufio.Reader) int {
	line, _ := in.ReadString('\n')
	line = strings.Trim(line, "\n\r")
	n, _ := strconv.Atoi(line)
	return n
}

func ReadLineParts(in *bufio.Reader) []string {
	line, _ := in.ReadString('\n')
	line = strings.Trim(line, "\n\r")
	parts := strings.Split(line, " ")
	return parts
}

func ReadIntSlice(in *bufio.Reader) []int {
	parts := ReadLineParts(in)
	slice := make([]int, len(parts))
	for i, n := range parts {
		slice[i] = MustAtoi(n)
	}
	return slice
}

func ReadInt64Slice(in *bufio.Reader) []int64 {
	parts := ReadLineParts(in)
	slice := make([]int64, len(parts))
	for i, n := range parts {
		slice[i] = MustParseInt(n, 10, 64)
	}
	return slice
}

func ReadUint64Slice(in *bufio.Reader) []uint64 {
	parts := ReadLineParts(in)
	slice := make([]uint64, len(parts))
	for i, n := range parts {
		slice[i] = MustParseUint(n, 10, 64)
	}
	return slice
}

func MustAtoi(s string) int {
	v, _ := strconv.Atoi(s)
	return v
}

func MustParseInt(s string, base, bitSize int) int64 {
	v, _ := strconv.ParseInt(s, base, bitSize)
	return v
}

func MustParseUint(s string, base, bitSize int) uint64 {
	v, _ := strconv.ParseUint(s, base, bitSize)
	return v
}

package main

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

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

	var T, n, l, r, c int
	for Fscan(in, &T); T > 0; T-- {
		ll, rr, lc, rc := int(2e9), 0, 0, 0
		sl, sr, sc := int(2e9), 0, 0
		for Fscan(in, &n); n > 0; n-- {
			Fscan(in, &l, &r, &c)
			if l < sl || l == sl && r > sr || l == sl && r == sr && c < sc {
				sl, sr, sc = l, r, c
			}
			if l < ll || l == ll && c < lc {
				ll, lc = l, c
			}
			if r > rr || r == rr && c < rc {
				rr, rc = r, c
			}
			res := lc + rc
			if sl == ll && sr == rr && sc < res {
				res = sc
			}
			Fprintln(out, res)
		}
	}
}

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

package main

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

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

	var T, n, l, r, c int
	for Fscan(in, &T); T > 0; T-- {
		ll, rr, lc, rc := int(2e9), 0, 0, 0
		sl, sr, sc := int(2e9), 0, 0
		for Fscan(in, &n); n > 0; n-- {
			Fscan(in, &l, &r, &c)
			if l < sl || l == sl && r > sr || l == sl && r == sr && c < sc {
				sl, sr, sc = l, r, c
			}
			if l < ll || l == ll && c < lc {
				ll, lc = l, c
			}
			if r > rr || r == rr && c < rc {
				rr, rc = r, c
			}
			res := lc + rc
			if sl == ll && sr == rr && sc < res {
				res = sc
			}
			Fprintln(out, res)
		}
	}
}

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

package main

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

func main() {
	reader := bReader(os.Stdin)
	writer := bWriter(os.Stdout)

	t := readInt(reader)

	for t > 0 {
		n := readInt(reader)

		cc := make([]int, n)
		ll := make([]int, n)
		rr := make([]int, n)

		idMin := 0
		idMax := 0
		idSub := 0
		for i := 0; i < n; i++ {
			l, r, c := readThreeInt(reader)
			cc[i] = c
			ll[i] = l
			rr[i] = r

			if l < ll[idMin] || l == ll[idMin] && cc[idMin] > c {
				idMin = i
			}

			if r > rr[idMax] || r == rr[idMax] && cc[idMax] > c {
				idMax = i
			}

			if r - l > rr[idSub] - ll[idSub] || r - l == rr[idSub] - ll[idSub] && cc[idSub] > c {
				idSub = i
			}

			if ll[idMin] >= ll[idSub] && rr[idMax] <= rr[idSub] && cc[idMin] + cc[idMax] >= cc[idSub] {
				fmt.Fprintln(writer, cc[idSub])
			} else {
				fmt.Fprintln(writer, cc[idMin] + cc[idMax])
			}
		}

		t--
	}

	writer.Flush()
}

func bReader(stdin *os.File) *bufio.Reader {
	return bufio.NewReaderSize(stdin, 16*1024*1024)
}

func bWriter(stdout *os.File) *bufio.Writer {
	return bufio.NewWriterSize(stdout, 16*1024*1024)
}

func readLine(reader *bufio.Reader) string {
	str, _, err := reader.ReadLine()
	if err == io.EOF {
		return ""
	}

	return strings.TrimRight(string(str), "\r\n")
}

func checkError(err error) {
	if err != nil {
		panic(err)
	}
}

func readInt(reader *bufio.Reader) int {
	return int(readInt64(reader))
}

func readInt64(reader *bufio.Reader) int64 {
	value, err := strconv.ParseInt(readLine(reader), 10, 64)
	checkError(err)

	return value
}

func readString(reader *bufio.Reader) string {
	return readLine(reader)
}

func readArrayString(reader *bufio.Reader) []string {
	return strings.Split(strings.TrimSpace(readLine(reader)), " ")
}

func readArrayInt(reader *bufio.Reader) []int {
	arTemp := strings.Split(strings.TrimSpace(readLine(reader)), " ")
	n := len(arTemp)

	var ar []int

	for i := 0; i < n; i++ {
		arItemTemp, err := strconv.ParseInt(arTemp[i], 10, 64)
		checkError(err)
		arItem := int(arItemTemp)
		ar = append(ar, arItem)
	}

	return ar
}

func readArrayInt64(reader *bufio.Reader) []int64 {
	arTemp := strings.Split(strings.TrimSpace(readLine(reader)), " ")
	n := len(arTemp)

	var ar []int64

	for i := 0; i < n; i++ {
		arItemTemp, err := strconv.ParseInt(arTemp[i], 10, 64)
		checkError(err)
		arItem := arItemTemp
		ar = append(ar, arItem)
	}

	return ar
}

func readTwoInt(reader *bufio.Reader) (int, int) {
	ar := readArrayInt(reader)
	return ar[0], ar[1]
}

func readTwoInt64(reader *bufio.Reader) (int64, int64) {
	ar := readArrayInt64(reader)
	return ar[0], ar[1]
}

func readThreeInt(reader *bufio.Reader) (int, int, int) {
	ar := readArrayInt(reader)
	return ar[0], ar[1], ar[2]
}

func readThreeInt64(reader *bufio.Reader) (int64, int64, int64) {
	ar := readArrayInt64(reader)
	return ar[0], ar[1], ar[2]
}

func gcd(x int64, y int64) int64 {
	for y > 0 {
		x, y = y, x % y
	}

	return x
}

func lcm(x int64, y int64) int64 {
	return x*y/gcd(x,y)
}

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++ {
		B()
	}
}

func B() {
	var n int
	scan(&n)
	var s = make([]struct{ l, r, c int64 }, n)
	for i := range s {
		scan(&s[i].l, &s[i].r, &s[i].c)
	}
	var (
		prevans, ans int64 = -1, -1
		l, r               = 0, 0
	)
	for i := range s {
		if prevans == -1 {
			l, r = 0, 0
			prevans = s[0].c
			printf("%v\n", prevans)
			continue
		}
		ans = prevans
		cnt := s[r].r - s[l].l
		if cnt < s[i].r-s[i].l || (cnt == s[i].r-s[i].l && ans > s[i].c) {
			cnt = s[i].r - s[i].l
			ans = s[i].c
		}
		if cnt < s[i].r-s[l].l || (cnt == s[i].r-s[l].l && ans > s[i].c+s[l].c) {
			cnt = s[i].r - s[l].l
			ans = s[i].c + s[l].c
		}
		if cnt < s[r].r-s[i].l || (cnt == s[r].r-s[i].l && ans > s[r].c+s[i].c) {
			cnt = s[r].r - s[i].l
			ans = s[r].c + s[i].c
		}
		printf("%v\n", ans)
		if s[i].l < s[l].l || (s[i].l == s[l].l && s[i].c < s[l].c) {
			l = i
		}
		if s[i].r > s[r].r || (s[i].r == s[r].r && s[i].c < s[r].c) {
			r = i
		}
		prevans = ans
	}
}

package main

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

type seg struct {
	l, r, c int
}

func min(a int, rest ...int) int {
	for _, r := range rest {
		if r < a {
			a = r
		}
	}
	return a
}

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

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

		var l, r, c int
		fmt.Fscan(in, &l, &r, &c)

		low := seg{l, r, c}
		high := seg{l, r, c}
		long := seg{l, r, c}
		fmt.Fprintln(out, low.c)
		for i := 1; i < n; i++ {
			fmt.Fscan(in, &l, &r, &c)
			s := seg{l, r, c}

			if s.l < low.l || (s.l == low.l && s.c < low.c) {
				low = s
			}

			if s.r > high.r || (s.r == high.r && s.c < high.c) {
				high = s
			}

			if s.l < long.l || s.r > long.r || s.l == long.l && s.r == long.r && s.c < long.c {
				long = s
			}

			if low.l == long.l && high.r == long.r {
				fmt.Fprintln(out, min(low.c+high.c, long.c))
			} else if low.l == high.l {
				fmt.Fprintln(out, high.c)
			} else if low.r == high.r {
				fmt.Fprintln(out, low.c)
			} else {
				fmt.Fprintln(out, low.c+high.c)
			}
		}
	}
}

package main

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

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

type segment struct {
	lt, rt, c int
}

func main() {
	defer out.Flush()

	var tc int
	fmt.Fscan(in, &tc)
	for ; tc > 0; tc-- {
		var n int
		var mn, mx, longest, seg segment
		fmt.Fscan(in, &n, &seg.lt, &seg.rt, &seg.c)
		fmt.Fprintln(out, seg.c)
		mn = seg
		mx = seg
		longest = seg
		for i := 1; i < n; i++ {
			fmt.Fscan(in, &seg.lt, &seg.rt, &seg.c)
			if seg.lt < mn.lt || (seg.lt == mn.lt && seg.c < mn.c) {
				mn = seg
			}
			if seg.rt > mx.rt || (seg.rt == mx.rt && seg.c < mx.c) {
				mx = seg
			}
			if seg.rt - seg.lt > longest.rt - longest.lt || (seg.rt - seg.lt == longest.rt - longest.lt && seg.c < longest.c) {
				longest = seg
			}
			c := mn.c
			if mx.rt > mn.rt {
				c += mx.c
			}
			if longest.lt == mn.lt && longest.rt == mx.rt && longest.c < c {
				c = longest.c
			}
			fmt.Fprintln(out, c)
		}
	}
}

package main

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

type Segment struct {
	L int
	R int
	C int
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var tests int
	fmt.Fscan(in, &tests)
	for ; tests > 0; tests-- {
		var n int
		fmt.Fscan(in, &n)

		ar := make([]Segment, n)
		for i := range ar {
			fmt.Fscan(in, &ar[i].L, &ar[i].R, &ar[i].C)
		}

		minSeg := ar[0]
		maxSeg := ar[0]
		bothSeg := ar[0]
		for i := range ar {
			minSeg = Min(minSeg, ar[i])
			maxSeg = Max(maxSeg, ar[i])
			res := 0
			if minSeg == maxSeg {
				res = minSeg.C
			} else {
				res = minSeg.C + maxSeg.C
			}

			if minSeg.L >= ar[i].L && maxSeg.R <= ar[i].R && res >= ar[i].C &&
				bothCheck(bothSeg, ar[i]) {
				bothSeg = ar[i]
			}

			if minSeg.L >= bothSeg.L &&
				maxSeg.R <= bothSeg.R && res > bothSeg.C {
				res = bothSeg.C
			}

			fmt.Fprintln(out, res)
		}
	}
}

func bothCheck(bothSeg, s Segment) bool {
	if bothSeg.L == s.L && bothSeg.R == s.R && bothSeg.C > s.C {
		return true
	}
	if bothSeg.L > s.L && bothSeg.R < s.R ||
		bothSeg.L > s.L && bothSeg.R == s.R ||
		bothSeg.L == s.L && bothSeg.R < s.R {
		return true
	}
	return false
}

func Min(a, b Segment) Segment {
	if a.L < b.L {
		return a
	} else if a.L > b.L {
		return b
	} else {
		if a.C < b.C {
			return a
		}
		return b
	}
}

func Max(a, b Segment) Segment {
	if a.R > b.R {
		return a
	} else if a.R < b.R {
		return b
	} else {
		if a.C < b.C {
			return a
		}
		return b
	}
}

package main

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

const INF = int(2e9+10)

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

func main() {
	defer out.Flush()

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

	for; t > 0; t-- {
		var n int
		fmt.Fscan(in, &n);
		left := INF
		right := 0
		cLeft := INF
		cRight := INF
		longest := 0
		cLongest := INF
		var ans int
		for ; n > 0; n-- {
			var l, r, c int
			fmt.Fscan(in, &l, &r, &c);

			// Left
			if (left > l) {
				left = l
				cLeft = c
			} else if (left == l && cLeft > c) {cLeft = c}

			// Right
			if (right < r) {
				right = r
				cRight = c
			} else if (right == r && cRight > c) {cRight = c}

			// Longest
			if (r-l+1 > longest) {
				longest = r-l+1
				cLongest = c
			} else if (r-l+1 == longest && cLongest > c) { cLongest = c }

			ans = cLeft + cRight
			if (right-left+1 == longest && ans > cLongest) {
				ans = cLongest
			}

			fmt.Fprintln(out, ans)
		}
	}
}

package main

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

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 _int() int {
	s := _string()
	i := 0
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int(r - '0')
	}
	return i
}

func _int64() int64 {
	s := _string()
	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 _string() string {
	if !scanner.Scan() {
		panic(scanner.Err())
	}
	return scanner.Text()
}

type segment struct {
	l int
	r int
	c int
}

func main() {
	defer writer.Flush()
	for t := _int(); t > 0; t-- {
		n := _int()
		s := make([]segment, n)
		for i := range s {
			l := _int()
			r := _int()
			c := _int()
			s[i] = segment{l, r, c}
		}
		sol(s)
	}
}

func lmin(s1, s2 segment) segment {
	if s1.l == s2.l {
		if s1.c < s2.c {
			return s1
		}
		return s2
	}
	if s1.l < s2.l {
		return s1
	}
	return s2
}

func rmax(s1, s2 segment) segment {
	if s1.r == s2.r {
		if s1.c < s2.c {
			return s1
		}
		return s2
	}
	if s1.r > s2.r {
		return s1
	}
	return s2
}

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

func sol(s []segment) {
	ls := segment{l: math.MaxInt, r: math.MaxInt, c: math.MaxInt}
	rs := segment{l: math.MinInt, r: math.MinInt, c: math.MaxInt}
	long := segment{l: math.MaxInt, r: math.MinInt, c: math.MaxInt}
	for i := range s {
		ls = lmin(ls, s[i])
		rs = rmax(rs, s[i])
		if s[i].r >= long.r && s[i].l <= long.l {
			if !(s[i].r == long.r && s[i].l == long.l && s[i].c > long.c) {
				long = s[i]
			}
		}
		// printf("debug %v %v %v\n", ls, rs, long)
		if long.l == ls.l && long.r == rs.r {
			printf("%d\n", min(long.c, ls.c+rs.c))
		} else {
			printf("%d\n", ls.c+rs.c)
		}
	}
}

package main

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

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 _int() int {
	s := _string()
	i := 0
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int(r - '0')
	}
	return i
}

func _int64() int64 {
	s := _string()
	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 _string() string {
	if !scanner.Scan() {
		panic(scanner.Err())
	}
	return scanner.Text()
}

type segment struct {
	l int64
	r int64
	c int64
}

func main() {
	defer writer.Flush()
	for t := _int(); t > 0; t-- {
		n := _int()
		s := make([]segment, n)
		for i := range s {
			l := _int64()
			r := _int64()
			c := _int64()
			s[i] = segment{l, r, c}
		}
		sol(s)
	}
}

func lmin(s1, s2 segment) segment {
	if s1.l == s2.l {
		if s1.c < s2.c {
			return s1
		}
		return s2
	}
	if s1.l < s2.l {
		return s1
	}
	return s2
}

func rmax(s1, s2 segment) segment {
	if s1.r == s2.r {
		if s1.c < s2.c {
			return s1
		}
		return s2
	}
	if s1.r > s2.r {
		return s1
	}
	return s2
}

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

func sol(s []segment) {
	ls := segment{l: math.MaxInt, r: math.MaxInt, c: math.MaxInt}
	rs := segment{l: math.MinInt, r: math.MinInt, c: math.MaxInt}
	long := segment{l: math.MaxInt, r: math.MinInt, c: math.MaxInt}
	for i := range s {
		ls = lmin(ls, s[i])
		rs = rmax(rs, s[i])
		if s[i].r >= long.r && s[i].l <= long.l {
			if !(s[i].r == long.r && s[i].l == long.l && s[i].c > long.c) {
				long = s[i]
			}
		}
		// printf("debug %v %v %v\n", ls, rs, long)
		if long.l == ls.l && long.r == rs.r {
			printf("%d\n", min(long.c, ls.c+rs.c))
		} else {
			printf("%d\n", ls.c+rs.c)
		}
	}
}

package main

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

/*
1
3
4 5 61
75 87 4
6 14 1
---
65 <- 61 + 4
=======
1
11
1 4 4
5 8 9
7 8 7
2 10 252
1 11 271
1 10 1
1 5 1
11 11 7
1 11 6
8 12 10
10 12 8
---
4
13
11
256
271
271
271
8
6
11
9
*/

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

	bld := strings.Builder{}

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

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

		minL := maxInt
		minLCost := -1

		maxR := minInt
		maxRCost := -1

		maxLen := 0
		maxLenCost := 0

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

			if l < minL || l == minL && c < minLCost {
				minL = l
				minLCost = c
			}

			if r > maxR || r == maxR && c < maxRCost {
				maxR = r
				maxRCost = c
			}

			length := r - l + 1
			if length > maxLen || length == maxLen && c < maxLenCost {
				maxLen = length
				maxLenCost = c
			}

			bestCost := 0

			if maxLen > maxR-minL+1 {
				bestCost = maxLenCost
			} else if maxLen < maxR-minL+1 {
				bestCost = minLCost + maxRCost
			} else {
				bestCost = min(maxLenCost, minLCost+maxRCost)
			}

			fmt.Fprintln(&bld, bestCost)
		}
	}

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

const maxUint = ^uint(0)

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

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

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

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

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

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

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

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

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

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

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

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

	return true
}

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

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

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

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

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

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	var buf bytes.Buffer
	tc := readNum(reader)

	for tc > 0 {
		tc--
		n := readNum(reader)
		segs := make([][]int, n)
		for i := 0; i < n; i++ {
			segs[i] = readNNums(reader, 3)
		}
		res := solve(n, segs)

		for _, num := range res {
			buf.WriteString(fmt.Sprintf("%d\n", num))
		}
	}
	fmt.Print(buf.String())
}

func normalize(s string) string {

	for i := len(s); i > 0; i-- {
		if s[i-1] >= 'a' && s[i-1] <= 'z' {
			return s[:i]
		}
	}
	return ""
}

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, segs [][]int) []int {
	//只有l,r 是关键
	res := make([]int, n)
	const INF = 1 << 30

	minL := INF
	costL := INF
	maxR := 0
	costR := INF
	maxLen := 0
	costLen := INF

	for i := 0; i < n; i++ {
		l, r, c := segs[i][0], segs[i][1], segs[i][2]
		if l < minL {
			minL = l
			costL = INF
		}
		if minL == l {
			costL = min(costL, c)
		}
		if maxR < r {
			maxR = r
			costR = INF
		}
		if maxR == r {
			costR = min(costR, c)
		}
		if maxLen < r-l+1 {
			maxLen = r - l + 1
			costLen = INF
		}
		if maxLen == r-l+1 {
			costLen = min(costLen, c)
		}
		tmp := costL + costR
		if maxLen == maxR-minL+1 {
			tmp = min(tmp, costLen)
		}
		res[i] = tmp
	}

	return res
}

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

package main

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

type seg struct {
	l, r, c int
}

func min(a int, rest ...int) int {
	for _, r := range rest {
		if r < a {
			a = r
		}
	}
	return a
}

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

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

		var l, r, c int
		fmt.Fscan(in, &l, &r, &c)

		low := seg{l, r, c}
		high := seg{l, r, c}
		long := seg{l, r, c}
		fmt.Fprintln(out, low.c)
		for i := 1; i < n; i++ {
			fmt.Fscan(in, &l, &r, &c)
			s := seg{l, r, c}

			if s.l < low.l || (s.l == low.l && s.c < low.c) {
				low = s
			}

			if s.r > high.r || (s.r == high.r && s.c < high.c) {
				high = s
			}

			if s.l < long.l || s.r > long.r || s.l == long.l && s.r == long.r && s.c < long.c {
				long = s
			}

			fmt.Println(low, high, long)
			if low.l == long.l && high.r == long.r {
				fmt.Fprintln(out, min(low.c+high.c, long.c))
			} else if low.l == high.l {
				fmt.Fprintln(out, high.c)
			} else if low.r == high.r {
				fmt.Fprintln(out, low.c)
			} else {
				fmt.Fprintln(out, low.c+high.c)
			}
		}
	}
}

package main

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

type seg struct {
	l, r, c int
}

func min(a int, rest ...int) int {
	for _, r := range rest {
		if r < a {
			a = r
		}
	}
	return a
}

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

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

		var l, r, c int
		fmt.Fscan(in, &l, &r, &c)

		low := seg{l, r, c}
		high := seg{l, r, c}
		long := seg{l, r, c}
		fmt.Fprintln(out, low.c)
		for i := 1; i < n; i++ {
			fmt.Fscan(in, &l, &r, &c)
			s := seg{l, r, c}

			if s.l < low.l || (s.l == low.l && s.c < low.c) {
				low = s
			}

			if s.r > high.r || (s.r == high.r && s.c < high.c) {
				high = s
			}

			if s.l == low.l && s.r == high.r {
				long = s
			}

			if low.l == long.l && high.r == long.r {
				fmt.Fprintln(out, min(low.c+high.c, long.c))
			} else if low.l == high.l {
				fmt.Fprintln(out, high.c)
			} else if low.r == high.r {
				fmt.Fprintln(out, low.c)
			} else {
				fmt.Fprintln(out, low.c+high.c)
			}
		}
	}
}

package main

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

type seg struct {
	l, r, c int
}

func min(a int, rest ...int) int {
	for _, r := range rest {
		if r < a {
			a = r
		}
	}
	return a
}

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

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

		var l, r, c int
		fmt.Fscan(in, &l, &r, &c)

		low := seg{l, r, c}
		high := seg{l, r, c}
		fmt.Fprintln(out, low.c)
		for i := 1; i < n; i++ {
			fmt.Fscan(in, &l, &r, &c)
			s := seg{l, r, c}

			if s.l < low.l || (s.l == low.l && s.c < low.c) || s.l == low.l && s.r == high.r && s.c < low.c+high.c {
				low = s
			}

			if s.r > high.r || (s.r == high.r && s.c < high.c) {
				high = s
			}

			if low.l == high.l && low.r == high.r {
				fmt.Fprintln(out, min(low.c, high.c))
			} else if low.l == high.l {
				fmt.Fprintln(out, high.c)
			} else if low.r == high.r {
				fmt.Fprintln(out, low.c)
			} else {
				fmt.Fprintln(out, low.c+high.c)
			}
		}
	}
}

package main

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

type seg struct {
	l, r, c int
}

func min(a int, rest ...int) int {
	for _, r := range rest {
		if r < a {
			a = r
		}
	}
	return a
}

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

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

		var l, r, c int
		fmt.Fscan(in, &l, &r, &c)

		low := seg{l, r, c}
		high := seg{l, r, c}
		fmt.Fprintln(out, low.c)
		for i := 1; i < n; i++ {
			fmt.Fscan(in, &l, &r, &c)
			s := seg{l, r, c}

			if s.l < low.l || (s.l == low.l && s.c < low.c) {
				low = s
			}

			if s.r > high.r || (s.r == high.r && s.c < high.c) {
				high = s
			}

			if low.l == high.l && low.r == high.r {
				fmt.Fprintln(out, min(low.c, high.c))
			} else if low.l <= high.l {
				fmt.Fprintln(out, high.c)
			} else if low.r >= high.r {
				fmt.Fprintln(out, low.c)
			} else {
				fmt.Fprintln(out, low.c+high.c)
			}
		}
	}
}

package main

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

type seg struct {
	l, r, c int
}

func min(a int, rest ...int) int {
	for _, r := range rest {
		if r < a {
			a = r
		}
	}
	return a
}

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

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

		var l, r, c int
		fmt.Fscan(in, &l, &r, &c)

		low := seg{l, r, c}
		high := seg{l, r, c}
		fmt.Fprintln(out, low.c)
		for i := 1; i < n; i++ {
			fmt.Fscan(in, &l, &r, &c)
			s := seg{l, r, c}

			if s.l < low.l || s.l == low.l && s.c < low.c {
				low = s
			}

			if s.r > high.r || s.r == high.r && s.c < high.c {
				high = s
			}

			if low.l == high.l && low.r == high.r {
				fmt.Fprintln(out, min(low.c, high.c))
			} else if low.l == high.l {
				fmt.Fprintln(out, high.c)
			} else if low.r == high.r {
				fmt.Fprintln(out, low.c)
			} else {
				fmt.Fprintln(out, low.c+high.c)
			}
		}
	}
}

package main

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

type seg struct {
	l, r, c int
}

func min(a int, rest ...int) int {
	for _, r := range rest {
		if r < a {
			a = r
		}
	}
	return a
}

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

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

		var l, r, c int
		fmt.Fscan(in, &l, &r, &c)

		low := seg{l, r, c}
		high := seg{l, r, c}
		fmt.Fprintln(out, low.c)
		for i := 1; i < n; i++ {
			fmt.Fscan(in, &l, &r, &c)
			s := seg{l, r, c}

			if s.l < low.l || s.l == low.l && s.c < low.c {
				low = s
			}

			if s.r > high.r || s.r == high.r && s.c < high.c {
				high = s
			}

			if low.l == high.l || low.r == high.r {
				fmt.Fprintln(out, min(low.c, high.c))
			} else {
				fmt.Fprintln(out, low.c+high.c)
			}
		}
	}
}

package main

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

type seg struct {
	l, r, c int
}

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

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

		var l, r, c int
		fmt.Fscan(in, &l, &r, &c)

		low := seg{l, r, c}
		high := seg{l, r, c}
		fmt.Fprintln(out, low.c)
		for i := 1; i < n; i++ {
			fmt.Fscan(in, &l, &r, &c)
			s := seg{l, r, c}

			if s.l < low.l || s.l == low.l && s.c < low.c {
				low = s
			}

			if s.r > high.r || s.r == high.r && s.c < high.c {
				high = s
			}

			if low.l == high.l {
				fmt.Fprintln(out, high.c)
			} else {
				fmt.Fprintln(out, low.c+high.c)
			}
		}
	}
}

package main

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

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 _int() int {
	s := _string()
	i := 0
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int(r - '0')
	}
	return i
}

func _int64() int64 {
	s := _string()
	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 _string() string {
	if !scanner.Scan() {
		panic(scanner.Err())
	}
	return scanner.Text()
}

type segment struct {
	l int64
	r int64
	c int64
}

func main() {
	defer writer.Flush()
	for t := _int(); t > 0; t-- {
		n := _int()
		s := make([]segment, n)
		for i := range s {
			l := _int64()
			r := _int64()
			c := _int64()
			s[i] = segment{l, r, c}
		}
		sol(s)
	}
}

func lmin(s1, s2 segment) segment {
	if s1.l == s2.l {
		if s1.c < s2.c {
			return s1
		}
		return s2
	}
	if s1.l < s2.l {
		return s1
	}
	return s2
}

func rmax(s1, s2 segment) segment {
	if s1.r == s2.r {
		if s1.c < s2.c {
			return s1
		}
		return s2
	}
	if s1.r > s2.r {
		return s1
	}
	return s2
}

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

func sol(s []segment) {
	ls := segment{l: math.MaxInt, r: math.MaxInt, c: math.MaxInt}
	rs := segment{l: math.MinInt, r: math.MinInt, c: math.MaxInt}
	long := segment{l: math.MaxInt, r: math.MinInt, c: math.MaxInt}
	for i := range s {
		ls = lmin(ls, s[i])
		rs = rmax(rs, s[i])
		if s[i].r == rs.r && s[i].l == ls.l && !(s[i] == long && s[i].c > long.c) {
			long = s[i]
		}
		// printf("debug %v %v %v\n", ls, rs, long)
		if long.l == ls.l && long.r == rs.r {
			printf("%d\n", min(long.c, ls.c+rs.c))
		} else {
			printf("%d\n", ls.c+rs.c)
		}
	}
}

package main

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

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 _int() int {
	s := _string()
	i := 0
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int(r - '0')
	}
	return i
}

func _int64() int64 {
	s := _string()
	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 _string() string {
	if !scanner.Scan() {
		panic(scanner.Err())
	}
	return scanner.Text()
}

type segment struct {
	l int
	r int
	c int
}

func main() {
	defer writer.Flush()
	for t := _int(); t > 0; t-- {
		n := _int()
		s := make([]segment, n)
		for i := range s {
			l := _int()
			r := _int()
			c := _int()
			s[i] = segment{l, r, c}
		}
		sol(s)
	}
}

func lmin(s1, s2 segment) segment {
	if s1.l == s2.l {
		if s1.c < s2.c {
			return s1
		}
		return s2
	}
	if s1.l < s2.l {
		return s1
	}
	return s2
}

func rmax(s1, s2 segment) segment {
	if s1.r == s2.r {
		if s1.c < s2.c {
			return s1
		}
		return s2
	}
	if s1.r > s2.r {
		return s1
	}
	return s2
}

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

func sol(s []segment) {
	ls := segment{l: math.MaxInt, r: math.MaxInt, c: math.MaxInt}
	rs := segment{l: math.MinInt, r: math.MinInt, c: math.MaxInt}
	long := segment{l: math.MaxInt, r: math.MinInt, c: math.MaxInt}
	for i := range s {
		ls = lmin(ls, s[i])
		rs = rmax(rs, s[i])
		if s[i].r == rs.r && s[i].l == ls.l && !(s[i] == long && s[i].c > long.c) {
			long = s[i]
		}
		// printf("debug %v %v %v\n", ls, rs, long)
		if long.l == ls.l && long.r == rs.r {
			printf("%d\n", min(long.c, ls.c+rs.c))
		} else {
			printf("%d\n", ls.c+rs.c)
		}
	}
}

package main

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

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 _int() int {
	s := _string()
	i := 0
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int(r - '0')
	}
	return i
}

func _int64() int64 {
	s := _string()
	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 _string() string {
	if !scanner.Scan() {
		panic(scanner.Err())
	}
	return scanner.Text()
}

type segment struct {
	l int
	r int
	c int
}

func main() {
	defer writer.Flush()
	for t := _int(); t > 0; t-- {
		n := _int()
		s := make([]segment, n)
		for i := range s {
			l := _int()
			r := _int()
			c := _int()
			s[i] = segment{l, r, c}
		}
		sol(s)
	}
}

func lmin(s1, s2 segment) segment {
	if s1.l == s2.l {
		if s1.c < s2.c {
			return s1
		}
		return s2
	}
	if s1.l < s2.l {
		return s1
	}
	return s2
}

func rmax(s1, s2 segment) segment {
	if s1.r == s2.r {
		if s1.c < s2.c {
			return s1
		}
		return s2
	}
	if s1.r > s2.r {
		return s1
	}
	return s2
}

func sol(s []segment) {
	ls := segment{l: math.MaxInt, r: math.MaxInt, c: math.MaxInt}
	rs := segment{l: math.MinInt, r: math.MinInt, c: math.MaxInt}
	for i := range s {
		ls = lmin(ls, s[i])
		rs = rmax(rs, s[i])
		if ls.l == rs.l {
			ls = rs
		}
		if ls.r == rs.r {
			rs = ls
		}
		// printf("debug: %v %v\n", ls, rs)
		if ls == rs {
			printf("%d\n", ls.c)
			continue
		}
		printf("%d\n", ls.c+rs.c)
	}
}

package main

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

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 _int() int {
	s := _string()
	i := 0
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int(r - '0')
	}
	return i
}

func _int64() int64 {
	s := _string()
	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 _string() string {
	if !scanner.Scan() {
		panic(scanner.Err())
	}
	return scanner.Text()
}

type segment struct {
	l int
	r int
	c int
}

func main() {
	defer writer.Flush()
	for t := _int(); t > 0; t-- {
		n := _int()
		s := make([]segment, n)
		for i := range s {
			l := _int()
			r := _int()
			c := _int()
			s[i] = segment{l, r, c}
		}
		sol(s)
	}
}

func lmin(s1, s2 segment) segment {
	if s1.l == s2.l {
		if s1.c < s2.c {
			return s1
		}
		return s2
	}
	if s1.l < s2.l {
		return s1
	}
	return s2
}

func rmax(s1, s2 segment) segment {
	if s1.r == s2.r {
		if s1.c < s2.c {
			return s1
		}
		return s2
	}
	if s1.r > s2.r {
		return s1
	}
	return s2
}

func sol(s []segment) {
	ls := segment{l: math.MaxInt, r: math.MaxInt, c: math.MaxInt}
	rs := segment{l: math.MinInt, r: math.MinInt, c: math.MaxInt}
	for i := range s {
		ls = lmin(ls, s[i])
		rs = rmax(rs, s[i])
		// printf("debug: %v %v\n", ls, rs)
		if ls.l == rs.l {
			printf("%d\n", rs.c)
			continue
		}
		if ls.r == rs.r {
			printf("%d\n", ls.c)
			continue
		}
		printf("%d\n", ls.c+rs.c)
	}
}

package main

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

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 _int() int {
	s := _string()
	i := 0
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int(r - '0')
	}
	return i
}

func _int64() int64 {
	s := _string()
	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 _string() string {
	if !scanner.Scan() {
		panic(scanner.Err())
	}
	return scanner.Text()
}

type segment struct {
	l int
	r int
	c int
}

func main() {
	defer writer.Flush()
	for t := _int(); t > 0; t-- {
		n := _int()
		s := make([]segment, n)
		for i := range s {
			l := _int()
			r := _int()
			c := _int()
			s[i] = segment{l, r, c}
		}
		sol(s)
	}
}

func min(s1, s2 segment) segment {
	if s1.l == s2.l {
		if s1.c < s2.c {
			return s1
		}
		return s2
	}
	if s1.l < s2.l {
		return s1
	}
	return s2
}

func max(s1, s2 segment) segment {
	if s1.r == s2.r {
		if s1.c < s2.c {
			return s1
		}
		return s2
	}
	if s1.r > s2.r {
		return s1
	}
	return s2
}

func sol(s []segment) {
	ls := segment{l: math.MaxInt, r: math.MaxInt, c: math.MaxInt}
	rs := segment{l: math.MinInt, r: math.MinInt, c: math.MinInt}
	for i := range s {
		ls = min(ls, s[i])
		rs = max(rs, s[i])
		// printf("debug: %v %v\n", ls, rs)
		if ls.l == rs.l {
			printf("%d\n", rs.c)
			continue
		}
		if ls.r == rs.r {
			printf("%d\n", ls.c)
			continue
		}
		printf("%d\n", ls.c+rs.c)
	}
}

package main

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

var in *bufio.Reader

type seg struct {
	l, r int
	c    int64
}

func calCost(l, r int, segs []seg) int64 {
	cost := segs[l].c
	if l != r {
		cost += segs[r].c
	}
	return cost
}

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

	segs := make([]seg, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &segs[i].l)
		fmt.Fscan(in, &segs[i].r)
		fmt.Fscan(in, &segs[i].c)
	}

	lIdx, rIdx := 0, 0
	fmt.Println(segs[0].c)
	for i := 1; i < n; i++ {
		if segs[i].l < segs[lIdx].l {
			lIdx = i
		}
		if segs[rIdx].r < segs[i].r {
			rIdx = i
		}

		cost := calCost(lIdx, rIdx, segs)
		if segs[i].l == segs[lIdx].l && segs[i].r == segs[rIdx].r {
			nCost := calCost(i, i, segs)
			if nCost < cost {
				cost = nCost
				lIdx = i
				rIdx = i
			}
		} else if segs[i].l == segs[lIdx].l {
			nCost := calCost(i, rIdx, segs)
			if nCost < cost {
				cost = nCost
				lIdx = i
			}
		} else if segs[i].r == segs[rIdx].r {
			nCost := calCost(lIdx, i, segs)
			if nCost < cost {
				cost = nCost
				rIdx = i
			}
		}
		fmt.Println(cost)
	}
}

func main() {
	in = bufio.NewReader(os.Stdin)

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

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

package main

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

var in *bufio.Reader

type seg struct {
	l, r, c int
}

func calCost(l, r int, segs []seg) int {
	cost := segs[l].c
	if l != r {
		cost += segs[r].c
	}
	return cost
}

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

	segs := make([]seg, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &segs[i].l)
		fmt.Fscan(in, &segs[i].r)
		fmt.Fscan(in, &segs[i].c)
	}

	lIdx, rIdx := 0, 0
	fmt.Println(segs[0].c)
	for i := 1; i < n; i++ {
		if segs[i].l < segs[lIdx].l {
			lIdx = i
		}
		if segs[rIdx].r < segs[i].r {
			rIdx = i
		}

		cost := calCost(lIdx, rIdx, segs)
		if segs[i].l == segs[lIdx].l && segs[i].r == segs[rIdx].r {
			nCost := calCost(i, i, segs)
			if nCost < cost {
				cost = nCost
				lIdx = i
				rIdx = i
			}
		} else if segs[i].l == segs[lIdx].l {
			nCost := calCost(i, rIdx, segs)
			if nCost < cost {
				cost = nCost
				lIdx = i
			}
		} else if segs[i].r == segs[rIdx].r {
			nCost := calCost(lIdx, i, segs)
			if nCost < cost {
				cost = nCost
				rIdx = i
			}
		}
		fmt.Println(cost)
	}
}

func main() {
	in = bufio.NewReader(os.Stdin)

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

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

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	var buf bytes.Buffer
	tc := readNum(reader)

	for tc > 0 {
		tc--
		n := readNum(reader)
		segs := make([][]int, n)
		for i := 0; i < n; i++ {
			segs[i] = readNNums(reader, 3)
		}
		res := solve(n, segs)

		for _, num := range res {
			buf.WriteString(fmt.Sprintf("%d\n", num))
		}
	}
	fmt.Print(buf.String())
}

func normalize(s string) string {

	for i := len(s); i > 0; i-- {
		if s[i-1] >= 'a' && s[i-1] <= 'z' {
			return s[:i]
		}
	}
	return ""
}

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, segs [][]int) []int64 {
	//只有l,r 是关键
	res := make([]int64, n)
	res[0] = int64(segs[0][2])
	l, r := 0, 0
	for i := 1; i < n; i++ {
		cur := segs[i]
		if cur[0] < segs[l][0] {
			if cur[1] >= segs[r][1] {
				res[i] = int64(cur[2])
				r = i
			} else {
				// cur[1] < segs[r][1]
				res[i] = int64(cur[2]) + int64(segs[r][2])
			}
			l = i
		} else if cur[0] > segs[l][0] {
			if cur[1] < segs[r][1] || cur[1] == segs[r][1] && cur[2] > segs[r][2] {
				res[i] = res[i-1]
			} else if cur[1] > segs[r][1] {
				// cur[1] > segs[r][1]
				res[i] = int64(segs[l][2]) + int64(cur[2])
				r = i
			} else {
				// cur[1] == segs[r][1]
				if l == r || cur[2] >= segs[r][2] {
					res[i] = res[i-1]
				} else {
					res[i] = int64(segs[l][2]) + int64(cur[2])
					r = i
				}
			}
		} else {
			// cur[0] == segs[l][0]
			if cur[1] > segs[r][1] {
				res[i] = int64(cur[2])
				l, r = i, i
			} else if cur[1] < segs[r][1] {
				if l == r || cur[2] >= segs[l][2] {
					res[i] = res[i-1]
				} else {
					res[i] = int64(cur[2]) + int64(segs[r][2])
					l = i
				}
			} else {
				// cur covers up all
				if int64(cur[2]) < res[i-1] {
					res[i] = int64(cur[2])
					l, r = i, i
				} else {
					res[i] = res[i-1]
				}
			}
		}
	}

	return res
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	var buf bytes.Buffer
	tc := readNum(reader)

	for tc > 0 {
		tc--
		n := readNum(reader)
		segs := make([][]int, n)
		for i := 0; i < n; i++ {
			segs[i] = readNNums(reader, 3)
		}
		res := solve(n, segs)

		for _, num := range res {
			buf.WriteString(fmt.Sprintf("%d\n", num))
		}
	}
	fmt.Print(buf.String())
}

func normalize(s string) string {

	for i := len(s); i > 0; i-- {
		if s[i-1] >= 'a' && s[i-1] <= 'z' {
			return s[:i]
		}
	}
	return ""
}

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, segs [][]int) []int64 {
	//只有l,r 是关键
	res := make([]int64, n)
	res[0] = int64(segs[0][2])
	l, r := 0, 0
	for i := 1; i < n; i++ {
		cur := segs[i]
		if cur[0] < segs[l][0] {
			if cur[1] >= segs[r][1] {
				res[i] = int64(cur[2])
				l, r = i, i
			} else {
				// cur[1] < segs[r][1]
				res[i] = int64(cur[2]) + int64(segs[r][2])
				l = i
			}
		} else if cur[0] > segs[l][0] {
			if cur[1] < segs[r][1] || cur[1] == segs[r][1] && cur[2] > segs[r][2] {
				res[i] = res[i-1]
			} else if cur[1] > segs[r][1] {
				// cur[1] > segs[r][1]
				res[i] = int64(segs[l][2]) + int64(cur[2])
				r = i
			} else {
				// cur[1] == segs[r][1] && and more cheaper
				if l == r {
					res[i] = res[i-1]
				} else {
					res[i] = int64(segs[l][2]) + int64(cur[2])
					r = i
				}
			}
		} else {
			// cur[0] == segs[l][0]
			if cur[1] > segs[r][1] {
				res[i] = int64(cur[2])
				l, r = i, i
			} else if cur[1] < segs[r][1] {
				if l == r {
					res[i] = res[i-1]
				} else {
					res[i] = int64(cur[2]) + int64(segs[r][2])
					l = i
				}
			} else {
				// cur covers up all
				if int64(cur[2]) < res[i-1] {
					res[i] = int64(cur[2])
					l, r = i, i
				} else {
					res[i] = res[i-1]
				}
			}
		}
	}

	return res
}

package main

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

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

	var T, n, l, r, c int
	for Fscan(in, &T); T > 0; T-- {
		ll, rr, lc, rc := int(2e9), 0, 0, 0
		sl, sr, sc := int(2e9), 0, 0
		for Fscan(in, &n); n > 0; n-- {
			Fscan(in, &l, &r, &c)
			if l < sl || l == sl && r > sr || l == sl && r == sr && c < sc {
				sl, sr, sc = l, r, c
			}
			if l < ll || l == ll && c < lc {
				ll, lc = l, c
			}
			if r > rr || r == rr && c < rc {
				rr, rc = r, c
			}
			if sl == ll && sr == rr {
				Fprintln(out, sc)
			} else {
				Fprintln(out, lc+rc)
			}
		}
	}
}

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

package main

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

func main() {
	reader := bReader(os.Stdin)
	writer := bWriter(os.Stdout)

	t := readInt(reader)

	for t > 0 {
		n := readInt(reader)

		cc := make([]int, n)
		ll := make([]int, n)
		rr := make([]int, n)

		idMin := 0
		idMax := 0
		idSub := 0
		for i := 0; i < n; i++ {
			l, r, c := readThreeInt(reader)
			cc[i] = c
			ll[i] = l
			rr[i] = r

			if l < ll[idMin] || l == ll[idMin] && cc[idMin] > c {
				idMin = i
			}

			if r > rr[idMax] || r == rr[idMax] && cc[idMax] > c {
				idMax = i
			}

			if r - l >= rr[idSub] - ll[idSub] || r - l == rr[idSub] - ll[idSub] && cc[idSub] > c {
				idSub = i
			}

			if ll[idMin] >= ll[idSub] && rr[idMax] <= rr[idSub] && cc[idMin] + cc[idMax] >= cc[idSub] {
				fmt.Fprintln(writer, cc[idSub])
			} else {
				fmt.Fprintln(writer, cc[idMin] + cc[idMax])
			}
		}

		t--
	}

	writer.Flush()
}

func bReader(stdin *os.File) *bufio.Reader {
	return bufio.NewReaderSize(stdin, 16*1024*1024)
}

func bWriter(stdout *os.File) *bufio.Writer {
	return bufio.NewWriterSize(stdout, 16*1024*1024)
}

func readLine(reader *bufio.Reader) string {
	str, _, err := reader.ReadLine()
	if err == io.EOF {
		return ""
	}

	return strings.TrimRight(string(str), "\r\n")
}

func checkError(err error) {
	if err != nil {
		panic(err)
	}
}

func readInt(reader *bufio.Reader) int {
	return int(readInt64(reader))
}

func readInt64(reader *bufio.Reader) int64 {
	value, err := strconv.ParseInt(readLine(reader), 10, 64)
	checkError(err)

	return value
}

func readString(reader *bufio.Reader) string {
	return readLine(reader)
}

func readArrayString(reader *bufio.Reader) []string {
	return strings.Split(strings.TrimSpace(readLine(reader)), " ")
}

func readArrayInt(reader *bufio.Reader) []int {
	arTemp := strings.Split(strings.TrimSpace(readLine(reader)), " ")
	n := len(arTemp)

	var ar []int

	for i := 0; i < n; i++ {
		arItemTemp, err := strconv.ParseInt(arTemp[i], 10, 64)
		checkError(err)
		arItem := int(arItemTemp)
		ar = append(ar, arItem)
	}

	return ar
}

func readArrayInt64(reader *bufio.Reader) []int64 {
	arTemp := strings.Split(strings.TrimSpace(readLine(reader)), " ")
	n := len(arTemp)

	var ar []int64

	for i := 0; i < n; i++ {
		arItemTemp, err := strconv.ParseInt(arTemp[i], 10, 64)
		checkError(err)
		arItem := arItemTemp
		ar = append(ar, arItem)
	}

	return ar
}

func readTwoInt(reader *bufio.Reader) (int, int) {
	ar := readArrayInt(reader)
	return ar[0], ar[1]
}

func readTwoInt64(reader *bufio.Reader) (int64, int64) {
	ar := readArrayInt64(reader)
	return ar[0], ar[1]
}

func readThreeInt(reader *bufio.Reader) (int, int, int) {
	ar := readArrayInt(reader)
	return ar[0], ar[1], ar[2]
}

func readThreeInt64(reader *bufio.Reader) (int64, int64, int64) {
	ar := readArrayInt64(reader)
	return ar[0], ar[1], ar[2]
}

func gcd(x int64, y int64) int64 {
	for y > 0 {
		x, y = y, x % y
	}

	return x
}

func lcm(x int64, y int64) int64 {
	return x*y/gcd(x,y)
}

package main

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

func main() {
	reader := bReader(os.Stdin)
	writer := bWriter(os.Stdout)

	t := readInt(reader)

	for t > 0 {
		n := readInt(reader)
		l_min := int(1e9)
		id_min := 0
		id_max := 0
		r_max := 0
		cc := make([]int, n)
		ll := make([]int, n)
		rr := make([]int, n)
		for i := 0; i < n; i++ {
			l, r, c := readThreeInt(reader)
			cc[i] = c
			ll[i] = l
			rr[i] = r
			if l < l_min {
				l_min = l
				id_min = i
			} else if l == l_min && (rr[id_min] < r || rr[id_min] == r && cc[id_min] > c) {
				l_min = l
				id_min = i
			}

			if r > r_max {
				r_max = r
				id_max = i
			} else if r == r_max && cc[id_max] > c {
				r_max = r
				id_max = i
			}

			if id_max == id_min {
				fmt.Fprintln(writer, cc[id_min])
			} else {
				fmt.Fprintln(writer, cc[id_min] + cc[id_max])
			}
		}

		t--
	}

	writer.Flush()
}

func bReader(stdin *os.File) *bufio.Reader {
	return bufio.NewReaderSize(stdin, 16*1024*1024)
}

func bWriter(stdout *os.File) *bufio.Writer {
	return bufio.NewWriterSize(stdout, 16*1024*1024)
}

func readLine(reader *bufio.Reader) string {
	str, _, err := reader.ReadLine()
	if err == io.EOF {
		return ""
	}

	return strings.TrimRight(string(str), "\r\n")
}

func checkError(err error) {
	if err != nil {
		panic(err)
	}
}

func readInt(reader *bufio.Reader) int {
	return int(readInt64(reader))
}

func readInt64(reader *bufio.Reader) int64 {
	value, err := strconv.ParseInt(readLine(reader), 10, 64)
	checkError(err)

	return value
}

func readString(reader *bufio.Reader) string {
	return readLine(reader)
}

func readArrayString(reader *bufio.Reader) []string {
	return strings.Split(strings.TrimSpace(readLine(reader)), " ")
}

func readArrayInt(reader *bufio.Reader) []int {
	arTemp := strings.Split(strings.TrimSpace(readLine(reader)), " ")
	n := len(arTemp)

	var ar []int

	for i := 0; i < n; i++ {
		arItemTemp, err := strconv.ParseInt(arTemp[i], 10, 64)
		checkError(err)
		arItem := int(arItemTemp)
		ar = append(ar, arItem)
	}

	return ar
}

func readArrayInt64(reader *bufio.Reader) []int64 {
	arTemp := strings.Split(strings.TrimSpace(readLine(reader)), " ")
	n := len(arTemp)

	var ar []int64

	for i := 0; i < n; i++ {
		arItemTemp, err := strconv.ParseInt(arTemp[i], 10, 64)
		checkError(err)
		arItem := arItemTemp
		ar = append(ar, arItem)
	}

	return ar
}

func readTwoInt(reader *bufio.Reader) (int, int) {
	ar := readArrayInt(reader)
	return ar[0], ar[1]
}

func readTwoInt64(reader *bufio.Reader) (int64, int64) {
	ar := readArrayInt64(reader)
	return ar[0], ar[1]
}

func readThreeInt(reader *bufio.Reader) (int, int, int) {
	ar := readArrayInt(reader)
	return ar[0], ar[1], ar[2]
}

func readThreeInt64(reader *bufio.Reader) (int64, int64, int64) {
	ar := readArrayInt64(reader)
	return ar[0], ar[1], ar[2]
}

func gcd(x int64, y int64) int64 {
	for y > 0 {
		x, y = y, x % y
	}

	return x
}

func lcm(x int64, y int64) int64 {
	return x*y/gcd(x,y)
}

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++ {
		B()
	}
}

func B() {
	var n int
	scan(&n)
	var s = make([]struct{ l, r, c int64 }, n)
	for i := range s {
		scan(&s[i].l, &s[i].r, &s[i].c)
	}
	var (
		ans    int64
		ml, mr = -1, -1
	)
	for i := range s {
		if ml == -1 || s[i].l < s[ml].l || (s[i].l == s[ml].l && s[i].c < s[ml].c) {
			ml = i
		}
		if mr == -1 || s[i].r > s[mr].r || (s[i].r == s[mr].r && s[i].c < s[mr].c) {
			mr = i
		}
		if s[ml].l <= s[mr].l && s[mr].r <= s[ml].r {
			ans = s[ml].c
		} else if s[mr].l <= s[ml].l && s[ml].r <= s[mr].r {
			ans = s[mr].c
		} else {
			ans = s[ml].c + s[mr].c
		}
		printf("%v\n", ans)
	}
}

func A() {
	var n, k int
	scan(&n, &k)
	if (n+1)/2 < k {
		printf("-1\n")
		return
	}
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			if k > 0 && i == j && i%2 == 0 && j%2 == 0 {
				printf("R")
				k--
			} else {
				printf(".")
			}
		}
		printf("\n")
	}
}

func C() {
	var n int
	scan(&n)

	var (
		cnt  = 0
		used = make([]bool, n+1)
		p    = make([]int, n+1)
	)
	for i := 1; i <= n; i++ {
		if used[i] {
			continue
		}
		s := make([]int, 0)
		for {
			x := req(i)
			cnt++
			if len(s) == 0 || x != s[0] {
				s = append(s, x)
			} else {
				break
			}
		}
		j := (len(s) - (cnt % len(s))) % len(s)
		for range s {
			// printf("@@@ MY J: %v\n", j)
			// writer.Flush()
			if j == len(s)-1 {
				p[s[j]] = s[0]
			} else {
				p[s[j]] = s[j+1]
			}
			j = (j + 1) % len(s)
		}
		for _, e := range s {
			used[e] = true
		}
	}
	printf("! ")
	for i := 1; i <= n; i++ {
		printf("%v ", p[i])
	}
	printf("\n")
	writer.Flush()
}

func req(i int) (x int) {
	printf("? %v\n", i)
	writer.Flush()
	scan(&x)
	return
}

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++ {
		B()
	}
}

func B() {
	var n int
	scan(&n)
	var segs = make([]struct{ l, r, c int64 }, n)
	for i := range segs {
		scan(&segs[i].l, &segs[i].r, &segs[i].c)
	}
	var (
		ans        int64
		minl, maxr = -1, -1
	)
	for i := range segs {
		if minl == -1 || segs[i].l < segs[minl].l || (segs[i].l == segs[minl].l && segs[i].c < segs[minl].c) {
			minl = i
		}
		if maxr == -1 || segs[i].r > segs[maxr].r || (segs[i].r == segs[maxr].r && segs[i].c < segs[maxr].c) {
			maxr = i
		}
		l, r := segs[minl].l, segs[maxr].r
		if segs[minl].l == l && segs[minl].r == r {
			maxr = minl
		} else if segs[maxr].l == l && segs[maxr].r == r {
			minl = maxr
		} else if (segs[minl].l == l && segs[minl].r == r) && (segs[maxr].l == l && segs[maxr].r == r) {
			j := minl
			if segs[j].c > segs[maxr].c {
				j = maxr
			}
			minl = j
			maxr = j
		}
		ans = segs[minl].c + segs[maxr].c
		if minl == maxr {
			ans -= segs[minl].c
		}
		printf("%v\n", ans)
	}
}

func A() {
	var n, k int
	scan(&n, &k)
	if (n+1)/2 < k {
		printf("-1\n")
		return
	}
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			if k > 0 && i == j && i%2 == 0 && j%2 == 0 {
				printf("R")
				k--
			} else {
				printf(".")
			}
		}
		printf("\n")
	}
}

func C() {
	var n int
	scan(&n)

	var (
		cnt  = 0
		used = make([]bool, n+1)
		p    = make([]int, n+1)
	)
	for i := 1; i <= n; i++ {
		if used[i] {
			continue
		}
		s := make([]int, 0)
		for {
			x := req(i)
			cnt++
			if len(s) == 0 || x != s[0] {
				s = append(s, x)
			} else {
				break
			}
		}
		j := (len(s) - (cnt % len(s))) % len(s)
		for range s {
			// printf("@@@ MY J: %v\n", j)
			// writer.Flush()
			if j == len(s)-1 {
				p[s[j]] = s[0]
			} else {
				p[s[j]] = s[j+1]
			}
			j = (j + 1) % len(s)
		}
		for _, e := range s {
			used[e] = true
		}
	}
	printf("! ")
	for i := 1; i <= n; i++ {
		printf("%v ", p[i])
	}
	printf("\n")
	writer.Flush()
}

func req(i int) (x int) {
	printf("? %v\n", i)
	writer.Flush()
	scan(&x)
	return
}

package main

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

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

type segment struct {
	lt, rt, c int
}

func main() {
	defer out.Flush()

	var tc int
	fmt.Fscan(in, &tc)
	for ; tc > 0; tc-- {
		var n int
		var mn, mx, longest, seg segment
		fmt.Fscan(in, &n, &seg.lt, &seg.rt, &seg.c)
		fmt.Fprintln(out, seg.c)
		mn = seg
		mx = seg
		longest = seg
		for i := 1; i < n; i++ {
			fmt.Fscan(in, &seg.lt, &seg.rt, &seg.c)
			if seg.lt < mn.lt || (seg.lt == mn.lt && seg.c < mn.c) {
				mn = seg
			}
			if seg.rt > mx.rt || (seg.rt == mx.rt && seg.c < mx.c) {
				mx = seg
			}
			if seg.rt - seg.lt > longest.rt - longest.lt || (seg.rt - seg.lt == longest.rt - longest.lt && seg.c < mx.c) {
				longest = seg
			}
			c := mn.c
			if mx.rt > mn.rt {
				c += mx.c
			}
			if longest.lt == mn.lt && longest.rt == mx.rt && longest.c < c {
				c = longest.c
			}
			fmt.Fprintln(out, c)
		}
	}
}

package main

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

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

type segment struct {
	lt, rt, c int
}

func main() {
	defer out.Flush()

	var tc int
	fmt.Fscan(in, &tc)
	for ; tc > 0; tc-- {
		var n int
		var mn, mx, longest, seg segment
		fmt.Fscan(in, &n, &seg.lt, &seg.rt, &seg.c)
		fmt.Fprintln(out, seg.c)
		mn = seg
		mx = seg
		longest = seg
		for i := 1; i < n; i++ {
			fmt.Fscan(in, &seg.lt, &seg.rt, &seg.c)
			if seg.lt < mn.lt || (seg.lt == mn.lt && seg.c < mn.c) {
				mn = seg
			}
			if seg.rt > mx.rt || (seg.rt == mx.rt && seg.c < mx.c) {
				mx = seg
			}
			if seg.rt - seg.lt > longest.rt - longest.lt {
				longest = seg
			}
			c := mn.c
			if mx.rt > mn.rt {
				c += mx.c
			}
			if longest.lt == mn.lt && longest.rt == mx.rt && longest.c < c {
				c = longest.c
			}
			fmt.Fprintln(out, c)
		}
	}
}

package main

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

type Segment struct {
	L int
	R int
	C int
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var tests int
	fmt.Fscan(in, &tests)
	for ; tests > 0; tests-- {
		var n int
		fmt.Fscan(in, &n)

		ar := make([]Segment, n)
		for i := range ar {
			fmt.Fscan(in, &ar[i].L, &ar[i].R, &ar[i].C)
		}

		minSeg := ar[0]
		maxSeg := ar[0]
		bothSeg := ar[0]
		for i := range ar {
			minSeg = Min(minSeg, ar[i])
			maxSeg = Max(maxSeg, ar[i])
			res := 0
			if minSeg == maxSeg {
				res = minSeg.C
			} else {
				res = minSeg.C + maxSeg.C
			}

			if minSeg.L >= ar[i].L && maxSeg.R <= ar[i].R && res > ar[i].C {
				bothSeg = ar[i]
			}

			if minSeg.L >= bothSeg.L &&
				maxSeg.R <= bothSeg.R && res > bothSeg.C {
				res = bothSeg.C
			}

			fmt.Fprintln(out, res)
		}
	}
}

func Min(a, b Segment) Segment {
	if a.L < b.L {
		return a
	} else if a.L > b.L {
		return b
	} else {
		if a.C < b.C {
			return a
		}
		return b
	}
}

func Max(a, b Segment) Segment {
	if a.R > b.R {
		return a
	} else if a.R < b.R {
		return b
	} else {
		if a.C < b.C {
			return a
		}
		return b
	}
}

package main

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

/*
1
3
4 5 61
75 87 4
6 14 1
---
65 <- 61 + 4
=======
1
9
1 4 4
5 8 9
7 8 7
2 10 252
1 11 271
1 10 1
1 5 1
8 12 10
10 12 8
---
4
13
11
256
271
271
271
11
9
*/

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

	bld := strings.Builder{}

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

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

		minL := maxInt
		minR := -1
		minLIdx := -1
		minLCost := -1

		maxR := minInt
		maxL := -1
		maxRIdx := -1
		maxRCost := -1

		bestRange := 0
		bestCost := maxInt

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

			if l < minL || l == minL && c < minLCost {
				minL = l
				minR = r
				minLIdx = i
				minLCost = c

				if minR == maxR {
					maxR = r
					maxL = l
					maxRIdx = i
					maxRCost = c
				}
			}

			if r > maxR || r == maxR && c < maxRCost {
				maxR = r
				maxL = l
				maxRIdx = i
				maxRCost = c

				if maxL == minL {
					minL = l
					minR = r
					minLIdx = i
					minLCost = c
				}
			}

			cost := minLCost
			if minLIdx != maxRIdx {
				cost = minLCost + maxRCost
			}

			rangee := maxR - minL
			if rangee > bestRange {
				bestRange = rangee
				bestCost = cost
			} else {
				bestCost = min(bestCost, cost)
			}

			fmt.Fprintln(&bld, bestCost)
		}
	}

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

const maxUint = ^uint(0)

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

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

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

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

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

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

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

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

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

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

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

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

	return true
}

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

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

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

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

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


package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	var t int
	fmt.Fscan(in, &t)
	for t > 0 {
		t--
		var n, k int
		var a, b string
		fmt.Fscan(in, &n, &k, &a, &b)
		freq1 := make([]int, 26)
		freq2 := make([]int, 26)
		for _, c := range a {
			freq1[c-'a']++
		}
		for _, c := range b {
			freq2[c-'a']++
		}
		possible := true
		for i := 0; i < 26; i++ {
			if freq1[i] < freq2[i] || (freq1[i]-freq2[i])%k != 0 {
				possible = false
				break
			}
			if i == 25 {
				if freq1[i] != freq2[i] {
					possible = false
					break
				}
				break
			}
			freq1[i+1] += freq1[i] - freq2[i]
		}
		if possible {
			fmt.Println("Yes")
		} else {
			fmt.Println("No")
		}
	}
}

package main

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

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

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

func solveA() {
	var n int
	scanf("%d\n", &n)
	ans := 0
	if n % 2 == 0 {
		if n >= 4 {
			ans = 2
		} else if n == 2 {
			ans = 1
		}
	} else {
		if n >= 5 {
			ans = 3
		} else if n == 3 {
			ans = 2
		}
	}
	printf("%d\n", ans)
}

type point struct {
	left int
	right int
}

func solveB() {
	var n, q int
	scanf("%d %d\n", &n, &q)
	var s string
	scanf("%s\n", &s)
	var P0, P1 point = point{n, -1}, point{n, -1}
	for i := 0; i < n; i++ {
		if s[i] == '0' {
			P0.right = i
		} else {
			P1.right = i
		}
	}

	for i := n - 1; i >= 0; i-- {
		if s[i] == '0' {
			P0.left = i
		} else {
			P1.left = i
		}
	}

	for ; q > 0; q-- {
		var l, r int
		scanf("%d %d\n", &l, &r)
		l--
		r--
		ans := "NO"
		if (s[l] == '0' && P0.left < l || s[l] == '1' && P1.left < l) ||
			(s[r] == '0' && P0.right > r || s[r] == '1' && P1.right > r) {
			ans = "YES"
		}

		printf("%s\n", ans)
	}
}

func sqr(x int64) float64 {
	return float64(x) * float64(x)
}

func solveD() {
	var d, k int64
	scanf("%d %d\n", &d, &k)
	ans := "Ashish"
	z := int64(math.Sqrt(0.5 * sqr(d) / sqr(k)))
	if k * k * (z * z + (z + 1) * (z + 1)) > d * d {
		ans = "Utkarsh"
	}
	printf("%s\n", ans)
}

func solveC() {
	var n, k int
	scanf("%d %d\n", &n, &k)
	var s, t string
	scanf("%s\n%s\n", &s, &t)
	needed := make([]int, 26)
	curr := make([]int, 26)
	for i := 0; i < n; i++ {
		needed[t[i] - 'a']++
		curr[s[i] - 'a']++
	}
	ans := "Yes"
	for i := 0; i < 26; i++ {
		if needed[i] > curr[i] {
			ans = "No"
			break
		}
		curr[i] -= needed[i]
		if curr[i] % k != 0 {
			ans = "No"
			break
		}
		if i + 1 < 26 {
			curr[i + 1] += curr[i]
		}
	}
	printf("%s\n", ans)
}

func main() {
	defer writer.Flush()
	var t int
	scanf("%d\n", &t)
	for ; t > 0; t-- {
		solveC()
	}
}

package main

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

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

	var test int
	fmt.Fscanf(in, "%d\n", &test)

	for testCase := 1; testCase <= test; testCase++ {
		var length, k int
		var a, b string
		fmt.Fscanf(in, "%d %d\n", &length, &k)
		fmt.Fscanf(in, "%s\n%s\n", &a, &b)

		ok := func() bool {
			have, need := make(map[int]int), make(map[int]int)
			for i := 0; i < length; i++ {
				have[int(a[i]-'a')]++
				need[int(b[i]-'a')]++
			}

			for i := 0; i < 26; i++ {
				if have[i] < need[i] {
					return false
				}
				have[i] -= need[i]
				if have[i]%k != 0 {
					return false
				} else {
					have[i+1] += have[i]
				}
			}
			return true
		}

		if ok() {
			fmt.Printf("Yes\n")
		} else {
			fmt.Printf("No\n")
		}
	}
}

package main

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

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

    var test int
    fmt.Fscanf(in, "%d\n", &test)

    for testCase := 1; testCase <= test; testCase++ {
        var length, k int
        var a, b string
        fmt.Fscanf(in, "%d %d\n", &length, &k)
        fmt.Fscanf(in, "%s\n%s\n", &a, &b)

        ok := func() bool {
            have, need := make(map[int] int), make(map[int] int)
            for i := 0; i < length; i++ {
                have[int(a[i] - 'a')] ++
                need[int(b[i] - 'a')] ++
            }

            for i := 0; i < 26; i++ {
                if (have[i] < need[i]) {
                    return false
                }
                have[i] -= need[i]
                if have[i] % k != 0 {
                    return false
                } else {
                    have[i + 1] += have[i]
                }
            }
            return true
        }

        if ok() {
            fmt.Printf("Yes\n")
        } else {
            fmt.Printf("No\n")
        }
    }
}

package main
import (
	"bufio"
	"fmt"
	"os"
)
var rd = bufio.NewReader(os.Stdin)
func main() {
	q := 0
	fmt.Fscan(rd, &q)
	for t := 0; t < q; t++ {
		n, k := 0, 0
		a, b := "", ""
		fmt.Fscan(rd, &n, &k, &a, &b)
		cnta, cntb := make([]int, 27), make([]int, 27)
		for _, v := range a {
			cnta[v - 'a']++
		}
		for _, v := range b {
			cntb[v - 'a']++
		}
		ok := true
		for i := 25; i >= 0; i-- {
			cnta[i] += cnta[i + 1]
			cntb[i] += cntb[i + 1]
			if cnta[i] > cntb[i] || (cntb[i] - cnta[i]) % k > 0 {
				ok = false
			}
		}
		if ok {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main
import (
	"os"
	"fmt"
	"bufio"
)
var rd = bufio.NewReader(os.Stdin)
func main() {
	q := 0
	fmt.Fscan(rd, &q)
	for t := 0; t < q; t++ {
		n, k := 0, 0
		a, b := "", ""
		fmt.Fscan(rd, &n, &k, &a, &b)
		cnta, cntb := make([]int, 27), make([]int, 27)
		for _, v := range a {
			cnta[v - 'a']++
		}
		for _, v := range b {
			cntb[v - 'a']++
		}
		ok := true
		for i := 0; i < 26; i++ {
			if cntb[i] > cnta[i] || (cnta[i] - cntb[i]) % k > 0 {
				ok = false
			}
			cnta[i] -= cntb[i]
			cnta[i + 1] += cnta[i]
		}
		if ok {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}


package main

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

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

	tc := readNum(reader)
	var buf bytes.Buffer
	for tc > 0 {
		tc--
		n := readNum(reader)
		s, _ := reader.ReadString('\n')
		res := solve(n, s)
		buf.WriteString(res)
		buf.WriteByte('\n')
	}
	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, s string) string {
	buf := make([]byte, n)
	var j int
	for i := 0; i < n; i++ {
		buf[i] = s[i+j]
		j++
	}

	return string(buf)
}

package main

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

var reader = bufio.NewReader(os.Stdin)

func main() {
	var t int
	fmt.Fscan(reader, &t)

	for i := 0; i < t; i++ {
		var n int
		fmt.Fscan(reader, &n)

		var s string
		fmt.Fscan(reader, &s)
		// s, _ := reader.ReadString('\n')
		fmt.Println(strings.Repeat(string(s[n-1]), n))
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"sync"
)
var out chan string
var in *bufio.Scanner
var outWg *sync.WaitGroup
func ReadString() string {
	in.Scan()
	return in.Text()
}
func ReadStringSlice(n int) []string {
	s := make([]string, n)
	for i := 0; i < n; i++ {
		s[i] = ReadString()
	}
	return s
}
func ReadInt() int {
	intStr := ReadString()
	i, _  := strconv.Atoi(intStr)
	return i
}
func ReadIntSlice(n int) []int {
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		arr[i] = ReadInt()
	}
	return arr
}
func init() {
	//set input
	in = bufio.NewScanner(os.Stdin)
	in.Buffer(make([]byte, 1024), int(2e+5))
	in.Split(bufio.ScanWords)

	//set output
	out = make(chan string, 16)
	outWg = &sync.WaitGroup{}
	outWg.Add(1)
	writer := bufio.NewWriterSize(os.Stdout, int(2e+5))
	go func(write *bufio.Writer) {
		defer outWg.Done()
		defer write.Flush()
		for line := range out {
			write.WriteString(line + "\n")
		}
	}(writer)
}
func work(){
	n:=ReadInt()
	s:=ReadString()
	for i:=0;i<n;i++{
		fmt.Printf("%c",s[n-1])
	}
	fmt.Println("")
}
func main(){
	for t:=ReadInt();t>0;t--{
		work()
	}
	close(out)
}

package main

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

func main() {
	var t, n int
	var s string
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		fmt.Fscan(in, &n)
		fmt.Fscan(in, &s)
		idx := len(s) / 2
		for j := 0; j < n; j++ {
			fmt.Printf("%s", string(s[idx]))
		}
		fmt.Println()
	}
}

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, s := io.NextInt(), io.Next()
		var ans []byte
		for i := 0; i < n; i++ {
			ok := false
			for j := 0; j < len(ans); j++ {
				if s[i+j] == ans[j] {
					ok = true
					break
				}
			}
			if !ok {
				ans = append(ans, s[i+len(ans)])
			}
		}
		for len(ans) < n {
			ans = append(ans, '0')
		}
		io.Println(string(ans))
	}
}

type FastIO struct {
	*bufio.Writer
	r    io.Reader
	buf  [8192]byte
	i, n int
}

func NewFastIO(r io.Reader, w io.Writer) *FastIO {
	return &FastIO{r: r, Writer: bufio.NewWriter(w)}
}

func (f *FastIO) readC() byte {
	if f.i == f.n {
		f.n, _ = f.r.Read(f.buf[:])
		if f.n == 0 {
			panic("EOF")
		}
		f.i = 0
	}
	f.i++
	return f.buf[f.i-1]
}

func (f *FastIO) NextInt() int {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := 0
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}

func (f *FastIO) Next() string {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	var s []byte
	for ; c > ' '; c = f.readC() {
		s = append(s, c)
	}
	return string(s)
}

func (f *FastIO) Println(a ...interface{}) {
	fmt.Fprintln(f, a...)
}

package main

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

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

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

	ins := func() string {
		sc.Scan()
		return sc.Text()
	}

	for t := ini(); t > 0; t-- {
		n := ini()

		s := ins()

		ns := make([]byte, n)

		for i := 0; i < n; i++ {
			ns[i] = s[2*i]
		}

		nss := string(ns)

		fmt.Println(nss)
	}
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	var t int
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		var n int
		fmt.Fscan(in, &n)
		var str string
		fmt.Fscan(in, &str)
		b := str[n-1]
		for j := 0; j < n; j++ {
			fmt.Print(string(b))
		}
		fmt.Println()
	}
}

package main

import (
	"fmt"
)

//找到一个字符串，他与任意长度为n的字符串相似
func main() {
	var t int

	n := 0
	fmt.Scan(&t)
	for t > 0 {
		t--

		fmt.Scan(&n)
		var str string
		fmt.Scan(&str)
		ans:=""
		for i:=0;i<len(str);i+=2 {
			ans += string(str[i])
		}
		fmt.Println(ans)
	}

}

package main

import (
	"bufio"
	"fmt"
	"os"
)
var reader = bufio.NewReader(os.Stdin)
//找到一个字符串，他与任意长度为n的字符串相似
func main() {
	var t int

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

		fmt.Fscan(reader,&n)
		//fmt.Scan(&n)
		var str string
		fmt.Fscan(reader,&str)
		//fmt.Scan(&str)
		ans:=""
		for i:=0;i<len(str);i+=2 {
			ans += string(str[i])
		}
		fmt.Println(ans)
	}

}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strings"
)
var reader = bufio.NewReader(os.Stdin)
//找到一个字符串，他与任意长度为n的字符串相似
func main() {
	var t int

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

		fmt.Fscan(reader,&n)
		//fmt.Scan(&n)
		var str string
		fmt.Fscan(reader,&str)
		//fmt.Scan(&str)
		fmt.Println(strings.Repeat(string(str[n-1]),n))
	}

}

/*
URL:
https://codeforces.com/contest/1400/problem/A
*/

package main

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

var (
	t int

	n int
	S []rune
)

func main() {
	t = readi()
	for tc := 0; tc < t; tc++ {
		n = readi()
		S = ReadRuneSlice()

		solve()
	}
}

func solve() {
	T := [][]rune{}
	for i := 0; i < n; i++ {
		sub := S[i : i+n]
		T = append(T, sub)
	}
	// PrintfDebug("===\n")
	// for _, sub := range T {
	// 	PrintfDebug("%v\n", string(sub))
	// }
	// PrintfDebug("===\n")

	A := make([]rune, n)
	for i := 0; i < n; i++ {
		A[i] = T[i][i]
	}
	fmt.Println(string(A))
}

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

/********** common constants **********/

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
)

/********** bufio setting **********/

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

/********** 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...)

/*********** Input ***********/

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

// readi returns an integer.
func readi() int {
	return int(readInt64())
}
func readi2() (int, int) {
	return int(readInt64()), int(readInt64())
}
func readi3() (int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64())
}
func readi4() (int, int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64()), int(readInt64())
}

// readll returns as integer as int64.
func readll() int64 {
	return readInt64()
}
func readll2() (int64, int64) {
	return readInt64(), readInt64()
}
func readll3() (int64, int64, int64) {
	return readInt64(), readInt64(), readInt64()
}
func readll4() (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
}

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

// readlls returns as int64 slice that has n integers.
func readlls(n int) []int64 {
	b := make([]int64, n)
	for i := 0; i < n; i++ {
		b[i] = readll()
	}
	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())
}

/*********** Output ***********/

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

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

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

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

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

package main

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

var t int

func main() {
	var input io.Reader = bufio.NewReader(os.Stdin)

	fmt.Fscan(input, &t)

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

		var n int
		var s string
		fmt.Fscan(input, &n)
		fmt.Fscan(input, &s)

		for i := 0; i < n; i++ {
			fmt.Print(string(s[n-1]))
		}

		fmt.Println()
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"sync"
)
var out chan string
var in *bufio.Scanner
var outWg *sync.WaitGroup
func ReadString() string {
	in.Scan()
	return in.Text()
}
func ReadStringSlice(n int) []string {
	s := make([]string, n)
	for i := 0; i < n; i++ {
		s[i] = ReadString()
	}
	return s
}
func ReadInt() int {
	intStr := ReadString()
	i, _  := strconv.Atoi(intStr)
	return i
}
func ReadIntSlice(n int) []int {
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		arr[i] = ReadInt()
	}
	return arr
}
func init() {
	//set input
	in = bufio.NewScanner(os.Stdin)
	in.Buffer(make([]byte, 1024), int(2e+5))
	in.Split(bufio.ScanWords)

	//set output
	out = make(chan string, 16)
	outWg = &sync.WaitGroup{}
	outWg.Add(1)
	writer := bufio.NewWriterSize(os.Stdout, int(2e+5))
	go func(write *bufio.Writer) {
		defer outWg.Done()
		defer write.Flush()
		for line := range out {
			write.WriteString(line + "\n")
		}
	}(writer)
}
func work(){
	n:=ReadInt()
	s:=ReadString()
	fmt.Println(s[0:n])
}
func main(){
	for t:=ReadInt();t>0;t--{
		work()
	}
	close(out)
}

package main

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

func main() {
	var t, n int
	var s string
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		fmt.Fscan(in, &n)
		fmt.Fscan(in, &s)
		for j := 0; j < n; j++ {
			fmt.Printf("%s", string(s[n/2]))
		}
		fmt.Println()
	}
}

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, s := io.NextInt(), io.Next()
		io.Println(s[:n])
	}
}

type FastIO struct {
	*bufio.Writer
	r    io.Reader
	buf  [8192]byte
	i, n int
}

func NewFastIO(r io.Reader, w io.Writer) *FastIO {
	return &FastIO{r: r, Writer: bufio.NewWriter(w)}
}

func (f *FastIO) readC() byte {
	if f.i == f.n {
		f.n, _ = f.r.Read(f.buf[:])
		if f.n == 0 {
			panic("EOF")
		}
		f.i = 0
	}
	f.i++
	return f.buf[f.i-1]
}

func (f *FastIO) NextInt() int {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := 0
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}

func (f *FastIO) Next() string {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	var s []byte
	for ; c > ' '; c = f.readC() {
		s = append(s, c)
	}
	return string(s)
}

func (f *FastIO) Println(a ...interface{}) {
	fmt.Fprintln(f, a...)
}

package main

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

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

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

	ins := func() string {
		sc.Scan()
		return sc.Text()
	}

	for t := ini(); t > 0; t-- {
		n := ini()

		s := ins()

		fmt.Println(s[0:n])
	}
}

package main

import (
	"fmt"
)

//找到一个字符串，他与任意长度为n的字符串相似
func main() {
	var t int

	n := 0
	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		fmt.Scan(&n)
		var str string
		fmt.Scan(&str)

		var tmp int64
		var ans int64 = 0xffffffff
		for j := 0; j <= len(str)-n; j++ {
			for k := j; k < j+n; k++ {
				tmp <<= 1
				if str[k] == '1' {
					tmp |= 1
				}
			}
		}
		ans &= tmp
		for i := 0; i < n; i++ {
			fmt.Printf("%d", (ans>>i)&1)
		}
	}

}

package main

import (
	"fmt"
)

//找到一个字符串，他与任意长度为n的字符串相似
func main() {
	var t int

	n := 0
	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		fmt.Scan(&n)
		var str string
		fmt.Scan(&str)

		var tmp int64
		var ans int64 = 0xffffffff
		for j := 0; j <= len(str)-n; j++ {
			for k := j; k < j+n; k++ {
				tmp <<= 1
				if str[k] == '1' {
					tmp |= 1
				}
			}
		}
		ans &= tmp
		for i := 0; i < n; i++ {
			fmt.Printf("%d", (ans>>i)&1)
		}
		println()
	}

}

package main

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

var t int

func main() {
	var input io.Reader = bufio.NewReader(os.Stdin)

	fmt.Fscan(input, &t)

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

		var n int
		var s string
		fmt.Fscan(input, &n)
		fmt.Fscan(input, &s)

		for i := 0; i < (n - 1); i++ {
			fmt.Print(string(48))
		}

		fmt.Println(string(s[n-1]))
	}
}

package main

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

var t int

func main() {
	var input io.Reader = bufio.NewReader(os.Stdin)

	fmt.Fscan(input, &t)

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

		var n int
		var s string
		fmt.Fscan(input, &n)
		fmt.Fscan(input, &s)

		fmt.Print(string(48))
		for i := 1; i < (n - 1); i++ {
			fmt.Print(string(49))
		}

		fmt.Println(string(48))
	}
}


package main

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

func main() {
	input := bufio.NewReader(os.Stdin)
	var h, l int
	var result float64

	_, err := fmt.Fscanf(input, "%d %d\n", &h, &l)
	if err != nil {
	}

	result = (float64(l) * float64(l) / (float64(h) * 2)) - float64(h)/2.0
	fmt.Printf("%.13f", result)

}

package main

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

func main() {

	//stdin, err := os.Open("in.txt")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdin.Close()
	stdin := os.Stdin
	reader := bufio.NewReaderSize(stdin, 1024*1024)

	//stdout, err := os.Create("test.txt")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdout.Close()
	stdout := os.Stdout
	writer := bufio.NewWriterSize(stdout, 1024*1024)

	var h, l int64
	fmt.Fscanf(reader, "%d %d\n", &h, &l)
	fmt.Fprintf(writer, "%0.10f\n", float64(l*l-h*h)/float64(2*h))
	writer.Flush()
}

package main

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

/* solution */

func solve() {

	h, l := float64(readInt()), float64(readInt())
	y := math.Atan(l / h)
	x := l/math.Sin(float64(math.Pi-2*y)) - h
	println(strconv.FormatFloat(x, 'f', 9, 64))

}

/* stubs */

func main() {

	var f *os.File
	if runtime.GOROOT() == "C:\\Soft\\Go" {
		f, _ = os.Open("input.txt")
	} else {
		f = os.Stdin
	}
	defer f.Close()

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

	reader = bufio.NewScanner(f)
	reader.Split(bufio.ScanWords)

	solve()

}

var reader *bufio.Scanner
var writer *bufio.Writer

func readInt() int {
	reader.Scan()
	ret, _ := strconv.Atoi(reader.Text())
	return ret
}

func print(s string) {
	writer.WriteString(s)
}

func println(s string) {
	print(s)
	print("\n")
}

package main

import "fmt"

func main() {
	var h, l float64
	fmt.Scanf("%f %f", &h, &l)
	fmt.Print((l*l-h*h)/2/h)
}

package main

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

func main() {
	input := bufio.NewReader(os.Stdin)
	var (
		H int
		L int
	)
	_, _ = fmt.Fscanf(input, "%d %d", &H, &L)

	var ans float64
	fL := float64(L)
	fH := float64(H)

	ans = (fL*fL)/(2*fH) - fH/2

	fmt.Printf("%.13f\n", ans)
}

package main

import "fmt"

func main() {
	var H, L float64
	fmt.Scan(&H, &L)
	fmt.Println((L*L - H*H) / (2 * H))
}

package main

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

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

	var H, L int
	fmt.Fscanf(input, "%d %d\n", &H, &L)
	h := float64(H)
	l := float64(L)
	fmt.Println((l*l - h*h) / h * .5)
}

package main

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

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

	var h, l float64
	Fscan(in, &h, &l)
	Fprintf(out, "%.13f", (l*l-h*h)/(2*h))
}

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

package main

import (
	"fmt"

)

func main() {
	//input
	h, l := 0, 0
	fmt.Scanf("%d %d", &h, &l)
	//output
	fmt.Print(solution(float64(h), float64(l)))
}
func solution(h float64, l float64) float64 {

	r := (l * l - h * h) / (2 * h)
	return r
}

package main

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

func main() {
	input := bufio.NewReader(os.Stdin)
	var h, l int64
	var result float64

	_, err := fmt.Fscanf(input, "%d %d\n", &h, &l)
	if err != nil {
	}

	result = (float64(l*l) / float64(2*h)) - float64(h)/2.0
	fmt.Printf("%.13f", result)

}

package main

import (
	"fmt"
)

func main(){
	var h, l uint64
	fmt.Scanln(&h, &l)

	x := float64(l * l - h * h) / float64(2 * h)
	fmt.Printf("%.6f", x)
}

package main

import (
	"bufio"
	"container/heap"
	"os"
	"strconv"
)

type myItem struct {
	v   uint8
	idx int
}

type myHeap []myItem

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

func (h *myHeap) Push(x interface{}) {
	*h = append(*h, x.(myItem))
}

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

func main() {

	f, _ := os.Open("input.txt")
	defer f.Close()

	scanner := bufio.NewScanner(f)
	scanner.Split(bufio.ScanWords)

	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	k := readInt(scanner)
	arrs := make([][]uint8, k, k)
	pointer := make([]int, k, k)

	h := &myHeap{}
	heap.Init(h)
	for i := 0; i < k; i++ {
		pointer[i] = 0
		n := readInt(scanner)
		if n > 0 {
			arrs[i] = make([]uint8, n, n)
		}
		for j := 0; j < n; j++ {
			v := uint8(readInt(scanner))
			arrs[i][j] = v
			if j == 0 {
				heap.Push(h, myItem{v, i})
			}
		}
	}

	for h.Len() > 0 {
		item := heap.Pop(h).(myItem)
		writer.WriteString(strconv.Itoa(int(item.v)))
		writer.WriteString(" ")
		pointer[item.idx]++
		if pointer[item.idx] < len(arrs[item.idx]) {
			heap.Push(h, myItem{arrs[item.idx][pointer[item.idx]], item.idx})
		}

	}

}

func readInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	v, _ := strconv.Atoi(scanner.Text())
	return v
}

package main

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

/* solution */

func solve() {

	h, l := float64(readInt()), float64(readInt())
	r := math.Sqrt(h*h + l*l)
	y := math.Asin(l / r)
	x := l/math.Sin(float64(math.Pi-2*y)) - h
	println(strconv.FormatFloat(x, 'f', 9, 64))

}

/* stubs */

func main() {

	var f *os.File
	if runtime.GOROOT() == "C:\\Soft\\Go" {
		f, _ = os.Open("input.txt")
	} else {
		f = os.Stdin
	}
	defer f.Close()

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

	reader = bufio.NewScanner(f)
	reader.Split(bufio.ScanWords)

	solve()

}

var reader *bufio.Scanner
var writer *bufio.Writer

func readInt() int {
	reader.Scan()
	ret, _ := strconv.Atoi(reader.Text())
	return ret
}

func print(s string) {
	writer.WriteString(s)
}

func println(s string) {
	print(s)
	print("\n")
}

package main

import "fmt"

func main() {
	var h, l float32
	fmt.Scanf("%f %f", &h, &l)
	fmt.Print((l*l-h*h)/2/h)
}

package main

import (
	"fmt"

)

func main() {
	//input
	h, l := 0, 0
	fmt.Scanf("%d %d", &h, &l)
	//output
	fmt.Print(solution(float64(h), float64(l)))
}
func solution(h float64, l float64) float64 {

	r := (l * l - h * h) / 2 * h
	return r
}

package main

import (
	"fmt"
	"math"
)

func main() {
	var h int
	var l int
	fmt.Scanf("%d %d", &h, &l)
	fmt.Print(solution(float64(h), float64(l)))
}

func solution(h float64, l float64) float64 {

	k := math.Sqrt((math.Pow(h,2) + math.Pow(l,2)))
	HA :=  (k / math.Sqrt(h))
	return math.Sqrt(HA)
}

package main

import (
	"fmt"
	"math"
)

func main() {
	var h int
	var l int
	fmt.Scanf("%d %d", &h, &l)
	result := fmt.Sprintf("%.1f", solution(float64(h), float64(l)))
	fmt.Print(result)
}

func solution(h float64, l float64) float64 {

	k := math.Sqrt((math.Pow(h,2) + math.Pow(l,2)))
	HA :=  (k / math.Sqrt(h))
	return math.Sqrt(HA)
}

package main

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

func main() {
	input := bufio.NewReader(os.Stdin)
	var h, l int
	var result float64

	_, err := fmt.Fscanf(input, "%d %d\n", &h, &l)
	if err != nil {
	}

	result = (float64(l*l) / float64(2*h)) - float64(h)/2.0
	fmt.Printf("%.13f", result)

}

package main

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

func main() {
	input := bufio.NewReader(os.Stdin)
	var h, l int
	var result float32

	_, err := fmt.Fscanf(input, "%d %d\n", &h, &l)
	if err != nil {
	}

	result = (float32(l)/float32(2*h))*float32(l) - float32(h)/2.0
	fmt.Println(result)

}

package main

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

func main() {
	input := bufio.NewReader(os.Stdin)
	var h, l int
	var result float64

	_, err := fmt.Fscanf(input, "%d %d\n", &h, &l)
	if err != nil {
	}

	result = (float64(l*l)/float64(2*h)) - float64(h)/2.0
	fmt.Println(result)

}

package main

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

func main() {
	input := bufio.NewReader(os.Stdin)
	var h, l int
	var result float64

	_, err := fmt.Fscanf(input, "%d %d\n", &h, &l)
	if err != nil {
	}

	result = float64(l*l-h*h) / float64(2*h)
	fmt.Println(result)

}

package main

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

func main() {
	input := bufio.NewReader(os.Stdin)
	var h, l int
	var result float32

	_, err := fmt.Fscanf(input, "%d %d\n", &h, &l)
	if err != nil {
	}

	result = (float32(l*l)/float32(2*h)) - float32(h)/2.0
	fmt.Println(result)

}

package main

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

func main() {
	input := bufio.NewReader(os.Stdin)
	var h, l int
	var result float32

	_, err := fmt.Fscanf(input, "%d %d\n", &h, &l)
	if err != nil {
	}

	result = float32(l*l-h*h) / float32(2*h)
	fmt.Println(result)

}

package main

import (
	"fmt"
)

func main(){
	var h, l uint32
	fmt.Scanln(&h, &l)

	x := float64(l * l - h * h) / float64(2 * h)
	fmt.Printf("%.6f", x)
}

package main

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

func main() {

	//stdin, err := os.Open("in.txt")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdin.Close()
	stdin := os.Stdin
	reader := bufio.NewReaderSize(stdin, 1024*1024)

	//stdout, err := os.Create("test.txt")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdout.Close()
	stdout := os.Stdout
	writer := bufio.NewWriterSize(stdout, 1024*1024)

	var h, l int
	fmt.Fscanf(reader, "%d %d\n", &h, &l)
	fmt.Fprintf(writer, "%0.7f\n", float64(l*l-h*h)/float64(2*h))
	writer.Flush()
}