package main

import (
	"bufio"
	"fmt"
	"os"

)

var n int

var a []int

func main() {

	r := bufio.NewReader(os.Stdin)

	var t int

	fmt.Fscan(r, &t)

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

		a = make([]int, n)

		for j := 0; j < n; j++ {
			fmt.Fscan(r, &a[j])
		}

		solve()

		fmt.Println()
	}
}

func solve()  {

	cnts := make([]int, 31)

	for i := 30; i >= 0; i-- {
		for j := 0; j < n; j++ {
			if a[j] >> uint(i) & 0x1 == 1 {
				cnts[i]++
			}
		}
	}

	var g int = cnts[0]

	for i := 1; i < 31; i++ {
		g = GCD(g, cnts[i])
	}

	for i := 1; i <= n; i++ {
		if g % i == 0 {
			fmt.Print(i)
			fmt.Print(" ")
		}
	}
}

// greatest common divisor (GCD) via Euclidean algorithm
func GCD(a, b int) int {
	for b != 0 {
		t := b
		b = a % b
		a = t
	}
	return a
}

package main

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

/*
1
6
5 6 4 1 6 1
2 4 1 5 4 1
---
1 = min(3, 0+1)
=====
1
5
9 5 5 5 1
1 5 5 5 9
---
0
=======
1
4
1 5 1 1
9 9 3 9
---
14 = min(8+8, 2+4+8)
*/

//goland:noinspection GoUnhandledErrorResult
func solve() {
	var n int
	scanf("%d\n", &n)

	bitToCnt := make(map[int]int)

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

		j := 1
		for j != 0 {
			if a&j > 0 {
				bitToCnt[j]++
			}
			j <<= 1
		}
	}
	scanf("\n")

	for i := 1; i <= n; i++ {
		divisibleToAll := true
		for _, cnt := range bitToCnt {
			if cnt%i != 0 {
				divisibleToAll = false
				break
			}
		}

		if divisibleToAll {
			fmt.Fprintf(writer, "%d ", i)
		}
	}
	fmt.Fprintln(writer)
}

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

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

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

const maxUint = ^uint(0)

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

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

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

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

//goland:noinspection GoUnusedFunction
func reversed(s string) string {
	input := []rune(s)
	li := len(input)
	ret := make([]rune, li)
	for i, r := range input {
		ret[li-i-1] = r
	}
	return string(ret)
}

//goland:noinspection GoUnusedFunction
func divCeil(num, div int64) int64 {
	quot := num / div
	if num%div > 0 {
		quot++
	} else if num%div < 0 {
		quot--
	}
	return quot
}

//goland:noinspection GoUnusedFunction
func min(nums ...int64) int64 {
	return nums[minIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func minIdx(nums ...int64) int {
	minNumIdx := 0
	for i, num := range nums {
		if num < nums[minNumIdx] {
			minNumIdx = i
		}
	}

	return minNumIdx
}

//goland:noinspection GoUnusedFunction
func max(nums ...int) int {
	return nums[maxIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func maxIdx(nums ...int) int {
	maxNumIdx := 0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = i
		}
	}

	return maxNumIdx
}

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

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

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

	for i := 5; i*i <= num; i += 6 {
		if num%i == 0 || num%(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 {
	if num == 0 {
		return 0
	}

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

//goland:noinspection GoUnusedFunction
func findSet(parent []int, v int) int {
	if v == parent[v] {
		return v
	}

	parent[v] = findSet(parent, parent[v])

	return parent[v]
}

//goland:noinspection GoUnusedFunction
func unionSets(parent []int, v, u int) {
	v = findSet(parent, v)
	u = findSet(parent, u)

	if v != u {
		parent[v] = u
	}
}

package main

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

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)

		m := make([]int, 30)
		for i := 0; i < n; i++ {
			var v int
			fmt.Fscan(in, &v)

			var pos int
			for v > 0 {
				if v%2 != 0 {
					m[pos]++
				}
				v /= 2
				pos++
			}
		}

		fmt.Fprintf(out, "1 ")
		for i := 2; i <= n; i++ {
			var f bool
			for _, v := range m {
				if v%i != 0 {
					f = false
					break
				}
				f = true
			}
			if f {
				fmt.Fprintf(out, "%d ", i)
			}
		}
		fmt.Fprintln(out)
	}
}

package main

import (
	"bufio"
	"fmt"
	"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 gcd(a, b int) int {
	if b == 0 {
		return a
	}
	return gcd(b, a%b)
}

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

func bin(a int) []int {
	var b []int
	for a != 0 {
		b = append(b, a&1)
		a >>= 1
	}
	// for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
	// 	b[i], b[j] = b[j], b[i]
	// }
	return b
}

func main() {
	var T int
	T = 1
	scanf("%d", &T)
	for t := 0; t < T; t++ {
		var n int
		scanf("\n%d\n", &n)
		arr := make([]int, n)
		mp := make(map[int]int)
		for i := 0; i < n; i++ {
			scanf("%d", &arr[i])
			b := bin(arr[i])
			for id, x := range b {
				if x == 1 {
					mp[id]++
				}
			}
		}
		for i := 1; i <= n; i++ {
			var ok bool = true
			for _, value := range mp {
				if value % i != 0 {
					ok = false
					break
				}
			}
			if ok {
				fmt.Printf("%d ", i)
			}
		}
		fmt.Printf("\n")
	}
}

package main

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

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

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

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

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

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

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

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

func MAXX(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 GCD(x, y int64) int64 {
	if x*y == 0 {
		return MAX(x, y)
	} else {
		var l, q int64
		l = MAX(x, y)
		q = MIN(x, y)
		var m int64
		for l*q > 0 {
			a := l % q
			b := l - a
			cc := int64(b / q)
			remain := l - cc*q
			l = MAX(remain, q)
			q = MIN(remain, q)
			if l*q == 0 {
				m = l + q
				break
			}

		}
		return m
	}
}

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

func PrimeFactorization(n int) (pfs map[int]int) {
	pfs = make(map[int]int)

	// Get the number of 2s that divide n
	for n%2 == 0 {
		if _, ok := pfs[2]; ok {
			pfs[2] += 1
		} else {
			pfs[2] = 1
		}
		n = n / 2
	}

	// n must be odd at this point. so we can skip one element
	// (note i = i + 2)
	for i := 3; i*i <= n; i = i + 2 {
		// while i divides n, append i and divide n
		for n%i == 0 {
			if _, ok := pfs[i]; ok {
				pfs[i] += 1
			} else {
				pfs[i] = 1
			}
			n = n / i
		}
	}

	// This condition is to handle the case when n is a prime number
	// greater than 2
	if n > 2 {
		pfs[n] = 1
	}

	return
}

func NumberOfDivisors(n int) int {
	pfs := PrimeFactorization(n)

	num := 1
	for _, exponents := range pfs {
		num *= (exponents + 1)
	}

	return num
}

func KthPrime(x int) (y int) {
	if x == 1 {
		y = 2
	}
	if x == 2 {
		y = 3
	}
	if x > 2 {
		var sum int = 2
		begin := 5
		for sum < x+1 {
			if IsItPrime(begin) == true {
				sum++
			}
			if sum == x {
				y = begin
				break
			} else {
				if begin%6 == 1 {
					begin = begin + 4
				} else {
					begin = begin + 2
				}
			}
		}
	}
	return
}

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

func BITAND(x, y int64) int64 {
	var ans int64
	begin := x
	sec := y
	var count int64 = 1
	for 1 == 1 {
		if (begin%2)*(sec%2) == 1 {
			ans = ans + count
			count = count * 2
			begin = int64(begin / 2)
			sec = int64(sec / 2)
			if begin*sec == 0 {
				break
			}

		} else {
			count = count * 2
			begin = int64(begin / 2)
			sec = int64(sec / 2)
			if begin*sec == 0 {
				break
			}
		}
	}
	return ans
}

func Sum(n, m int) int {
	var ans int

	if n == 1 {
		return 0
	} else {
		for i := 2; i < n; i++ {
			ans = (ans + Sum(i, m)) % m
		}
		for j := 2; j < n+1; j++ {
			ans = (ans + Sum(n/j, m)) % m
		}
		return ans
	}
}

func ChangeStringToDate(x string) (t time.Time, err error) {
	t, err = time.Parse("2006-01-02", x)
	return
}

func DiffrenceBetweenTwoDates(first, second time.Time) int64 {
	date := first
	date2 := second
	k := date.Unix() - date2.Unix()
	return k
}

func Union(a, b []int) []int {
	m := make(map[int]bool)

	for _, item := range a {
		m[item] = true
	}

	for _, item := range b {
		if _, ok := m[item]; !ok {
			a = append(a, item)
		}
	}
	return a
}

// for o := 0; o < t; o++ {
// 	var a, b, c int
// 	fmt.Fscan(input, &a, &b, &c)

// 	A := a % 2
// 	B := b % 2
// 	C := c % 2
// 	if A+B+C == 0 {
// 		fmt.Println(0)
// 	}
// 	if A+B+C == 1 {
// 		if A == 1 {
// 			fmt.Println(1)
// 		}
// 		if B == 1 {
// 			if a >= 2 {
// 				fmt.Println(0)
// 			} else {
// 				fmt.Println(2)
// 			}
// 		}
// 		if C == 1 {
// 			if a > 0
// 			fmt.Println(1)
// 		}
// 	}
// 	if A+B+C == 2 {

// 	}
// 	if A+B+C == 3 {
// 		fmt.Println(0)
// 	}

// }

func ABS(x int) int {
	if x < 0 {
		return -x
	} else {
		return x
	}
}

func Ans(ARR []int) {
	if len(ARR) < 2 {
		return
	}
	if len(ARR) == 2 {
		fmt.Println(ARR[1])
		fmt.Println(-ARR[0])
	} else {
		if len(ARR) == 3 {
			first := ARR[0]
			Midd := ARR[1]
			Last := ARR[2]
			var F, M, L int
			Pos := 0
			Neg := 0
			if first > 0 {
				Pos++
			} else {
				Neg++
			}
			if Midd > 0 {
				Pos++
			} else {
				Neg++
			}
			if Last > 0 {
				Pos++
			} else {
				Neg++
			}
			if Pos*Neg != 0 {
				if ABS(first) == ABS(Midd) && ABS(Midd) == ABS(Last) {
					if first == Midd {
						if Midd == Last {
							F = -1
							M = -1
							L = 2
						} else {
							F = 1
							M = 1
							L = 2
						}
					} else {
						if first == Last {
							F = 1
							L = 1
							M = 2
						} else {
							M = 1
							L = 1
							F = 2
						}
					}
				} else {
					One := first + Midd
					Two := Midd + Last
					Thr := first + Last
					l := 0
					if One != 0 && ABS(One) <= 10000 {
						F = Last
						M = Last
						L = -One
						l = 1
					}

					if Two != 0 && ABS(Two) <= 10000 && l == 0 {
						F = -Two
						M = first
						L = first
						l = 1
					}
					if Thr != 0 && ABS(Thr) <= 10000 && l == 0 {
						F = Midd
						M = -Thr
						L = Midd
					}

				}
			} else {
				if ABS(first) == ABS(Midd) && ABS(Midd) == ABS(Last) {
					F = -1
					M = -1
					L = 2
				} else {
					One := first - Midd
					Two := Midd - Last
					Thr := first - Last
					l := 0
					if One != 0 && ABS(One) <= 10000 {
						F = -Last
						M = Last
						L = One
						l = 1
					}

					if Two != 0 && ABS(Two) <= 10000 && l == 0 {
						F = Two
						M = -first
						L = first
						l = 1
					}
					if Thr != 0 && ABS(Thr) <= 10000 && l == 0 {
						F = -Midd
						M = Thr
						L = Midd
					}

				}
			}

			fmt.Println(F)
			fmt.Println(M)
			fmt.Println(L)
		} else {
			Q := make([]int, 0)
			P := make([]int, 0)
			P = append(P, ARR[0], ARR[1])
			p := len(ARR)
			for i := 2; i < p; i++ {
				Q = append(Q, ARR[i])
			}
			Ans(P)
			Ans(Q)
		}
	}
}

var t int

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

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

		ARR := make([]int64, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(input, &ARR[i])
		}

		B := make([]int, 0)
		for j := 1; j < 32; j++ {
			p := 0
			for i := 0; i < n; i++ {
				if ARR[i]%2 == 1 {
					p++
				}
				ARR[i] = ARR[i] / 2
			}
			if p != 0 {
				B = append(B, p)
			}
		}

		if len(B) == 0 {
			for i := 1; i < n+1; i++ {
				fmt.Println(i)
			}
		} else {
			var Init int64
			Init = int64(B[0])
			s := len(B)
			for i := 1; i < s; i++ {
				Init = GCD(int64(Init), int64(B[i]))
			}

			for i := 1; int64(i) <= Init; i++ {
				if Init%int64(i) == 0 {
					fmt.Println(i)
				}
			}

		}

	}

}


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 n int
		Fscan(in, &n)
		a := make([]int64, n)
		for i := range a {
			Fscan(in, &a[i])
		}
ans := int64(0)
		dp := make([]int64, n)
		for i := len(a) - 1; i >= 0; i-- {
			v := a[i]
			if int64(i)+v >= int64(n) {
				dp[i] = v

			} else {
				dp[i] = v+dp[int64(i)+v]
			}
			ans = max(ans, dp[i])
		}

		Fprintln(out, ans)
	}

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

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

