package main

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

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

	solve := func(Case int) {
		var s, t string
		Fscan(in, &s, &t)
		n, m := len(s), len(t)
		for l := min(n, m); l > 0; l-- {
			for r := l; r <= n; r++ {
				for r2 := l; r2 <= m; r2++ {
					if s[r-l:r] == t[r2-l:r2] {
						Fprintln(out, n+m-2*l)
						return
					}
				}
			}
		}
		Fprintln(out, n+m)
	}

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

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

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

package main

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

func main() {
	input := bufio.NewReader(os.Stdin)
	var testCases int
	fmt.Fscan(input, &testCases)

	for testCases > 0 {
		var str1, str2 string
		fmt.Fscan(input, &str1, &str2)
		LCS := findLongestCommonSubstringLength(str1,str2)
		fmt.Println(getTheNumberOfRemovedCharacters(str1, str2, LCS))
		testCases --
	}

}

func findLongestCommonSubstringLength(str1 , str2 string) int {
	dp := make([][] int, len(str1) + 1)
	for i := range dp {
		dp[i] = make([] int, len(str2) + 1)
	}

	LCS := 0

	for i := range dp {
		for j := range dp[i] {
			if i == 0 || j == 0 {
				dp[i][j] = 0
			} else if str1[i - 1] == str2[j - 1] {
				dp[i][j] = 1 + dp[i - 1][j - 1]
				LCS = max(LCS, dp[i][j])
			}
		}
	}

	return LCS
}

func getTheNumberOfRemovedCharacters(str1, str2 string, LCS int) int {
	return (len(str1) + len(str2)) - LCS * 2
}

func max(arg1 int, rest ...int) int {
	curr := arg1
	for _, num := range rest {
		if curr < num {
			curr = num
		}
	}
	return curr
}

package main

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

var fields []string

func init() {
	f, err := os.Open("in.txt")

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

	bytes, err := ioutil.ReadAll(bufio.NewReader(f))

	if err != nil {
		panic(err)
	}

	fields = strings.Fields(string(bytes))
}

func ScanString() string {
	for {
		field := fields[0]
		fields = fields[1:]

		if field != "" {
			return field
		}
	}
}

func ScanInt() int {
	n, err := strconv.Atoi(ScanString())

	if err != nil {
		panic(err)
	}

	return n
}

func ScanInt64() int64 {
	n, err := strconv.ParseInt(ScanString(), 10, 64)

	if err != nil {
		panic(err)
	}

	return n
}

func main() {
	for t := ScanInt(); t > 0; t-- {
		a, b := ScanString(), ScanString()

		if len(a) > len(b) {
			a, b = b, a
		}

		res := len(a) + len(b)

		for l := 1; l <= len(a); l++ {
			for s, e := 0, l; e <= len(a); s, e = s+1, e+1 {
				if strings.Contains(b, a[s:e]) {
					res = len(a) + len(b) - 2*l
				}
			}
		}

		fmt.Println(res)
	}
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	var t int
	fmt.Fscan(in, &t)
	for t > 0 {
		t--
		var a, b string
		fmt.Fscan(in, &a, &b)
		n, m := len(a), len(b)
		dp := make([][]int, n+1)
		for i := range dp {
			dp[i] = make([]int, m+1)
		}
		res := 0
		for i := n;i>=0; i-- {
			for j := m; j>=0; j-- {
				if i == n || j == m {
					continue
				}
				if a[i] == b[j] {
					dp[i][j] = 1 + dp[i+1][j+1]
				}
				if res < dp[i][j] {
					res = dp[i][j]
				}
			}
		}
		fmt.Println(n+m-2*res)
	}
}

package main

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

type Scanner struct {
	bScanner *bufio.Scanner
}

func NewScanner() *Scanner {
	bScanner := bufio.NewScanner(os.Stdin)
	bScanner.Split(bufio.ScanWords)
	bScanner.Buffer([]byte{}, math.MaxInt32)
	return &Scanner{
		bScanner,
	}
}
func (s *Scanner) next() string {
	s.bScanner.Scan()
	return s.bScanner.Text()
}
func (s *Scanner) nextInt() int {
	s.bScanner.Scan()
	res, err := strconv.Atoi(s.bScanner.Text())
	if err != nil {
		panic(err)
	}
	return res
}
func (s *Scanner) nextInt64() int64 {
	s.bScanner.Scan()
	res, err := strconv.ParseInt(s.bScanner.Text(), 10, 64)
	if err != nil {
		panic(err)
	}
	return res
}
func (s *Scanner) nextFloat() float64 {
	s.bScanner.Scan()
	res, err := strconv.ParseFloat(s.bScanner.Text(), 64)
	if err != nil {
		panic(err)
	}
	return res
}
func (s *Scanner) nextInts(N int) []int {
	res := make([]int, N)
	for i := 0; i < N; i++ {
		res[i] = s.nextInt()
	}
	return res
}
func (s *Scanner) nextInt64s(N int) []int64 {
	res := make([]int64, N)
	for i := 0; i < N; i++ {
		res[i] = s.nextInt64()
	}
	return res
}
func min(nums ...int) int {
	ans := math.MaxInt32
	for _, num := range nums {
		if num < ans {
			ans = num
		}
	}
	return ans
}
func max(nums ...int) int {
	ans := math.MinInt32
	for _, num := range nums {
		if num > ans {
			ans = num
		}
	}
	return ans
}

func minOperation(a *string, b *string) int {
	N := len(*a)
	M := len(*b)

	for length := min(N, M); length > 0; length-- {
		for i := 0; i+length <= N; i++ {
			for j := 0; j+length <= M; j++ {
				if (*a)[i:i+length] == (*b)[j:j+length] {
					return N + M - 2*length
				}
			}
		}
	}

	return N + M
}

func main() {
	sc := NewScanner()
	wr := bufio.NewWriter(os.Stdout)
	defer wr.Flush()

	TC := sc.nextInt()
	for tc := 0; tc < TC; tc++ {
		a := sc.next()
		b := sc.next()

		fmt.Fprintln(wr, minOperation(&a, &b))
	}

}

package main
import "fmt"

func max(n int, m int) int {
	if (n > m) {
		return n
	}
	return m
}

func solve(a string, b string) int {

	m := [20][20]int{}

	var maxL int = 0
	for i := 0; i < len(a); i++ {
		for j := 0; j < len(b); j++ {
			eq := 0
			if (a[i] == b[j]) {
				eq = 1
			}
			if (i*j == 0) {
				m[i][j] = eq
			} else {
				if (eq == 1) {
					m[i][j] = m[i - 1][j - 1] + 1
				}
			}
			maxL = max(m[i][j], maxL)
		}
	}
	return len(a) + len(b) - 2*maxL
}

func main() {
	var t int
	var a,b string
	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		fmt.Scan(&a, &b)
		fmt.Println(solve(a,b))
	}
}

package main

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

// var m []int
// err = json.Unmarshal([]byte(r.FormValue("example")), &m)

// for i := 0; i < len(m); i++ {
// 	fmt.Println(m[i])
// }

func MIN(x, y int64) int64 {
	if x > y {
		return y
	} else {
		return x
	}
}

func MKO(x, y int) int {
	if x < y {
		return 0
	} else {
		return x - y + 1
	}
}

func MAX(x, y int) int {
	if x > y {
		return x
	} else {
		return y
	}
}

func LCM(x, y int) int {
	if x >= y {
		initial := x
		for i := 1; i < y+2; i++ {
			if initial%y == 0 {
				break
			} else {
				initial = initial + x
			}

		}
		return initial
	} else {
		initial := y
		for i := 1; i < x+2; i++ {
			if initial%x == 0 {
				break
			} else {
				initial = initial + y
			}

		}
		return initial
	}
}

func IsItPrime(x int) bool {
	if x < 10 {
		if x == 2 || x == 3 || x == 5 || x == 7 {
			return true
		} else {
			return false
		}
	} else {
		j := 0
		k := math.Sqrt(float64(x))
		for m := 3; m <= int(k); {
			if x%m == 0 {
				j = 1
				break
			}
			m = m + 2
		}
		if j == 1 {
			return false
		} else {
			return true
		}
	}

}

// sort.Slice(arr, func(i, j int) bool {
// 	return arr[i] > arr[j]
// })
var t int

func main() {
	var input io.Reader = bufio.NewReader(os.Stdin)
	fmt.Fscan(input, &t)

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

		STRING1 := make([]byte, 0)
		SOZZ := make([]byte, 0)
		fmt.Fscan(input, &STRING1)
		fmt.Fscan(input, &SOZZ)

		a := len(STRING1)
		b := len(SOZZ)
		mu := make([]int, 0)
		for i := 0; i < a; i++ {
			for j := 0; j < b; j++ {
				if STRING1[i] == SOZZ[j] {
					beg := i + 1
					secbeg := j + 1
					leggg := 1
					for q := 0; q > -1; q++ {
						if beg < a && secbeg < b && STRING1[beg] == SOZZ[secbeg] {
							leggg++
							beg++
							secbeg++
						} else {
							mu = append(mu, leggg)
							break
						}
					}
				}
			}
		}
		ll := len(mu)
		if ll > 0 {
			sort.Ints(mu)
			fmt.Println(a + b - 2*mu[ll-1])
		} else {
			fmt.Println(a + b)
		}
	}

}

package main

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

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

	var T int
	fmt.Fscanf(in, "%d\n", &T)
	for t := 0; t < T; t++ {
		var a string
		var b string

		fmt.Fscanf(in, "%s\n", &a)
		fmt.Fscanf(in, "%s\n", &b)

		ans := math.MaxInt32

		mp := make(map[rune]int)
		for i, c := range a {
			mp[c] = i
		}

		for j := range a {

			for i := range b {
				ea := j
				eb := i
				for ea < len(a) && eb < len(b) && a[ea] == b[eb] {
					ea++
					eb++
				}

				count := len(a) - ea + j + len(b) - eb + i

				// fmt.Println(count)
				if ans > count {
					ans = count
				}

			}

		}

		fmt.Fprintf(out, "%d\n", ans)
	}
}

