package main

import (
	"fmt"
)

type time struct {
	t int
	a []bool
}

func newTime(t int) time {
	ti := time{}
	ti.t = t
	ti.a = make([]bool, 301+t)
	return ti
}

func (ti *time) get(t int) bool {
	return ti.a[t+ti.t]
}

func (ti *time) set(t int, b bool) {
	ti.a[t+ti.t] = b
}

func (ti *time) count() int {
	c := 0
	for i := 0; i < len(ti.a); i++ {
		if ti.a[i] {
			c++
		}
	}
	return c
}

func main() {
	var m, t, r int
	fmt.Scan(&m, &t, &r)
	w := make([]int, m)
	for i := 0; i < m; i++ {
		fmt.Scan(&w[i])
	}

	if m > 0 && t < r {
		fmt.Println(-1)
		return
	}

	ti := newTime(t)
	for i := m - 1; i >= 0; i-- {
		c := 0
		for j := 1; j <= t; j++ {
			if ti.get(w[i] - j) {
				c++
			}
		}

		for c < r {
			for j := t; j >= 1; j-- {
				if ti.get(w[i]-j) == false {
					ti.set(w[i]-j, true)
					c++
					if c >= r {
						break
					}
				}
			}
		}
	}

	fmt.Println(ti.count())
}

package main

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

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

type Candle struct {
	time int
	numbers int
}

//Wrong
func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	m, t, r := nextInt(), nextInt(), nextInt()
	w, remainCandles, total := 0, 0, 0
	candles := make([]Candle, 0, 10)

	if t < r {
		println(-1)
		return
	}

	for ; m > 0; m-- {
		w = nextInt()
		remainCandles = 0
		// println(w)
		for i := 0; i < len(candles); i++ {
			// println(remainCandles, candles)
			if candles[i].time + candles[i].numbers + t < w {
				candles = candles[1:]
				i--
			} else {

				if w - t <= candles[i].time {
					remainCandles += candles[i].numbers
				} else {
					remainCandles = candles[i].numbers - (w - t - candles[i].time)
				}
			}

			// println(remainCandles, candles)
		}

		if remainCandles < r {
			nextCandles := r - remainCandles
			total += nextCandles

			candles = append(candles, Candle{w - nextCandles, nextCandles})
		}

		// println(total, "\n")
	}

	println(total)
}

func scan(a ...interface{}) {
	fmt.Fscan(reader, a...)
}

func next() string {
	if !scanner.Scan() {
		panic("Scan error")
	}
	return scanner.Text()
}

func nextInt() int {
	n, _ := strconv.Atoi(next())
	return n
}

func nextFloat() float64 {
	n, _ := strconv.ParseFloat(next(), 64)
	return n
}

func println(a ...interface{}) {
	fmt.Fprintln(writer, a...)
}

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

package main

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

var (
	m, t, r int
	w       []int
	result  int
)

func in() {
	read(&m, &t, &r)
	w = make([]int, m)
	for i := 0; i < m; i++ {
		read(&w[i])
		w[i] += 300
	}
}

func out() {
	println(result)
	defer output.Flush()
}

func run() {
	c := make([]bool, 610)
	for i := 0; i < m; i++ {
		count := 0
		for j := w[i] - 1; j >= w[i]-t; j-- {
			if c[j] {
				count++
			}
		}
		if count >= r {
			continue
		} else {
			count = r - count
		}

		for j := w[i] - 1; j >= w[i]-t && count > 0; j-- {
			if !c[j] {
				c[j] = true
				count--
			}
		}

		if count > 0 {
			result = -1
			return
		}
	}

	result = 0
	for _, candle := range c {
		if candle {
			result++
		}
	}
}

/** main **/
func main() {
	in()
	run()
	out()
}

// Input & Output
var (
	input  = bufio.NewReader(os.Stdin)
	output = bufio.NewWriter(os.Stdout)
)

func read(a ...interface{}) {
	_, err := fmt.Fscan(input, a...)
	if err != nil {
		println(err)
	}
}

func println(a ...interface{}) {
	fmt.Fprintln(output, a...)
}

func printf(format string, a ...interface{}) {
	fmt.Fprintf(output, format, a...)
}

package main

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

type Pair struct {
	pos int
	count int
}

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

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

	var m, t, r int
	fmt.Fscanf(in, "%v %v %v ", &m, &t, &r)
	ws := make([]Pair, m)
	for i := 0; i < m; i++ {
		var w int
		fmt.Fscanf(in, "%v ", &w)
		ws[i] = Pair{w, 0}
	}
	if t < r {
		fmt.Printf("-1\n")
		return
	}
	total_count := 0
	for i := 0; i < m; i++ {
		if ws[i].count < r {
			delta := r - ws[i].count
			total_count += delta
			for j := i; j < m; j++ {
				if ws[j].pos >= ws[i].pos + t {
					break
				}
				ws[j].count += min(delta, ws[i].pos + t - ws[j].pos)
			}
		}
	}
  fmt.Printf("%v\n", total_count)
}

package main

import (
	"fmt"
)

func main() {
	var res int
	q := make([]int, 0, 310)
	var M, T, R int
	fmt.Scan(&M, &T, &R)
	if R > T {
		fmt.Println(-1)
		return
	}
	for i := 0; i < M; i++ {
		var W int
		fmt.Scan(&W)
		for len(q) > 0 && q[0]+T-1 < W {
			q = q[1:]
		}
		for j := W - (R - len(q)) + 1; j <= W; j++ {
			res++
			q = append(q, j)
		}
	}
	fmt.Println(res)
}

package main

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

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

type Candle struct {
	time int
	numbers int
}

//Wrong
func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	m, t, r := nextInt(), nextInt(), nextInt()
	w, remainCandles, total := 0, 0, 0
	candles := make([]Candle, 0, 10)

	if t < r {
		println(-1)
		return
	}

	for ; m > 0; m-- {
		w = nextInt()
		remainCandles = 0
		println(w)
		for i := 0; i < len(candles); i++ {
			// println(remainCandles, candles)
			if candles[i].time + candles[i].numbers + t < w {
				candles = candles[1:]
				i--
			} else {

				if w - t <= candles[i].time {
					remainCandles += candles[i].numbers
				} else {
					remainCandles = candles[i].numbers - (w - t - candles[i].time)
				}
			}

			println(remainCandles, candles)
		}

		if remainCandles < r {
			nextCandles := r - remainCandles
			total += nextCandles

			candles = append(candles, Candle{w - nextCandles, nextCandles})
		}

		println(total, "\n")
	}

	println(total)
}

func scan(a ...interface{}) {
	fmt.Fscan(reader, a...)
}

func next() string {
	if !scanner.Scan() {
		panic("Scan error")
	}
	return scanner.Text()
}

func nextInt() int {
	n, _ := strconv.Atoi(next())
	return n
}

func nextFloat() float64 {
	n, _ := strconv.ParseFloat(next(), 64)
	return n
}

func println(a ...interface{}) {
	fmt.Fprintln(writer, a...)
}

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

package main

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

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

type Candle struct {
	time int
	numbers int
}

//Wrong
func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	m, t, r := nextInt(), nextInt(), nextInt()
	w, remainCandles, total := 0, 0, 0
	candles := make([]Candle, 0, 10)

	if t < r-1 {
		println(-1)
		return
	}

	for ; m > 0; m-- {
		w = nextInt()
		remainCandles = 0
		// println(w)
		for i := 0; i < len(candles); i++ {
			// println(remainCandles, candles)
			if candles[i].time + candles[i].numbers + t < w {
				candles = candles[1:]
				i--
			} else {

				if w - t <= candles[i].time {
					remainCandles += candles[i].numbers
				} else {
					remainCandles = candles[i].numbers - (w - t - candles[i].time)
				}
			}

			// println(remainCandles, candles)
		}

		if remainCandles < r {
			nextCandles := r - remainCandles
			total += nextCandles

			candles = append(candles, Candle{w - nextCandles, nextCandles})
		}

		// println(total, "\n")
	}

	println(total)
}

func scan(a ...interface{}) {
	fmt.Fscan(reader, a...)
}

func next() string {
	if !scanner.Scan() {
		panic("Scan error")
	}
	return scanner.Text()
}

func nextInt() int {
	n, _ := strconv.Atoi(next())
	return n
}

func nextFloat() float64 {
	n, _ := strconv.ParseFloat(next(), 64)
	return n
}

func println(a ...interface{}) {
	fmt.Fprintln(writer, a...)
}

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

package main

import "fmt"

type Pair struct {
	pos int
	count int
}

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

func main() {
	var m, t, r int
	fmt.Scanf("%v %v %v", &m, &t, &r)
	ws := make([]Pair, m)
	for i := 0; i < m; i++ {
		var w int
		fmt.Scanf("%d", &w)
		ws[i] = Pair{w, 0}
	}
	if t < r {
		fmt.Printf("-1\n")
		return
	}
	total_count := 0
	for i := 0; i < m; i++ {
		if ws[i].count < r {
			delta := r - ws[i].count
			total_count += delta
			for j := i; j < m; j++ {
				if ws[j].pos >= ws[i].pos + t {
					break
				}
				ws[j].count += min(delta, ws[i].pos + t - ws[j].pos)
			}
		}
	}
  fmt.Printf("%v\n", total_count)
}