func main() { run(os.Stdin, os.Stdout) }
func max(a, b int64) int64 {
	if a > b {
		return a
	}
	return b
}

package main

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

var (
	t      int
	input  io.Reader = bufio.NewReader(os.Stdin)
	output           = bufio.NewWriter(os.Stdout)
)

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

	arr := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(input, &arr[i])
	}

	var ans int64
	d := make([]int64, n)
	for i := (n - 1); i >= 0; i-- {
		if (i + arr[i]) >= n {
			d[i] = int64(arr[i])
		} else {
			ff := i + arr[i]
			d[i] = int64(arr[i]) + int64(d[ff])
		}

		if d[i] > ans {
			ans = d[i]
		}
	}

	fmt.Fprintln(output, ans)
}

func main() {
	fmt.Fscan(input, &t)

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

	//

	defer output.Flush()
}

package main

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

var (
	t      int
	input  io.Reader = bufio.NewReader(os.Stdin)
	output           = bufio.NewWriter(os.Stdout)
)

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

	arr := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(input, &arr[i])
	}

	var ans int64
	d := make([]int64, n)
	for i := (n - 1); i >= 0; i-- {
		if (i + arr[i]) >= n {
			d[i] = int64(arr[i])
		} else {
			ff := i + arr[i]
			d[i] = int64(arr[i]) + int64(d[ff])
		}

		if d[i] > ans {
			ans = d[i]
		}
	}

	fmt.Fprintln(output, ans)
}

func main() {
	fmt.Fscan(input, &t)

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

	defer output.Flush()
}

package main

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

var t int

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

	fmt.Fscan(input, &t)

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

		arr := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(input, &arr[i])
		}

		var ans int64
		d := make([]int64, n)
		for i := (n - 1); i >= 0; i-- {
			if (i + arr[i]) >= n {
				d[i] = int64(arr[i])
			} else {
				ff := i + arr[i]
				d[i] = int64(arr[i]) + int64(d[ff])
			}

			if d[i] > ans {
				ans = d[i]
			}
		}

		fmt.Println(ans)
	}
}

package main

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

var t int

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

	fmt.Fscan(input, &t)

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

		arr := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(input, &arr[i])
		}

		d := make([]int64, n)

		for i := (n - 1); i >= 0; i-- {
			if (i + arr[i]) >= n {
				d[i] = int64(arr[i])
			} else {
				ff := i + arr[i]
				d[i] = int64(arr[i]) + int64(d[ff])
			}
		}

		var ans int64
		for i := 0; i < n; i++ {
			if d[i] > ans {
				ans = d[i]
			}
		}

		fmt.Println(ans)
	}
}

package main

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

var t int

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

	fmt.Fscan(input, &t)

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

		arr := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(input, &arr[i])
		}

		d := make([]int64, n)

		for i := (n - 1); i >= 0; i-- {
			if (i + arr[i]) >= n {
				d[i] = int64(arr[i])
			} else {
				ff := i + arr[i]
				d[i] = int64(arr[i]) + int64(d[ff])
			}
		}

		var anss int64
		for i := 0; i < n; i++ {
			if d[i] > anss {
				anss = d[i]
			}
		}

		fmt.Println(anss)
	}
}

package main

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

var (
	in  = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
	t   int
	n   int
)

func main() {

	defer out.Flush()

	fmt.Fscan(in, &t)

	for ; t > 0; t-- {

		fmt.Fscan(in, &n)
		a := make([]int, n)

		for i := range a {
			fmt.Fscan(in, &a[i])
		}

		fmt.Fprintln(out, solve(a, n))
		//fmt.Fprintln(out, solve2(a, max, maxP))

	}

}

func solve(a []int, n int) int64 {

	max := int64(0)

	/*for i := int64(0); i < n; i++ {

		index := i
		sum := int64(0)

		for index < n {

			sum += a[index]
			index += a[index]

		}

		if sum > max {
			max = sum
		}

	}*/

	arr := make([]int64, n)

	for i := n - 1; i >= 0; i-- {

		if a[i]+i >= n {
			arr[i] = int64(a[i])
		} else {
			next := i + a[i]
			arr[i] = int64(a[i]) + arr[next]
		}

	}

	for i := 0; i < n; i++ {
		if arr[i] > max {
			max = arr[i]
		}
	}

	return max

}

//取絕對值
func abs(n int) int {
	if n >= 0 {
		return n
	} else {
		return -n
	}
}

//印出數字陣列
func printArr(array []int) {

	for i := range array {
		fmt.Printf("%d ", array[i])
	}

	fmt.Println()

}

//取質數
func Prime(a int, b int) []int {
	result := make([]int, 0)

	switch a {
	case 0, 1:
		if b >= 3 {
			result = append(result, 2)
			result = append(result, 3)

			a = 5
		} else if b == 2 {
			result = append(result, 2)
			return result
		} else {
			return result
		}
	case 2:
		result = append(result, 2)

		if b == 2 {
			return result
		}

		result = append(result, 3)
		a = 5
	case 3:
		result = append(result, 3)
		a = 5
	default:
		if a%2 == 0 {
			a++
		}
	}

outer:
	for n := a; n <= b; n += 2 {
		m := n % 6

		if m != 1 && m != 5 {
			continue
		}

		nSqrt := int(math.Floor(math.Sqrt(float64(n))))

		if nSqrt >= a {
			for i := 5; i < a; i += 6 {
				if n%i == 0 || n%(i+2) == 0 { // n % i: 6n + 5 -> 6(n + 1) - 1 -> 6n - 1, n % (i + 2): 6n + 1
					continue outer
				}
			}

			for _, p := range result {
				if p > nSqrt {
					break
				}

				if n%p == 0 {
					continue outer
				}
			}
		} else {
			for i := 5; i <= nSqrt; i += 6 {
				if n%i == 0 || n%(i+2) == 0 { // n % i: 6n + 5 -> 6(n + 1) - 1 -> 6n - 1, n % (i + 2): 6n + 1
					continue outer
				}
			}
		}

		result = append(result, n)
	}

	return result
}

/*
func gain() {
	var input io.Reader = bufio.NewReader(os.Stdin)

	fmt.Fscan(input, &t)

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

		arr := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(input, &arr[i])
		}

		d := make([]int64, n)

		for i := (n - 1); i >= 0; i-- {
			if (i + arr[i]) >= n {
				d[i] = int64(arr[i])
			} else {
				ff := i + arr[i]
				d[i] = int64(arr[i]) + int64(d[ff])
			}
		}

		var anss int64
		for i := 0; i < n; i++ {
			if d[i] > anss {
				anss = d[i]
			}
		}

		fmt.Println(anss)
	}
}
*/

package main

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

func Solve() {
	n := ri()
	dp := make([]int64, n+1)
	for i := 0; i < n; i++ {
		x := ri64()
		pos := min64(int64(n), int64(i)+x)
		dp[pos] = max64(dp[pos], dp[i]+x)
	}
	println(dp[n])
}

func main() {
	T := 0 // 如果是多组样例，请讲 T := 1 改为 T := 0
	in, out = bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
	if T == 0 {
		scan(&T)
	}
	for t := 0; t < T; t++ {
		Solve()
	}
	out.Flush()
}

var in *bufio.Reader
var out *bufio.Writer

// ri 将直接读入并返回一个 int
func ri() (x int) {
	scan(&x)
	return x
}

// ri64 将直接读入并返回一个 int64
func ri64() (x int64) {
	scan(&x)
	return x
}

//rs 将直接读入并返回一个 string
func rs() (s string) {
	scan(&s)
	return s
}

//scan 用法与 fmt.scan() 相似，可以传入多个变量的地址进行读入操作
func scan(a ...interface{}) { Fscan(in, a...) }

//print 用法与 fmt.print() 相似，可以传入多个变量进行输出。
//注意，这个函数不会换行，多个变量之间有空格
func print(a ...interface{}) { Fprint(out, a...) }

//println 用法与 fmt.println() 相似，可以传入多个变量进行输出。
//这个函数会换行，多个变量之间有空格
func println(a ...interface{}) { Fprintln(out, a...) }

//min 可以传入多个 int 变量，求出这些变量的最小值
func min(a ...int) int {
	ans := a[0]
	for i := 1; i < len(a); i++ {
		if a[i] < ans {
			ans = a[i]
		}
	}
	return ans
}

//min64 可以传入多个 int64 变量，求出这些变量的最小值
func min64(a ...int64) int64 {
	ans := a[0]
	for i := 1; i < len(a); i++ {
		if a[i] < ans {
			ans = a[i]
		}
	}
	return ans
}

//max 可以传入多个 int 变量，求出这些变量的最大值
func max(a ...int) int {
	ans := a[0]
	for i := 1; i < len(a); i++ {
		if a[i] > ans {
			ans = a[i]
		}
	}
	return ans
}

//max64 可以传入多个 int64 变量，求出这些变量的最大值
func max64(a ...int64) int64 {
	ans := a[0]
	for i := 1; i < len(a); i++ {
		if a[i] > ans {
			ans = a[i]
		}
	}
	return ans
}

//sum 可以传入多个 int 变量，求出这些变量的总和，并返回总和。
//你可以使用 ... 运算符打散数组，使得 sum 函数可以求出数组的和。
func sum(a ...int) int {
	ans := 0
	for _, x := range a {
		ans += x
	}
	return ans
}

//sum64 可以传入多个 int64 变量，求出这些变量的总和，并返回总和。
//你可以使用 ... 运算符打散数组，使得 sum 函数可以求出数组的和。
func sum64(a ...int64) int64 {
	ans := int64(0)
	for _, x := range a {
		ans += x
	}
	return ans
}

//reverse 用于翻转 int 切片，所有元素将会变成原切片的倒序
func reverse(a []int) {
	for l, r := 0, len(a)-1; l < r; l, r = l+1, r-1 {
		a[l], a[r] = a[r], a[l]
	}
}

//reverse64 用于翻转 int64 切片，所有元素将会变成原切片的倒序
func reverse64(a []int64) {
	for l, r := 0, len(a)-1; l < r; l, r = l+1, r-1 {
		a[l], a[r] = a[r], a[l]
	}
}

//lowerBound 类似于 C++ 中的 lower_bound。
//用于二分查找 int 切片中，第一个大于等于 k 的元素在切片中的位置。
//如果元素不存在，则返回切片的长度。
//注意，lowerBound 作用的 int 切片必须升序。
func lowerBound(a []int, k int) int {
	l, r, pos := 0, len(a)-1, len(a)
	for l <= r {
		mid := (l + r) >> 1
		if a[mid] < k {
			l = mid + 1
		} else {
			pos, r = mid, mid-1
		}
	}
	return pos
}

//lowerBound64 类似于 C++ 中的 lower_bound。
//用于二分查找 int64 切片中，第一个大于等于 k 的元素在切片中的位置。
//如果元素不存在，则返回切片的长度。
//注意，lowerBound 作用的 int64 切片必须升序。
func lowerBound64(a []int64, k int64) int {
	l, r, pos := 0, len(a)-1, len(a)
	for l <= r {
		mid := (l + r) >> 1
		if a[mid] < k {
			l = mid + 1
		} else {
			pos, r = mid, mid-1
		}
	}
	return pos
}