package main

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

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 s1, s2 string
		fmt.Fscan(in, &s1, &s2)

		if len(s2) > len(s1) {
			s1, s2 = s2, s1
		}

		c := len(s1) + len(s2)
	Loop:
		for win := len(s1); win >= 1; win-- {
			for i := 0; i+win <= len(s1); i++ {
				if strings.Index(s2, s1[i:i+win]) >= 0 {
					c = (len(s1) - win) + (len(s2) - win)
					break Loop
				}
			}
		}

		fmt.Fprintln(out, c)
	}
}

package main

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

var (
	reader = bufio.NewReader(os.Stdin)
	t      int
	a, b   string
)

func min(x, y int) int {
	if x > y {
		return y
	}
	return x
}

func equal(x, y string) bool {
	if len(x) != len(y) {
		return false
	}
	for i := range x {
		if x[i] != y[i] {
			return false
		}
	}
	return true
}

func solve(a, b string) int {
	var (
		t = min(len(a), len(b))
	)
	for i := t; i > 0; i-- {
		for j := 0; j <= len(a)-i; j++ {
			for k := 0; k <= len(b)-i; k++ {
				p, q := a[j:j+i], b[k:k+i]
				if equal(p, q) {
					return len(a) + len(b) - 2*i
				}
			}
		}
	}
	return len(a) + len(b)
}

func main() {
	fmt.Fscan(reader, &t)
	for ; t > 0; t-- {
		fmt.Fscan(reader, &a, &b)
		fmt.Println(solve(a, b))
	}
}

package main

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

func solve() {
	f := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	N := 0
	fmt.Fscan(f, &N)
	for i := 0; i < N; i++ {
		a, b := "", ""
		fmt.Fscan(f, &a, &b)
		la, lb, ok := len(a), len(b), false
		if la < lb {
			a, b = b, a
			la, lb = len(a), len(b)
		}
		for j := lb; j > 0; j-- {
			for k := 0; k < lb; k++ {
				if j+k > lb {
					break
				}
				if strings.Contains(a, string(b[k:j+k])) {
					fmt.Fprintln(w, la+lb-2*j)
					ok, j, k = true, -1, 1000
				}
			}
		}
		if !ok {
			fmt.Fprintln(w, la+lb)
		}
	}
	w.Flush()
}

func main() {
	solve()
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"strings"

	//"math"
	"os"
)

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

	var t int
	for Fscan(in, &t); t > 0; t-- {
		var a, b string
		Fscan(in, &a, &b)

		if len(a) > len(b) {
			a, b = b, a
		}

		delta := 0
		for flag, stride := true, len(a); stride >= 1; stride-- {
			for i := 0; i + stride <= len(a); i++ {
				pos := strings.Index(b, a[i:i+stride])
				if pos != -1 {
					flag, delta = false, stride
					break
				}
			}
			if !flag {
				break
			}
		}
		Fprintln(out, len(a) + len(b) - 2 * delta)
	}
}

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

func decomposition(n int) []int {
	var res []int
	for p := 2; n >= p * p;  {
		if n % p == 0 {
			res = append(res, p)
			n /= p
		} else {
			p++
		}
	}
	if n > 1 {
		res = append(res, n)
	}
	return res
}

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

package main

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

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 s1, s2 string
		fmt.Fscan(in, &s1, &s2)

		if len(s2) > len(s1) {
			s1, s2 = s2, s1
		}

		var c int
	Loop:
		for win := len(s1); win >= 1; win-- {
			for i := 0; i+win <= len(s1); i++ {
				if strings.Index(s2, s1[i:i+win]) >= 0 {
					c = (len(s1) - win) + (len(s2) - win)
					break Loop
				}
			}
		}

		fmt.Fprintln(out, c)
	}
}

package main

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

var (
	reader = bufio.NewReader(os.Stdin)
	t      int
	a, b   string
)

func min(x, y int) int {
	if x > y {
		return y
	}
	return x
}

func equal(x, y string) bool {
	if len(x) != len(y) {
		return false
	}
	for i := range x {
		if x[i] != y[i] {
			return false
		}
	}
	return true
}

func solve(a, b string) int {
	var (
		t = min(len(a), len(b))
	)
	for i := t; i > 0; i-- {
		for j := 0; j <= len(a)-i; j++ {
			for k := 0; k <= len(b)-i; k++ {
				p, q := a[j:j+i], b[k:k+i]
				if equal(p, q) {
					return len(a) + len(b) - 2*i
				}
			}
		}
	}
	return 0
}

func main() {
	fmt.Fscan(reader, &t)
	for ; t > 0; t-- {
		fmt.Fscan(reader, &a, &b)
		fmt.Println(solve(a, b))
	}
}


package main

import (
    "fmt"
    "strconv"
    // "strings"
)

func main() {
    var (
        ps, qs, ls, rs string
        p, q, l, r int
        a, b, c, d []int
    )

    fmt.Scanln(&ps, &qs, &ls, &rs)

    p, _ = strconv.Atoi(ps)
    q, _ = strconv.Atoi(qs)
    l, _ = strconv.Atoi(ls)
    r, _ = strconv.Atoi(rs)

    a = make([]int, p)
    b = make([]int, p)

    c = make([]int, q)
    d = make([]int, q)

    for i := 0; i < p; i += 1 {
        var as, bs string
        fmt.Scanln(&as, &bs)

        a[i], _ = strconv.Atoi(as)
        b[i], _ = strconv.Atoi(bs)
    }

    for i := 0; i < q; i += 1 {
        var cs, ds string
        fmt.Scanln(&cs, &ds)

        c[i], _ = strconv.Atoi(cs)
        d[i], _ = strconv.Atoi(ds)
    }

    var res int
    for i := l; i <= r; i += 1 {
        var pi, qi int
        for pi < p && qi < q {
            if (a[pi] <= c[qi] + i && c[qi] + i <= b[pi]) || (a[pi] <= d[qi] + i && d[qi] + i <= b[pi]) || (c[qi] + i <= a[pi] && b[pi] <= d[qi] + i){
                res += 1
                break
            }
            if b[pi] < c[qi] + i {
                pi += 1
            } else if d[qi] + i < a[pi] {
                qi += 1
            }
        }
    }
    fmt.Println(res)
}

package main

import (
	"fmt"
)

func main() {
	var p, q, l, r int
	fmt.Scanf("%d %d %d %d\n", &p, &q, &l, &r)
	a := make([]int, p)
	b := make([]int, p)
	c := make([]int, q)
	d := make([]int, q)
	for i := 0; i < p; i++ {
		fmt.Scanf("%d %d\n", &a[i], &b[i])
	}

	for i := 0; i < q; i++ {
		fmt.Scanf("%d %d\n", &c[i], &d[i])
	}

	ans := 0
	for t := l; t <= r; t++ {
		f := false
		for i := 0; (i < p) && !(f); i++ {
			for j := 0; (j < q) && !(f); j++ {
				f = (c[j]+t <= b[i]) && (d[j]+t >= a[i])
			}
		}
		if f {
			ans++
		}
	}

	fmt.Println(ans)
}

package main

import "fmt"

func intersect(X []int, Z []int, t int) bool {
    i := 0
    j := 0
    for i < len(X) && j < len(Z) {
        if X[i] + t < Z[j] {
            if (j & 1) != 0 {
                return true
            }
            i++
        } else if X[i] + t > Z[j] {
            if (i & 1) != 0 {
                return true
            }
            j++
        } else {
            return true
        }
    }
    return false
}