package main

import "fmt"

type Pair struct {
	pos int
	count int
}

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

func main() {
	var m, t, r int
	fmt.Scanf("%v %v %v", &m, &t, &r)
	ws := make([]Pair, m)
	for i := 0; i < m; i++ {
		var w int
		fmt.Scanf("%d", &w)
		ws[i] = Pair{w, 0}
	}
	if t < r {
		fmt.Printf("-1\n")
		return
	}
	total_count := 0
	for i := 0; i < m; i++ {
		if ws[i].count < r {
			delta := r - ws[i].count
			total_count += delta
			for j := i; j < m; j++ {
				if ws[j].pos >= ws[i].pos + t {
					break
				}
				ws[j].count += min(delta, ws[i].pos + t - ws[j].pos)
			}
		}
		fmt.Printf("%#v\n", ws)
	}
  fmt.Printf("%v\n", total_count)
}

package main

import (
	"fmt"
)

func main() {
	var res int
	q := make([]int, 0, 310)
	var M, T, R int
	fmt.Scan(&M, &T, &R)
	if R > T {
		fmt.Println(-1)
		return
	}
	for i := 0; i < M; i++ {
		var W int
		fmt.Scan(&W)
		for len(q) > 0 && q[0]+T < W {
			q = q[1:]
		}
		for j := W - (R - len(q)) + 1; j <= W; j++ {
			res++
			q = append(q, j)
		}
	}
	fmt.Println(res)
}


package main

import "fmt"

func main() {
	var n, k, t int
	fmt.Scan(&t)
	for ; t > 0; t-- {
		fmt.Scan(&n, &k)
		s := ""
		for i := 0; i < n; i++ {
			s += string('a' + i%k)
		}
		fmt.Println(s)
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)
func getInt(s *bufio.Scanner) int {
	(*s).Scan()
	n,_ := strconv.Atoi((*s).Text())
	return n
}

func buildString(n,a int)  string {
	alphabets := []byte("abcdefghijklmnopqrstuvwxyz")
	var s strings.Builder
	for i :=0 ; i < n ; i++  { s.WriteByte(alphabets[int(i%a)])}
	return s.String()
}

func main() {
	s := bufio.NewScanner(os.Stdin)
	s.Split(bufio.ScanWords)
	n := getInt(s)
	for i:=0; i<n; i++ {
		a,b := getInt(s), getInt(s)
		fmt.Println(buildString(a,b))
	}
}

package main

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

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

    var t, n, k int
    fmt.Fscan(stdin, &t)
    for t > 0 {
        t--
        fmt.Fscan(stdin, &n, &k)

        var x int = 0
        var res []byte = make([]byte, n)
        for i, _ := range(res) {
            res[i] = 97 + byte(x)
            x = (x + 1) % k
        }
        fmt.Println(string(res))
    }
}

package main

import (
	"fmt"
	"strings"
)

func main() {
	var tc int

	for fmt.Scan(&tc); tc > 0; tc-- {
		var n, k int
		fmt.Scan(&n, &k)
		var sb strings.Builder
		for n > 0 {
			sb.WriteString(string('a' + (n % k)))
			n--
		}
		fmt.Printf("%s\n", sb.String())
	}
}

package main

import "fmt"

func main() {
	var tc int
	fmt.Scan(&tc)

	for tc > 0 {
		var n, k int
		fmt.Scan(&n, &k)
		for n > 0 {
			fmt.Printf("%c", 'a' + n % k)
			n--
		}
		fmt.Println()
		tc--
	}
}

package main

import "fmt"

func main() {
	var tc int

	for fmt.Scan(&tc); tc > 0; tc-- {
		var n, k int
		fmt.Scan(&n, &k)
		for n > 0 {
			fmt.Printf("%c", 'a' + n % k)
			n--
		}
		fmt.Println()
	}
}

package main

import (
	"fmt"
	"math"
)

func main(){
	var T int
	fmt.Scan(&T)
	for {
		if T <= 0 {
			return
		}
		T--
		var n, k int
		fmt.Scan(&n, &k)
		clen := int(math.Floor(float64(1.0 * n / k)))
		for i := 0; i < clen; i++ {
			for j := 0; j < k; j++ {
				fmt.Print(string('a' + j))
			}
		}
		for i := 0; i < n - clen * k; i++ {
			fmt.Print(string('a' + i))
		}
		fmt.Println()
	}
}

package main

import "fmt"
var letters string = "abcdefghijklmnopqrstuvwxyz"

func main() {
	var request, length, limit int
	fmt.Scan(&request)

	for i := 0; i < request; i++ {
		fmt.Scan(&length, &limit)
		repeat := int(length / limit)

		for j := 0; j < repeat; j++ {
			for x := 0; x < limit; x++ {
				fmt.Printf("%s", string(letters[x]))
			}
		}

		remain := length - repeat * limit
		for x := 0; x < remain; x++ {
			fmt.Printf("%s", string(letters[x]))
		}

		fmt.Printf("\n")
	}
}

package main

import "fmt"

func main() {
	var n int
	fmt.Scanf("%d\n", &n)
	for i := 0; i < n; i++ {
		var a, b int
		fmt.Scanf("%d %d\n", &a, &b)
		cnt := 0
		for cnt < a {
			for j := 0; j < b; j++ {
				fmt.Printf("%c", 'a'+j)
				cnt++
				if cnt >= a {
					break
				}
			}
		}
		fmt.Printf("\n")
	}
}

package main

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

func main(){
	buf := bufio.NewReader(os.Stdin)
	var n,t,k int
	fmt.Fscanf(buf, "%d\n", &t)
	a := byte('a')
	for i:= 1;i <= t;i++{
		fmt.Fscanf(buf, "%d %d\n", &n,&k)
		var f int = 0
		for j := 0;j < n;j++{
			fmt.Printf("%s",string(a+byte(f)))
			f++
			if f >=k{
				f=0
			}
		}
		fmt.Printf("\n")
	}

}

package main

import "fmt"

func main(){

	var t,n,k int
	fmt.Scanf("%d\n",&t)
	for ;t!=0;t--{
		fmt.Scanf("%d%d\n",&n,&k)
		for i:=0;i<n;i++{
			fmt.Printf("%c",i%k + 'a')
		}
		fmt.Println()
	}
}

package main

import "fmt"

var n, k int

func main() {

	var T int;
	fmt.Scanf("%d\n", &T)
	// fmt.Print(T)
	for t := 0; t < T; t++{
		fmt.Scanf("%d%d\n", &n, &k)
		j := 0;
		for i := 0; i < n; i++ {
			fmt.Printf("%c", 'a' + j)
			j++;
			j %= k;
		}
		fmt.Println()
	}
}

package main
import "strings"
import "fmt"

func main(){
    var t,n,k int
    fmt.Scanf("%d\n", &t)
    for ; t > 0; t = t-1 {
        fmt.Scanf("%d %d\n", &n, &k)
        c := 0
        var s strings.Builder
        for i := 0; i < n; i++ {
            s.WriteString(string('a'+c)); c++
            if c == k {
                c = 0
            }
        }
        fmt.Printf("%s\n", s.String());
    }
}

package main

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

var scanner *bufio.Scanner

func getI64() int64 {
	scanner.Scan()
	x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
	return x
}
func getI() int {
	return int(getI64())
}
func getF() float64 {
	scanner.Scan()
	x, _ := strconv.ParseFloat(scanner.Text(), 64)
	return x
}
func getS() string {
	scanner.Scan()
	return scanner.Text()
}

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

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

const alphabets = "abcdefghijklmnopqrstuvwxyz"

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	Problem1092A()
}

func Problem1092A() {
	t := getI()
	for i:=0;i < t;i++ {
		n := getI()
		k := getI()
		for j := 0; j < n; j++ {
			fmt.Printf("%s", (fmt.Sprintf("%c", alphabets[j%k])))
		}
		fmt.Printf("\n")
	}
}

package main

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

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

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

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

		var answer string

		cnt := "a"
		for i := 0; i < n; i++ {
			if int(cnt[0]-"a"[0]) >= k {
				cnt = "a"
			}

			answer += cnt

			cnt = string(cnt[0] + 1)
		}

		fmt.Println(answer)
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)
func getInt(s *bufio.Scanner) int {
	(*s).Scan()
	n,_ := strconv.Atoi((*s).Text())
	return n
}

func buildString(n,a int)  string {
	alphabets := []byte("abcdefghijklmnopqrstuvwyz")
	var s strings.Builder
	for i :=0 ; i < n ; i++  { s.WriteByte(alphabets[int(i%a)])}
	return s.String()
}

func main() {
	s := bufio.NewScanner(os.Stdin)
	s.Split(bufio.ScanWords)
	n := getInt(s)
	for i:=0; i<n; i++ {
		a,b := getInt(s), getInt(s)
		fmt.Println(buildString(a,b))
	}
}

package main

import "fmt"