//upperBound 类似于 C++ 中的 upper_bound。
//用于二分查找 int 切片中，第一个大于 k 的元素在切片中的位置。
//如果元素不存在，则返回切片的长度。
//注意，upperBound 作用的 int 切片必须升序。
func upperBound(a []int, k int) int {
	l, r, pos := 0, len(a)-1, len(a)
	for l <= r {
		mid := (l + r) >> 1
		if a[mid] <= k {
			l = mid + 1
		} else {
			pos, r = mid, mid-1
		}
	}
	return pos
}

//upperBound64 类似于 C++ 中的 upper_bound。
//用于二分查找 int64 切片中，第一个大于 k 的元素在切片中的位置。
//如果元素不存在，则返回切片的长度。
//注意，upperBound 作用的 int64 切片必须升序。
func upperBound64(a []int64, k int64) int {
	l, r, pos := 0, len(a)-1, len(a)
	for l <= r {
		mid := (l + r) >> 1
		if a[mid] <= k {
			l = mid + 1
		} else {
			pos, r = mid, mid-1
		}
	}
	return pos
}

type Stack struct {
	s []int
}

func (st *Stack) top() int   { return st.s[len(st.s)-1] }
func (st *Stack) size() int  { return len(st.s) }
func (st *Stack) pop()       { st.s = st.s[:len(st.s)-1] }
func (st *Stack) push(x int) { st.s = append(st.s, x) }

type Queue struct {
	que chan int
}

func MakeQueue(size int) Queue {
	var q Queue
	q.que = make(chan int, size)
	return q
}

package main

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

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

	var T int
	for fmt.Fscan(r, &T); T > 0; T-- {

		var n, res int
		fmt.Fscan(r, &n)

		a := make([]int, n)
		for i := range a { fmt.Fscan(r, &a[i]) }

		dp := make([]int, n)
		for i := n-1; i >= 0; i-- {
			dp[i] = a[i]
			if i + a[i] < n { dp[i] += dp[i+a[i]] }
			if dp[i] > res { res = dp[i] }
		}

		fmt.Fprintln(w, res)
	}
}

package main

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

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

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

func IntArray(size int) []int {
	arr := make([]int, size)
	for i := 0; i < size; i++ {
		scanf("%d", &arr[i])
	}
	scanf("\n")
	return arr
}

func Max(args ...int) int {
	m := args[0]
	for i := range args {
		if args[i] > m {
			m = args[i]
		}
	}
	return m
}

func Min(args ...int) int {
	m := args[0]
	for i := range args {
		if args[i] < m {
			m = args[i]
		}
	}
	return m
}

func solve() {
	var n int
	scanf("%d\n", &n)

	arr := IntArray(n)

	dp := make([]int, n)
	dp[n-1] = arr[n-1]
	max := dp[n-1]

	for i := n - 2; i >= 0; i-- {
		if i+arr[i] < n {
			dp[i] = dp[i+arr[i]] + arr[i]
		} else {
			dp[i] = arr[i]
		}
		max = Max(max, dp[i])
	}

	printf("%d\n", max)
}

func main() {
	defer writer.Flush()

	var cases int

	scanf("%d\n", &cases)

	for c := 0; c < cases; c++ {
		solve()
	}
}

/*

 */

package main

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

var (
	reader = bufio.NewReader(os.Stdin)
	t      int
	n      int64
)

func solve(n int64, a []int64) int64 {
	maxScore := int64(0)
	s := make([]int64, n)
	for i := n - 1; i >= 0; i-- {
		if i+a[i] > n-1 {
			s[i] = a[i]
		} else {
			s[i] = a[i] + s[i+a[i]]
		}
		if maxScore < s[i] {
			maxScore = s[i]
		}
	}
	return maxScore
}

func main() {
	fmt.Fscan(reader, &t)
	for ; t > 0; t-- {
		fmt.Fscan(reader, &n)
		a := make([]int64, n)
		for i := int64(0); i < n; i++ {
			fmt.Fscan(reader, &a[i])
		}
		fmt.Println(solve(n, a))
	}
}

package main

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

var bytes []byte

func nextByte() byte {
	b := bytes[0]
	bytes = bytes[1:]
	return b
}

func scanInt64() int64 {
	var negative bool
	var result int64

	b := nextByte()

	for ; b < '0' || b > '9'; b = nextByte() {
		if b == '-' {
			negative = true
		}
	}

	for ; '0' <= b && b <= '9'; b = nextByte() {
		result = (result << 3) + (result << 1) + int64(b-'0')
	}

	if negative {
		return -result
	} else {
		return result
	}
}

func scanInt() int {
	return int(scanInt64())
}

func ScanString() string {
	builder := strings.Builder{}

	for b := nextByte(); b != '\r'; b = nextByte() {
		if b != '\n' {
			builder.WriteByte(b)
		}
	}

	return builder.String()
}

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

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

	bytes, _ = ioutil.ReadAll(bufio.NewReader(f))
}

func main() {
	var a [200000]int64
	for t := scanInt(); t > 0; t-- {
		n, max := scanInt64(), int64(0)

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

		for i := n - 1; i >= 0; i-- {
			if i+a[i] < n {
				a[i] += a[i+a[i]]
			}

			if a[i] > max {
				max = a[i]
			}
		}

		fmt.Println(max)
	}
}

package main

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

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

func scan(a ...interface{}) (n int, err error)             { return fmt.Fscan(in, a...) }
func scanf(f string, a ...interface{}) (n int, err error)  { return fmt.Fscanf(in, f, a...) }
func printf(f string, a ...interface{}) (n int, err error) { return fmt.Fprintf(out, f, a...) }

//https://codeforces.com/problemset/problem/1472/C
// dp

func main() {
	defer out.Flush()

	var t, n int
	scanf("%d\n", &t)
	for i := 0; i < t; i++ {
		scanf("%d\n", &n)
		a := make([]int, n)
		dp := make([]int, n)
		for j := 0; j < n; j++ {
			scan(&a[j])
			if j == n-1 {
				scanf("\n")
			}
		}
		ans := 0
		for j := n - 1; j >= 0; j-- {
			dp[j] = a[j]
			if j+a[j] < n {
				dp[j] += dp[j+a[j]]
			}
			ans = max(ans, dp[j])
		}
		printf("%d\n", ans)
	}
}

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

package main

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

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

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

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

func sint64() int64 {
	scanner.Scan()
	bb := scanner.Bytes()
	i := int64(0)
	if bb[0] == '-' {
		for _, b := range bb[1:] {
			i *= 10
			i -= int64(b - '0')
		}
		return i
	}
	for _, b := range bb {
		i *= 10
		i += int64(b - '0')
	}
	return i
}

func suint64() uint64 {
	scanner.Scan()
	bb := scanner.Bytes()
	i := uint64(0)
	for _, b := range bb {
		i *= 10
		i += uint64(b - '0')
	}
	return i
}

func sstring() string {
	scanner.Scan()
	return scanner.Text()
}

func sbytes() []byte {
	scanner.Scan()
	return scanner.Bytes()
}

func main() {
	defer writer.Flush()
	for t := sint64(); t > 0; t-- {
		n := sint64()
		a := make([]int64, n)
		for i := range a {
			a[i] = sint64()
		}
		printf("%d\n", sol(a))
	}
}

func sol(a []int64) int64 {
	for i := int64(len(a) - 1); i > -1; i-- {
		if i+a[i] < int64(len(a)) {
			a[i] += a[i+a[i]]
		}
	}
	maxSoFar := a[0]
	for _, v := range a {
		if v > maxSoFar {
			maxSoFar = v
		}
	}
	return maxSoFar
}

package main

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

var reader *bufio.Reader = bufio.NewReader(os.Stdin)

// var file, _ = os.Open("input")
// var reader *bufio.Reader = bufio.NewReader(file)
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 main() {
	var t int
	scanf("%d\n", &t)
	for i := 0; i < t; i++ {
		var n int
		scanf("%d\n", &n)
		fmt.Println(process(reader, int64(n)))
	}
}

func process(reader *bufio.Reader, n int64) int64 {
	a := make([]int64, n)
	var i int64
	for i = 0; i < n; i++ {
		if i != n-1 {
			scanf("%d", &a[i])
		} else {
			scanf("%d\n", &a[i])
		}
	}
	result := make([]int64, n)
	var maxResult int64
	for i = n - 1; i >= 0; i-- {
		if a[i]+i < n {
			result[i] = a[i] + result[i+a[i]]
		} else {
			result[i] = a[i]
		}
		if maxResult < result[i] {
			maxResult = result[i]
		}
	}
	return maxResult
}

package main

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

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

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

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

func solve() int {
	var n int
	scanf("%d\n", &n)

	a := make([]int, n)
	for i := 0; i < n; i++ {
		if i+1 == n {
			scanf("%d\n", &a[i])
		} else {
			scanf("%d ", &a[i])
		}
	}

	dp := make([]int64, n)
	var ans int64

	for i := n - 1; i >= 0; i-- {
		dp[i] = int64(a[i])
		if (i + a[i]) < n {
			dp[i] += dp[i+a[i]]
		}
		if dp[i] > ans {
			ans = dp[i]
		}
	}

	printf("%d\n", ans)

	return 0
}

func main() {
	defer writer.Flush()
	tt := 1
	scanf("%d\n", &tt)
	for tt > 0 {
		solve()
		tt--
	}
}

package main

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

func main() {
	t := ReadInt()
	for i := 0; i < t; i++ {
		size := ReadInt()
		weights := ReadIntSliceln(size)
		fmt.Println(process(weights))
	}
}

func process(w []int) (ans int) {
	for i := len(w) - 1; i >= 0; i-- {
		if i+w[i] < len(w) {
			w[i] = w[i] + w[i+w[i]]
		}
		if w[i] > ans {
			ans = w[i]
		}
		//fmt.Println(w)
	}
	return ans
}

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

// ReadInt Read a single integer followed by a space
func ReadInt() (v int) {
	fmt.Fscan(reader, &v)
	return v
}

// ReadIntSliceln reads a line of integers into a slice.
func ReadIntSliceln(size int) []int {
	a := make([]int, size)
	for i := range a {
		fmt.Fscan(reader, &a[i])
	}
	//fmt.Scanf("\n")
	return a
}

package main

import (
	"bufio"
	"fmt"
	"os"
)
var (
    input = bufio.NewReader(os.Stdin)
    output = bufio.NewWriter(os.Stdout)
)

func assert(test bool) {
    if !test {panic("assertion!")}
}

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)
func main() {
    var t int
	fmt.Fscan(input, &t)
    for t > 0 {
        t--
        solve()
    }
    output.Flush()
}

func solve() {
    var n int
    fmt.Fscan(input, &n)
    a := make([]int, n)
    dp := make([]int64, n + 1) // max result of i
    ans := int64(0)
    for i := 0; i < len(a); i++ {
        fmt.Fscan(input, &a[i])
        if i + a[i] >= n {
            ans = max(ans, dp[i] + int64(a[i]))
            continue
        }
        if dp[i + a[i]] < dp[i] + int64(a[i]) {
            dp[i + a[i]] = dp[i] + int64(a[i])
        }
        ans = max(ans, dp[i + a[i]])
    }
    fmt.Fprintln(output, ans)
}

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

package main

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

func main() {
	sc := NewScanner()
	t := sc.NextInt()
	for i := 0; i < t; i++ {
		N := sc.NextInt()
		a := sc.NextInt64Array()
		fmt.Println(solve(N, a))
	}

}

func dfs(memo []int64, v int, a []int64) int64 {
	if v > len(memo) {
		return 0
	}
	if memo[v-1] >= 0 {
		return memo[v-1]
	}
	ret := dfs(memo, v+int(a[v-1]), a)
	memo[v-1] = ret + a[v-1]
	return memo[v-1]
}

func solve(N int, a []int64) int64 {
	memo := make([]int64, N)
	fillInt(memo, -1)
	mx := int64(0)
	for i := 1; i <= N; i++ {
		mx = max(mx, dfs(memo, i, a))
	}
	return mx
}
func max(a int64, b int64) int64 {
	if a < b {
		return b
	}
	return a
}