func main() {
    var p, q, l, r int
    fmt.Scan(&p, &q, &l, &r)
    Z := make([]int, 2 * p)
    X := make([]int, 2 * q)
    for i := 0; i < 2 * p; i++ {
        fmt.Scan(&Z[i])
    }
    for i := 0; i < 2 * q; i++ {
        fmt.Scan(&X[i])
    }
    ret := 0
    for t := l; t <= r; t++ {
        if intersect(X, Z, t) {
            ret++
        }
    }
    fmt.Println(ret)
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	line, _ := reader.ReadString('\n')

	var p, q, l, r int

	fmt.Sscanf(line, "%d %d %d %d", &p, &q, &l, &r)

	zSegment := make([][2]int, p, p)
	for i := 0; i < p; i++ {
		line, _ := reader.ReadString('\n')
		fmt.Sscanf(line, "%d %d", &zSegment[i][0], &zSegment[i][1])
	}

	xSegment := make([][2]int, q, q)
	for i := 0; i < q; i++ {
		line, _ := reader.ReadString('\n')
		fmt.Sscanf(line, "%d %d", &xSegment[i][0], &xSegment[i][1])
	}

	counter := make(map[int]bool)

	for i := l; i <= r; i++ {
		for j := 0; j < p; j++ {
			startZ, endZ := zSegment[j][0], zSegment[j][1]
			for k := 0; k < q; k++ {
				startX, endX := xSegment[k][0], xSegment[k][1]
				if (startZ >= startX+i && startZ <= endX+i) ||
					(endZ <= endX+i && endZ >= startX+i) ||
					(startZ < startX+i && endZ > endX+i) {
					counter[i] = true
				}
			}
		}
	}
	fmt.Println(len(counter))
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	line, _ := reader.ReadString('\n')

	var p, q, l, r int

	fmt.Sscanf(line, "%d %d %d %d", &p, &q, &l, &r)

	zSegment := make([][2]int, p, p)
	for i := 0; i < p; i++ {
		line, _ := reader.ReadString('\n')
		fmt.Sscanf(line, "%d %d", &zSegment[i][0], &zSegment[i][1])
	}

	xSegment := make([][2]int, q, q)
	for i := 0; i < q; i++ {
		line, _ := reader.ReadString('\n')
		fmt.Sscanf(line, "%d %d", &xSegment[i][0], &xSegment[i][1])
	}

	counter := make(map[int]bool)

	for i := l; i <= r; i++ {
		for j := 0; j < p; j++ {
			startZ, endZ := zSegment[j][0], zSegment[j][1]
			for k := 0; k < q; k++ {
				startX, endX := xSegment[k][0], xSegment[k][1]
				if (startZ >= startX+i && startZ <= endX+i) || (endZ <= endX+i && endZ >= startX+i) {
					counter[i] = true
				}
			}
		}
	}
	fmt.Println(len(counter))
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	line, _ := reader.ReadString('\n')

	var p, q, l, r int

	fmt.Sscanf(line, "%d %d %d %d", &p, &q, &l, &r)

	zSegment := make([][2]int, p, p)
	for i := 0; i < p; i++ {
		line, _ := reader.ReadString('\n')
		fmt.Sscanf(line, "%d %d", &zSegment[i][0], &zSegment[i][1])
	}

	xSegment := make([][2]int, q, q)
	for i := 0; i < q; i++ {
		line, _ := reader.ReadString('\n')
		fmt.Sscanf(line, "%d %d", &xSegment[i][0], &xSegment[i][1])
	}

	counter := make(map[int]bool)

	for i := l; i <= r; i++ {
		for j := 0; j < p; j++ {
			startZ, endZ := zSegment[j][0], zSegment[j][1]
			fmt.Println(startZ, endZ)
			for k := 0; k < q; k++ {
				startX, endX := xSegment[k][0], xSegment[k][1]
				fmt.Println(startX, endX, startX+i, endX+i)
				if (startZ >= startX+i && startZ <= endX+i) ||
					(endZ <= endX+i && endZ >= startX+i) ||
					(startZ < startX+i && endZ > endX+i) {
					counter[i] = true
				}
			}
		}
	}
	fmt.Println(len(counter))
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	line, _ := reader.ReadString('\n')

	var p, q, l, r int

	fmt.Sscanf(line, "%d %d %d %d", &p, &q, &l, &r)

	zSegment := make([][2]int, p, p)
	for i := 0; i < p; i++ {
		line, _ := reader.ReadString('\n')
		fmt.Sscanf(line, "%d %d", &zSegment[i][0], &zSegment[i][1])
	}

	xSegment := make([][2]int, q, q)
	for i := 0; i < q; i++ {
		line, _ := reader.ReadString('\n')
		fmt.Sscanf(line, "%d %d", &xSegment[i][0], &xSegment[i][1])
	}

	counter := make(map[int]bool)

	for j := 0; j < p; j++ {
		startZ, endZ := zSegment[j][0], zSegment[j][1]
		for k := 0; k < q; k++ {
			startX, endX := xSegment[k][0], xSegment[k][1]
			for i := l; i <= r; i++ {
				if (startZ >= startX+i && startZ <= endX+i) || (endZ <= endX+i && endZ >= startX+i) {
					counter[i] = true
				}
			}
		}
	}
	fmt.Println(len(counter))
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	line, _ := reader.ReadString('\n')

	var p, q, l, r int

	fmt.Sscanf(line, "%d %d %d %d", &p, &q, &l, &r)

	zSegment := make([][2]int, p, p)
	for i := 0; i < p; i++ {
		line, _ := reader.ReadString('\n')
		fmt.Sscanf(line, "%d %d", &zSegment[i][0], &zSegment[i][1])
	}

	xSegment := make([][2]int, q, q)
	for i := 0; i < q; i++ {
		line, _ := reader.ReadString('\n')
		fmt.Sscanf(line, "%d %d", &xSegment[i][0], &xSegment[i][1])
	}

	counter := make(map[int]bool)

	for j := 0; j < p; j++ {
		startZ, endZ := zSegment[j][0], zSegment[j][1]
		for k := 0; k < q; k++ {
			startX, endX := xSegment[k][0], xSegment[k][1]
			for i := l; i <= r; i++ {
				if startZ > endX+i || endZ < startX+i {
					// break
					continue
				} else if startZ >= startX+i || endZ <= endX+i {
					counter[i] = true
				}
			}
		}
	}
	fmt.Println(len(counter))
}


package main

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

type pair struct {
	a, b int
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	solve := func(Case int) {
		var n int
		var s string
		fmt.Fscan(in, &n, &s)
		dp := map[pair]int{}
		z, o := 0, 0
		for i := range s {
			switch s[i] {
			case 'D':
				z++
			default:
				o++
			}
			_gcd := z + o
			if z != 0 && o != 0 {
				_gcd = gcd(o, z)
			}
			dp[pair{z / _gcd, o / _gcd}] += 1
			fmt.Fprint(out, dp[pair{z / _gcd, o / _gcd}])
			fmt.Fprint(out, " ")
		}
		fmt.Fprint(out, "\n")
	}

	T := 1
	fmt.Fscan(in, &T)
	for Case := 1; Case <= T; Case++ {
		solve(Case)
	}
}

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

func gcd(x, y int) int {
	for y != 0 {
		x, y = y, x%y
	}
	return x
}

func reverse(s []byte) []byte {
	a := []byte(s)
	for i, j := 0, len(a)-1; i < j; i, j = i+1, j-1 {
		a[i], a[j] = a[j], a[i]
	}
	return a
}

package main

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

var in *bufio.Reader

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

func normalize(dCnt, kCnt int) int64 {
	if dCnt == 0 {
		kCnt = 1
	}
	if kCnt == 0 {
		dCnt = 1
	}
	if dCnt != 0 && kCnt != 0 {
		g := gcd(dCnt, kCnt)
		dCnt /= g
		kCnt /= g
	}

	return int64(dCnt)*1000000 + int64(kCnt)
}

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

	var a string
	fmt.Fscan(in, &a)

	dCnt, kCnt := 0, 0
	ratio := make(map[int64]int)
	dp := make([]int, n)
	for i := 0; i < n; i++ {
		if a[i] == 'D' {
			dCnt++
		} else if a[i] == 'K' {
			kCnt++
		} else {
			panic("impossible")
		}

		split := 1
		key := normalize(dCnt, kCnt)
		oldIdx, ok := ratio[key]
		if ok {
			split += dp[oldIdx]
		}
		ratio[key] = i
		dp[i] = split
	}

	for i := 0; i < n; i++ {
		fmt.Printf("%d ", dp[i])
	}
	fmt.Println()
}

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

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

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

	var n int
	var s string
	for ; t > 0; t-- {
		fmt.Fscan(reader, &n, &s)

		freq := map[int64]int{}
		var cntd, cntk int64
		for i := 0; i < n; i++ {
			if s[i] == 'D' {
				cntd++
			} else {
				cntk++
			}
			base := Gcd(cntd, cntk)
			key := cntd/base*x + cntk/base
			freq[key]++
			if i > 0 {
				fmt.Fprint(writer, " ")
			}
			fmt.Fprint(writer, freq[key])
		}
		fmt.Fprintln(writer)
	}
}

var x = int64(5e5 + 1)

func Gcd(a, b int64) int64 {
	for b > 0 {
		a, b = b, a%b
	}
	return a
}

package main

import (
	"bufio"
	"io/ioutil"
	"os"
	"strconv"
	"unicode"
)

var bytes []byte
var l, max int
var ws *bufio.Writer

/**
 * Perform Quick Scan of 1 data(with no spaces in-between) on the buffered input
 */
func fastInt() int {
	for unicode.IsSpace(rune(bytes[l])) {l++}
	b := bytes[l]

	// Check if the bytes is not a number
	for b < 48 || b > 57 {
		l++
		b = bytes[l]
	}

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

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

func fastStr() []byte {
	for unicode.IsSpace(rune(bytes[l])) {l++}
	result := make([]byte, 0)
	for !unicode.IsSpace(rune(bytes[l])) {
		result = append(result, bytes[l])
		l++
	}
	return result
}

var sc *bufio.Scanner

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

type Pair struct {
	fi, se int
}

func solve(){
	n := fastInt()
	s := fastStr()
	m := make(map[Pair]int)
	atas := 0
	for i := 0;i < n;i++ {
		if(s[i] == 'D') {
			atas++
		}
		curgcd := gcd(atas, i + 1)
		currentKey := Pair{atas / curgcd, (i + 1) / curgcd}
		val := m[currentKey]
		ws.WriteString(strconv.Itoa(val + 1))
		ws.WriteString(" ")
		m[currentKey]++
	}
	ws.WriteString("\n")
}

func main(){

	// Read all the input into Buffer
	bytes, _ = ioutil.ReadAll(bufio.NewReader(os.Stdin))
	max = len(bytes) - 1

	// Create a write buffer directed to STDOUT
	ws = bufio.NewWriter(os.Stdout)

	tc := fastInt()
	for i := 0;i < tc;i++{
		//fmt.Printf("Case #%v:", i)
		solve()
	}
	ws.Flush()
}

package main

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

// github.com/EndlessCheng/codeforces-go
func run(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	type pair struct{ d, k int }
	solve := func(Case int) {
		var n int
		var s []byte
		Fscan(in, &n, &s)
		c := map[pair]int{}
		p := pair{}
		for _, b := range s {
			if b == 'D' {
				p.d++
			} else {
				p.k++
			}
			if p.d == 0 {
				c[pair{0, 1}]++
				Fprint(out, c[pair{0, 1}], " ")
				continue
			} else if p.k == 0 {
				c[pair{1, 0}]++
				Fprint(out, c[pair{1, 0}], " ")
				continue
			}
			g := gcd(p.d, p.k)
			q := pair{p.d / g, p.k / g}
			c[q]++
			Fprint(out, c[q], " ")
		}
		Fprintln(out)
	}

	T := 1
	Fscan(in, &T) //
	for Case := 1; Case <= T; Case++ {
		solve(Case)
	}

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

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

func gcd(a, b int) int {
	for a != 0 {
		a, b = b%a, a
	}
	return b
}

package main

import (
	"bufio"
	"os"
	"fmt"
	"math/rand"
	"time"
	"bytes"
)

const LOCAL = false

func main() {

	rand.Seed(time.Now().UnixNano())

	str := "KDKD";

	if LOCAL {

		w := bufio.NewWriter(os.Stdout)
		start := time.Now().UnixNano()

		for i := 0; i < 18; i++ {
			var n int
			n = 27814

			var s string

			var bts bytes.Buffer

			for k := 0; k < n; k++ {
				bts.WriteByte(str[rand.Intn(4)])
			}
			s = bts.String()

			solveNow(n, s, w)
			fmt.Fprintln(w)
		}
		w.Flush()

		end := time.Now().UnixNano()
		fmt.Println((end - start)/1000000)

	} else {
		r := bufio.NewReader(os.Stdin)
		w := bufio.NewWriter(os.Stdout)
		defer w.Flush()

		var t int

		fmt.Fscan(r, &t)

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

			var s string
			fmt.Fscan(r, &s)

		//	test(s, n)

			solveNow(n, s, w)

			fmt.Fprintln(w)
		}
	}
}

type pair struct {
	d, k int
}

func solveNow(n int, s string, w *bufio.Writer) {
	mp := make(map[pair]int)

	p := pair{}

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

		if s[i] == 'D' {
			p.d++

		} else {
			p.k++
		}

		if p.d == 0 {
			mp[pair{0, 1}]++
			v := mp[pair{0, 1}]
			fmt.Fprintf(w, "%d ", v)

		} else if p.k == 0 {
			mp[pair{1, 0}]++
			v := mp[pair{1, 0}]
			fmt.Fprintf(w, "%d ", v)

		} else {
			g := gcd(p.d, p.k)
			mp[pair{p.d/g, p.k/g}]++

			v := mp[pair{p.d/g, p.k/g}]

			fmt.Fprintf(w, "%d ", v)
		}
	}
}