func main() {
	var tc int
	fmt.Scanf("%d", &tc)
	for tc > 0 {
		var n, k int
		fmt.Scanf("%d %d", &n, &k)
		for n > 0 {
			fmt.Printf("%c", 'a' + n % k)
			n--
		}
		fmt.Println()
		tc--
	}
}

package main

import "fmt"
var letters string = "abcdefghijklmnopqrstuvwxyz"

func main() {
	var request, length, limit int
	fmt.Scan(&request)

	for i := 0; i < request; i++ {
		fmt.Scan(&length, &limit)
		repeat := int(length / limit)

		for j := 0; j < repeat; j++ {
			for x := 0; x < limit; x++ {
				fmt.Printf("%s", string(letters[x]))
			}
		}

		if length % repeat != 0 {
			fmt.Printf("%s\n", "a")
		} else {
			fmt.Printf("\n")
		}
	}
}

package main

import "fmt"
var letters string = "abcdefghijklmnopqrstuvwxyz"

func main() {
	var request, length, limit int
	fmt.Scan(&request)

	for i := 0; i < request; i++ {
		fmt.Scan(&length, &limit)
		repeat := int(length / limit)

		for j := 0; j < repeat; j++ {
			for x := 0; x < limit; x++ {
				fmt.Printf("%s", string(letters[x]))
			}
		}

		if repeat * limit != length {
			fmt.Println("a")
		} else {
			fmt.Printf("\n")
		}
	}
}

package main

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

var scanner *bufio.Scanner

func getI64() int64 {
	scanner.Scan()
	x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
	return x
}
func getI() int {
	return int(getI64())
}
func getF() float64 {
	scanner.Scan()
	x, _ := strconv.ParseFloat(scanner.Text(), 64)
	return x
}
func getS() string {
	scanner.Scan()
	return scanner.Text()
}

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

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

const alphabets = "abcdefghijklmnopqrstuvwxyz"

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	Problem1092A()
}

func Problem1092A() {
	t := getI()
	for i:=0;i < t;i++ {
		n := getI()
		k := getI()
		for j := 0; j < n; j++ {
			print(fmt.Sprintf("%c", alphabets[j%k]))
		}
	}
}

package main

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

var scanner *bufio.Scanner

func getI64() int64 {
	scanner.Scan()
	x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
	return x
}
func getI() int {
	return int(getI64())
}
func getF() float64 {
	scanner.Scan()
	x, _ := strconv.ParseFloat(scanner.Text(), 64)
	return x
}
func getS() string {
	scanner.Scan()
	return scanner.Text()
}

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

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

const alphabets = "abcdefghijklmnopqrstuvwxyz"

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	Problem1092A()
}

func Problem1092A() {
	t := getI()
	sol := make([]string, t)
	for i:=0;i < t;i++ {
		n := getI()
		k := getI()
		str := ""
		for j := 0; j < n; j++ {
			for alpha := 0; alpha < k; alpha++ {
				if len(str) < n {
					str += fmt.Sprintf("%c", alphabets[alpha])
				}else {
					break
				}
			}
		}
		sol[i] = str
	}

	for _, item := range sol {
		println(item)
	}
}

package main

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

var scanner *bufio.Scanner

func getI64() int64 {
	scanner.Scan()
	x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
	return x
}
func getI() int {
	return int(getI64())
}
func getF() float64 {
	scanner.Scan()
	x, _ := strconv.ParseFloat(scanner.Text(), 64)
	return x
}
func getS() string {
	scanner.Scan()
	return scanner.Text()
}

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

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

const alphabets = "abcdefghijklmnopqrstuvwxyz"

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	Problem1092A()
}

func Problem1092A() {
	t := getI()
	for i:=0;i < t;i++ {
		n := getI()
		k := getI()
		for j := 0; j < n; j++ {
			fmt.Printf("%s", (fmt.Sprintf("%c", alphabets[j%k])))
		}
	}
}

package main

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

var scanner *bufio.Scanner

func getI64() int64 {
	scanner.Scan()
	x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
	return x
}
func getI() int {
	return int(getI64())
}
func getF() float64 {
	scanner.Scan()
	x, _ := strconv.ParseFloat(scanner.Text(), 64)
	return x
}
func getS() string {
	scanner.Scan()
	return scanner.Text()
}

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

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

const alphabets = "abcdefghijklmnopqrstuvwxyz"

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	Problem1092A()
}

func Problem1092A() {
	t := getI()
	for i:=0;i < t;i++ {
		n := getI()
		k := getI()
		for j := 0; j < n; j++ {
			fmt.Printf("%s \n", (fmt.Sprintf("%c", alphabets[j%k])))
		}
	}
}

package main

import "fmt"

func main() {
	var n int
	fmt.Scanf("%d\n", &n)
	for i := 0; i < n; i++ {
		var a, b int
		fmt.Scanf("%d %d\n", &a, &b)
		for j := 0; j < b; j++ {
			fmt.Printf("%c", 'a'+j)
		}
		for j := b; j < a; j++ {
			fmt.Printf("a")
		}
		fmt.Printf("\n")
	}
}

package main

import "fmt"

func main(){

	var t,n,k int
	fmt.Scan(&t)
	for ;t!=0;t--{
		fmt.Scanf("%d%d",&n,&k)
		for i:=0;i<n;i++{
			fmt.Printf("%c",i%k + 'a')
		}
		fmt.Println()
	}
}


package main

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

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

func printf(x string, a ...interface{}) (n int, err error) {
	return fmt.Fprintf(out, x, a...)
}

func scanf(x string, a ...interface{}) (n int, err error) {
	return fmt.Fscanf(in, x, a...)
}

var err error

func next() (n string) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return ""
		}
	}
}

func nextInt() (n int) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return 0
		}
	}
}

func nextLong() (n int64) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return 0
		}
	}
}

func nextDouble() (n float64) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return 0
		}
	}
}

func Fill(a []int, v int) {
	for i := range a {
		a[i] = v
	}
}

func FillB(a []bool, v bool) {
	for i := range a {
		a[i] = v
	}
}

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

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

const (
	mod = 1000000009
)

func mult(a, b int64) int64 {
	return (a * b) % mod
}
func add(a, b int64) int64 {
	return (a + b) % mod
}
func sub(a, b int64) int64 {
	return (a - b + mod) % mod
}

func solve() {
	n, d, m := nextInt(), nextInt(), nextLong()
	less, more := []int64{}, []int64{}
	for i := 0; i < n; i++ {
		x := nextLong()
		if x > m {
			more = append(more, x)
		} else {
			less = append(less, x)
		}
	}
	sort.Slice(less, func(a, b int) bool {
		return less[a] < less[b]
	})
	sort.Slice(more, func(a, b int) bool {
		return more[a] < more[b]
	})

	var res, best int64 = 0, 0
	for _, v := range less {
		res += v
	}

	best = res

	curr := 0
	for i := 1; i <= len(more); i++ {
		if (i-1)*(d+1) >= n {
			break
		}
		res += more[len(more)-i]
		ext := Min((i-1)*(d+1)+1-len(more), n)
		for ; curr < ext; curr++ {
			res -= less[curr]
		}
		if res > best {
			best = res
		}
	}
	printf("%d\n", best)
}

func main() {
	in = bufio.NewReaderSize(os.Stdin, 8192)
	out = bufio.NewWriterSize(os.Stdout, 8192)
	if false {
		file1, err := os.Open("input.txt")
		if err != nil {
			log.Fatal(err)
		}
		file2, err := os.Create("output.txt")
		if err != nil {
			log.Fatal(err)
		}
		in = bufio.NewReaderSize(file1, 8192)
		out = bufio.NewWriterSize(file2, 8192)
	}
	defer out.Flush()

	// start := time.Now()

	// for t := nextInt(); t > 0; t-- {
	solve()
	// }

	// printf("%v\n", time.Now().Sub(start))
}

package main

import (
	"bufio"
	"fmt"
	"hash/crc32"
	"io"
	"os"
	"sort"
)

func stringToHash(val string, max uint32) uint32 {
	ieee := crc32.NewIEEE()
	io.WriteString(ieee, fmt.Sprintf("%v", val))
	return ieee.Sum32() % max
}

type Int64Slice []int64

func (s Int64Slice) Len() int           { return len(s) }
func (s Int64Slice) Swap(i, j int)      { s[i], s[j] = s[j], s[i] }
func (s Int64Slice) Less(i, j int) bool { return s[i] > s[j] }