func fillInt(arr []int64, v int) {
	for i := 0; i < len(arr); i++ {
		arr[i] = int64(v)
	}
}

type Scanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func NewScanner() *Scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1000)
	return &Scanner{r: rdr}
}
func (s *Scanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *Scanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *Scanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}
func (s *Scanner) NextInt64() int64 {
	v, _ := strconv.ParseInt(s.Next(), 10, 64)
	return v
}

func (s *Scanner) NextFloat() float64 {
	v, _ := strconv.ParseFloat(s.Next(), 64)
	return v
}

func (s *Scanner) NextIntArray() []int {
	s.pre()
	start := s.p
	result := []int{}
	for ; s.p < len(s.buf)+1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 0)
			result = append(result, int(v))
			start = s.p + 1
		}
	}

	return result
}
func (s *Scanner) NextInt64Array() []int64 {
	s.pre()
	start := s.p
	result := []int64{}
	for ; s.p < len(s.buf)+1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 64)
			result = append(result, v)
			start = s.p + 1
		}
	}
	return result
}

func (s *Scanner) NextMap() map[int]int {
	s.pre()
	start := s.p
	mp := map[int]int{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			mp[v]++
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	mp[v]++

	return mp
}

func (s *Scanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *Scanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}

package main

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

func main () {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t,n int
	Fscan(in, &t)
	for tt := 0; tt < t ; tt++ {
		Fscan(in, &n)
		p := make([]int, n)
		a := make([]int, n)
		for i := 0; i<n; i++ {
			Fscan(in, &p[i])
		}
		ans := 0
		for i := range p{
			now := count(p, a, i)
			if now > ans {
				ans = now
			}
		}
		out.WriteString(strconv.Itoa(ans))
		out.WriteString("\n")
	}
}

func count(p []int, a []int, i int) int {
	if a[i] > 0 {
		return a[i]
	}
	c := p[i]
	if i + p[i] < len(p){
		c += count(p, a, i + p[i])
	}
	a[i] = c
	return c

}

package main

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

func main() {
	defer _w.Flush()
	t := ReadInt()
	for i := 0; i < t; i++ {
		n := ReadInt()
		println(solve(ReadInts(n)))
	}
}

func solve(a []int) int {
	max := 0
	memo := make([]*int, len(a))
	for i := 0; i < len(a); i++ {
		max = Max(max, f(memo, i, a))
	}
	return max
}

func Max(xs ...int) int {
	max := xs[0]
	for _, x := range xs[1:] {
		if max < x {
			max = x
		}
	}
	return max
}

func f(memo []*int, i int, a []int) int {
	if i >= len(memo) {
		return 0
	}
	if memo[i] != nil {
		return *memo[i]
	}
	ret := a[i] + f(memo, i+a[i], a)
	memo[i] = &ret
	return *memo[i]
}

var _r, _w = bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)

func print(a ...interface{})            { fmt.Fprint(_w, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(_w, f, a...) }
func println(a ...interface{})          { fmt.Fprintln(_w, a...) }
func printInts(a ...int)                { s := fmt.Sprint(a); println(s[1 : len(s)-1]) }
func Scan(a ...interface{}) {
	if _, err := fmt.Fscan(_r, a...); err != nil {
		panic(err)
	}
}
func ReadString() (s string) { Scan(&s); return }
func ReadInt() (i int)       { Scan(&i); return }
func ReadInts(n int) (a []int) {
	for i := 0; i < n; i++ {
		a = append(a, ReadInt())
	}
	return
}
func ReadInt64() (i int64) { Scan(&i); return }
func ReadInt64s(n int) (a []int64) {
	for i := 0; i < n; i++ {
		a = append(a, ReadInt64())
	}
	return
}

package main

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

var t int

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

	fmt.Fscan(input, &t)

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

		arr := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(input, &arr[i])
		}

		d := make([]int64, n)

		for i := (n - 1); i >= 0; i-- {
			if (i + arr[i]) >= n {
				d[i] = int64(arr[i])
			} else {
				d[i] = int64(arr[i] + arr[i+arr[i]])
			}
		}

		var ans int64
		for i := 0; i < n; i++ {
			if d[i] > ans {
				ans = d[i]
			}
		}

		fmt.Println(ans)
	}
}

package main

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

var (
	in  = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
	t   int
	n   int
)

func main() {

	defer out.Flush()

	fmt.Fscan(in, &t)

	for ; t > 0; t-- {

		fmt.Fscan(in, &n)
		a := make([]int, n)

		for i := range a {
			fmt.Fscan(in, &a[i])
		}

		fmt.Fprintln(out, solve(a, n))
		//fmt.Fprintln(out, solve2(a, max, maxP))

	}

}

func solve(a []int, n int) int {

	max := 0
	maxIndex := 0

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

		if a[i] <= maxIndex {
			continue
		}

		maxIndex = a[i]

		index := i
		sum := 0

		for index < n {

			sum += a[index]
			index += a[index]

		}

		if sum > max {
			max = sum
		}

	}

	return max

}

//取絕對值
func abs(n int) int {
	if n >= 0 {
		return n
	} else {
		return -n
	}
}

//印出數字陣列
func printArr(array []int) {

	for i := range array {
		fmt.Printf("%d ", array[i])
	}

	fmt.Println()

}

//取質數
func Prime(a int, b int) []int {
	result := make([]int, 0)

	switch a {
	case 0, 1:
		if b >= 3 {
			result = append(result, 2)
			result = append(result, 3)

			a = 5
		} else if b == 2 {
			result = append(result, 2)
			return result
		} else {
			return result
		}
	case 2:
		result = append(result, 2)

		if b == 2 {
			return result
		}

		result = append(result, 3)
		a = 5
	case 3:
		result = append(result, 3)
		a = 5
	default:
		if a%2 == 0 {
			a++
		}
	}

outer:
	for n := a; n <= b; n += 2 {
		m := n % 6

		if m != 1 && m != 5 {
			continue
		}

		nSqrt := int(math.Floor(math.Sqrt(float64(n))))

		if nSqrt >= a {
			for i := 5; i < a; i += 6 {
				if n%i == 0 || n%(i+2) == 0 { // n % i: 6n + 5 -> 6(n + 1) - 1 -> 6n - 1, n % (i + 2): 6n + 1
					continue outer
				}
			}

			for _, p := range result {
				if p > nSqrt {
					break
				}

				if n%p == 0 {
					continue outer
				}
			}
		} else {
			for i := 5; i <= nSqrt; i += 6 {
				if n%i == 0 || n%(i+2) == 0 { // n % i: 6n + 5 -> 6(n + 1) - 1 -> 6n - 1, n % (i + 2): 6n + 1
					continue outer
				}
			}
		}

		result = append(result, n)
	}

	return result
}


package main

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

func solve() {
	n := readInt()
	m := readInt()
	k := readInt()
	s := readInt()
	goods := make([][]int, n)
	for i := 0; i < n; i++ {
		value := readInt() - 1
		goods[value] = append(goods[value], i)
	}
	uv := make([][]int, n)
	for i := 0; i < len(uv); i++ {
		uv[i] = make([]int, 0)
	}
	for i := 0; i < m; i++ {
		u := readInt() - 1
		v := readInt() - 1
		uv[u] = append(uv[u], v)
		uv[v] = append(uv[v], u)
	}
	cost := make([][]int, n)
	for i := 0; i < n; i++ {
		cost[i] = make([]int, k)
	}
	for j := 0; j < k; j++ {
		queue := make([]int, 0)
		ans := make([]int, n)
		for i := 0; i < n; i++ {
			ans[i] = math.MaxInt32

		}
		for i := 0; i < len(goods[j]); i++ {
			cityId := goods[j][i]
			queue = append(queue, cityId)
			ans[cityId] = 0
		}
		for len(queue) > 0 {
			curCity := queue[0]
			queue = queue[1:]
			for _, nextCity := range uv[curCity] {
				if ans[nextCity] > ans[curCity]+1 {
					queue = append(queue, nextCity)
					ans[nextCity] = ans[curCity] + 1
				}
			}
		}
		for i := 0; i < n; i++ {
			cost[i][j] = ans[i]
		}
	}
	for i := 0; i < len(cost); i++ {
		res := 0
		sort.Ints(cost[i])
		for j := 0; j < s; j++ {
			res += cost[i][j]
		}
		if i > 0 {
			fmt.Print(" ")
		}
		fmt.Print(res)
	}
	fmt.Println()
}

var scanner *bufio.Scanner

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

// IO

func readString() string {
	scanner.Scan()
	return scanner.Text()
}

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

func readInt64() int64 {
	v, _ := strconv.ParseInt(readString(), 10, 64)
	return v
}

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

func readInt64Array(n int) []int64 {
	res := make([]int64, n)
	for i := 0; i < n; i++ {
		res[i] = readInt64()
	}
	return res
}

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

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

package main

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

const (
	maxk, maxn, inf int = 105, 1e5 + 5, 0x7fffffff
)

var (
	n, m, s, k int
	t [maxk][]int
	g, d [maxn][]int
)

func str2Ints(str string) []int {
	ret := make([]int, 0)
	str = strings.TrimSpace(str)
	// fmt.Println(strings.Split(str, " "))
	for _, item := range strings.Split(str, " ") {
		num,_ := strconv.Atoi(item)
		ret = append(ret, num)
	}
	return ret
}

func getInts() []int {
	//assumes POSITIVE INTEGERS. Check v for '-' if you have negative.
	var buf []byte
	buf, _ = ioutil.ReadAll(os.Stdin)
	// fmt.Println("here")
	var ints []int
	num := int(0)
	found := false
	for _, v := range buf {
		if '0' <= v && v <= '9' {
			num = 10*num + int(v - '0') //could use bitshifting here.
			found = true
		} else if found {
			ints = append(ints, num)
			found = false
			num = 0
		}
	}
	if found {
		ints = append(ints, num)
		found = false
		num = 0
	}
	// fmt.Println(len(ints))
	return ints
}

func read() {
	nums := getInts()

	n, m, k, s = nums[0], nums[1], nums[2], nums[3]
	idx := 4

	for ; idx < 4 + n; idx++ {
		t[nums[idx]] = append(t[nums[idx]], idx - 3)
	}

	for idx < 4 + n + 2 * m {
		st := nums[idx]
		idx++
		ed := nums[idx]
		idx++
		g[st] = append(g[st], ed)
		g[ed] = append(g[ed], st)
	}

	for i := 1; i <= n; i++ {
		d[i] = append(d[i], 0)
		for j := 1; j <= k; j++ { d[i] = append(d[i], inf) }
	}

}

func bfs(x int) {
	q := make([]int, 0)
	for _, item := range t[x] {
		q = append(q, item)
		d[item][x] = 0
	}
	for len(q) != 0 {
		hd := q[0]
		q = q[1:]
		for _, nxt := range g[hd] {
			if d[nxt][x] != inf { continue }
			d[nxt][x] = d[hd][x] + 1
			q = append(q, nxt)
		}
	}
}

func swap(x, y *int) {
	*x, *y = *y, *x
}

func partition(a *[]int, l, r int) int {
	// fmt.Println("l: ", l, ", r: ", r)
	idx := r
	r--
	for l <= r {
		for l < idx && (*a)[l] <= (*a)[idx] { l++ }
		for r > l && (*a)[r] >= (*a)[idx] { r-- }
		if l >= r { break }
		swap(&((*a)[l]), &((*a)[r]))
	}
	swap(&((*a)[l]), &((*a)[idx]))
	return l
}

func kthElement(a *[]int, l, r, k int) int {
	// fmt.Println("searching ", k, "...")
	idx := -1
	for {
		idx = partition(a, l, r)
		if idx < k {
			l = idx + 1
		} else if idx > k {
			r = idx - 1
		} else { break }
	}
	return (*a)[idx]
}

func solve() {
	for i := 1; i <= k; i++ {
		bfs(i)
	}
	for i := 1; i <= n; i++ {
		kthElement(&d[i], 1, k, s)
		var ans = 0
		for j := 1; j <= s; j++ { ans += d[i][j] }
		fmt.Printf("%v ", ans)
	}
}

func main() {
	read()
	solve()
}

package main

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

const (
    maxk, maxn, inf int = 105, 1e5 + 5, 0x7fffffff
)