func solve(n int, s string, w *bufio.Writer) {

	dp := make([][2]int, n)

	if s[0] == 'D' {

		// 0 - D
		//1 - K
		dp[0][0] = 1
		dp[0][1] = 0

	} else {
		// 0 - D
		//1 - K
		dp[0][0] = 0
		dp[0][1] = 1
	}

	for i := 1; i < n; i++ {

		if s[i] == 'D' {

			dp[i][0] = dp[i - 1][0] + 1
			dp[i][1] = dp[i - 1][1]

		} else {
			dp[i][0] = dp[i - 1][0]
			dp[i][1] = dp[i - 1][1] + 1
		}
	}

	for i := 0; i < n; i++ {
		ds := dp[i][0]
		ks := dp[i][1]

		g := gcd(ds, ks)

		if  g == 1 {
			fmt.Fprintf(w, "%d ", 1)
			continue
		}

		var maxCnt int

		for gg := g; gg >= 1; gg-- {

			if g % gg == 0 {
				d := ds/gg
				k := ks/gg

				l := d + k

				done := true

				var cnt int

				var prev int
				var j int

				prev = -1
				j = l - 1

				for ; j <= i; j += l {

					yes := false

					var vd int
					var vk int

					if prev != -1 {
						vd = dp[j][0] - dp[prev][0]
						vk = dp[j][1] - dp[prev][1]
					} else {
						vd = dp[j][0]
						vk = dp[j][1]
					}

					if d * vk == k * vd {
						cnt++
						prev = j
						yes = true
					}

					if j >= i && !yes {
						done = false
					}
				}

				if done {

					if cnt > maxCnt {
						maxCnt = cnt
					}
				}
			}
		}

		fmt.Fprintf(w, "%d ", maxCnt)
	}
}

func gcd(a, b int) int {
	for b != 0 {
		t := b
		b = a % b
		a = t
	}
	return a
}

package main

import (
	"bufio"
	"os"
	"fmt"
	"math/rand"
	"time"
	"bytes"
)

const LOCAL = true

func main() {

	rand.Seed(time.Now().UnixNano())

	str := "KDKD";

	if LOCAL {

		w := bufio.NewWriter(os.Stdout)
		start := time.Now().UnixNano()

		for i := 0; i < 18; i++ {
			var n int
			n = 27814

			var s string

			var bts bytes.Buffer

			for k := 0; k < n; k++ {
				bts.WriteByte(str[rand.Intn(4)])
			}
			s = bts.String()

			solve(n, s, w)
			fmt.Fprintln(w)
		}
		w.Flush()

		end := time.Now().UnixNano()
		fmt.Println((end - start)/1000000)

	} else {
		r := bufio.NewReader(os.Stdin)
		w := bufio.NewWriter(os.Stdout)
		defer w.Flush()

		var t int

		fmt.Fscan(r, &t)

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

			var s string
			fmt.Fscan(r, &s)

		//	test(s, n)

			solve(n, s, w)

			fmt.Fprintln(w)
		}
	}
}

func test(s string, n int) {

	dp := make([][2]int, n)

	if s[0] == 'D' {

		// 0 - D
		//1 - K
		dp[0][0] = 1
		dp[0][1] = 0

	} else {
		// 0 - D
		//1 - K
		dp[0][0] = 0
		dp[0][1] = 1
	}

	for i := 1; i < n; i++ {

		if s[i] == 'D' {

			dp[i][0] = dp[i - 1][0] + 1
			dp[i][1] = dp[i - 1][1]

		} else {
			dp[i][0] = dp[i - 1][0]
			dp[i][1] = dp[i - 1][1] + 1
		}
	}

	for i := 0; i < len(s); i++ {
		leftD := dp[i][0]
		leftK := dp[i][1]

		d := dp[n - 1][0] - leftD
		k := dp[n - 1][1] - leftK

		if leftD * k == d * leftK {
			fmt.Println("FOUND")
			fmt.Println(i)
			fmt.Println(leftD)
			fmt.Println(leftK)
			fmt.Println(d)
			fmt.Println(k)
		}
	}
}

func solve(n int, s string, w *bufio.Writer) {

	dp := make([][2]int, n)

	if s[0] == 'D' {

		// 0 - D
		//1 - K
		dp[0][0] = 1
		dp[0][1] = 0

	} else {
		// 0 - D
		//1 - K
		dp[0][0] = 0
		dp[0][1] = 1
	}

	for i := 1; i < n; i++ {

		if s[i] == 'D' {

			dp[i][0] = dp[i - 1][0] + 1
			dp[i][1] = dp[i - 1][1]

		} else {
			dp[i][0] = dp[i - 1][0]
			dp[i][1] = dp[i - 1][1] + 1
		}
	}

	for i := 0; i < n; i++ {
		ds := dp[i][0]
		ks := dp[i][1]

		g := gcd(ds, ks)

		if  g == 1 {
			fmt.Fprintf(w, "%d ", 1)
			continue
		}

		var maxCnt int

		for gg := g; gg >= 1; gg-- {

			if g % gg == 0 {
				d := ds/gg
				k := ks/gg

				l := d + k

				done := true

				var cnt int

				var prev int
				var j int

				prev = -1
				j = l - 1

				for ; j <= i; j += l {

					yes := false

					var vd int
					var vk int

					if prev != -1 {
						vd = dp[j][0] - dp[prev][0]
						vk = dp[j][1] - dp[prev][1]
					} else {
						vd = dp[j][0]
						vk = dp[j][1]
					}

					if d * vk == k * vd {
						cnt++
						prev = j
						yes = true
					}

					if j >= i && !yes {
						done = false
					}
				}

				if done {

					if cnt > maxCnt {
						maxCnt = cnt
					}
				}
			}
		}

		fmt.Fprintf(w, "%d ", maxCnt)
	}
}

func gcd(a, b int) int {
	for b != 0 {
		t := b
		b = a % b
		a = t
	}
	return a
}

package main

import (
	"bufio"
	"os"
	"fmt"
	"math/rand"
	"time"
	"bytes"
)

const LOCAL = false

func main() {

	rand.Seed(time.Now().UnixNano())

	str := "KDKD";

	if LOCAL {

		for i := 0; i < 1000; i++ {
			var n int
			n = 500

			var s string

			var bts bytes.Buffer

			for k := 0; k < n; k++ {
				bts.WriteByte(str[rand.Intn(4)])
			}
			s = bts.String()

			ans := solve(n, s)

			for i := 0; i < len(ans); i++ {
				fmt.Print(ans[i])
				fmt.Print(" ")
			}
			fmt.Println()

		}

	} else {
		r := bufio.NewReader(os.Stdin)
		w := bufio.NewWriter(os.Stdout)

		var t int

		fmt.Fscan(r, &t)

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

			var s string
			fmt.Fscan(r, &s)

			ans := solve(n, s)

			for i := 0; i < len(ans); i++ {
				fmt.Fprint(w, ans[i])
				fmt.Fprint(w, " ")
			}
			fmt.Fprintln(w)
			w.Flush()
		}
	}
}

func solve(n int, s string) []int {

	dp := make([][2]int, n)

	if s[0] == 'D' {

		// 0 - D
		//1 - K
		dp[0][0] = 1
		dp[0][1] = 0

	} else {
		// 0 - D
		//1 - K
		dp[0][0] = 0
		dp[0][1] = 1
	}

	for i := 1; i < n; i++ {

		if s[i] == 'D' {

			dp[i][0] = dp[i - 1][0] + 1
			dp[i][1] = dp[i - 1][1]

		} else {
			dp[i][0] = dp[i - 1][0]
			dp[i][1] = dp[i - 1][1] + 1
		}
	}

	var ans []int

	cache := make(map[int][2]int)

	for i := 0; i < n; i++ {
		ds := dp[i][0]
		ks := dp[i][1]

		g := gcd(ds, ks)

		if  g == 1 {
			ans = append(ans, 1)
			continue
		}

		var maxCnt int
		var lc int
		var indexToCache int
		var cntCache int

		for gg := g; gg >= 1; gg-- {

			if g % gg == 0 {
				d := ds/gg
				k := ks/gg

				l := d + k

				done := true

				var cnt int

				var prev int
				var j int

				if v, ok := cache[l]; ok {
					prev = v[0]
					cnt += v[1]
					j = prev + l
				} else {
					prev = -1
					j = l - 1
				}

				for ; j <= i; j += l {

					yes := false

					var vd int
					var vk int

					if prev != -1 {
						vd = dp[j][0] - dp[prev][0]
						vk = dp[j][1] - dp[prev][1]
					} else {
						vd = dp[j][0]
						vk = dp[j][1]
					}

					if d * vk == k * vd {
						cnt++
						prev = j
						yes = true
					}

					if j >= i && !yes {
						done = false
					}
				}

				if done {

					if cnt > maxCnt {
						cntCache = cnt
						indexToCache = i
						lc = l
						maxCnt = cnt
					}
				}
			}
		}

		var arr [2]int
		arr[0] = indexToCache
		arr[1] = cntCache
		cache[lc] = arr
		ans = append(ans, maxCnt)
	}

	return ans
}