func splitAndSort(mat []int64, m int64) (minMat []int64, maxMat []int64) {
	//minMat := make([]int64,0)
	//maxMat := make([]int64,0)
	for _, item := range mat {
		if item > m {
			maxMat = append(maxMat, item)
		} else {
			minMat = append(minMat, item)
		}
	}
	sort.Sort(Int64Slice(minMat))
	sort.Sort(Int64Slice(maxMat))
	return minMat, maxMat
}
func minValue(a, b int) int {
	if a < b {
		return a
	}
	return b
}
func solve(mat []int64, m int64, n, d int) int64 {
	minMat, maxMat := splitAndSort(mat, m)
	minlen := len(minMat)

	sum := make([]int64, minlen)
	for index, item := range minMat {
		if index == 0 {
			sum[index] = item
		} else {
			sum[index] = sum[index-1] + item
		}
	}
	ans := int64(0)
	if minlen > 0 {
		ans = sum[minValue(n-1, minlen-1)]
	}
	maxSum := int64(0)
	weiCnt := 0
	for index, item := range maxMat {
		if index == 0 {
			weiCnt = 1
		} else {
			weiCnt = weiCnt + d + 1
		}
		if weiCnt > n {
			break
		}
		maxSum = maxSum + item
		tmpAns := maxSum
		if n-weiCnt > 0 && minlen > 0{
			tmpAns = maxSum + sum[minValue(minlen-1, n-weiCnt-1)]
		}
		if tmpAns > ans {
			ans = tmpAns
		}
	}
	return ans

}
func main() {
	in := bufio.NewReader(os.Stdin)
	//out := bufio.NewWriter(os.Stdout)
	var n, d int
	var m int64
	fmt.Fscanf(in, "%d %d %d\n", &n, &d, &m)
	mat := make([]int64, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(in, "%d", &mat[i])
	}
	fmt.Printf("%v",solve(mat, m, n, d))
	//fmt.Fprintln(out, solve(mat, m, n, d))
	//mat := []int64{8,10,15,23,5}
	//fmt.Printf("%v\n",solve(mat,11,5,2))
	//
	//
	//mat = []int64{20, 5, 8, 2, 18, 16, 2, 16, 16, 1, 5, 16, 2, 13, 6, 16, 4, 17, 21, 7}
	//fmt.Printf("%v\n",solve(mat,16,20,2))

}

/*
URL:
https://codeforces.com/contest/1395/problem/D
*/

package main

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

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

var (
	// n, d, m    int
	n, d       int
	m          int64
	A          []int64
	big, small []int64
)

func main() {
	// n, d, m = ReadInt3()
	n, d = ReadInt2()
	m = ReadInt64()
	A = ReadInt64Slice(n)

	for _, a := range A {
		// if a > int64(m) {
		if a > m {
			big = append(big, a)
		} else {
			small = append(small, a)
		}
	}

	// bigは降順, smallは昇順
	sort.Slice(big, func(i, j int) bool { return big[i] > big[j] })
	sort.Slice(small, func(i, j int) bool { return small[i] < small[j] })

	bs := make([]int64, len(big)+1)
	for i := 0; i < len(big); i++ {
		bs[i+1] = bs[i] + big[i]
	}
	ss := make([]int64, len(small)+1)
	for i := 0; i < len(small); i++ {
		ss[i+1] = ss[i] + small[i]
	}

	var ans int64
	smallAll := Sum(small...)
	if len(big) == 0 {
		ans = smallAll
	} else {
		ans = 0
	}
	for i := 1; i <= len(big); i++ {
		cur := bs[i]
		// mini, maxi := d*(i-1), d*i
		mini, maxi := int(Min(int64(n), int64(d)*int64(i-1))), int(Min(int64(n), int64(d)*int64(i)))
		resBig := len(big) - i

		if resBig > maxi {
			continue
		}
		if mini <= resBig && resBig <= maxi {
			cur += smallAll
			ChMax(&ans, cur)
			continue
		}

		nokori := mini - resBig
		if nokori > len(small) {
			continue
		}
		cur += smallAll - ss[nokori]
		ChMax(&ans, cur)
	}
	fmt.Println(ans)
}

// Min returns the min integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Min(integers ...int64) int64 {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m > integer {
			m = integer
		}
	}
	return m
}

// ChMax accepts a pointer of integer and a target value.
// If target value is LARGER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMax(updatedValue *int64, target int64) bool {
	if *updatedValue < target {
		*updatedValue = target
		return true
	}
	return false
}

// Sum returns multiple integers sum.
func Sum(integers ...int64) int64 {
	var s int64
	s = 0

	for _, i := range integers {
		s += i
	}

	return s
}

const (
	// General purpose
	MOD = 1000000000 + 7
	// MOD          = 998244353
	ALPHABET_NUM = 26
	INF_INT64    = math.MaxInt64
	INF_BIT60    = 1 << 60
	INF_INT32    = math.MaxInt32
	INF_BIT30    = 1 << 30
	NIL          = -1

	// for dijkstra, prim, and so on
	WHITE = 0
	GRAY  = 1
	BLACK = 2
)

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

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

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

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

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

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

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

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

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

var (
	// ReadString returns a WORD string.
	ReadString func() string
	stdout     *bufio.Writer
)

func newReadString(ior io.Reader, sf bufio.SplitFunc) func() string {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	r.Split(sf)

	return func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
}

// ReadInt returns an integer.
func ReadInt() int {
	return int(readInt64())
}
func ReadInt2() (int, int) {
	return int(readInt64()), int(readInt64())
}
func ReadInt3() (int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64())
}
func ReadInt4() (int, int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64()), int(readInt64())
}

// ReadInt64 returns as integer as int64.
func ReadInt64() int64 {
	return readInt64()
}
func ReadInt64_2() (int64, int64) {
	return readInt64(), readInt64()
}
func ReadInt64_3() (int64, int64, int64) {
	return readInt64(), readInt64(), readInt64()
}
func ReadInt64_4() (int64, int64, int64, int64) {
	return readInt64(), readInt64(), readInt64(), readInt64()
}