var (
    n, m, s, k int
    t [maxk][]int
    g, d [maxn][]int
)

func str2Ints(str string) []int {
	ret := make([]int, 0)
	str = strings.TrimSpace(str)
	// fmt.Println(strings.Split(str, " "))
	for _, item := range strings.Split(str, " ") {
		num,_ := strconv.Atoi(item)
		ret = append(ret, num)
	}
	return ret
}

func read() {
    reader := bufio.NewReader(os.Stdin)
	input,_ := reader.ReadString('\n')
	nums := str2Ints(input)
	n, m, k, s = nums[0], nums[1], nums[2], nums[3]
	// fmt.Println(n, m, k, s)

	input,_ = reader.ReadString('\n')
	nums = str2Ints(input)
	for i, item := range nums {
		t[item] = append(t[item], i + 1)
	}

	for i := 0; i < m; i++ {
		input,_ = reader.ReadString('\n')
		nums = str2Ints(input)
		st, ed := nums[0], nums[1]
		g[st] = append(g[st], ed)
		g[ed] = append(g[ed], st)
	}

	for i := 1; i <= n; i++ {
	    d[i] = append(d[i], 0)
	    for j := 1; j <= k; j++ { d[i] = append(d[i], inf) }
	}

}

func bfs(x int) {
    q := make([]int, 0)
    for _, item := range t[x] {
        q = append(q, item)
        d[item][x] = 0
    }
    for len(q) != 0 {
        hd := q[0]
        q = q[1:]
        for _, nxt := range g[hd] {
            if d[nxt][x] != inf { continue }
            d[nxt][x] = d[hd][x] + 1
            q = append(q, nxt)
        }
    }
}

func swap(x, y *int) {
	*x, *y = *y, *x
}

func partition(a *[]int, l, r int) int {
	// fmt.Println("l: ", l, ", r: ", r)
	idx := r
	r--
	for l <= r {
		for l < idx && (*a)[l] <= (*a)[idx] { l++ }
		for r > l && (*a)[r] >= (*a)[idx] { r-- }
		if l >= r { break }
		swap(&((*a)[l]), &((*a)[r]))
	}
	swap(&((*a)[l]), &((*a)[idx]))
	return l
}

func kthElement(a *[]int, l, r, k int) int {
	// fmt.Println("searching ", k, "...")
	idx := -1
	for {
		idx = partition(a, l, r)
		if idx < k {
			l = idx + 1
		} else if idx > k {
			r = idx - 1
		} else { break }
	}
	return (*a)[idx]
}

func solve() {
    for i := 1; i <= k; i++ {
        bfs(i)
    }
    for i := 1; i <= n; i++ {
        kthElement(&d[i], 1, k, s)
        var ans = 0
        for j := 1; j <= s; j++ { ans += d[i][j] }
        fmt.Printf("%v ", ans)
    }
}

func main() {
    read()
    solve()
}

package main

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

const (
	maxk, maxn, max, inf int = 105, 1e5 + 5, 1e7 + 5, 0x7fffffff
)

var (
	n, m, s, k int
	t [maxk][]int
	g, d [maxn][]int
)

func str2Ints(str string) []int {
	ret := make([]int, 0)
	str = strings.TrimSpace(str)
	// fmt.Println(strings.Split(str, " "))
	for _, item := range strings.Split(str, " ") {
		num,_ := strconv.Atoi(item)
		ret = append(ret, num)
	}
	return ret
}

func getInts() []int {
	//assumes POSITIVE INTEGERS. Check v for '-' if you have negative.
	var buf [max]byte
	os.Stdin.Read(buf[:])
	var ints []int
	num := int(0)
	found := false
	for _, v := range buf {
		if '0' <= v && v <= '9' {
			num = 10*num + int(v - '0') //could use bitshifting here.
			found = true
		} else if found {
			ints = append(ints, num)
			found = false
			num = 0
		}
	}
	if found {
		ints = append(ints, num)
		found = false
		num = 0
	}
	return ints
}

func read() {
	nums := getInts()

	n, m, k, s = nums[0], nums[1], nums[2], nums[3]
	idx := 4

	for ; idx < 4 + n; idx++ {
		t[nums[idx]] = append(t[nums[idx]], idx - 3)
	}

	for idx < 4 + n + 2 * m {
		st := nums[idx]
		idx++
		ed := nums[idx]
		idx++
		g[st] = append(g[st], ed)
		g[ed] = append(g[ed], st)
	}

	for i := 1; i <= n; i++ {
		d[i] = append(d[i], 0)
		for j := 1; j <= k; j++ { d[i] = append(d[i], inf) }
	}

}

func bfs(x int) {
	q := make([]int, 0)
	for _, item := range t[x] {
		q = append(q, item)
		d[item][x] = 0
	}
	for len(q) != 0 {
		hd := q[0]
		q = q[1:]
		for _, nxt := range g[hd] {
			if d[nxt][x] != inf { continue }
			d[nxt][x] = d[hd][x] + 1
			q = append(q, nxt)
		}
	}
}

func swap(x, y *int) {
	*x, *y = *y, *x
}

func partition(a *[]int, l, r int) int {
	// fmt.Println("l: ", l, ", r: ", r)
	idx := r
	r--
	for l <= r {
		for l < idx && (*a)[l] <= (*a)[idx] { l++ }
		for r > l && (*a)[r] >= (*a)[idx] { r-- }
		if l >= r { break }
		swap(&((*a)[l]), &((*a)[r]))
	}
	swap(&((*a)[l]), &((*a)[idx]))
	return l
}

func kthElement(a *[]int, l, r, k int) int {
	// fmt.Println("searching ", k, "...")
	idx := -1
	for {
		idx = partition(a, l, r)
		if idx < k {
			l = idx + 1
		} else if idx > k {
			r = idx - 1
		} else { break }
	}
	return (*a)[idx]
}

func solve() {
	for i := 1; i <= k; i++ {
		bfs(i)
	}
	for i := 1; i <= n; i++ {
		kthElement(&d[i], 1, k, s)
		var ans = 0
		for j := 1; j <= s; j++ { ans += d[i][j] }
		fmt.Printf("%v ", ans)
	}
}

func main() {
	read()
	solve()
}

package main

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

const (
	INF = 1234567890
)

func Init() {
}

func Solve(io *FastIO) {
	N := io.NextInt()
	M := io.NextInt()
	K := io.NextInt()
	S := io.NextInt()
	A := io.NextIntArrayOffset(N, 1)

	adj := make([][]int, N + 1)
	for i := 0; i < M; i++ {
		U := io.NextInt()
		V := io.NextInt()

		adj[U] = append(adj[U], V)
		adj[V] = append(adj[V], U)
	}

	count := make([]int, N + 1)
	best := make([][]int, N + 1)
	for i := 1; i <= N; i++ {
		best[i] = make([]int, K + 1)
		for j := 1; j <= K; j++ {
			best[i][j] = INF
		}
	}

	qlen := S * (N + M)
	qu := make([]int, qlen)
	qi := make([]int, qlen)
	qd := make([]int, qlen)
	idx := 0
	sz := 0
	for i := 1; i <= N; i++ {
		qu[sz] = i
		qi[sz] = A[i]
		sz++
		best[i][A[i]] = 0
		count[i]++
	}
	for idx < sz {
		u := qu[idx]
		i := qi[idx]
		d := qd[idx]
		idx++
		for _, v := range adj[u] {
			if count[v] < S && best[v][i] == INF {
				qu[sz] = v
				qi[sz] = i
				qd[sz] = d + 1
				sz++
				best[v][i] = d + 1
				count[v]++
			}
		}
	}

	for i := 1; i <= N; i++ {
		sum := 0
		for j := 1; j <= K; j++ {
			if best[i][j] != INF {
				sum += best[i][j]
			}
		}
		io.Printf("%d ", sum)
	}
	io.Println()
}

type FastIO struct {
	reader *bufio.Reader
	writer *bufio.Writer
}

func (io *FastIO) NextChar() byte {
	c, _ := io.reader.ReadByte()
	return c
}

func (io *FastIO) NextInt() int {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := 1
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := 0
	for !IsSpaceChar(c) {
		res = (res * 10) + int(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextLong() int64 {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := int64(1)
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := int64(0)
	for !IsSpaceChar(c) {
		res = (res * 10) + int64(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextIntArray(size int) []int {
	return io.NextIntArrayOffset(size, 0)
}

func (io *FastIO) NextIntArrayOffset(size, offset int) []int {
	arr := make([]int, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextInt()
	}
	return arr
}

func (io *FastIO) NextLongArray(size int) []int64 {
	return io.NextLongArrayOffset(size, 0)
}

func (io *FastIO) NextLongArrayOffset(size, offset int) []int64 {
	arr := make([]int64, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextLong()
	}
	return arr
}

func (io *FastIO) NextString() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsSpaceChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) NextLine() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsLineBreakChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) Println(args ...interface{}) {
	io.writer.WriteString(fmt.Sprintln(args...))
}

func (io *FastIO) Printf(format string, args ...interface{}) {
	io.writer.WriteString(fmt.Sprintf(format, args...))
}

func (io *FastIO) FlushOutput() {
	io.writer.Flush()
}

func IsSpaceChar(c byte) bool {
	switch c {
	case 0, '\t', '\n', '\r', ' ':
		return true
	default:
		return false
	}
}

func IsLineBreakChar(c byte) bool {
	switch c {
		case 0, '\n', '\r':
			return true
		default:
			return false
	}
}

func main() {
	io := FastIO{reader: bufio.NewReader(os.Stdin), writer: bufio.NewWriter(os.Stdout)}
	Init()
	Solve(&io)
	io.FlushOutput()
}

package main

import (
    "fmt"
	"time"
)

const (
    maxk, maxn, inf int = 105, 1e5 + 5, 0x7fffffff
)

var (
    n, m, s, k int
    t [maxk][]int
    g, d [maxn][]int
)

func read() {
    fmt.Scanf("%d %d %d %d", &n, &m, &k, &s)
    var tmp, st, ed int
    for i := 1; i <= n; i++ {
        fmt.Scan(&tmp)
        t[tmp] = append(t[tmp], i)
    }
    for i := 0; i < m; i++ {
        fmt.Scan(&st, &ed)
        g[st] = append(g[st], ed)
        g[ed] = append(g[ed], st)
    }
    for i := 1; i <= n; i++ {
        d[i] = append(d[i], 0)
        for j := 1; j <= k; j++ { d[i] = append(d[i], inf) }
    }
}

func bfs(x int) {
    q := make([]int, 0)
    for _, item := range t[x] {
        q = append(q, item)
        d[item][x] = 0
    }
    for len(q) != 0 {
        hd := q[0]
        q = q[1:]
        for _, nxt := range g[hd] {
            if d[nxt][x] != inf { continue }
            d[nxt][x] = d[hd][x] + 1
            q = append(q, nxt)
        }
    }
}

func swap(x, y *int) {
	*x, *y = *y, *x
}

func partition(a *[]int, l, r int) int {
	idx := r
	r--
	for l <= r {
		for l < idx && (*a)[l] <= (*a)[idx] { l++ }
		for r > l && (*a)[r] >= (*a)[idx] { r-- }
		if l >= r { break }
		swap(&((*a)[l]), &((*a)[r]))
	}
	swap(&((*a)[l]), &((*a)[idx]))
	return l
}

func kthElement(a *[]int, l, r, k int) int {
	idx := -1
	for {
		idx = partition(a, l, r)
		if idx < k {
			l = idx + 1
		} else if idx > k {
			r = idx - 1
		} else { break }
	}
	return (*a)[idx]
}

func solve() {
    for i := 1; i <= k; i++ {
        bfs(i)
    }
    for i := 1; i <= n; i++ {
        kthElement(&d[i], 1, k, s)
        var ans = 0
        for j := 1; j <= s; j++ { ans += d[i][j] }
        fmt.Printf("%v ", ans)
    }
}

func main() {
	start := time.Now()
    read()
	end := time.Now()
	fmt.Println(end.Sub(start))
    solve()
}


package main

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

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

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

	var T int
	var x string
	var n int

	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)

		ans := int64(0)
		cnt := map[string]int{}
		for i := 0; i < n; i++ {
			Fscan(in, &x)

			for a := byte('a'); a <= 'k'; a++ {
				if a != x[0] {
					ans += int64(cnt[string([]byte{a, x[1]})])
				}
			}
			for a := byte('a'); a <= 'k'; a++ {
				if a != x[1] {
					ans += int64(cnt[string([]byte{x[0], a})])
				}
			}

			cnt[x]++
		}

		Fprintln(out, ans)
	}
}

package main

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

const MOD int64 = 1000000007

var n int

var str []string

// ByAge implements sort.Interface based on the Age field.
type ByV []*pair

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

func main() {

	read := bufio.NewReader(os.Stdin)

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

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

		fmt.Fscan(read, &n)

		str = make([]string, n)

		for i := 0; i < n; i++ {
			fmt.Fscan(read, &str[i])
		}

		solve()
	}
}