func gcd(a, b int) int {
	for b != 0 {
		t := b
		b = a % b
		a = t
	}
	return a
}

package main

import (
	"bufio"
	"os"
	"fmt"
	"math/rand"
	"time"
	"bytes"
)

const LOCAL = false

func main() {

	rand.Seed(time.Now().UnixNano())

	str := "KDKD";

	if LOCAL {

		for i := 0; i < 1000; i++ {
			var n int
			n = 500

			var s string

			var bts bytes.Buffer

			for k := 0; k < n; k++ {
				bts.WriteByte(str[rand.Intn(4)])
			}
			s = bts.String()

			ans := solve(n, s)

			for i := 0; i < len(ans); i++ {
				fmt.Print(ans[i])
				fmt.Print(" ")
			}
			fmt.Println()

		}

	} else {
		r := bufio.NewReader(os.Stdin)
		w := bufio.NewWriter(os.Stdout)

		var t int

		fmt.Fscan(r, &t)

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

			var s string
			fmt.Fscan(r, &s)

			ans := solve(n, s)

			for i := 0; i < len(ans); i++ {
				fmt.Fprint(w, ans[i])
				fmt.Fprint(w, " ")
			}
			fmt.Fprintln(w)
			w.Flush()
		}
	}
}

func solve(n int, s string) []int {

	dp := make([][2]int, n)

	if s[0] == 'D' {

		// 0 - D
		//1 - K
		dp[0][0] = 1
		dp[0][1] = 0

	} else {
		// 0 - D
		//1 - K
		dp[0][0] = 0
		dp[0][1] = 1
	}

	for i := 1; i < n; i++ {

		if s[i] == 'D' {

			dp[i][0] = dp[i - 1][0] + 1
			dp[i][1] = dp[i - 1][1]

		} else {
			dp[i][0] = dp[i - 1][0]
			dp[i][1] = dp[i - 1][1] + 1
		}
	}

	var ans []int

	cache := make(map[int][2]int)

	for i := 0; i < n; i++ {
		ds := dp[i][0]
		ks := dp[i][1]

		g := gcd(ds, ks)

		if  g == 1 {
			ans = append(ans, 1)
			continue
		}

		for gg := g; gg >= 1; gg-- {

			if g % gg == 0 {
				d := ds/gg
				k := ks/gg

				l := d + k

				done := true

				var cnt int

				var prev int
				var j int

				if v, ok := cache[l]; ok {
					prev = v[0]
					cnt += v[1]
					j = prev + l
				} else {
					prev = -1
					j = l - 1
				}

				for ; j <= i; j += l {

					yes := false

					var vd int
					var vk int

					if prev != -1 {
						vd = dp[j][0] - dp[prev][0]
						vk = dp[j][1] - dp[prev][1]
					} else {
						vd = dp[j][0]
						vk = dp[j][1]
					}

					if d * vk == k * vd {
						cnt++
						prev = j
						yes = true
					}

					if j >= i && !yes {
						done = false
					}
				}

				if done {
					var arr [2]int
					arr[0] = i
					arr[1] = cnt
					cache[l] = arr
					ans = append(ans, cnt)
					break
				}
			}
		}
	}

	return ans
}

func gcd(a, b int) int {
	for b != 0 {
		t := b
		b = a % b
		a = t
	}
	return a
}

package main

import (
	"bufio"
	"os"
	"fmt"
	"math/rand"
	"time"
	"bytes"
)

const LOCAL = false

func main() {

	rand.Seed(time.Now().UnixNano())

	str := "KDKD";

	if LOCAL {

		for i := 0; i < 1000; i++ {
			var n int
			n = 500

			var s string

			var bts bytes.Buffer

			for k := 0; k < n; k++ {
				bts.WriteByte(str[rand.Intn(4)])
			}
			s = bts.String()

			ans := solve(n, s)

			for i := 0; i < len(ans); i++ {
				fmt.Print(ans[i])
				fmt.Print(" ")
			}
			fmt.Println()

		}

	} else {
		r := bufio.NewReader(os.Stdin)
		w := bufio.NewWriter(os.Stdout)

		var t int

		fmt.Fscan(r, &t)

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

			var s string
			fmt.Fscan(r, &s)

			ans := solve(n, s)

			for i := 0; i < len(ans); i++ {
				fmt.Fprint(w, ans[i])
				fmt.Fprint(w, " ")
			}
			fmt.Fprintln(w)
			w.Flush()
		}
	}
}

func solve(n int, s string) []int {

	dp := make([][2]int, n)

	if s[0] == 'D' {

		// 0 - D
		//1 - K
		dp[0][0] = 1
		dp[0][1] = 0

	} else {
		// 0 - D
		//1 - K
		dp[0][0] = 0
		dp[0][1] = 1
	}

	for i := 1; i < n; i++ {

		if s[i] == 'D' {

			dp[i][0] = dp[i - 1][0] + 1
			dp[i][1] = dp[i - 1][1]

		} else {
			dp[i][0] = dp[i - 1][0]
			dp[i][1] = dp[i - 1][1] + 1
		}
	}

	var ans []int

	cache := make(map[int][2]int)

	for i := 0; i < n; i++ {
		ds := dp[i][0]
		ks := dp[i][1]

		g := gcd(ds, ks)

		if  g == 1 {
			ans = append(ans, 1)
			continue
		}

		for gg := g; gg >= 1; gg-- {

			if g % gg == 0 {
				d := ds/gg
				k := ks/gg

				l := d + k

				done := true

				var cnt int

				var prev int
				var j int

				if v, ok := cache[l]; ok {
					prev = v[0]
					cnt += v[1]
					j = prev + l
				} else {
					prev = -1
					j = l - 1
				}

				for ; j <= i; j += l {

					yes := false

					var vd int
					var vk int

					if prev != -1 {
						vd = dp[j][0] - dp[prev][0]
						vk = dp[j][1] - dp[prev][1]
					} else {
						vd = dp[j][0]
						vk = dp[j][1]
					}

					if d * vk == k * vd {
						cnt++
						prev = j
						yes = true
					}

					if j >= i && !yes {
						done = false
					}
				}

				if done {
					var arr [2]int
					arr[0] = i
					arr[1] = cnt
					cache[l] = arr
					ans = append(ans, cnt)
					break
				}
			}
		}
	}

	return ans
}

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


package main

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

type FastIO struct {
	in  *os.File
	out *bufio.Writer
	_i  int
	_n  int
	buf []byte
}

func (FastRead *FastIO) rc() byte {
	if FastRead._i == FastRead._n {
		FastRead._n, _ = FastRead.in.Read(FastRead.buf)
		if FastRead._n == 0 { // EOF
			return 0
		}
		FastRead._i = 0
	}
	b := FastRead.buf[FastRead._i]
	FastRead._i++
	return b
}

func (FastRead *FastIO) ReadInt() (x int) {
	rc := FastRead.rc
	b := rc()
	neg := false
	for ; '0' > b || b > '9'; b = rc() {
		if b == 0 { //EOF
			return
		}
		if b == '-' {
			neg = true
		}
	}
	for ; '0' <= b && b <= '9'; b = rc() {
		x = x*10 + int(b&15)
	}
	if neg {
		return -x
	}
	return
}

func (FastRead *FastIO) ReadInt64() (x int64) {
	rc := FastRead.rc
	b := rc()

	for ; '0' > b || b > '9'; b = rc() {
		if b == 0 {
			return
		}
	}
	for ; '0' <= b && b <= '9'; b = rc() {
		x = x*10 + int64(b&15)
	}
	return x
}

var FastRead *FastIO

func slove() {
	n := FastRead.ReadInt()
	k := FastRead.ReadInt()
	nums := make([]int64, n)
	for i := 0; i < n; i++ {
		nums[i] = FastRead.ReadInt64()
	}
	if n == 1 {
		ans := int64(nums[0])
		ans += int64(k - 1)
		fmt.Println(ans)
	} else if k <= n {
		var cur, ans int64
		for i := 0; i < k; i++ {
			cur += nums[i]
		}
		ans = cur
		for i := k; i < n; i++ {
			cur += nums[i] - nums[i-k]
			if cur > ans {
				ans = cur
			}
		}
		ans += (int64(k) - 1) * (int64(k)) / 2
		fmt.Println(ans)
	} else {
		var ans int64
		for i := range nums {
			ans += nums[i]
		}
		start := (k - 1) % (n - 1)
		repeat := (k-1)/(n-1) - 1
		ans += int64(repeat) * (2 + 2*int64(n-1)) * int64(n-1) / 2
		ans += int64(start+start+n-1) * int64(n) / 2
		fmt.Println(ans)
	}
}

func main() {
	//debug.SetGCPercent(-1)
	FastRead = &FastIO{
		os.Stdin,
		bufio.NewWriter(os.Stdout),
		0, 0, make([]byte, 1<<12),
	}

	t := FastRead.ReadInt()
	for ; t > 0; t-- {
		slove()
	}
}

package main

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

func main() {
	scanner := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	var ntests int
	fmt.Fscan(scanner, &ntests)
outer:
	for iter := 0; iter < ntests; iter++ {
		var n, k int
		fmt.Fscan(scanner, &n, &k)
		slice := make([]int64, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(scanner, &slice[i])
		}
		if n == 1 {
			fmt.Fprintln(writer, slice[0]+int64(k)-1)
			continue outer
		}
		if k <= n {
			j := 0
			res := int64(0)
			max := int64(0)
			//startl, startr := 0, 0
			for i := 0; i < n; i++ {
				j++
				res += slice[i]
				// if k == j {
				// 	max = res
				// }
				if j > k {
					res -= slice[i-k]
				}
				if j == k {
					max = res
				}
				if max < res {
					max = res
					//startl = i - k
					//startr = i
				}
			}
			fmt.Fprintln(writer, max+((int64)(k)*(int64)(k-1)/2))
		} else {
			res := int64(0)
			for i := 0; i < n; i++ {
				res += slice[i]
			}
			//fmt.Println("!!! sum", res)
			// forward
			// elems := int64((k % n) + 1)
			// fres := elems * (elems + 1) / 2
			// res += fres
			// fmt.Println("!!! forward", res)
			// // square
			// squares := (int64(k) - elems) / (int64)(n-1)
			// multi := int64(n-1) * int64(n-1)
			// res += squares * multi
			// fmt.Println("!!! backward", res)
			// fmt.Fprintln(writer, res)
			res += int64(n)*int64(k) - int64(int64(int64(n)*int64(n+1))/2)
			fmt.Fprintln(writer, res)
		}
	}
}