func readInt64() int64 {
	i, err := strconv.ParseInt(ReadString(), 0, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

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

// ReadInt64Slice returns as int64 slice that has n integers.
func ReadInt64Slice(n int) []int64 {
	b := make([]int64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt64()
	}
	return b
}

// ReadFloat64 returns an float64.
func ReadFloat64() float64 {
	return float64(readFloat64())
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(ReadString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// ReadFloatSlice returns an float64 slice that has n float64.
func ReadFloat64Slice(n int) []float64 {
	b := make([]float64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadFloat64()
	}
	return b
}

// ReadRuneSlice returns a rune slice.
func ReadRuneSlice() []rune {
	return []rune(ReadString())
}

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

// PrintIntsLine returns integers string delimited by a space.
func PrintIntsLine(A ...int) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.Itoa(A[i])
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintIntsLine returns integers string delimited by a space.
func PrintInts64Line(A ...int64) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.FormatInt(A[i], 10) // 64bit int version
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintfBufStdout is function for output strings to buffered os.Stdout.
// You may have to call stdout.Flush() finally.
func PrintfBufStdout(format string, a ...interface{}) {
	fmt.Fprintf(stdout, format, a...)
}

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

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

// ZeroPaddingRuneSlice returns binary expressions of integer n with zero padding.
// For debugging use.
func ZeroPaddingRuneSlice(n, digitsNum int) []rune {
	sn := fmt.Sprintf("%b", n)

	residualLength := digitsNum - len(sn)
	if residualLength <= 0 {
		return []rune(sn)
	}

	zeros := make([]rune, residualLength)
	for i := 0; i < len(zeros); i++ {
		zeros[i] = '0'
	}

	res := []rune{}
	res = append(res, zeros...)
	res = append(res, []rune(sn)...)

	return res
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	n, d, m := readThreeNums(reader)
	A := readNNums(reader, n)
	fmt.Println(solve(n, d, m, A))
}

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

const N_INF = -(int64(1) << 60)

func solve(n int, d int, m int, A []int) int64 {
	X := make([]int, 0, n)
	Y := make([]int, 0, n)

	for i := 0; i < n; i++ {
		if A[i] > m {
			X = append(X, A[i])
		} else {
			Y = append(Y, A[i])
		}
	}

	sort.Ints(X)
	sort.Ints(Y)

	reverse(X)
	reverse(Y)

	XX := sum(X)
	YY := sum(Y)

	if len(X) == 0 {
		return YY[n]
	}

	l := len(YY)
	for j := l; j <= n; j++ {
		YY = append(YY, YY[l-1])
	}

	k := len(X)
	var res int64
	N := int64(n)
	for i := (k + d) / (d + 1); i <= k; i++ {
		if int64(i-1)*int64(d+1)+1 <= N {
			j := n - (i-1)*(d+1) - 1
			tmp := XX[i] + YY[j]
			if tmp > res {
				res = tmp
			}
		}
	}
	return res
}

func sum(arr []int) []int64 {
	n := len(arr)
	res := make([]int64, n+1)
	for i := 0; i < n; i++ {
		res[i+1] = res[i] + int64(arr[i])
	}
	return res
}

type Pair struct {
	first  int
	second int64
}

func reverse(arr []int) {
	for i, j := 0, len(arr)-1; i < j; i, j = i+1, j-1 {
		arr[i], arr[j] = arr[j], arr[i]
	}
}

/*
URL:
https://codeforces.com/contest/1395/problem/D
*/

package main

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

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

var (
	// n, d, m    int
	n, d       int
	m          int64
	A          []int64
	big, small []int64
)

func main() {
	// n, d, m = ReadInt3()
	n, d = ReadInt2()
	m = ReadInt64()
	A = ReadInt64Slice(n)

	for _, a := range A {
		// if a > int64(m) {
		if a > m {
			big = append(big, a)
		} else {
			small = append(small, a)
		}
	}

	// bigは降順, smallは昇順
	sort.Slice(big, func(i, j int) bool { return big[i] > big[j] })
	sort.Slice(small, func(i, j int) bool { return small[i] < small[j] })

	bs := make([]int64, len(big)+1)
	for i := 0; i < len(big); i++ {
		bs[i+1] = bs[i] + big[i]
	}
	ss := make([]int64, len(small)+1)
	for i := 0; i < len(small); i++ {
		ss[i+1] = ss[i] + small[i]
	}

	smallAll := Sum(small...)
	ans := smallAll
	for i := 1; i <= len(big); i++ {
		cur := bs[i]
		mini, maxi := d*(i-1), d*i
		resBig := len(big) - i

		if resBig > maxi {
			continue
		}
		if mini <= resBig && resBig <= maxi {
			cur += smallAll
			ChMax(&ans, cur)
			continue
		}

		nokori := mini - resBig
		if nokori > len(small) {
			continue
		}
		cur += smallAll - ss[nokori]
		ChMax(&ans, cur)
	}
	fmt.Println(ans)
}

// ChMax accepts a pointer of integer and a target value.
// If target value is LARGER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMax(updatedValue *int64, target int64) bool {
	if *updatedValue < target {
		*updatedValue = target
		return true
	}
	return false
}

// Sum returns multiple integers sum.
func Sum(integers ...int64) int64 {
	var s int64
	s = 0

	for _, i := range integers {
		s += i
	}

	return s
}

const (
	// General purpose
	MOD = 1000000000 + 7
	// MOD          = 998244353
	ALPHABET_NUM = 26
	INF_INT64    = math.MaxInt64
	INF_BIT60    = 1 << 60
	INF_INT32    = math.MaxInt32
	INF_BIT30    = 1 << 30
	NIL          = -1

	// for dijkstra, prim, and so on
	WHITE = 0
	GRAY  = 1
	BLACK = 2
)

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

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

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

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

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

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

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

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

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

var (
	// ReadString returns a WORD string.
	ReadString func() string
	stdout     *bufio.Writer
)

func newReadString(ior io.Reader, sf bufio.SplitFunc) func() string {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	r.Split(sf)

	return func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
}

// ReadInt returns an integer.
func ReadInt() int {
	return int(readInt64())
}
func ReadInt2() (int, int) {
	return int(readInt64()), int(readInt64())
}
func ReadInt3() (int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64())
}
func ReadInt4() (int, int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64()), int(readInt64())
}

// ReadInt64 returns as integer as int64.
func ReadInt64() int64 {
	return readInt64()
}
func ReadInt64_2() (int64, int64) {
	return readInt64(), readInt64()
}
func ReadInt64_3() (int64, int64, int64) {
	return readInt64(), readInt64(), readInt64()
}
func ReadInt64_4() (int64, int64, int64, int64) {
	return readInt64(), readInt64(), readInt64(), readInt64()
}

func readInt64() int64 {
	i, err := strconv.ParseInt(ReadString(), 0, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

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

// ReadInt64Slice returns as int64 slice that has n integers.
func ReadInt64Slice(n int) []int64 {
	b := make([]int64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt64()
	}
	return b
}

// ReadFloat64 returns an float64.
func ReadFloat64() float64 {
	return float64(readFloat64())
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(ReadString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// ReadFloatSlice returns an float64 slice that has n float64.
func ReadFloat64Slice(n int) []float64 {
	b := make([]float64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadFloat64()
	}
	return b
}

// ReadRuneSlice returns a rune slice.
func ReadRuneSlice() []rune {
	return []rune(ReadString())
}

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

// PrintIntsLine returns integers string delimited by a space.
func PrintIntsLine(A ...int) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.Itoa(A[i])
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintIntsLine returns integers string delimited by a space.
func PrintInts64Line(A ...int64) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.FormatInt(A[i], 10) // 64bit int version
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintfBufStdout is function for output strings to buffered os.Stdout.
// You may have to call stdout.Flush() finally.
func PrintfBufStdout(format string, a ...interface{}) {
	fmt.Fprintf(stdout, format, a...)
}

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

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

// ZeroPaddingRuneSlice returns binary expressions of integer n with zero padding.
// For debugging use.
func ZeroPaddingRuneSlice(n, digitsNum int) []rune {
	sn := fmt.Sprintf("%b", n)

	residualLength := digitsNum - len(sn)
	if residualLength <= 0 {
		return []rune(sn)
	}

	zeros := make([]rune, residualLength)
	for i := 0; i < len(zeros); i++ {
		zeros[i] = '0'
	}

	res := []rune{}
	res = append(res, zeros...)
	res = append(res, []rune(sn)...)

	return res
}

/*
URL:
https://codeforces.com/contest/1395/problem/D
*/

package main

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

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

var (
	// n, d, m    int
	n, d       int
	m          int64
	A          []int64
	big, small []int64
)

func main() {
	// n, d, m = ReadInt3()
	n, d = ReadInt2()
	m = ReadInt64()
	A = ReadInt64Slice(n)

	for _, a := range A {
		// if a > int64(m) {
		if a > m {
			big = append(big, a)
		} else {
			small = append(small, a)
		}
	}

	// bigは降順, smallは昇順
	sort.Slice(big, func(i, j int) bool { return big[i] > big[j] })
	sort.Slice(small, func(i, j int) bool { return small[i] < small[j] })

	bs := make([]int64, len(big)+1)
	for i := 0; i < len(big); i++ {
		bs[i+1] = bs[i] + big[i]
	}
	ss := make([]int64, len(small)+1)
	for i := 0; i < len(small); i++ {
		ss[i+1] = ss[i] + small[i]
	}

	var ans int64
	smallAll := Sum(small...)
	if len(big) == 0 {
		ans = smallAll
	} else {
		ans = 0
	}
	for i := 1; i <= len(big); i++ {
		cur := bs[i]
		mini, maxi := d*(i-1), d*i
		resBig := len(big) - i

		if resBig > maxi {
			continue
		}
		if mini <= resBig && resBig <= maxi {
			cur += smallAll
			ChMax(&ans, cur)
			continue
		}

		nokori := mini - resBig
		if nokori > len(small) {
			continue
		}
		cur += smallAll - ss[nokori]
		ChMax(&ans, cur)
	}
	fmt.Println(ans)
}

// ChMax accepts a pointer of integer and a target value.
// If target value is LARGER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMax(updatedValue *int64, target int64) bool {
	if *updatedValue < target {
		*updatedValue = target
		return true
	}
	return false
}

// Sum returns multiple integers sum.
func Sum(integers ...int64) int64 {
	var s int64
	s = 0

	for _, i := range integers {
		s += i
	}

	return s
}

const (
	// General purpose
	MOD = 1000000000 + 7
	// MOD          = 998244353
	ALPHABET_NUM = 26
	INF_INT64    = math.MaxInt64
	INF_BIT60    = 1 << 60
	INF_INT32    = math.MaxInt32
	INF_BIT30    = 1 << 30
	NIL          = -1

	// for dijkstra, prim, and so on
	WHITE = 0
	GRAY  = 1
	BLACK = 2
)

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

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

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

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

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

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

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

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

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

var (
	// ReadString returns a WORD string.
	ReadString func() string
	stdout     *bufio.Writer
)

func newReadString(ior io.Reader, sf bufio.SplitFunc) func() string {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	r.Split(sf)

	return func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
}

// ReadInt returns an integer.
func ReadInt() int {
	return int(readInt64())
}
func ReadInt2() (int, int) {
	return int(readInt64()), int(readInt64())
}
func ReadInt3() (int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64())
}
func ReadInt4() (int, int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64()), int(readInt64())
}

// ReadInt64 returns as integer as int64.
func ReadInt64() int64 {
	return readInt64()
}
func ReadInt64_2() (int64, int64) {
	return readInt64(), readInt64()
}
func ReadInt64_3() (int64, int64, int64) {
	return readInt64(), readInt64(), readInt64()
}
func ReadInt64_4() (int64, int64, int64, int64) {
	return readInt64(), readInt64(), readInt64(), readInt64()
}

func readInt64() int64 {
	i, err := strconv.ParseInt(ReadString(), 0, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

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

// ReadInt64Slice returns as int64 slice that has n integers.
func ReadInt64Slice(n int) []int64 {
	b := make([]int64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt64()
	}
	return b
}

// ReadFloat64 returns an float64.
func ReadFloat64() float64 {
	return float64(readFloat64())
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(ReadString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// ReadFloatSlice returns an float64 slice that has n float64.
func ReadFloat64Slice(n int) []float64 {
	b := make([]float64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadFloat64()
	}
	return b
}

// ReadRuneSlice returns a rune slice.
func ReadRuneSlice() []rune {
	return []rune(ReadString())
}

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

// PrintIntsLine returns integers string delimited by a space.
func PrintIntsLine(A ...int) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.Itoa(A[i])
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintIntsLine returns integers string delimited by a space.
func PrintInts64Line(A ...int64) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.FormatInt(A[i], 10) // 64bit int version
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintfBufStdout is function for output strings to buffered os.Stdout.
// You may have to call stdout.Flush() finally.
func PrintfBufStdout(format string, a ...interface{}) {
	fmt.Fprintf(stdout, format, a...)
}

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

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

// ZeroPaddingRuneSlice returns binary expressions of integer n with zero padding.
// For debugging use.
func ZeroPaddingRuneSlice(n, digitsNum int) []rune {
	sn := fmt.Sprintf("%b", n)

	residualLength := digitsNum - len(sn)
	if residualLength <= 0 {
		return []rune(sn)
	}

	zeros := make([]rune, residualLength)
	for i := 0; i < len(zeros); i++ {
		zeros[i] = '0'
	}

	res := []rune{}
	res = append(res, zeros...)
	res = append(res, []rune(sn)...)

	return res
}

/*
URL:
https://codeforces.com/contest/1395/problem/D
*/

package main

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

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

var (
	n, d, m    int
	A          []int64
	big, small []int64
)

func main() {
	n, d, m = ReadInt3()
	A = ReadInt64Slice(n)

	for _, a := range A {
		if a > int64(m) {
			big = append(big, a)
		} else {
			small = append(small, a)
		}
	}

	// bigは降順, smallは昇順
	sort.Slice(big, func(i, j int) bool { return big[i] > big[j] })
	sort.Slice(small, func(i, j int) bool { return small[i] < small[j] })

	bs := make([]int64, len(big)+1)
	for i := 0; i < len(big); i++ {
		bs[i+1] = bs[i] + big[i]
	}
	ss := make([]int64, len(small)+1)
	for i := 0; i < len(small); i++ {
		ss[i+1] = ss[i] + small[i]
	}

	smallAll := Sum(small...)
	ans := smallAll
	for i := 1; i <= len(big); i++ {
		cur := bs[i]
		mini, maxi := d*(i-1), d*i
		resBig := len(big) - i

		if resBig > maxi {
			continue
		}
		if mini <= resBig && resBig <= maxi {
			cur += smallAll
			ChMax(&ans, cur)
			continue
		}

		nokori := mini - resBig
		if nokori > len(small) {
			continue
		}
		cur += smallAll - ss[nokori]
		ChMax(&ans, cur)
	}
	fmt.Println(ans)
}

// ChMax accepts a pointer of integer and a target value.
// If target value is LARGER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMax(updatedValue *int64, target int64) bool {
	if *updatedValue < target {
		*updatedValue = target
		return true
	}
	return false
}

// Sum returns multiple integers sum.
func Sum(integers ...int64) int64 {
	var s int64
	s = 0

	for _, i := range integers {
		s += i
	}

	return s
}

const (
	// General purpose
	MOD = 1000000000 + 7
	// MOD          = 998244353
	ALPHABET_NUM = 26
	INF_INT64    = math.MaxInt64
	INF_BIT60    = 1 << 60
	INF_INT32    = math.MaxInt32
	INF_BIT30    = 1 << 30
	NIL          = -1

	// for dijkstra, prim, and so on
	WHITE = 0
	GRAY  = 1
	BLACK = 2
)

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

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

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

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

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

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

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

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

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

var (
	// ReadString returns a WORD string.
	ReadString func() string
	stdout     *bufio.Writer
)

func newReadString(ior io.Reader, sf bufio.SplitFunc) func() string {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	r.Split(sf)

	return func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
}

// ReadInt returns an integer.
func ReadInt() int {
	return int(readInt64())
}
func ReadInt2() (int, int) {
	return int(readInt64()), int(readInt64())
}
func ReadInt3() (int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64())
}
func ReadInt4() (int, int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64()), int(readInt64())
}

// ReadInt64 returns as integer as int64.
func ReadInt64() int64 {
	return readInt64()
}
func ReadInt64_2() (int64, int64) {
	return readInt64(), readInt64()
}
func ReadInt64_3() (int64, int64, int64) {
	return readInt64(), readInt64(), readInt64()
}
func ReadInt64_4() (int64, int64, int64, int64) {
	return readInt64(), readInt64(), readInt64(), readInt64()
}

func readInt64() int64 {
	i, err := strconv.ParseInt(ReadString(), 0, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

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

// ReadInt64Slice returns as int64 slice that has n integers.
func ReadInt64Slice(n int) []int64 {
	b := make([]int64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt64()
	}
	return b
}

// ReadFloat64 returns an float64.
func ReadFloat64() float64 {
	return float64(readFloat64())
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(ReadString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// ReadFloatSlice returns an float64 slice that has n float64.
func ReadFloat64Slice(n int) []float64 {
	b := make([]float64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadFloat64()
	}
	return b
}

// ReadRuneSlice returns a rune slice.
func ReadRuneSlice() []rune {
	return []rune(ReadString())
}

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

// PrintIntsLine returns integers string delimited by a space.
func PrintIntsLine(A ...int) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.Itoa(A[i])
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintIntsLine returns integers string delimited by a space.
func PrintInts64Line(A ...int64) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.FormatInt(A[i], 10) // 64bit int version
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintfBufStdout is function for output strings to buffered os.Stdout.
// You may have to call stdout.Flush() finally.
func PrintfBufStdout(format string, a ...interface{}) {
	fmt.Fprintf(stdout, format, a...)
}

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

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

// ZeroPaddingRuneSlice returns binary expressions of integer n with zero padding.
// For debugging use.
func ZeroPaddingRuneSlice(n, digitsNum int) []rune {
	sn := fmt.Sprintf("%b", n)

	residualLength := digitsNum - len(sn)
	if residualLength <= 0 {
		return []rune(sn)
	}

	zeros := make([]rune, residualLength)
	for i := 0; i < len(zeros); i++ {
		zeros[i] = '0'
	}

	res := []rune{}
	res = append(res, zeros...)
	res = append(res, []rune(sn)...)

	return res
}

package main

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

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

func printf(x string, a ...interface{}) (n int, err error) {
	return fmt.Fprintf(out, x, a...)
}

func scanf(x string, a ...interface{}) (n int, err error) {
	return fmt.Fscanf(in, x, a...)
}

var err error

func next() (n string) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return ""
		}
	}
}

func nextInt() (n int) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return 0
		}
	}
}

func nextLong() (n int64) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return 0
		}
	}
}