type pair struct {
	v int
	idx []int
}

//a b c d

//20 12 -- 4
//4  2  -- 4

func solve()  {
	//ab
	//ac
	//aa
	//ab

	//bd
	//bc

	var cnt int64

	mp := make(map[byte][]byte)

	for i := 0; i < n; i++ {
		mp[str[i][0]] = append(mp[str[i][0]], str[i][1])
	}

	for _ ,v := range mp {
		all := int64(len(v)) * int64(len(v) - 1)/2

		mp2 := make(map[byte]int)

		for j := 0; j < len(v); j++ {
			mp2[v[j]]++
		}

		for _, v2 := range mp2 {
			tmp := int64(v2) * (int64(v2) - 1) /2

			all -= tmp
		}

		cnt += all
	}

	mp = make(map[byte][]byte)

	for i := 0; i < n; i++ {
		mp[str[i][1]] = append(mp[str[i][1]], str[i][0])
	}

	for _ ,v := range mp {
		all := int64(len(v)) * int64(len(v) - 1)/2

		mp2 := make(map[byte]int)

		for j := 0; j < len(v); j++ {
			mp2[v[j]]++
		}

		for _, v2 := range mp2 {
			tmp := int64(v2) * (int64(v2) - 1) /2

			all -= tmp
		}

		cnt += all
	}

	fmt.Println(cnt)
}

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

package main

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

func solve(cin *scanner, cout *writer, cerr *writer) {
	n := cin.NextInt()
	cnt := make(map[string]int)
	var ans int64
	for i := 0; i < n; i++ {
		s := cin.Next()
		for t, v := range cnt {
			jm9 := 0
			if s[0] == t[0] {
				jm9++
			}
			if s[1] == t[1] {
				jm9++
			}
			if jm9 == 1 {
				ans += int64(v)
			}
		}
		cnt[s]++
	}
	cout.Println(ans)
}

func main() {
	cin := NewScanner(os.Stdin)
	cout, cerr := NewWriter(os.Stdout), NewWriter(os.Stderr)
	defer cout.Flush()
	defer cerr.Flush()
	t := cin.NextInt()
	// t := 1
	for ti := 0; ti < t; ti++ {
		solve(cin, cout, cerr)
	}
}

// Template Bolierplate {{{
type scanner struct {
	*bufio.Scanner
}

func NewScanner(r io.Reader) *scanner {
	s := bufio.NewScanner(r)
	s.Split(bufio.ScanWords)
	s.Buffer(nil, 1024*1024)
	return &scanner{s}
}

func (i *scanner) Next() string {
	i.Scan()
	return i.Text()
}

func (i *scanner) Nexts(n int) []string {
	res := make([]string, n)
	for j := range res {
		res[j] = i.Next()
	}
	return res
}

func (i *scanner) NextInt() int {
	v, _ := strconv.Atoi(i.Next())
	return v
}

func (i *scanner) NextInt64() int64 {
	v, _ := strconv.ParseInt(i.Next(), 10, 64)
	return v
}

func (i *scanner) NextInts(n int) []int {
	res := make([]int, n)
	for j := range res {
		res[j] = i.NextInt()
	}
	return res
}

func (i *scanner) NextInt64s(n int) []int64 {
	res := make([]int64, n)
	for j := range res {
		res[j] = i.NextInt64()
	}
	return res
}

type writer struct {
	w *bufio.Writer
}

func NewWriter(w io.Writer) *writer {
	return &writer{bufio.NewWriter(w)}
}

func (i *writer) Print(a ...interface{}) {
	fmt.Fprint(i.w, a...)
}

func (i *writer) Println(a ...interface{}) {
	fmt.Fprintln(i.w, a...)
}

func (i *writer) Printf(format string, a ...interface{}) {
	fmt.Fprintf(i.w, format, a...)
}

func (i *writer) Flush() {
	i.w.Flush()
}

// }}}

// vim: fdm=marker

package main

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

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

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

	t, n := 0, 0
	Fscan(in, &t)
	for ; t > 0; t-- {
		Fscan(in, &n)
		cnt1, cnt2 := [11]uint64{}, [11]uint64{}
		vis := make(map[string]uint64)
		ans := uint64(0)
		for i := 1; i <= n; i++ {
			str := ""
			Fscan(in, &str)

			ans += cnt1[str[0]-'a'] - vis[str]
			ans += cnt2[str[1]-'a'] - vis[str]

			cnt1[str[0]-'a']++
			cnt2[str[1]-'a']++
			vis[str]++
		}
		Fprintln(out, ans)
	}
}

package main

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

/*
1
6
ab
cb
db
aa
cc
ef
---
5
=======
1
7
aa
bb
cc
ac
ca
bb
aa
---
6
*/

//goland:noinspection GoUnhandledErrorResult
func solve() {
	var n int
	scanf("%d\n", &n)

	strToCnt := make(map[string]int)

	for i := 0; i < n; i++ {
		var s string
		scanf("%s\n", &s)

		strToCnt[s]++
	}

	totalCnt := int64(0)

	strs := make([]string, 0)

	for s := range strToCnt {
		strs = append(strs, s)
	}

	for i, s1 := range strs {
		for _, s2 := range strs[i+1:] {
			if (s1[0] == s2[0] && s1[1] != s2[1]) || (s1[0] != s2[0] && s1[1] == s2[1]) {
				totalCnt += int64(strToCnt[s1]) * int64(strToCnt[s2])
			}
		}
	}

	fmt.Fprintln(writer, totalCnt)
}

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

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

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

const maxUint = ^uint(0)

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

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

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

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

//goland:noinspection GoUnusedFunction
func reversed(s string) string {
	input := []rune(s)
	li := len(input)
	ret := make([]rune, li)
	for i, r := range input {
		ret[li-i-1] = r
	}
	return string(ret)
}

//goland:noinspection GoUnusedFunction
func divCeil(num, div int64) int64 {
	quot := num / div
	if num%div > 0 {
		quot++
	} else if num%div < 0 {
		quot--
	}
	return quot
}

//goland:noinspection GoUnusedFunction
func min(nums ...int) int {
	return nums[minIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func minIdx(nums ...int) int {
	minNumIdx := 0
	for i, num := range nums {
		if num < nums[minNumIdx] {
			minNumIdx = i
		}
	}

	return minNumIdx
}

//goland:noinspection GoUnusedFunction
func max(nums ...int) int {
	return nums[maxIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func maxIdx(nums ...int) int {
	maxNumIdx := 0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = i
		}
	}

	return maxNumIdx
}

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

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

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

	for i := 5; i*i <= num; i += 6 {
		if num%i == 0 || num%(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 {
	if num == 0 {
		return 0
	}

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

//goland:noinspection GoUnhandledErrorResult
func factorize(num int) []int {
	factors := []int{1}

	sqrtA := int(math.Sqrt(float64(num)))

	for ftr := 2; ftr <= sqrtA; ftr++ {
		for num%ftr == 0 {
			num /= ftr
			factors = append(factors, ftr)
		}
	}

	if num > sqrtA {
		factors = append(factors, num)
	}

	return factors
}

package main

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

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

func isok(s string) bool {
	b := strings.Count(s, "B")
	rr := strings.Count(s, "R")
	if rr == 0 && b == 0 {
		return true
	} else if rr == 0 || b == 0 {
		return false
	} else {
		return true
	}
}

func slove() {
	defer w.Flush()
	var n int
	fmt.Fscan(r, &n)
	ss := make([]string, n)
	for i := range ss {
		fmt.Fscan(r, &ss[i])
	}
	pre, last := make(map[byte]int64), make(map[byte]int64)
	cnt := make(map[string]int64)
	var ans int64
	for i := range ss {
		ans += pre[ss[i][0]]
		ans += last[ss[i][1]]
		ans -= 2 * cnt[ss[i]]
		pre[ss[i][0]]++
		last[ss[i][1]]++
		cnt[ss[i]]++
	}
	fmt.Fprintln(w, ans)

}

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

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

package main

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

func main() {
	in, out := bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in, &t)

	for ii := 0; ii < t; ii++ {
		var n int
		fmt.Fscan(in, &n)
		m := make([]int64, 200)
		for i := 0; i < n; i++ {
			var s string
			fmt.Fscan(in, &s)
			m[(s[0]-'a')*11+s[1]-'a']++
		}
		var ans int64
		for i := 0; i < 200; i++ {
			for j := i + 1; j < 200; j++ {
				if i%11 == j%11 && i/11 != j/11 {
					ans += m[i] * m[j]
				}
				if i%11 != j%11 && i/11 == j/11 {
					ans += m[i] * m[j]
				}
			}
		}
		fmt.Fprintln(out, ans)
	}
}

package main

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

func main_div4_784_E() {
	_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()
	sol = func() {
		var n int
		Fscan(in, &n)
		s := make([]string, n)
		for i := 0; i < n; i++ {
			Fscan(in, &s[i])
		}

		var solo func() int64
		solo = func() int64 {
			var ans int64
			set := make(map[byte]map[byte]int64, 0)
			for i := byte(97); i <= byte(122); i++ {
				for j := 0; j < n; j++ {
					if s[j][1] == i {
						if len(set[i]) == 0 {
							set[i] = make(map[byte]int64, 0)
						}
						set[i][s[j][0]]++
					}
				}
			}
			for i := byte(97); i <= byte(122); i++ {
				child_set := set[i]
				var total int64
				for _, v := range child_set {
					total += v
				}
				for _, v := range child_set {
					ans += v * (total - v)
				}
			}
			return ans
		}
		var res int64
		res += solo()

		for i := 0; i < n; i++ {
			tmp := []byte(s[i])
			tmp[0], tmp[1] = tmp[1], tmp[0]
			s[i] = string(tmp)
		}
		res += solo()
		Fprintln(out, res/2)
	}
	for i := 0; i < cas; i++ {
		sol()
	}
}

func main() {
	main_div4_784_E()
}

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(curCase int) {
		var n int
		Fscan(in, &n)
		ans := int64(0)

		cnt := map[string]int{}
		for i := 0; i < n; i++ {
			var s []byte
			Fscan(in, &s)
			for i := byte('a'); i <= 'k'; i++ {
				if s[0] != i {
					t := append([]byte{}, s...)
					t[0] = i
					ans += int64(cnt[string(t)])
				}
				if s[1] != i {
					t := append([]byte{}, s...)
					t[1] = i
					ans += int64(cnt[string(t)])
				}
			}
			cnt[string(s)]++
		}

		Fprintln(out, ans)
	}

	cases := 1
	Fscan(in, &cases)
	for curCase := 1; curCase <= cases; curCase++ {
		solve(curCase)
	}

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

}

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

// @Title
// @Description
// @Author
// @Update
package main

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

func fun(in *bufio.Reader) {
        n := 1
        mp := make([][]int64, 12)
        for i := 0; i < 12; i++ {
                mp[i] = make([]int64, 12)
        }
        fmt.Fscan(in, &n)
        var ans int64 = 0
        for i := 0; i < n; i++ {
                s := ""
                fmt.Fscan(in, &s)
                for j := 0; j < 2; j++ {
                        for c := byte('a'); c <= byte('k'); c++ {
                                if c == s[j] {
                                        continue
                                }
                                a, b := s[0], s[1]
                                if j == 0 {
                                        a = c
                                } else {
                                        b = c
                                }
                                ans += mp[a-byte('a')][b-byte('a')]
                        }
                }
                mp[s[0]-byte('a')][s[1]-byte('a')]++
        }
        fmt.Println(ans)
}
func main() {
        t := 1
        fmt.Scan(&t)
        in := bufio.NewReader(os.Stdin)
        for i := 1; i <= t; i++ {
                fun(in)
        }

}

package main

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

var sc = bufio.NewScanner(os.Stdin)
var wr = bufio.NewWriter(os.Stdout)

func out(x ...interface{}) {
	fmt.Fprint(wr, x...)
}

func nextInt() int {
	i, e := strconv.Atoi(next())
	if e != nil {
		panic(e)
	}
	return i
}

func nextInt64() int64 {
	i, e := strconv.ParseInt(next(), 10, 64)
	if e != nil {
		panic(e)
	}
	return i
}

func next() string {
	sc.Scan()
	return sc.Text()
}

func main() {
	defer wr.Flush()
	sc.Buffer(make([]byte, 0), 1000000009)
	sc.Split(bufio.ScanWords)

	T := nextInt()
	for Case := 1; Case <= T; Case++ {
		solve()
	}
}

func solve() {
	n := nextInt()
	mp := make(map[string]int, n)
	var res int64 = 0
	for i := 0; i < n; i++ {
		s := next()
		for i := 0; i < 26; i++ {
			if i+int('a') != int(s[0]) {
				ts := fmt.Sprintf("%c%c", (int)('a')+i,s[1])
				res+=int64(mp[ts])
			}
		}
		for i := 0; i < 26; i++ {
			if i+int('a') != int(s[1]) {
				ts := fmt.Sprintf("%c%c",s[0], (int)('a')+i)
				res+=int64(mp[ts])
			}
		}
		mp[s] ++
	}

	fmt.Printf("%d\n", res)
}

package main

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

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

func main() {
	defer out.Flush()
	var t int
	fmt.Fscanf(in, "%d\n", &t)
	for ; t > 0; t-- {
		var n int
		fmt.Fscanf(in, "%d\n", &n)
		mmid := make(map[uint8]map[uint8]int)
		for i := 'a'; i <= 'k'; i++ {
			mmid[uint8(i)] = make(map[uint8]int)
		}
		var str string
		res := int64(0)

		for i := 0; i < n; i++ {
			fmt.Fscanf(in, "%s\n", &str)

			for k := 'a'; k <= 'k'; k++ {
				if str[0] == uint8(k) {
					continue
				}
				res += int64(mmid[uint8(k)][str[1]])
			}
			for k := 'a'; k <= 'k'; k++ {
				if str[1] == uint8(k) {
					continue
				}
				res += int64(mmid[str[0]][uint8(k)])
			}
			mmid[str[0]][str[1]]++

		}

		fmt.Fprintln(out, res)
	}
}

package main

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

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)

		m := make(map[string]int64)
		for i := 0; i < n; i++ {
			var s string
			fmt.Fscan(in, &s)

			m[s]++
		}

		var tot int64
		for k, v := range m {

			for i := 0; i < 26; i++ {
				s1 := fmt.Sprintf("%c%c", k[0], i+'a')
				s2 := fmt.Sprintf("%c%c", i+'a', k[1])

				if s1 != k {
					tot += v * m[s1]
				}

				if s2 != k {
					tot += v * m[s2]
				}
			}
		}
		fmt.Fprintln(out, tot/2)
	}
}