package main

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

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

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

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

		arr := make([]int64, n+1)
		for i := int64(1); i <= n; i++ {
			fmt.Fscan(in, &arr[i])
			arr[i] += arr[i-1]
		}

		if k >= n {
			fmt.Fprintln(out, arr[n]+(k-1+k-n)*n/2)
		} else {
			z := arr[k]
			for i := k + 1; i <= n; i++ {
				z = max(z, arr[i]-arr[i-k])
			}
			fmt.Fprintln(out, z+k*(k-1)/2)
		}
	}
}

package main

import (
	"bufio"
	"fmt"

	"os"
)

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

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

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

        a := make([]int64, n)
        pre := make([]int64, n+1)
        for i= 0; i <n; i++{
            fmt.Fscan(in,&a[i])
        }
        pre[0] = 0
        for i = 0; i < n; i++ {
            pre[i+1] = pre[i] + a[i]
        }
        if k >= n {
            ans := pre[n]
            ans += (n*(n-1)/2)
            ans += (k-n)*n
            fmt.Fprintln(out,ans)
        } else{
            var ans int64 = 0
            for i=k; i < n + 1; i ++ {
                if ans < pre[i] - pre[i-k] {
                    ans = pre[i]-pre[i-k]
                }
            }
            ans += (k*(k-1)/2)
            fmt.Fprintln(out,ans)
        }

    }
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)
var wrtr = bufio.NewWriterSize(os.Stdout, 10_000_000)
var rdr = bufio.NewScanner(os.Stdin)
func gs() string  { rdr.Scan(); return rdr.Text() }
func gi() int     { i,e := strconv.Atoi(gs()); if e != nil {panic(e)}; return i }
func gi64() int64 { i,e := strconv.ParseInt(gs(),10,64); if e != nil {panic(e)}; return i }
func gi2() (int,int) { return gi(),gi() }
func gi3() (int,int,int) { return gi(),gi(),gi() }
func gi4() (int,int,int,int) { return gi(),gi(),gi(),gi() }
func gis(n int) []int  { res := make([]int,n); for i:=0;i<n;i++ { res[i] = gi() }; return res }
func gf() float64 { f,e := strconv.ParseFloat(gs(),64); if e != nil {panic(e)}; return f }
func gbs() []byte { return []byte(gs()) }
func gfs(n int) []float64  { res := make([]float64,n); for i:=0;i<n;i++ { res[i] = gf() }; return res }
func gss(n int) []string  { res := make([]string,n); for i:=0;i<n;i++ { res[i] = gs() }; return res }
func ia(m int) []int { return make([]int,m) }
func iai(m int,v int) []int { a := make([]int,m); for i:=0;i<m;i++ { a[i] = v }; return a }
func twodi(n int,m int,v int) [][]int {
	r := make([][]int,n); for i:=0;i<n;i++ { x := make([]int,m); for j:=0;j<m;j++ { x[j] = v }; r[i] = x }; return r
}
func fill2(m int) ([]int,[]int) { a,b := ia(m),ia(m); for i:=0;i<m;i++ {a[i],b[i] = gi(),gi()}; return a,b }
func fill3(m int) ([]int,[]int,[]int) { a,b,c := ia(m),ia(m),ia(m); for i:=0;i<m;i++ {a[i],b[i],c[i] = gi(),gi(),gi()}; return a,b,c }
func fill4(m int) ([]int,[]int,[]int,[]int) { a,b,c,d := ia(m),ia(m),ia(m),ia(m); for i:=0;i<m;i++ {a[i],b[i],c[i],d[i] = gi(),gi(),gi(),gi()}; return a,b,c,d }
func abs(a int) int { if a < 0 { return -a }; return a }
func rev(a []int) { i,j := 0,len(a)-1; for i < j { a[i],a[j] = a[j],a[i]; i++; j-- } }
func max(a,b int) int { if a > b { return a }; return b }
func min(a,b int) int { if a > b { return b }; return a }
func tern(cond bool, a int, b int) int { if cond { return a }; return b }
func terns(cond bool, a string, b string) string { if cond { return a }; return b }
func maxarr(a []int) int { ans := a[0]; for _,aa := range(a) { if aa > ans { ans = aa } }; return ans }
func minarr(a []int) int { ans := a[0]; for _,aa := range(a) { if aa < ans { ans = aa } }; return ans }
func sumarr(a []int) int { ans := 0; for _,aa := range(a) { ans += aa }; return ans }
func zeroarr(a []int) { for i:=0; i<len(a); i++ { a[i] = 0 } }
func powmod(a,e,mod int) int { res, m := 1, a; for e > 0 { if e&1 != 0 { res = res * m % mod }; m = m * m % mod; e >>= 1 }; return res }
func powint(a,e int) int { res, m := 1, a; for e > 0 { if e&1 != 0 { res = res * m }; m = m * m; e >>= 1 }; return res }
func gcd(a,b int) int { for b != 0 { t:=b; b=a%b; a=t }; return a }
func gcdExtended(a,b int) (int,int,int) { if a == 0 { return b,0,1 }; gcd,x1,y1 := gcdExtended(b%a,a); return gcd, y1-(b/a)*x1,x1 }
func modinv(a,m int) (int,bool) { g,x,_ := gcdExtended(a,m); if g != 1 { return 0,false }; return (x % m + m) % m,true  }
func makefact(n int,mod int) ([]int,[]int) {
	fact,factinv := make([]int,n+1),make([]int,n+1)
	fact[0] = 1; for i:=1;i<=n;i++ { fact[i] = fact[i-1] * i % mod }
	factinv[n] = powmod(fact[n],mod-2,mod); for i:=n-1;i>=0;i-- { factinv[i] = factinv[i+1] * (i+1) % mod }
	return fact,factinv
}
func vecintstring(a []int) string { astr := make([]string,len(a)); for i,a := range a { astr[i] = strconv.Itoa(a) }; return strings.Join(astr," ") }
func main() {
	//f1, _ := os.Create("cpu.prof"); pprof.StartCPUProfile(f1); defer pprof.StopCPUProfile()
	defer wrtr.Flush()
	infn := ""; if infn == "" && len(os.Args) > 1 {	infn = os.Args[1] }
	if infn != "" {	f, e := os.Open(infn); if e != nil { panic(e) }; rdr = bufio.NewScanner(f) }
	rdr.Split(bufio.ScanWords); rdr.Buffer(make([]byte,1024),1_000_000_000)
	// PROGRAM STARTS HERE
	T := gi()
	for tt:=1;tt<=T;tt++ {
		N,K := gi64(),gi64(); A := make([]int64,N); for i:=int64(0);i<N;i++ { A[i] = gi64() }
		ans := int64(0)
		cum := int64(0)
		for i:=int64(0);i<N;i++ {
			cum += A[i]; if i-K>=0 { cum -= A[i-K] }; if cum > ans { ans = cum }
		}
		if N >= K {
			ans += K * (K-1 + 0) / 2
		} else {
			ans += N * (K-1 + K-N) / 2
		}
		fmt.Fprintln(wrtr,ans)
	}
}

package main

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

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

func d1688(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var T int
	var n int
	var k int
	var x int

	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		Fscan(in, &k)
		a := make([]int, n)
		for i := 0; i < n; i++ {
			Fscan(in, &x)
			a[i] = x
		}

		ans := int64(0)
		if k < n {
			s := int64(0)
			for i := 0;i < k; i++ {
				s += int64(a[i])
			}
			ans = s
			for i := k; i< n;i++ {
				s += int64(a[i] - a[i-k])
				if s > ans {
					ans = s
				}
			}
			ans += (int64(k) * int64(k-1))/2
		} else {
			for _, v := range a {
				ans += int64(v)
			}
			ans += int64(n) * (int64(k-1) + int64(k-n)) / 2
		}

		Fprintln(out, ans)
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"runtime/debug"
)

/* begin of MyIO */
const eof = 0

type any = interface{}

type MyIO struct {
	in  *os.File
	out *bufio.Writer
	_i  int
	_n  int
	buf []byte
}

func NewIO(in *os.File, out *os.File) *MyIO {
	return &MyIO{
		in,
		bufio.NewWriter(out),
		0, 0, make([]byte, 1<<12),
	}
}

func (myio *MyIO) rc() byte {
	if myio._i == myio._n {
		myio._n, _ = myio.in.Read(myio.buf)
		if myio._n == 0 { // EOF
			return eof
		}
		myio._i = 0
	}
	b := myio.buf[myio._i]
	myio._i++
	return b
}

func (myio *MyIO) ReadInt() (x int) {
	rc := myio.rc
	b := rc()
	neg := false
	for ; '0' > b || b > '9'; b = rc() {
		if b == eof {
			return
		}
		if b == '-' {
			neg = true
		}
	}
	for ; '0' <= b && b <= '9'; b = rc() {
		x = (x << 1) + (x << 3) + int(b&15)
	}
	if neg {
		return -x
	}
	return
}

func (myio *MyIO) ReadInt64() (x int64) {
	rc := myio.rc
	b := rc()
	neg := false

	for ; '0' > b || b > '9'; b = rc() {
		if b == eof {
			return
		}
		if b == '-' {
			neg = true
		}
	}
	for ; '0' <= b && b <= '9'; b = rc() {
		x = (x << 1) + (x << 3) + int64(b&15)
	}
	if neg {
		return -x
	}
	return x
}

func (myio *MyIO) ReadBytes() (s []byte) {
	rc := myio.rc
	b := rc()
	for ; b <= ' '; b = rc() {
	}
	for ; b > ' '; b = rc() {
		s = append(s, b)
	}
	return
}

func (myio *MyIO) ReadStr() (s string) {
	return string(myio.ReadBytes())
}