func nextDouble() (n float64) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return 0
		}
	}
}

func Fill(a []int, v int) {
	for i := range a {
		a[i] = v
	}
}

func FillB(a []bool, v bool) {
	for i := range a {
		a[i] = v
	}
}

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

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

const (
	mod = 1000000009
)

func mult(a, b int64) int64 {
	return (a * b) % mod
}
func add(a, b int64) int64 {
	return (a + b) % mod
}
func sub(a, b int64) int64 {
	return (a - b + mod) % mod
}

func solve() {
	n, d, m := nextInt(), nextInt(), nextLong()
	less, more := []int64{}, []int64{}
	for i := 0; i < n; i++ {
		x := nextLong()
		if x > m {
			more = append(more, x)
		} else {
			less = append(less, x)
		}
	}
	sort.Slice(less, func(a, b int) bool {
		return less[a] < less[b]
	})
	sort.Slice(more, func(a, b int) bool {
		return more[a] < more[b]
	})

	var res, best int64 = 0, 0
	for _, v := range less {
		res += v
	}

	curr := 0
	for i := 1; i <= len(more); i++ {
		if (i-1)*(d+1) >= n {
			break
		}
		res += more[len(more)-i]
		ext := Min((i-1)*(d+1)+1-len(more), n)
		for ; curr < ext; curr++ {
			res -= less[curr]
		}
		if res > best {
			best = res
		}
	}
	printf("%d\n", best)
}

func main() {
	in = bufio.NewReaderSize(os.Stdin, 8192)
	out = bufio.NewWriterSize(os.Stdout, 8192)
	if false {
		file1, err := os.Open("input.txt")
		if err != nil {
			log.Fatal(err)
		}
		file2, err := os.Create("output.txt")
		if err != nil {
			log.Fatal(err)
		}
		in = bufio.NewReaderSize(file1, 8192)
		out = bufio.NewWriterSize(file2, 8192)
	}
	defer out.Flush()

	// start := time.Now()

	// for t := nextInt(); t > 0; t-- {
	solve()
	// }

	// printf("%v\n", time.Now().Sub(start))
}

package main

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

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

func printf(x string, a ...interface{}) (n int, err error) {
	return fmt.Fprintf(out, x, a...)
}

func scanf(x string, a ...interface{}) (n int, err error) {
	return fmt.Fscanf(in, x, a...)
}

var err error

func next() (n string) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return ""
		}
	}
}

func nextInt() (n int) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return 0
		}
	}
}

func nextLong() (n int64) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return 0
		}
	}
}

func nextDouble() (n float64) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return 0
		}
	}
}

func Fill(a []int, v int) {
	for i := range a {
		a[i] = v
	}
}

func FillB(a []bool, v bool) {
	for i := range a {
		a[i] = v
	}
}

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

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

const (
	mod = 1000000009
)

func mult(a, b int64) int64 {
	return (a * b) % mod
}
func add(a, b int64) int64 {
	return (a + b) % mod
}
func sub(a, b int64) int64 {
	return (a - b + mod) % mod
}

