The integers shop sells $$$n$$$ segments. The $$$i$$$-th of them contains all integers from $$$l_i$$$ to $$$r_i$$$ and costs $$$c_i$$$ coins.Tomorrow Vasya will go to this shop and will buy some segments there. He will get all integers that appear in at least one of bought segments. The total cost of the purchase is the sum of costs of all segments in it.After shopping, Vasya will get some more integers as a gift. He will get integer $$$x$$$ as a gift if and only if all of the following conditions are satisfied:   Vasya hasn't bought $$$x$$$.  Vasya has bought integer $$$l$$$ that is less than $$$x$$$.  Vasya has bought integer $$$r$$$ that is greater than $$$x$$$. Vasya can get integer $$$x$$$ as a gift only once so he won't have the same integers after receiving a gift.For example, if Vasya buys segment $$$[2, 4]$$$ for $$$20$$$ coins and segment $$$[7, 8]$$$ for $$$22$$$ coins, he spends $$$42$$$ coins and receives integers $$$2, 3, 4, 7, 8$$$ from these segments. He also gets integers $$$5$$$ and $$$6$$$ as a gift.Due to the technical issues only the first $$$s$$$ segments (that is, segments $$$[l_1, r_1], [l_2, r_2], \ldots, [l_s, r_s]$$$) will be available tomorrow in the shop.Vasya wants to get (to buy or to get as a gift) as many integers as possible. If he can do this in differents ways, he selects the cheapest of them.For each $$$s$$$ from $$$1$$$ to $$$n$$$, find how many coins will Vasya spend if only the first $$$s$$$ segments will be available.

The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of segments in the shop. Each of next $$$n$$$ lines contains three integers $$$l_i$$$, $$$r_i$$$, $$$c_i$$$ ($$$1 \leq l_i \leq r_i \leq 10^9, 1 \leq c_i \leq 10^9$$$) — the ends of the $$$i$$$-th segments and its cost. It is guaranteed that the total sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.

For each test case output $$$n$$$ integers: the $$$s$$$-th ($$$1 \leq s \leq n$$$) of them should be the number of coins Vasia will spend in the shop if only the first $$$s$$$ segments will be available.

NoteIn the first test case if $$$s = 1$$$ then Vasya can buy only the segment $$$[2, 4]$$$ for $$$20$$$ coins and get $$$3$$$ integers.The way to get $$$7$$$ integers for $$$42$$$ coins in case $$$s = 2$$$ is described in the statement.In the second test case note, that there can be the same segments in the shop.

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

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
}

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

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


While playing yet another strategy game, Mans has recruited $$$n$$$ Swedish heroes, whose powers which can be represented as an array $$$a$$$.Unfortunately, not all of those mighty heroes were created as capable as he wanted, so that he decided to do something about it. In order to accomplish his goal, he can pick two consecutive heroes, with powers $$$a_i$$$ and $$$a_{i+1}$$$, remove them and insert a hero with power $$$-(a_i+a_{i+1})$$$ back in the same position. For example if the array contains the elements $$$[5, 6, 7, 8]$$$, he can pick $$$6$$$ and $$$7$$$ and get $$$[5, -(6+7), 8] = [5, -13, 8]$$$.After he will perform this operation $$$n-1$$$ times, Mans will end up having only one hero. He wants his power to be as big as possible. What's the largest possible power he can achieve?

The first line contains a single integer $$$n$$$ ($$$1 \le n \le 200000$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$) — powers of the heroes.

Print the largest possible power he can achieve after $$$n-1$$$ operations.

NoteSuitable list of operations for the first sample:$$$[5, 6, 7, 8] \rightarrow [-11, 7, 8] \rightarrow [-11, -15] \rightarrow [26]$$$

package main

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

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

	n := readNum(reader)

	A := readNNums(reader, n)

	fmt.Println(solve(n, A))
}

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, A []int) int64 {
	ps := make([]Pair, n)
	var sum int64
	for i := 0; i < n; i++ {
		ps[i] = Pair{A[i], i}
		sum += int64(A[i])
	}
	sort.Sort(Pairs(ps))
	var best int64 = math.MinInt64
	if n%3 == 1 {
		best = sum
	}
	vis := make([]bool, n)

	check := func() bool {
		for i := 2; i < n; i++ {
			if vis[i] != vis[i%2] {
				return true
			}
		}
		return false
	}

	var tmp int64
	var safe bool
	for i := 0; i < n; i++ {
		cur := ps[i]
		tmp += int64(cur.first)
		vis[ps[i].second] = true
		if !safe {
			if ps[i].second > 0 {
				safe = vis[ps[i].second-1]
			}
			if !safe && ps[i].second < n-1 {
				safe = vis[ps[i].second+1]
			}
		}
		if (n+(i+1))%3 == 1 {
			tmp2 := -1 * tmp
			tmp3 := sum - tmp
			if !safe && ((i+1) == n/2 || (n-(i+1)) == n/2) {
				// not safe and alternating signs
				if !check() && i+1 < n {
					tmp2 += int64(cur.first) - int64(ps[i+1].first)
					tmp3 += int64(cur.first) - int64(ps[i+1].first)
				}
			}
			if tmp3+tmp2 > best {
				best = tmp3 + tmp2
			}
		}
	}

	return best
}

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