package main

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

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

	var n int
	Fscan(in, &n)

	for ; n > 0; n-- {
		var m int
		Fscan(in, &m)
		maps := map[string]int64{}
		for ; m > 0; m-- {
			var str string
			Fscan(in, &str)
			maps[str]++
		}

		var res int64
		for str, cnt := range maps {
			if cnt == 0 {
				continue
			}
			a, b := []byte(str), []byte(str)
			for i := 0; i <= 11; i++ {
				if b[0] != byte(i+'a') {
					a[0] = (byte)(i + 'a')
					count, _ := maps[string(a)]
					if count != 0 {
						res += count * cnt
					}
				}

				if a[1] != byte(i+'a') {
					b[1] = byte(i + 'a')
					count, _ := maps[string(b)]
					if count != 0 {
						res += count * cnt
					}
				}
			}
			maps[str] = 0
		}
		Fprintln(out, res)
	}
}

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

package main

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

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

	var T, n int
	for fmt.Fscan(in, &T); T > 0; T-- {
		fmt.Fscan(in, &n)
		var ans int64
		var s string
		var cnt1, cnt2 [11][11]int
		var cnta, cntb [11]int
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &s)
			a, b := s[0] - 'a', s[1] - 'a'
			ans += int64(cnta[a] - cnt1[a][b]) + int64(cntb[b] - cnt2[b][a])

			cnta[a]++
			cntb[b]++
			cnt1[a][b]++
			cnt2[b][a]++
			// fmt.Fprintln(out, ans)
		}
		fmt.Fprintln(out, ans)
	}
}

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

package main

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

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-- {
		var n int
		fmt.Fscan(in, &n)

		fstore, sstore := map[uint8]int64{}, map[uint8]int64{}
		fdstore, sdstore := map[uint8]map[uint8]int64{}, map[uint8]map[uint8]int64{}
		ans := int64(0)

		for ; n > 0; n-- {
			var input string
			fmt.Fscan(in, &input)

			firstLetter, secondLetter := input[0], input[1]

			if _, ok := fdstore[firstLetter]; !ok {
				fdstore[firstLetter] = map[uint8]int64{}
			}

			if _, ok := sdstore[secondLetter]; !ok {
				sdstore[secondLetter] = map[uint8]int64{}
			}

			ans += fstore[firstLetter] - fdstore[firstLetter][secondLetter]
			fstore[firstLetter] = fstore[firstLetter] + 1
			fdstore[firstLetter][secondLetter] = fdstore[firstLetter][secondLetter] + 1

			ans += sstore[secondLetter] - sdstore[secondLetter][firstLetter]
			sstore[secondLetter] = sstore[secondLetter] + 1
			sdstore[secondLetter][firstLetter] = sdstore[secondLetter][firstLetter] + 1
		}

		fmt.Fprintln(out, ans)
	}

}

package main

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

var sc = bufio.NewScanner(os.Stdin)
var wr = bufio.NewWriter(os.Stdout)

func out(x ...interface{}) {
	fmt.Fprintln(wr, x...)
}

func next() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	i, e := strconv.Atoi(next())
	if e != nil {
		panic(e)
	}
	return i
}

func nextInt64() int64 {
	i, e := strconv.ParseInt(next(), 10, 64)
	if e != nil {
		panic(e)
	}
	return i
}

func main() {
	defer wr.Flush()
	sc.Buffer(make([]byte, 0), 1000000009)
	sc.Split(bufio.ScanWords)

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

func solve() {
	n := nextInt()

	ans := int64(0)
	memo := make([][]int, 11)
	for i := range memo {
		memo[i] = make([]int, 11)
	}

	for i := 0; i < n; i++ {
		s := next()
		s1 := int(s[0] - 'a')
		s2 := int(s[1] - 'a')

		for alp1 := 0; alp1 < 11; alp1++ {
			for alp2 := 0; alp2 < 11; alp2++ {
				if alp1 != s1 && alp2 == s2 {
					ans += int64(memo[alp1][alp2])
				}
				if alp1 == s1 && alp2 != s2 {
					ans += int64(memo[alp1][alp2])
				}
			}
		}
		memo[s1][s2]++
	}

	out(ans)
}

package main

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

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

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

    const MAXC int = 11

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

        f := make([][]int,MAXC + 1)
        for i, _ := range f {
            f[i] = make([]int,MAXC + 1)
        }

        var ans int64
        ans = 0

        for i := 1; i <= n; i++ {
            var s string
            fmt.Fscan(reader,&s)

            for p := 0; p <= 1; p++ {
                for j := 0;j < MAXC; j++ {
                    if j != int(s[p ^ 1]) - int('a') {
                        cur := int(s[p]) - int('a')
                        if p == 0 {
                            ans += int64(f[cur][j])
                        } else {
                            ans += int64(f[j][cur])
                        }
                    }
                }
            }
            f[int(s[0]) - int('a')][int(s[1]) - 'a'] += 1
        }

        fmt.Println(ans)
    }
}

// @Title
// @Description
// @Author
// @Update
package main

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

func fun(in *bufio.Reader) {
        n := 1
        mp := make(map[byte]int64)
        st := make(map[string]int64)
        fmt.Fscan(in, &n)
        var ans int64 = 0
        for i := 0; i < n; i++ {
                s := ""
                fmt.Fscan(in, &s)
                t := s
                if s[0] > s[1] {
                        t = s[1:2] + s[0:1]
                }
                a, b := s[0], s[1]
                ans += mp[a] - st[s]
                if s != t {
                        ans -= st[t]
                }
                if a != b {
                        ans += mp[b] - st[s]
                        if s != t {
                                ans -= st[t]
                        }
                }
                st[s]++
                mp[a]++
                if a != b {
                        mp[b]++
                }
        }
        fmt.Println(ans)
}

func main() {
        t := 1
        fmt.Scan(&t)
        in := bufio.NewReader(os.Stdin)
        for i := 1; i <= t; i++ {
                fun(in)
        }

}

// @Title
// @Description
// @Author
// @Update
package main

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

func fun(in *bufio.Reader) {
        n := 1
        mp := make(map[byte]int64)
        st := make(map[string]int64)
        fmt.Fscan(in, &n)
        var ans int64 = 0
        for i := 0; i < n; i++ {
                s := ""
                fmt.Fscan(in, &s)
                if s[0] > s[1] {
                        t := s[1:2] + s[0:1]
                        s = t
                }
                a, b := s[0], s[1]
                ans += mp[a] - st[s]
                if a != b {
                        ans += mp[b] - st[s]
                }
                st[s]++
                mp[a]++
                if a != b {
                        mp[b]++
                }
        }
        fmt.Println(ans)
}

func main() {
        t := 1
        fmt.Scan(&t)
        in := bufio.NewReader(os.Stdin)
        for i := 1; i <= t; i++ {
                fun(in)
        }

}

package main

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

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

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

	var T int
	var x string
	var n int

	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)

		ans := 0
		cnt := map[string]int{}
		for i := 0; i < n; i++ {
			Fscan(in, &x)

			for a := byte('a'); a <= 'k'; a++ {
				if a != x[0] {
					ans += cnt[string([]byte{a, x[1]})]
				}
			}
			for a := byte('a'); a <= 'k'; a++ {
				if a != x[1] {
					ans += cnt[string([]byte{x[0], a})]
				}
			}

			cnt[x]++
		}

		Fprintln(out, ans)
	}
}

package main

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

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

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

	var T int
	var x string
	var n int

	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)

		ans := 0
		cnt := map[string]int{}
		for i := 0; i < n; i++ {
			Fscan(in, &x)

			for a := byte('a'); a <= 'z'; a++ {
				if a != x[0] {
					ans += cnt[string([]byte{a, x[1]})]
				}
			}
			for a := byte('a'); a <= 'z'; a++ {
				if a != x[1] {
					ans += cnt[string([]byte{x[0], a})]
				}
			}

			cnt[x]++
		}

		Fprintln(out, ans)
	}
}

package main

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

var sc = bufio.NewScanner(os.Stdin)
var wr = bufio.NewWriter(os.Stdout)

func out(x ...interface{}) {
	fmt.Fprint(wr, x...)
}

func nextInt() int {
	i, e := strconv.Atoi(next())
	if e != nil {
		panic(e)
	}
	return i
}

func nextInt64() int64 {
	i, e := strconv.ParseInt(next(), 10, 64)
	if e != nil {
		panic(e)
	}
	return i
}

func next() string {
	sc.Scan()
	return sc.Text()
}

func main() {
	defer wr.Flush()
	sc.Buffer(make([]byte, 0), 1000000009)
	sc.Split(bufio.ScanWords)

	T := nextInt()
	for Case := 1; Case <= T; Case++ {
		solve()
	}
}

func solve() {
	n := nextInt()
	mp := make(map[string]int, n)
	res := 0
	for i := 0; i < n; i++ {
		s := next()
		for i := 0; i < 26; i++ {
			if i+int('a') != int(s[0]) {
				ts := fmt.Sprintf("%c%c", (int)('a')+i,s[1])
				res+=mp[ts]
			}
		}
		for i := 0; i < 26; i++ {
			if i+int('a') != int(s[1]) {
				ts := fmt.Sprintf("%c%c",s[0], (int)('a')+i)
				res+=mp[ts]
			}
		}
		mp[s] ++
	}

	fmt.Printf("%d\n", res)
}