func solve() {
	n, d, m := nextInt(), nextInt(), nextLong()
	less, more := []int64{}, []int64{}
	for i := 0; i < n; i++ {
		x := nextLong()
		if x > m {
			more = append(more, x)
		} else {
			less = append(less, x)
		}
	}
	sort.Slice(less, func(a, b int) bool {
		return less[a] < less[b]
	})
	sort.Slice(more, func(a, b int) bool {
		return more[a] < more[b]
	})

	var res, best int64 = 0, 0
	for _, v := range less {
		res += v
	}

	res = best

	curr := 0
	for i := 1; i <= len(more); i++ {
		if (i-1)*(d+1) >= n {
			break
		}
		res += more[len(more)-i]
		ext := Min((i-1)*(d+1)+1-len(more), n)
		for ; curr < ext; curr++ {
			res -= less[curr]
		}
		if res > best {
			best = res
		}
	}
	printf("%d\n", best)
}

func main() {
	in = bufio.NewReaderSize(os.Stdin, 8192)
	out = bufio.NewWriterSize(os.Stdout, 8192)
	if false {
		file1, err := os.Open("input.txt")
		if err != nil {
			log.Fatal(err)
		}
		file2, err := os.Create("output.txt")
		if err != nil {
			log.Fatal(err)
		}
		in = bufio.NewReaderSize(file1, 8192)
		out = bufio.NewWriterSize(file2, 8192)
	}
	defer out.Flush()

	// start := time.Now()

	// for t := nextInt(); t > 0; t-- {
	solve()
	// }

	// printf("%v\n", time.Now().Sub(start))
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	n, d, m := readThreeNums(reader)
	A := readNNums(reader, n)
	fmt.Println(solve(n, d, m, A))
}

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

const N_INF = -(int64(1) << 60)

func solve(n int, d int, m int, A []int) int64 {
	X := make([]int, 0, n)
	Y := make([]int, 0, n)

	for i := 0; i < n; i++ {
		if A[i] > m {
			X = append(X, A[i])
		} else {
			Y = append(Y, A[i])
		}
	}

	sort.Ints(X)
	sort.Ints(Y)

	reverse(X)
	reverse(Y)

	XX := sum(X)
	YY := sum(Y)

	if len(X) == 0 {
		return YY[n]
	}

	l := len(YY)
	for j := l; j <= n; j++ {
		YY = append(YY, YY[l-1])
	}

	k := len(X)
	var res int64

	for i := (k + d) / (d + 1); i <= k; i++ {
		if (i-1)*(d+1)+1 <= n && (i-1)*(d+1) >= 0 {
			j := n - (i-1)*(d+1) - 1
			tmp := XX[i] + YY[j]
			if tmp > res {
				res = tmp
			}
		}
	}
	return res
}

func sum(arr []int) []int64 {
	n := len(arr)
	res := make([]int64, n+1)
	for i := 0; i < n; i++ {
		res[i+1] = res[i] + int64(arr[i])
	}
	return res
}

type Pair struct {
	first  int
	second int64
}

func reverse(arr []int) {
	for i, j := 0, len(arr)-1; i < j; i, j = i+1, j-1 {
		arr[i], arr[j] = arr[j], arr[i]
	}
}


package main

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

func I(i ...interface{}) error {
	_, err := fmt.Fscan(r, i...)
	return err
}

func O(o ...interface{}) {
	fmt.Fprint(w, o...)
}

var (
	r *bufio.Reader
	w *bufio.Writer
)

func init() {
	if f, err := os.Open("in.goC"); err == nil {
		r = bufio.NewReader(f)
	} else {
		r = bufio.NewReader(os.Stdin)
	}
	w = bufio.NewWriter(os.Stdout)
}

var n, k, oo int
var a [][]int

func main() {
	for I(&n, &k) == nil {
		solve()
	}
	w.Flush()
}

func solve() {
	if k > n {
		if k > n*n {
			O("-1\n")
			return
		}
	}
	a = make([][]int, n)
	for i := range a {
		a[i] = make([]int, n)
	}
	for i := range a {
		for j := i; j < n; j++ {
			if i != j && k >= 2 {
				k -= 2
				a[i][j] = 1
				a[j][i] = 1
			}
			if i == j && k >= 1 {
				k--
				a[i][j] = 1
			}
		}
	}
	for i := range a {
		for j := 0; j < n; j++ {
			O(a[i][j])
			if j != n-1 {
				O(" ")
			}
		}
		O("\n")
	}
}

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())
	scanner.Scan()
	k, _ := strconv.Atoi(scanner.Text())
	if n*n < k {
		fmt.Println(-1)
		os.Exit(0)
	}
	m := make([][]int, n)
	for i := 0; i < n; i++ {
		m[i] = make([]int, n)
	}
	for i := 0; i < n; i++ {
		if k == 0 {
			break
		}
		k--
		m[i][i] = 1
		j := i + 1
		for k >= 2 && j < n {
			m[i][j] = 1
			m[j][i] = 1
			k -= 2
			j++
		}
	}

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

package main

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

func I(i ...interface{}) error {
	_, err := fmt.Fscan(r, i...)
	return err
}

func O(o ...interface{}) {
	fmt.Fprint(w, o...)
}

var (
	r *bufio.Reader
	w *bufio.Writer
)

func init() {
	if f, err := os.Open("in.goC"); err == nil {
		r = bufio.NewReader(f)
	} else {
		r = bufio.NewReader(os.Stdin)
	}
	w = bufio.NewWriter(os.Stdout)
}

var n, k, oo int
var a [][]int

func main() {
	for I(&n, &k) == nil {
		solve()
	}
	w.Flush()
}

func solve() {
	if k > n {
		if k > n*n || (k-n)%2 == 1 {
			O("-1\n")
			return
		}
	}
	a = make([][]int, n)
	for i := range a {
		a[i] = make([]int, n)
	}
	oo = 1
	do(0, 0, 0)
	for i := range a {
		for j := 0; j < n; j++ {
			O(a[i][j])
			if j != n-1 {
				O(" ")
			}
		}
		O("\n")
	}
}

func do(i, j, d int) {
	if i >= n || j >= n {
		return
	}
	if d == 0 {
		for i < n && j < n && k > 0 {
			a[i][j] = 1
			k--
			i++
			j++
		}
		if k > 0 {
			do(0, oo, 1)
		}
	} else if d == 1 {
		do(i, j, -1)
		do(i+oo, j-oo, 2)
	} else if d == 2 {
		do(i, j, -1)
		do(i, j, 3)
	} else if d == 3 {
		if j >= n-1 || i >= n-1 {
			oo++
			do(0, oo, 1)
		} else {
			do(i-oo+1, j+oo+1, 1)
		}
	} else if d == -1 {
		if k > 0 {
			k--
			a[i][j] = 1
		}
	}
}

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())
	scanner.Scan()
	k, _ := strconv.Atoi(scanner.Text())
	if n*n < k {
		fmt.Println(-1)
		os.Exit(0)
	}
	m := make([][]int, n)
	for i := 0; i < n; i++ {
		m[i] = make([]int, n)
	}
Loop:
	for i := 0; i < n; i++ {
		if k == 0 {
			break Loop
		}
		k--
		m[i][i] = 1
		j := i
		for k > 1 && j < n {
			m[i][j] = 1
			m[j][i] = 1
			k -= 2
			j++
		}
	}

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


package main

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

func scanInt(scanner *bufio.Scanner) int64 {
	scanner.Scan()
	x, _ := strconv.Atoi(scanner.Text())
	return int64(x)
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	n := scanInt(scanner)
	sum := int64(0)
	for i := int64(0); i < n; i++ {
		sum += scanInt(scanner)
	}
	result := n-1
	if sum%n == 0 {
		result++
	}
	writer.WriteString(fmt.Sprintf("%d\n", result))
}

// 246B-mic
package main

import (
    "fmt"
)

func main() {
    var n, sum int
    var a [100005]int
    fmt.Scan(&n)
    if n == 1 {
        fmt.Println("1")
        return
    }
    for i := 1; i <= n; i++ {
        fmt.Scan(&a[i])
        sum += a[i]
    }
    if sum%n != 0 {
        fmt.Println(n - 1)
        return
    }
    sum /= n
    for i := 1; i < n; i++ {
        a[i+1] += a[i] - sum
    }
    if a[n] == sum {
        fmt.Println(n)
    } else {
        fmt.Println(n - 1)
    }
}

package main

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

func main() {
    scanner := bufio.NewScanner(os.Stdin)
    scanner.Split(bufio.ScanWords)
    var n int
    fmt.Scan(&n)
    total := 0;
    for i := 0; i < n; i++ {
        scanner.Scan()
        a, _ := strconv.Atoi(scanner.Text())
        total += a
    }
    if total%n == 0 { fmt.Println(n) } else { fmt.Println(n-1) }
}

package main
import (
	"fmt"
	//"bufio"
	//"os"
	//"strconv"
)