func (myio *MyIO) Flush() {
	myio.out.Flush()
}

func (myio *MyIO) Printf(format string, a ...any) (n int, err error) {
	return fmt.Fprintf(myio.out, format, a...)
}

func (myio *MyIO) Println(a ...any) (n int, err error) {
	return fmt.Fprintln(myio.out, a...)
}

func (myio *MyIO) Print(a ...any) (n int, err error) {
	return fmt.Fprint(myio.out, a...)
}

var myio *MyIO

func ri() int {
	return myio.ReadInt()
}

func ri64() int64 {
	return myio.ReadInt64()
}

func ria(_n int) []int {
	a := make([]int, _n)
	for i := range a {
		a[i] = ri()
	}
	return a
}

/* End of MyIO */

func getSum(s, e int) int64 {
	return (int64(s) + int64(e)) * (int64(e) - int64(s) + 1) / 2
}

func sol() int64 {
	n := ri()
	k := ri()
	a := ria(n)
	var ans int64
	sum := make([]int64, n+1)
	sum[0] = 0

	if n == 1 {
		// wait until
		return int64(a[0] + k - 1)
	}
	for i := 0; i < n; i++ {
		sum[i+1] = sum[i] + int64(a[i])
	}

	if k >= n {
		return getSum(k-n+1-1, k-1) + sum[n]
	}

	t := int64(0)
	for i := 0; i+k <= n; i++ {
		z := sum[i+k] - sum[i]
		if z > t {
			t = z
		}
	}
	ans = t + getSum(0, k-1)
	return ans
}

func main() {
	debug.SetGCPercent(-1)
	in, out := os.Stdin, os.Stdout
	myio = NewIO(in, out)
	defer myio.Flush()

	var t = 1
	t = ri()
	for ; t > 0; t-- {
		myio.Println(sol())
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"runtime/debug"
)

/* begin of MyIO */
const eof = 0

type any = interface{}

type MyIO struct {
	in  *os.File
	out *bufio.Writer
	_i  int
	_n  int
	buf []byte
}

func NewIO(in *os.File, out *os.File) *MyIO {
	return &MyIO{
		in,
		bufio.NewWriter(out),
		0, 0, make([]byte, 1<<12),
	}
}

func (myio *MyIO) rc() byte {
	if myio._i == myio._n {
		myio._n, _ = myio.in.Read(myio.buf)
		if myio._n == 0 { // EOF
			return eof
		}
		myio._i = 0
	}
	b := myio.buf[myio._i]
	myio._i++
	return b
}

func (myio *MyIO) ReadInt() (x int) {
	rc := myio.rc
	b := rc()
	neg := false
	for ; '0' > b || b > '9'; b = rc() {
		if b == eof {
			return
		}
		if b == '-' {
			neg = true
		}
	}
	for ; '0' <= b && b <= '9'; b = rc() {
		x = x*10 + int(b&15)
	}
	if neg {
		return -x
	}
	return
}

func (myio *MyIO) ReadInt64() (x int64) {
	rc := myio.rc
	b := rc()
	neg := false

	for ; '0' > b || b > '9'; b = rc() {
		if b == eof {
			return
		}
		if b == '-' {
			neg = true
		}
	}
	for ; '0' <= b && b <= '9'; b = rc() {
		x = x*10 + int64(b&15)
	}
	if neg {
		return -x
	}
	return x
}

func (myio *MyIO) ReadBytes() (s []byte) {
	rc := myio.rc
	b := rc()
	for ; b <= ' '; b = rc() {
	}
	for ; b > ' '; b = rc() {
		s = append(s, b)
	}
	return
}

func (myio *MyIO) ReadStr() (s string) {
	return string(myio.ReadBytes())
}

func (myio *MyIO) Flush() {
	myio.out.Flush()
}

func (myio *MyIO) Printf(format string, a ...any) (n int, err error) {
	return fmt.Fprintf(myio.out, format, a...)
}

func (myio *MyIO) Println(a ...any) (n int, err error) {
	return fmt.Fprintln(myio.out, a...)
}

func (myio *MyIO) Print(a ...any) (n int, err error) {
	return fmt.Fprint(myio.out, a...)
}

var myio *MyIO

func ri() int {
	return myio.ReadInt()
}

func ri64() int64 {
	return myio.ReadInt64()
}

func ria(_n int) []int {
	a := make([]int, _n)
	for i := range a {
		a[i] = ri()
	}
	return a
}

/* End of MyIO */

func getSum(s, e int) int64 {
	return (int64(s) + int64(e)) * (int64(e) - int64(s) + 1) / 2
}

func sol() int64 {
	n := ri()
	k := ri()
	a := ria(n)
	var ans int64
	sum := make([]int64, n+1)
	sum[0] = 0

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

	if n == 1 {
		// wait until
		return int64(a[0] + k - 1)
	}

	var tot int64 = 0
	for i := 0; i < n; i++ {
		tot += int64(a[i])
	}

	if k >= n {
		return getSum(k-n+1-1, k-1) + tot
	}

	t := int64(0)
	for i := 0; i+k <= n; i++ {
		z := sum[i+k] - sum[i]
		if z > t {
			t = z
		}
	}
	ans = t + getSum(0, k-1)
	return ans
}

func main() {
	debug.SetGCPercent(-1)
	in, out := os.Stdin, os.Stdout
	myio = NewIO(in, out)
	defer myio.Flush()

	var t = 1
	t = ri()
	for ; t > 0; t-- {
		myio.Println(sol())
	}
}

package main

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

type FastIO struct {
	in  *os.File
	out *bufio.Writer
	_i  int
	_n  int
	buf []byte
}

func (FastRead *FastIO) rc() byte {
	if FastRead._i == FastRead._n {
		FastRead._n, _ = FastRead.in.Read(FastRead.buf)
		if FastRead._n == 0 { // EOF
			return 0
		}
		FastRead._i = 0
	}
	b := FastRead.buf[FastRead._i]
	FastRead._i++
	return b
}

func (FastRead *FastIO) ReadInt() (x int) {
	rc := FastRead.rc
	b := rc()
	neg := false
	for ; '0' > b || b > '9'; b = rc() {
		if b == 0 { //EOF
			return
		}
		if b == '-' {
			neg = true
		}
	}
	for ; '0' <= b && b <= '9'; b = rc() {
		x = x*10 + int(b&15)
	}
	if neg {
		return -x
	}
	return
}

func (FastRead *FastIO) ReadInt64() (x int64) {
	rc := FastRead.rc
	b := rc()

	for ; '0' > b || b > '9'; b = rc() {
		if b == 0 {
			return
		}
	}
	for ; '0' <= b && b <= '9'; b = rc() {
		x = x*10 + int64(b&15)
	}
	return x
}

var FastRead *FastIO

func slove() {
	//var n, k int
	//fmt.Fscan(r, &n, &k)
	n := FastRead.ReadInt()
	k := FastRead.ReadInt()
	nums := make([]int64, n)
	for i := 0; i < n; i++ {
		//fmt.Fscan(r, &nums[i])
		nums[i] = FastRead.ReadInt64()
	}
	if n == 1 {
		ans := int64(nums[0])
		ans += int64(k - 1)
		fmt.Println(ans)
	} else if k <= n {
		var cur, ans int64
		for i := 0; i < k; i++ {
			cur += nums[i]
		}
		ans = cur
		for i := k; i < n; i++ {
			cur += nums[i] - nums[i-k]
			//ans = max(ans, cur)
			if cur > ans {
				ans = cur
			}
		}
		ans += (int64(k) - 1) * (int64(k)) / 2
		fmt.Println(ans)
	} else {
		var ans int64
		for i := range nums {
			ans += nums[i]
		}
		start := (k - 1) % (n - 1)
		repeat := (k-1)/(n-1) - 1
		ans += int64(repeat) * (2 + 2*int64(n-1)) * int64(n-1) / 2
		ans += int64(start+start+n-1) * int64(n) / 2
		fmt.Println(ans)
	}
}

func main() {
	//debug.SetGCPercent(-1)
	FastRead = &FastIO{
		os.Stdin,
		bufio.NewWriter(os.Stdout),
		0, 0, make([]byte, 1<<12),
	}

	var t = 1
	t = FastRead.ReadInt()
	for ; t > 0; t-- {
		slove()
	}
}

package main

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

var r = bufio.NewReader(os.Stdin)
var w = bufio.NewWriter(os.Stdout)

func slove() {
	var n, k int
	fmt.Fscan(r, &n, &k)
	nums := make([]int64, n)
	for i:=0;i<n;i++ {
		fmt.Fscan(r, &nums[i])
	}
	if n == 1{
		ans := int64(nums[0])
		ans += int64(k - 1)
		fmt.Fprintln(w, ans)
	} else if k <= n {
		var cur, ans int64
		for i:=0;i<k;i++ {
			cur += nums[i]
		}
		ans = cur
		for i:=k;i<n;i++ {
			cur += nums[i] - nums[i-k]
			ans = max(ans, cur)
		}
		ans += (int64(k) - 1) * (int64(k)) / 2
		fmt.Fprintln(w, ans)
	} else {
		var ans int64
		for i := range nums {
			ans += nums[i]
		}
		start := (k - 1) % (n - 1)
		repeat := (k - 1) / (n - 1) - 1
		ans += int64(repeat) * (2 + 2*int64(n-1))*int64(n-1) / 2
		ans += int64(start+start+n-1)*int64(n)/2
		fmt.Fprintln(w, ans)
	}
}

func max(a, b int64) int64 {if a > b {return a} else {return b}}

func main() {
	defer w.Flush()
	var n int
	fmt.Fscan(r, &n)
	for i:=1;i<=n;i++ {
		slove()
	}
}

package main

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

func A3() {
	_in, _out := os.Stdin, os.Stdout
	in, out := bufio.NewReader(_in), bufio.NewWriter(_out)
	defer out.Flush()
	var cas int = 1
	Fscan(in, &cas)
	var sol func()
	// Coding...beg!
	sol = func() {
		var n, k, total, sum int64

		Fscan(in, &n, &k)
		a := make([]int, n)
		for i := int64(0); i < n; i++ {
			Fscan(in, &a[i])

			sum += int64(a[i])
		}

		if k <= n {
			//sort.Ints(a)
			var sum2, s int64
			for i := 0; i < int(k); i++ {
				sum2 += int64(a[i])
			}
			s = sum2
			for i := int(k); i < int(n); i++ {
				sum2 += int64(a[i])
				sum2 -= int64(a[i-int(k)])
				if sum2 > s {
					s = sum2
				}
			}
			Fprintln(out, s+(k-1)*k/2)
			return
		}
		total = sum + n*(n-1)/2
		k -= n
		total += (n*(n+1)/2 + n*(n-1)/2) * (k / n)
		k %= n
		total += (2*n-(k-1))*k/2 + (k-1)*k/2
		Fprintln(out, total)
	}
	// Coding...ove!
	for i := 0; i < cas; i++ {
		sol()
	}
}

func main() { A3() }

package main

import "fmt"

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

func main() {
    var t int
    fmt.Scanf("%d",&t)
    for ;t > 0; t-- {
        var n,k,i int64
        fmt.Scanf("%d %d",&n,&k)
        // var a [n] int64
        a := make([]int64, n)
        // var pre[n+1]int64
        pre := make([]int64, n+1)
        for i= 0; i <n; i++{
            fmt.Scanf("%d",&a[i])
        }
        pre[0] = 0
        for i = 0; i < n; i++ {
            pre[i+1] = pre[i] + a[i]
        }
        if k >= n {
            ans := pre[n]
            ans += (n*(n-1)/2)
            ans += (k-n)*n
            fmt.Println(ans)
        } else{
            var ans int64 = 0
            for i=k; i < n + 1; i ++ {
                ans = Max(ans,pre[i]-pre[i-k])
            }
            ans += (k*(k-1)/2)
            fmt.Println(ans)
        }

    }
}

package main
import (
	"bufio"
	"os"
	"strconv"
    "strings"
)
var wrtr = bufio.NewWriterSize(os.Stdout, 10_000_000)
var rdr = bufio.NewScanner(os.Stdin)
func gs() string  { rdr.Scan(); return rdr.Text() }
func gi() int     { i,e := strconv.Atoi(gs()); if e != nil {panic(e)}; return i }
func gi2() (int,int) { return gi(),gi() }
func gi3() (int,int,int) { return gi(),gi(),gi() }
func gi4() (int,int,int,int) { return gi(),gi(),gi(),gi() }
func gis(n int) []int  { res := make([]int,n); for i:=0;i<n;i++ { res[i] = gi() }; return res }
func gf() float64 { f,e := strconv.ParseFloat(gs(),64); if e != nil {panic(e)}; return f }
func gbs() []byte { return []byte(gs()) }
func gfs(n int) []float64  { res := make([]float64,n); for i:=0;i<n;i++ { res[i] = gf() }; return res }
func gss(n int) []string  { res := make([]string,n); for i:=0;i<n;i++ { res[i] = gs() }; return res }
func ia(m int) []int { return make([]int,m) }
func iai(m int,v int) []int { a := make([]int,m); for i:=0;i<m;i++ { a[i] = v }; return a }
func twodi(n int,m int,v int) [][]int {
	r := make([][]int,n); for i:=0;i<n;i++ { x := make([]int,m); for j:=0;j<m;j++ { x[j] = v }; r[i] = x }; return r
}
func fill2(m int) ([]int,[]int) { a,b := ia(m),ia(m); for i:=0;i<m;i++ {a[i],b[i] = gi(),gi()}; return a,b }
func fill3(m int) ([]int,[]int,[]int) { a,b,c := ia(m),ia(m),ia(m); for i:=0;i<m;i++ {a[i],b[i],c[i] = gi(),gi(),gi()}; return a,b,c }
func fill4(m int) ([]int,[]int,[]int,[]int) { a,b,c,d := ia(m),ia(m),ia(m),ia(m); for i:=0;i<m;i++ {a[i],b[i],c[i],d[i] = gi(),gi(),gi(),gi()}; return a,b,c,d }
func abs(a int) int { if a < 0 { return -a }; return a }
func rev(a []int) { i,j := 0,len(a)-1; for i < j { a[i],a[j] = a[j],a[i]; i++; j-- } }
func max(a,b int) int { if a > b { return a }; return b }
func min(a,b int) int { if a > b { return b }; return a }
func tern(cond bool, a int, b int) int { if cond { return a }; return b }
func terns(cond bool, a string, b string) string { if cond { return a }; return b }
func maxarr(a []int) int { ans := a[0]; for _,aa := range(a) { if aa > ans { ans = aa } }; return ans }
func minarr(a []int) int { ans := a[0]; for _,aa := range(a) { if aa < ans { ans = aa } }; return ans }
func sumarr(a []int) int { ans := 0; for _,aa := range(a) { ans += aa }; return ans }
func zeroarr(a []int) { for i:=0; i<len(a); i++ { a[i] = 0 } }
func powmod(a,e,mod int) int { res, m := 1, a; for e > 0 { if e&1 != 0 { res = res * m % mod }; m = m * m % mod; e >>= 1 }; return res }
func powint(a,e int) int { res, m := 1, a; for e > 0 { if e&1 != 0 { res = res * m }; m = m * m; e >>= 1 }; return res }
func gcd(a,b int) int { for b != 0 { t:=b; b=a%b; a=t }; return a }
func gcdExtended(a,b int) (int,int,int) { if a == 0 { return b,0,1 }; gcd,x1,y1 := gcdExtended(b%a,a); return gcd, y1-(b/a)*x1,x1 }
func modinv(a,m int) (int,bool) { g,x,_ := gcdExtended(a,m); if g != 1 { return 0,false }; return (x % m + m) % m,true  }
func makefact(n int,mod int) ([]int,[]int) {
	fact,factinv := make([]int,n+1),make([]int,n+1)
	fact[0] = 1; for i:=1;i<=n;i++ { fact[i] = fact[i-1] * i % mod }
	factinv[n] = powmod(fact[n],mod-2,mod); for i:=n-1;i>=0;i-- { factinv[i] = factinv[i+1] * (i+1) % mod }
	return fact,factinv
}
func vecintstring(a []int) string { astr := make([]string,len(a)); for i,a := range a { astr[i] = strconv.Itoa(a) }; return strings.Join(astr," ") }
func main() {
	//f1, _ := os.Create("cpu.prof"); pprof.StartCPUProfile(f1); defer pprof.StopCPUProfile()
	defer wrtr.Flush()
	infn := ""; if infn == "" && len(os.Args) > 1 {	infn = os.Args[1] }
	if infn != "" {	f, e := os.Open(infn); if e != nil { panic(e) }; rdr = bufio.NewScanner(f) }
	rdr.Split(bufio.ScanWords); rdr.Buffer(make([]byte,1024),1_000_000_000)
	// PROGRAM STARTS HERE
}

package main

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

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

func d1688(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var T int
	var n int
	var k int
	var x int

	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		Fscan(in, &k)
		a := make([]int, n)
		for i := 0; i < n; i++ {
			Fscan(in, &x)
			a[i] = x
		}

		ans := int64(0)
		if k < n {
			s := int64(0)
			for i := 0;i < k; i++ {
				s += int64(a[i])
			}
			ans = s
			for i := k; i< n;i++ {
				s += int64(a[i] - a[i-k])
				if s > ans {
					ans = s
				}
			}
			ans += int64(k * (k-1))/2
		} else {
			for _, v := range a {
				ans += int64(v)
			}
			ans += int64(n) * (int64(k-1) + int64(k-n)) / 2
		}

		Fprintln(out, ans)
	}
}