package main

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

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

func main() {
	defer out.Flush()
	var t int
	fmt.Fscanf(in, "%d\n", &t)
	for ; t > 0; t-- {
		var n int
		fmt.Fscanf(in, "%d\n", &n)
		mmid := make(map[uint8]map[uint8]int)
		for i := 'a'; i <= 'k'; i++ {
			mmid[uint8(i)] = make(map[uint8]int)
		}
		var str string
		res := 0

		for i := 0; i < n; i++ {
			fmt.Fscanf(in, "%s\n", &str)

			for k := 'a'; k <= 'k'; k++ {
				if str[0] == uint8(k) {
					continue
				}
				res += mmid[uint8(k)][str[1]]
			}
			for k := 'a'; k <= 'k'; k++ {
				if str[1] == uint8(k) {
					continue
				}
				res += mmid[str[0]][uint8(k)]
			}
			mmid[str[0]][str[1]]++

		}

		fmt.Fprintln(out, res)
	}
}

package main

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

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)

		m := make(map[string]int)
		for i := 0; i < n; i++ {
			var s string
			fmt.Fscan(in, &s)

			m[s]++
		}

		var tot int
		for k, v := range m {

			for i := 0; i < 26; i++ {
				s1 := fmt.Sprintf("%c%c", k[0], i+'a')
				s2 := fmt.Sprintf("%c%c", i+'a', k[1])

				if s1 != k {
					tot += v * m[s1]
				}

				if s2 != k {
					tot += v * m[s2]
				}
			}
		}
		fmt.Fprintln(out, tot/2)
	}
}


package main

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

type vec struct {
	x, y int
}

func (a vec) sub(b vec) vec { return vec{a.x - b.x, a.y - b.y} }
func (a vec) len() float64  { return math.Hypot(float64(a.x), float64(a.y)) }

// github.com/EndlessCheng/codeforces-go
func Sol671A(reader io.Reader, writer io.Writer) {
	type pair struct {
		d   float64
		idx int
	}
	read := func(in io.Reader) vec {
		var x, y int
		Fscan(in, &x, &y)
		return vec{x, y}
	}
	max := func(a, b float64) float64 {
		if a > b {
			return a
		}
		return b
	}
	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()

	pa, pb, bin := read(in), read(in), read(in)
	pa = pa.sub(bin)
	pb = pb.sub(bin)
	var n int
	Fscan(in, &n)
	das := make([]pair, n)
	dbs := make([]pair, n)
	ans := 0.0
	for i := range das {
		p := read(in).sub(bin)
		lenP := p.len()
		ans += lenP
		das[i] = pair{lenP - p.sub(pa).len(), i}
		dbs[i] = pair{lenP - p.sub(pb).len(), i}
	}
	ans *= 2

	sort.Slice(das, func(i, j int) bool { return das[i].d > das[j].d })
	sort.Slice(dbs, func(i, j int) bool { return dbs[i].d > dbs[j].d })
	if n == 1 || das[0].d <= 0 || dbs[0].d <= 0 {
		ans -= max(das[0].d, dbs[0].d)
	} else if das[0].idx == dbs[0].idx {
		sum1 := das[0].d
		sum2 := dbs[0].d
		if dbs[1].d > 0 {
			sum1 += dbs[1].d
		}
		if das[1].d > 0 {
			sum2 += das[1].d
		}
		ans -= max(sum1, sum2)
	} else {
		ans -= das[0].d + dbs[0].d
	}
	Fprintf(out, "%.12f", ans)
}

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

package main

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

type point struct {
	x, y float64
}

func dist(a, b point) float64 {
	dx := a.x - b.x
	dy := a.y - b.y
	return math.Sqrt(dx*dx + dy*dy)
}

func main() {
	in := bufio.NewReaderSize(os.Stdin, 2<<16)
	var a, b, t point
	var n int
	fmt.Fscan(in, &a.x, &a.y, &b.x, &b.y, &t.x, &t.y, &n)
	bottles := make([]point, n)
	sum := 0.
	type best struct {
		i   int
		win float64
	}
	update := func(b *[]best, i int, p point) {
		win := dist(bottles[i], t) - dist(bottles[i], p)
		if len(*b) == 0 || (len(*b) == 1 && (*b)[0].win > win) {
			*b = append(*b, best{i, win})
		} else if len(*b) == 1 {
			*b = []best{{i, win}, (*b)[0]}
		} else if win > (*b)[0].win {
			(*b)[1] = (*b)[0]
			(*b)[0] = best{i, win}
		} else if win > (*b)[1].win {
			(*b)[1] = best{i, win}
		}
	}
	var bestA, bestB []best
	for i := range bottles {
		fmt.Fscan(in, &bottles[i].x, &bottles[i].y)
		sum += dist(bottles[i], t)
		update(&bestA, i, a)
		update(&bestB, i, b)
	}

	var wins []float64
	if bestA[0].i == bestB[0].i {
		wins = []float64{bestA[0].win, bestB[0].win}
		if n > 1 {
			wins = append(wins, bestA[0].win+bestB[1].win, bestA[1].win+bestB[0].win)
		}
	} else {
		wins = []float64{bestA[0].win, bestB[0].win, bestA[0].win + bestB[0].win}
	}
	sort.Float64s(wins)

	ans := sum*2 - wins[len(wins)-1]
	fmt.Println(ans)
}

package main

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

func sqr(a int) int64 {
	return int64(a) * int64(a)
}
func dist(ax, ay, bx, by int) float64 {
	return math.Sqrt(float64(sqr(ax-bx) + sqr(ay-by)))
}

const Inf = 1000000001

func main() {
	ax, ay, bx, by, tx, ty, n := getInt(), getInt(), getInt(), getInt(), getInt(), getInt(), getInt()

	var dsum float64
	const ival = -4.0 * Inf * Inf
	var ansa, ansb struct {
		best, next float64
		idx        int
	}
	ansa.best, ansa.next, ansb.best, ansb.next = ival, ival, ival, ival

	for i := 0; i < n; i++ {
		px, py := getInt(), getInt()
		t := dist(tx, ty, px, py)
		a := t - dist(ax, ay, px, py)
		b := t - dist(bx, by, px, py)
		dsum += t

		if a > ansa.best {
			ansa.next = ansa.best
			ansa.best = a
			ansa.idx = i
		} else if a > ansa.next {
			ansa.next = a
		}
		if b > ansb.best {
			ansb.next = ansb.best
			ansb.best = b
			ansb.idx = i
		} else if b > ansb.next {
			ansb.next = b
		}
	}
	dsum *= 2

	a, b := ansa.best, ansb.best
	ans := math.Max(a, b) // only one

	// two
	if ansa.idx != ansb.idx {
		ans = math.Max(ans, a+b)
	} else {
		ans = math.Max(ans, math.Max(a+ansb.next, b+ansa.next))
	}

	fmt.Printf("%.7f\n", dsum-ans)
}

type Input struct {
	*bufio.Scanner
}

var globalInput = wrapStdin()

func wrapStdin() Input {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	return Input{Scanner: sc}
}
func getWord() string {
	globalInput.Scan()
	return globalInput.Text()
}
func getInt() int {
	globalInput.Scan()
	x, _ := strconv.Atoi(globalInput.Text())
	return x
}
func getIntOk() (int, bool) {
	ok := globalInput.Scan()
	x, _ := strconv.Atoi(globalInput.Text())
	return x, ok
}
func getNint(n int) []int {
	v := make([]int, n)
	for i := 0; i < n; i++ {
		v[i] = getInt()
	}
	return v
}
func getVint(v []int) bool {
	var ok bool
	for i := 0; i < len(v); i++ {
		v[i], ok = getIntOk()
	}
	return ok
}

package main

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

var in = bufio.NewScanner(os.Stdin)

func readInt() int64 {
	if !in.Scan() {
		panic(in.Err())
	}
	res := int64(0)
	for _, b := range in.Bytes() {
		res = res*10 + int64(b-'0')
	}
	return res
}

func dist(ax, ay, bx, by int64) float64 {
	dx := ax - bx
	dy := ay - by
	return math.Sqrt(float64(dx*dx + dy*dy))
}

func main() {
	in.Split(bufio.ScanWords)

	ax := readInt()
	ay := readInt()
	bx := readInt()
	by := readInt()
	tx := readInt()
	ty := readInt()

	ans := 0.
	winA := float64(-1e15)
	winA2 := float64(-1e15)
	winB := float64(-1e15)
	winB2 := float64(-1e15)
	for n := readInt(); n > 0; n-- {
		x := readInt()
		y := readInt()
		d := dist(x, y, tx, ty)
		ans += 2 * d
		wa := d - dist(x, y, ax, ay)
		wb := d - dist(x, y, bx, by)
		if wa > wb {
			if winA < wa {
				winA = wa
				if winB < winB2 {
					winB = winB2
				}
				winB2 = wb
			} else {
				if winB < wb {
					winB = wb
				}
			}
		} else {
			if winB < wb {
				winB = wb
				if winA < winA2 {
					winA = winA2
				}
				winA2 = wa
			} else {
				if winA < wa {
					winA = wa
				}
			}
		}
	}

	if winA > winB {
		winA, winB = winB, winA
	}

	ans -= winB
	if winA > 0 {
		ans -= winA
	}
	fmt.Printf("%.12f", ans)
}


// Codeforces 374 B Inna nad Nine, train
package main

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

var in *bufio.Reader

func readFive() (int, int, int, int, int) {
	var a [5]int
	s, err := in.ReadString('\n')
	if err != nil {
		fmt.Println("first read failure", err)
		panic(err)
	}
	ss := strings.Split(strings.Trim(s, " \n\r"), " ")
	for i := 0; i < len(ss); i++ {
		a[i], err = strconv.Atoi(ss[i])
		if err != nil {
			fmt.Println("n atoi failure", err)
			panic(err)
		}
	}
	return a[0], a[1], a[2], a[3], a[4]
}

func readLineNumbs(n int) []int {
	a := make([]int, n)
	s, err := in.ReadString('\n')
	if err != nil {
		fmt.Println("read failure", err)
		panic(err)
	}
	ss := strings.Split(strings.Trim(s, " \n\r"), " ")
	for j := 0; j < n; j++ {
		aa, err := strconv.Atoi(ss[j])
		if err != nil {
			fmt.Println("aa conv failed", err)
			panic(err)
		}
		a[j] = aa
	}
	return a
}

var a string
var ans int64

// readData read data for out problem.
func readData() {
	in = bufio.NewReader(os.Stdin)
	// reading string a
	a,_ = in.ReadString('\n')
}

// solve func solves our problem.
func solve() {
	var n90,nw0 int64
	var n91,nw1 int64
	nw0 = 1
	nw1 = 1
	for i:=1; i<len(a)-1; i++ {
		n92 := n91
		nw2 := nw1
		sm := ((a[i-1]-'0') + (a[i]-'0'))
		if sm==9 {
			n92 = n90+1
			//fmt.Printf("n92,n91 = %d %d\n", n92,n91)
			if n92==n91 {
				nw2 = nw1 + nw0
			} else {
				nw2 = nw0
			}
		}
		//fmt.Printf("i,ai,sm %d %d %d 09w_ %d %d 1_ %d %d N_ %d %d\n",
		//	i,a[i],sm,n90,nw0,n91,nw1,n92,nw2)
		n90 = n91; nw0 = nw1
		n91 = n92; nw1 = nw2;
	}
	ans = nw1
}

// printRes print problem solution in expected format.
func printRes() {
	fmt.Println(ans)
}

func main() {
	readData()
	solve()
	printRes()
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	var s string
	var cnt, res int64 = 1, 1
	fmt.Fscan(reader, &s)
	var n int = len(s)
	for i := 1; i < n; i++ {
		if s[i] + s[i - 1] - 2 * '0' == 9 {
			cnt++
		} else {
			if cnt % 2 == 1 {
				res *= cnt / 2 + 1
			}
			cnt = 1
		}
	}
	if cnt % 2 == 1 {
		res *= cnt / 2 + 1
	}
	fmt.Fprintln(writer, res)
}//2020-07-30 23:02:49.469