func main() {
	var a,n,s int
	fmt.Scan(&n)
	for i:=0;i<n;i++ {
		fmt.Scan(&a)
		s+=a
	}
	if s%n==0 {
		fmt.Println(n)
	} else {
		fmt.Println(n-1)
	}

}

package main

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

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	n := scanInt(scanner)
	sum := 0
	for i := 0; i < n; i++ {
		sum += scanInt(scanner)
	}
	result := sum % n
	if n-result > result {
		result = n-result
	}
	writer.WriteString(fmt.Sprintf("%d\n", result))
}

package main

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

func scanInt(scanner *bufio.Scanner) int64 {
	scanner.Scan()
	x, _ := strconv.Atoi(scanner.Text())
	return int64(x)
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	n := scanInt(scanner)
	head := scanInt(scanner)
	for i := int64(1); i < n; i++ {
		head += scanInt(scanner)
	}
	result := n-1
	if head%(n-1) == 0 {
		result++
	}
	writer.WriteString(fmt.Sprintf("%d\n", result))
}

package main

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

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	n := scanInt(scanner)
	sum := int64(0)
	for i := 0; i < n; i++ {
		sum += int64(scanInt(scanner))
	}
	result := (sum+int64(n)*10000) % int64(n)
	if int64(n)-result > result {
		result = int64(n)-result
	}
	writer.WriteString(fmt.Sprintf("%d\n", result))
}

package main

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

func main() {
    scanner := bufio.NewScanner(os.Stdin)
    scanner.Split(bufio.ScanWords)
    var n int
    fmt.Scan(&n)
    total := 0;
    for i := 0; i < n; i++ {
        scanner.Scan()
        a, _ := strconv.Atoi(scanner.Text())
        total += int(math.Abs(float64(a)))
    }
    fmt.Println(n-(total%n))
}

package main

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

func main() {
    scanner := bufio.NewScanner(os.Stdin)
    scanner.Split(bufio.ScanWords)
    var n int
    fmt.Scan(&n)
    total := 0;
    for i := 0; i < n; i++ {
        scanner.Scan()
        a, _ := strconv.Atoi(scanner.Text())
        total += a
    }
    fmt.Println(n-(total%n))
}


package main

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

func main() {
	n := ReadInt()
	m := make(map[int]int)
	spans := make([][]int, n)
	for i := 0; i < n; i++ {
		b, d := ReadInt(), ReadInt()-1
		spans[i] = []int{b, d}
		m[b] = 0
		m[d] = 0
	}

	years := make([]int, len(m))
	for year := range m {
		years = append(years, year)
	}
	sort.Ints(years)

	pops := make([]int, len(years)+1)

	for i := 0; i < n; i++ {
		b, d := spans[i][0], spans[i][1]
		i := sort.SearchInts(years, b)
		j := sort.SearchInts(years, d+1)
		pops[i]++
		pops[j]--
	}

	for i := 0; i < len(pops)-1; i++ {
		pops[i+1] += pops[i]
	}

	maxY, maxK := 0, 0
	for i := 0; i < len(years); i++ {
		y := years[i]
		k := pops[i]
		if maxK < k {
			maxY, maxK = y, k
		}
	}
	fmt.Println(maxY, maxK)
}

var reader = bufio.NewReader(os.Stdin)

func Scan(a ...interface{}) {
	if _, err := fmt.Fscan(reader, a...); err != nil {
		panic(err)
	}
}
func ReadString() (s string) { Scan(&s); return }
func ReadInt() (i int)       { Scan(&i); return }
func ReadInts(n int) []int {
	v := make([]int, n)
	for i := 0; i < n; i++ {
		Scan(&v[i])
	}
	return v
}
func ReadInt64() (i int64) { Scan(&i); return }
func ReadInt64s(n int) []int64 {
	v := make([]int64, n)
	for i := 0; i < n; i++ {
		Scan(&v[i])
	}
	return v
}

package main

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

func main() {
	n := ReadInt()
	m := make(map[int]int)
	spans := make([][]int, n)
	for i := 0; i < n; i++ {
		b, d := ReadInt(), ReadInt()-1
		spans[i] = []int{b, d}
		m[b] = 0
		m[d] = 0
	}

	years := make([]int, len(m))
	for year := range m {
		years = append(years, year)
	}
	sort.Ints(years)

	pops := make([]int, len(years)+1)

	for i := 0; i < n; i++ {
		b, d := spans[i][0], spans[i][1]
		i := sort.SearchInts(years, b)
		j := sort.SearchInts(years, d+1)
		// for k := i; k < j; k++ {
		// 	m[years[k]]++
		// }
		pops[i]++
		pops[j]--
	}

	for i := 0; i < len(pops)-1; i++ {
		pops[i+1] += pops[i]
	}

	maxY, maxK := 0, 0
	for i := 0; i < len(years); i++ {
		y := years[i]
		k := pops[i]
		if maxK < k {
			maxY, maxK = y, k
		}
	}
	fmt.Println(maxY, maxK)

	// maxY := 0
	// for y, k := range m {
	// 	if m[maxY] < k || (m[maxY] == k && y < maxY) {
	// 		maxY = y
	// 	}
	// }
	// fmt.Println(maxY, m[maxY])
}

var reader = bufio.NewReader(os.Stdin)

func Scan(a ...interface{}) {
	if _, err := fmt.Fscan(reader, a...); err != nil {
		panic(err)
	}
}
func ReadString() (s string) { Scan(&s); return }
func ReadInt() (i int)       { Scan(&i); return }
func ReadInts(n int) []int {
	v := make([]int, n)
	for i := 0; i < n; i++ {
		Scan(&v[i])
	}
	return v
}
func ReadInt64() (i int64) { Scan(&i); return }
func ReadInt64s(n int) []int64 {
	v := make([]int64, n)
	for i := 0; i < n; i++ {
		Scan(&v[i])
	}
	return v
}

package main

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

/*
1 5
2 4
5 6
*/

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

	var T int64
	var target [][2]int
	for Fscan(in, &T); T > 0; T-- {
		var A1, A2 int
		Fscan(in, &A1)
		Fscan(in, &A2)
		target = append(target, [2]int{A1, A2})
	}

	sort.Slice(target, func(i, j int) bool {
		return target[i][0] < target[j][0]
	})

	var sum, sumidx int
	for i := 0; i < len(target); {
		tmpSum := 1
		if i+1 < len(target) && target[i+1][0] == target[i][0] {
			i++
			continue
		}

		for j := i - 1; j >= 0; j-- {
			if target[j][1] > target[i][0] {
				tmpSum++
			}
		}

		if sum < tmpSum {
			sum = tmpSum
			sumidx = target[i][0]
		}
		i++
	}

	Fprintln(out, sumidx, sum)

}

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

	var T int
	var total []int
	life := make(map[int]int)
	dead := make(map[int]int)
	for Fscan(in, &T); T > 0; T-- {
		var A1, A2 int
		Fscan(in, &A1)
		Fscan(in, &A2)
		life[A1] ++
		dead[A2] ++
		total = append(total, A1, A2)

	}

	sort.Ints(total)

	tmpSum := 0
	sum := 0
	sumIdx := 0
	for i := 0; i < len(total); i++ {
		if num, ok := life[total[i]];ok {
			tmpSum += num
			delete(life,total[i])
		}

		if num, ok := dead[total[i]];ok {
			tmpSum -= num
			delete(dead,total[i])
		}

		if sum < tmpSum {
			sum = tmpSum
			sumIdx = total[i]
		}
	}

	Fprintln(out,sumIdx,sum)

}

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

package main

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

/*
1 5
2 4
5 6
*/

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

	var T int64
	var target [][2]int
	for Fscan(in, &T); T > 0; T-- {
		var A1, A2 int
		Fscan(in, &A1)
		Fscan(in, &A2)
		target = append(target, [2]int{A1, A2})
	}

	sort.Slice(target, func(i, j int) bool {
		return target[i][0] < target[j][0]
	})

	var sum, sumidx int
	for i := 0; i < len(target); {
		tmpSum := 1
		if i+1 < len(target) && target[i+1][0] == target[i][0] {
			i++
			continue
		}

		for j := i - 1; j >= 0; j-- {
			if target[j][1] > target[i][0] {
				tmpSum++
			}
		}

		if sum < tmpSum {
			sum = tmpSum
			sumidx = target[i][0]
		}
		i++
	}

	Fprintln(out, sumidx, sum)

}

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

	var T int
	var total []int
	life := make(map[int]int)
	dead := make(map[int]int)
	for Fscan(in, &T); T > 0; T-- {
		var A1, A2 int
		Fscan(in, &A1)
		Fscan(in, &A2)
		life[A1] ++
		dead[A2] ++
		total = append(total, A1, A2)

	}

	sort.Ints(total)

	tmpSum := 0
	sum := 0
	sumIdx := 0
	for i := 0; i < len(total); i++ {
		if num, ok := life[total[i]];ok {
			tmpSum += num
		}

		if num, ok := dead[total[i]];ok {
			tmpSum -= num
		}

		if sum < tmpSum {
			sum = tmpSum
			sumIdx = total[i]
		}
	}

	Fprintln(out, sum,sumIdx)

}

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