package main

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

var r = bufio.NewReader(os.Stdin)
var w = bufio.NewWriter(os.Stdout)

func slove() {
	var n, k int
	fmt.Fscan(r, &n, &k)
	nums := make([]int64, n)
	for i:=0;i<n;i++ {
		fmt.Fscan(r, &nums[i])
	}
	if n == 1{
		ans := int64(nums[0])
		ans += int64(0 + k - 1) * int64(k) / 2
		fmt.Fprintln(w, ans)
	} else if k <= n {
		var cur, ans int64
		for i:=0;i<k;i++ {
			cur += nums[i]
		}
		ans = cur
		for i:=k;i<n;i++ {
			cur += nums[i] - nums[i-k]
			ans = max(ans, cur)
		}
		ans += (int64(k) - 1) * (int64(k)) / 2
		fmt.Fprintln(w, ans)
	} else {
		var ans int64
		for i := range nums {
			ans += nums[i]
		}
		start := (k - 1) % (n - 1)
		repeat := (k - 1) / (n - 1) - 1
		ans += int64(repeat) * (2 + 2*int64(n-1))*int64(n-1) / 2
		ans += int64(start+start+n-1)*int64(n)/2
		fmt.Fprintln(w, ans)
	}
}

func max(a, b int64) int64 {if a > b {return a} else {return b}}

func main() {
	defer w.Flush()
	var n int
	fmt.Fscan(r, &n)
	for i:=1;i<=n;i++ {
		slove()
	}
}

package main

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

func A3() {
	_in, _out := os.Stdin, os.Stdout
	in, out := bufio.NewReader(_in), bufio.NewWriter(_out)
	defer out.Flush()
	var cas int = 1
	Fscan(in, &cas)
	var sol func()
	// Coding...beg!
	sol = func() {
		var n, k, total, sum int64

		Fscan(in, &n, &k)
		a := make([]int, n)
		for i := int64(0); i < n; i++ {
			Fscan(in, &a[i])

			sum += int64(a[i])
		}

		if k <= n {
			sort.Ints(a)
			var sum2 int64
			for i := n - 1; i >= n-k; i-- {
				sum2 += int64(a[i])
			}
			Fprintln(out, sum2+(k-1)*k/2)
			return
		}
		total = sum + n*(n-1)/2
		k -= n
		total += (n*(n+1)/2 + n*(n-1)/2) * (k / n)
		k %= n
		total += (2*n-(k-1))*k/2 + (k-1)*k/2
		Fprintln(out, total)
	}
	// Coding...ove!
	for i := 0; i < cas; i++ {
		sol()
	}
}

func main() { A3() }