type Pair struct {
	first, second int
}

type Pairs []Pair

func (ps Pairs) Len() int {
	return len(ps)
}

func (ps Pairs) Less(i, j int) bool {
	return ps[i].first < ps[j].first || ps[i].first == ps[j].first && ps[i].second < ps[j].second
}

func (ps Pairs) Swap(i, j int) {
	ps[i], ps[j] = ps[j], ps[i]
}

package main

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

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

	n := readNum(reader)

	A := readNNums(reader, n)

	fmt.Println(solve(n, A))
}

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, A []int) int64 {
	ps := make([]Pair, n)
	var sum int64
	for i := 0; i < n; i++ {
		ps[i] = Pair{A[i], i}
		sum += int64(A[i])
	}
	sort.Sort(Pairs(ps))
	var best int64 = math.MinInt64
	if n%3 == 1 {
		best = sum
	}
	vis := make([]bool, n)
	var tmp int64
	var safe bool
	for i := 0; i < n; i++ {
		cur := ps[i]
		tmp += int64(cur.first)
		vis[ps[i].second] = true
		if !safe {
			if ps[i].second > 0 {
				safe = vis[ps[i].second-1]
			}
			if !safe && ps[i].second < n-1 {
				safe = vis[ps[i].second+1]
			}
		}
		if (n+(i+1))%3 == 1 {
			tmp2 := -1 * tmp
			tmp3 := sum - tmp
			if !safe && ((i+1) == n/2 || (n-(i+1)) == n/2) {
				// not safe and alternating signs
				if i+1 < n {
					tmp2 += int64(cur.first) - int64(ps[i+1].first)
					tmp3 += int64(cur.first) - int64(ps[i+1].first)
				}
			}
			if tmp3+tmp2 > best {
				best = tmp3 + tmp2
			}
		}
	}

	return best
}

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

type Pair struct {
	first, second int
}

type Pairs []Pair

func (ps Pairs) Len() int {
	return len(ps)
}

func (ps Pairs) Less(i, j int) bool {
	return ps[i].first < ps[j].first || ps[i].first == ps[j].first && ps[i].second < ps[j].second
}

func (ps Pairs) Swap(i, j int) {
	ps[i], ps[j] = ps[j], ps[i]
}


Ashish has two strings $$$a$$$ and $$$b$$$, each of length $$$n$$$, and an integer $$$k$$$. The strings only contain lowercase English letters.He wants to convert string $$$a$$$ into string $$$b$$$ by performing some (possibly zero) operations on $$$a$$$.In one move, he can either   choose an index $$$i$$$ ($$$1 \leq i\leq n-1$$$) and swap $$$a_i$$$ and $$$a_{i+1}$$$, or  choose an index $$$i$$$ ($$$1 \leq i \leq n-k+1$$$) and if $$$a_i, a_{i+1}, \ldots, a_{i+k-1}$$$ are all equal to some character $$$c$$$ ($$$c \neq$$$ 'z'), replace each one with the next character $$$(c+1)$$$, that is, 'a' is replaced by 'b', 'b' is replaced by 'c' and so on. Note that he can perform any number of operations, and the operations can only be performed on string $$$a$$$. Help Ashish determine if it is possible to convert string $$$a$$$ into $$$b$$$ after performing some (possibly zero) operations on it.

The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^5$$$) — the number of test cases. The description of each test case is as follows. The first line of each test case contains two integers $$$n$$$ ($$$2 \leq n \leq 10^6$$$) and $$$k$$$ ($$$1 \leq k \leq n$$$). The second line of each test case contains the string $$$a$$$ of length $$$n$$$ consisting of lowercase English letters. The third line of each test case contains the string $$$b$$$ of length $$$n$$$ consisting of lowercase English letters. It is guaranteed that the sum of values $$$n$$$ among all test cases does not exceed $$$10^6$$$.

For each test case, print "Yes" if Ashish can convert $$$a$$$ into $$$b$$$ after some moves, else print "No". You may print the letters of the answer in any case (upper or lower).

4
3 3
abc
bcd
4 2
abba
azza
2 1
zz
aa
6 2
aaabba
ddddcc

NoteIn the first test case it can be shown that it is impossible to convert $$$a$$$ into $$$b$$$.In the second test case,"abba" $$$\xrightarrow{\text{inc}}$$$ "acca" $$$\xrightarrow{\text{inc}}$$$ $$$\ldots$$$ $$$\xrightarrow{\text{inc}}$$$ "azza".Here "swap" denotes an operation of the first type, and "inc" denotes an operation of the second type.In the fourth test case,"aaabba" $$$\xrightarrow{\text{swap}}$$$ "aaabab" $$$\xrightarrow{\text{swap}}$$$ "aaaabb" $$$\xrightarrow{\text{inc}}$$$ $$$\ldots$$$ $$$\xrightarrow{\text{inc}}$$$ "ddaabb" $$$\xrightarrow{\text{inc}}$$$ $$$\ldots$$$ $$$\xrightarrow{\text{inc}}$$$ "ddddbb" $$$\xrightarrow{\text{inc}}$$$ $$$\ldots$$$ $$$\xrightarrow{\text{inc}}$$$ "ddddcc".

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