A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer hi. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all i's from 1 to n - 1 the inequality |hi - hi + 1| ≤ 1 holds.At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |hi - hi + 1| ≤ 1.

The first line contains two space-separated numbers, n and m (1 ≤ n ≤ 108, 1 ≤ m ≤ 105) — the number of days of the hike and the number of notes left in the journal. Next m lines contain two space-separated integers di and hdi (1 ≤ di ≤ n, 0 ≤ hdi ≤ 108) — the number of the day when the i-th note was made and height on the di-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all i from 1 to m - 1 the following condition holds: di < di + 1.

If the notes aren't contradictory, print a single integer — the maximum possible height value throughout the whole route. If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes).

NoteFor the first sample, an example of a correct height sequence with a maximum of 2: (0, 0, 1, 2, 1, 1, 0, 1).In the second sample the inequality between h7 and h8 does not hold, thus the information is inconsistent.

package main

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

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

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

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

	var n, m, maxH int

	fmt.Fscanf(stdin, "%d %d\n", &n, &m)

	var d, h [2]int
	d[1] = 1
	for i := 0; i < m; i++ {
		d[0], h[0] = d[1], h[1]
		fmt.Fscanf(stdin, "%d %d\n", &d[1], &h[1])

		if i == 0 {
			h[0] = h[1] + d[1] - 1
		}

		tmp := d[1] - d[0] - abs(h[1] - h[0])
		if tmp < 0 {
			fmt.Println("IMPOSSIBLE")
			return
		}

		maxH = max(maxH, tmp/2 + max(h[0], h[1]))
	}
	fmt.Println(max(maxH, h[1] + n - d[1]))
}

package main

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

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

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

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

	var n, m int

	fmt.Fscanf(stdin, "%d %d\n", &n, &m)

	var d, h [2]int
	maxH := 0
	d[1] = 1
	for i := 0; i < m; i++ {
		d[0], h[0] = d[1], h[1]
		fmt.Fscanf(stdin, "%d %d\n", &d[1], &h[1])

		if i == 0 {
			h[0] = h[1] + d[1] - 1
		}

		tmp := d[1] - d[0] - abs(h[1] - h[0])
		if tmp < 0 {
			fmt.Println("IMPOSSIBLE")
			return
		}
		tmp /= 2

		tmp += max(h[0],h[1])
		maxH = max(maxH, tmp)
	}

	fmt.Println(max(maxH, h[1] + n - d[1]))
}

package main

import "fmt"

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

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

func main() {
	var n, m int

	fmt.Scanf("%d %d", &n, &m)

	var d, h [2]int
	maxH := 0
	d[1] = 1
	for i := 0; i < m; i++ {
		d[0], h[0] = d[1], h[1]
		fmt.Scanf("%d %d", &d[1], &h[1])
		if i == 0 && d[1] == 1 {
			h[0] = h[1]
		}
		fmt.Println(d[1], h[1])
		tmp := (d[1] - d[0] - abs(h[1] - h[0]))/2
		if tmp < 0 {
			fmt.Println("IMPOSSIBLE")
			return
		}

		tmp += max(h[0],h[1])
		if tmp > maxH {
			maxH = tmp
		}
	}

	if tmp := h[1] + n - d[1]; tmp > maxH {
		fmt.Println(tmp)
		return
	}
	fmt.Println(maxH)
}

package main

import "fmt"

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

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

func main() {
	var n, m int

	fmt.Scanf("%d %d\n", &n, &m)

	var d, h [2]int
	maxH := 0
	d[1] = 1
	for i := 0; i < m; i++ {
		d[0], h[0] = d[1], h[1]
		fmt.Scanf("%d %d\n", &d[1], &h[1])

		if i == 0 && d[1] == 1 {
			h[0] = h[1]
		}

		tmp := (d[1] - d[0] - abs(h[1] - h[0]))/2
		if tmp < 0 {
			fmt.Println("IMPOSSIBLE")
			return
		}

		tmp += max(h[0],h[1])
		if tmp > maxH {
			maxH = tmp
		}
	}

	if tmp := h[1] + n - d[1]; tmp > maxH {
		fmt.Println(tmp)
		return
	}
	fmt.Println(maxH)
}

package main

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

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

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

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

	var n, m, maxH int

	fmt.Fscanf(stdin, "%d %d\n", &n, &m)

	var d, h [2]int
	d[1] = 1
	for ; m > 0; m-- {
		d[0], h[0] = d[1], h[1]
		fmt.Fscanf(stdin, "%d %d\n", &d[1], &h[1])

		if d[0] == 1 {
			h[0] = h[1] + d[1] - 1
		}

		tmp := d[1] - d[0] - abs(h[1] - h[0])
		if tmp < 0 {
			fmt.Println("IMPOSSIBLE")
			return
		}

		maxH = max(maxH, tmp/2 + max(h[0], h[1]))
	}
	fmt.Println(max(maxH, h[1] + n - d[1]))
}

package main

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

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

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

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

	var n, m int

	fmt.Fscanf(stdin, "%d %d\n", &n, &m)

	var d, h [2]int
	maxH := 0
	d[1] = 1
	for i := 0; i < m; i++ {
		d[0], h[0] = d[1], h[1]
		fmt.Fscanf(stdin, "%d %d\n", &d[1], &h[1])

		if i == 0 {
			h[0] = h[1] + d[1] - 1
		}

		tmp := (d[1] - d[0] - abs(h[1] - h[0]))/2
		if tmp < 0 {
			fmt.Println("IMPOSSIBLE")
			return
		}

		tmp += max(h[0],h[1])
		maxH = max(maxH, tmp)
	}

	fmt.Println(max(maxH, h[1] + n - d[1]))
}

package main

import "fmt"

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

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

func main() {
	var n, m int

	fmt.Scanf("%d %d", &n, &m)

	var d, h [2]int
	maxH := 0
	d[1] = 1
	for i := 0; i < m; i++ {
		d[0], h[0] = d[1], h[1]
		fmt.Scanf("%d %d", &d[1], &h[1])
		if i == 0 && d[1] == 1 {
			h[0] = h[1]
		}

		tmp := (d[1] - d[0] - abs(h[1] - h[0]))/2
		if tmp < 0 {
			fmt.Println("IMPOSSIBLE")
			return
		}

		tmp += max(h[0],h[1])
		if tmp > maxH {
			maxH = tmp
		}
	}

	if tmp := h[1] + n - d[1]; tmp > maxH {
		fmt.Println(tmp)
		return
	}
	fmt.Println(maxH)
}

package main

import "fmt"

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

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

func main() {
	var n, m int

	fmt.Scanf("%d %d", &n, &m)

	var d, h [2]int
	maxH := 0
	d[1] = 1
	for i := 0; i < m; i++ {
		d[0], h[0] = d[1], h[1]
		fmt.Scanf("%d %d", &d[1], &h[1])
		if i == 0 && d[1] == 1 {
			h[0] = h[1]
		}

		tmp := (d[1] - d[0] - abs(h[1] - h[0]))/2
		if tmp < 0 {
			fmt.Println("IMPOSSIBLE")
			return
		}
		fmt.Println(tmp)
		tmp += max(h[0],h[1])
		if tmp > maxH {
			maxH = tmp
		}
	}

	if tmp := h[1] + n - d[1]; tmp > maxH {
		fmt.Println(tmp)
		return
	}
	fmt.Println(maxH)
}

package main

import "fmt"

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

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

func main() {
	var n, m int

	fmt.Scanf("%d %d", &n, &m)

	var d, h [2]int
	maxH := 0
	d[1] = 1
	for i := 0; i < m; i++ {
		d[0], h[0] = d[1], h[1]
		fmt.Scanf("%d %d", &d[1], &h[1])
		fmt.Println(n, m)
		if i == 0 && d[1] == 1 {
			h[0] = h[1]
		}

		tmp := (d[1] - d[0] - abs(h[1] - h[0]))/2
		if tmp < 0 {
			fmt.Println("IMPOSSIBLE")
			return
		}

		tmp += max(h[0],h[1])
		if tmp > maxH {
			maxH = tmp
		}
	}

	if tmp := h[1] + n - d[1]; tmp > maxH {
		fmt.Println(tmp)
		return
	}
	fmt.Println(maxH)
}

package main

import "fmt"

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

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

func main() {
	var n, m int

	fmt.Scanf("%d %d", &n, &m)

	var d, h [2]int
	maxH := 0
	d[1] = 1
	for i := 0; i < m; i++ {
		d[0], h[0] = d[1], h[1]
		fmt.Scanf("%d %d", &d[1], &h[1])
		fmt.Println(d[0], h[0], d[1], h[1])
		if i == 0 && d[1] == 1 {
			h[0] = h[1]
		}

		tmp := (d[1] - d[0] - abs(h[1] - h[0]))/2
		if tmp < 0 {
			fmt.Println("IMPOSSIBLE")
			return
		}

		tmp += max(h[0],h[1])
		if tmp > maxH {
			maxH = tmp
		}
	}

	if tmp := h[1] + n - d[1]; tmp > maxH {
		fmt.Println(tmp)
		return
	}
	fmt.Println(maxH)
}


Mad scientist Mike does not use slow hard disks. His modification of a hard drive has not one, but n different heads that can read data in parallel.When viewed from the side, Mike's hard drive is an endless array of tracks. The tracks of the array are numbered from left to right with integers, starting with 1. In the initial state the i-th reading head is above the track number hi. For each of the reading heads, the hard drive's firmware can move the head exactly one track to the right or to the left, or leave it on the current track. During the operation each head's movement does not affect the movement of the other heads: the heads can change their relative order; there can be multiple reading heads above any of the tracks. A track is considered read if at least one head has visited this track. In particular, all of the tracks numbered h1, h2, ..., hn have been read at the beginning of the operation.  Mike needs to read the data on m distinct tracks with numbers p1, p2, ..., pm. Determine the minimum time the hard drive firmware needs to move the heads and read all the given tracks. Note that an arbitrary number of other tracks can also be read.

The first line of the input contains two space-separated integers n, m (1 ≤ n, m ≤ 105) — the number of disk heads and the number of tracks to read, accordingly. The second line contains n distinct integers hi in ascending order (1 ≤ hi ≤ 1010, hi < hi + 1) — the initial positions of the heads. The third line contains m distinct integers pi in ascending order (1 ≤ pi ≤ 1010, pi < pi + 1) - the numbers of tracks to read. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is recommended to use the cin, cout streams or the %I64d specifier.

Print a single number — the minimum time required, in seconds, to read all the needed tracks.

NoteThe first test coincides with the figure. In this case the given tracks can be read in 2 seconds in the following way:   during the first second move the 1-st head to the left and let it stay there;  move the second head to the left twice;  move the third head to the right twice (note that the 6-th track has already been read at the beginning). One cannot read the tracks in 1 second as the 3-rd head is at distance 2 from the 8-th track.

package main

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

var in = bufio.NewReaderSize(os.Stdin, 2<<16)

func readSlice(n int) []int64 {
	s := make([]int64, n)
	for i := range s {
		fmt.Fscan(in, &s[i])
	}
	return s
}

func main() {
	var n, m int
	fmt.Fscan(in, &n, &m)
	h := readSlice(n)
	p := readSlice(m)

	fmt.Println(search(2e10, func(t int64) bool {
		var hi, pi int
		for hi < n && pi < m {
			if p[pi] < h[hi] && h[hi]-p[pi] > t {
				return false
			}
			left := p[pi]
			if h[hi] < left {
				left = h[hi]
			}
			for pi < m && p[pi] <= h[hi] {
				pi++
			}
			for pi < m && (t >= (h[hi]-left)*2+p[pi]-h[hi] || t >= (p[pi]-h[hi])*2+h[hi]-left) {
				pi++
			}
			hi++
		}
		return pi == m
	}))
}

// Tiktų sort.Search, jei CF int būtų 64 bitų...
func search(n int64, f func(i int64) bool) int64 {
	i, j := int64(0), n
	for i < j {
		h := (i + j) >> 1
		if !f(h) {
			i = h + 1
		} else {
			j = h
		}
	}
	return i
}

package main

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

var (
	rw   *bufio.ReadWriter = bufio.NewReadWriter(bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout))
	pos, vis []int64
	n, m int
)

func nextString() string {
	token := []byte{}
	for {
		c, err := rw.ReadByte()
		if err != nil || c == ' ' || c == '\n' {
			return string(token)
		} else if c == '\r' {
			_, _ = rw.ReadByte()
			return string(token)
		} else {
			token = append(token, c)
		}
	}
}

func nextInt() int {
	ans, _ := strconv.Atoi(nextString())
	return ans
}

func nextInt64() int64 {
	ans, _ := strconv.ParseInt(nextString(), 10, 64)
	return ans
}

func abs(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}

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

func goleft(vi int, p, t int64) int {
	vj := vi
	for ; vj < m && abs(vis[vi]-p) + (vis[vj]-vis[vi]) <= t; vj++ {}
	return vj
}

func goright(vi int, p, t int64) int {
	if p <= vis[vi] {
		return 0
	}
	vj := vi
	for ; vj < m && abs(vis[vj]-p) + (vis[vj]-vis[vi]) <= t; vj++ {}
	return vj
}

func can(time int64) bool {
	vi := 0
	for i := 0; i < n && vi < m; i++ {
		vi = max(goleft(vi, pos[i], time), goright(vi, pos[i], time))
	}
	return vi == m
}

func main() {
	defer rw.Flush()
	n, m = nextInt(), nextInt()
	pos = make([]int64, n)
	vis = make([]int64, m)
	for i := 0; i < n; i++ {
		pos[i] = nextInt64()
	}
	for i := 0; i < m; i++ {
		vis[i] = nextInt64()
	}
	if can(0) {
		fmt.Fprintln(rw, 0)
		return
	}
	lastFalse := int64(0)
	for t := int64(1) << 34; t > 0; t >>= 1 {
		if !can(lastFalse + t) {
			lastFalse += t
		}
	}
	fmt.Fprintln(rw, lastFalse+1)
}

package main

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

var in = bufio.NewReaderSize(os.Stdin, 2<<16)

func readSlice(n int) []int64 {
	s := make([]int64, n)
	for i := range s {
		fmt.Fscan(in, &s[i])
	}
	return s
}

func main() {
	var n, m int
	fmt.Fscan(in, &n, &m)
	h := readSlice(n)
	p := readSlice(m)

	fmt.Println(search(2e10, func(t int64) bool {
		var hi, pi int
		for hi < n && pi < m {
			if abs(h[hi]-p[pi]) > t {
				return false
			}
			left := p[pi]
			if h[hi] < left {
				left = h[hi]
			}
			for pi < m && p[pi] <= h[hi] {
				pi++
			}
			for pi < m && (t >= (h[hi]-left)*2+p[pi]-h[hi] || t >= (p[pi]-h[hi])*2+h[hi]-left) {
				pi++
			}
			hi++
		}
		return pi == m
	}))
}

// Tiktų sort.Search, jei CF int būtų 64 bitų...
func search(n int64, f func(i int64) bool) int64 {
	i, j := int64(0), n
	for i < j {
		h := (i + j) >> 1
		if !f(h) {
			i = h + 1
		} else {
			j = h
		}
	}
	return i
}

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

package main

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

var (
	rw   *bufio.ReadWriter = bufio.NewReadWriter(bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout))
	pos, vis []int64
	n, m int
)

func nextString() string {
	token := []byte{}
	for {
		c, err := rw.ReadByte()
		if err != nil || c == ' ' || c == '\n' {
			return string(token)
		} else if c == '\r' {
			_, _ = rw.ReadByte()
			return string(token)
		} else {
			token = append(token, c)
		}
	}
}

func nextInt() int {
	ans, _ := strconv.Atoi(nextString())
	return ans
}

func nextInt64() int64 {
	ans, _ := strconv.ParseInt(nextString(), 10, 64)
	return ans
}

func abs(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}

func can(time int64) bool {
	vi := 0
	for i := 0; i < n; i++ {
		t, p := time, pos[i]
		for {
			target := vis[vi]
			delta := abs(p - target)
			if delta > t {
				break
			}
			t -= delta
			p = target
			vi++
			if vi == m {
				return true
			}
		}
	}
	return false
}

func main() {
	defer rw.Flush()
	n, m = nextInt(), nextInt()
	pos = make([]int64, n)
	vis = make([]int64, m)
	for i := 0; i < n; i++ {
		pos[i] = nextInt64()
	}
	for i := 0; i < m; i++ {
		vis[i] = nextInt64()
	}
	if can(0) {
		fmt.Fprintln(rw, 0)
		return
	}
	lastFalse := int64(0)
	for t := int64(1) << 34; t > 0; t = t / 2 {
		if !can(lastFalse + t) {
			lastFalse += t
		}
	}
	fmt.Fprintln(rw, lastFalse+1)
}

package main

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

var (
	rw   *bufio.ReadWriter = bufio.NewReadWriter(bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout))
	pos, vis []int64
	n, m int
)

func nextString() string {
	token := []byte{}
	for {
		c, err := rw.ReadByte()
		if err != nil || c == ' ' || c == '\n' {
			return string(token)
		} else if c == '\r' {
			_, _ = rw.ReadByte()
			return string(token)
		} else {
			token = append(token, c)
		}
	}
}

func nextInt() int {
	ans, _ := strconv.Atoi(nextString())
	return ans
}

func nextInt64() int64 {
	ans, _ := strconv.ParseInt(nextString(), 10, 64)
	return ans
}

func abs(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}

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

func goleft(vi int, p, t int64) int {
	vj := vi
	for ; vj < m && abs(p - vis[vj]) <= t; vj++ {
		p, t = vis[vj], t - abs(p - vis[vj])
	}
	return vj
}

func goright(vi int, p, t int64) int {
	if p <= vis[vi] || vi == m-1 {
		return 0
	}
	vj := vi+1
	for ; vj < m && vis[vj] < p; vj++ {}
	for ; vj < m && (vis[vj]-p) + (vis[vj]-vis[vi]) <= t; vj++ {}
	return vj
}

func can(time int64) bool {
	vi := 0
	for i := 0; i < n && vi < m; i++ {
		vi = max(goleft(vi, pos[i], time), goright(vi, pos[i], time))
	}
	return vi == m
}

func main() {
	defer rw.Flush()
	n, m = nextInt(), nextInt()
	pos = make([]int64, n)
	vis = make([]int64, m)
	for i := 0; i < n; i++ {
		pos[i] = nextInt64()
	}
	for i := 0; i < m; i++ {
		vis[i] = nextInt64()
	}
	if can(0) {
		fmt.Fprintln(rw, 0)
		return
	}
	lastFalse := int64(0)
	for t := int64(1) << 34; t > 0; t = t / 2 {
		if !can(lastFalse + t) {
			lastFalse += t
		}
	}
	fmt.Fprintln(rw, lastFalse+1)
}

package main

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

var (
	rw   *bufio.ReadWriter = bufio.NewReadWriter(bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout))
	pos, vis []int64
	n, m int
)

func nextString() string {
	token := []byte{}
	for {
		c, err := rw.ReadByte()
		if err != nil || c == ' ' || c == '\n' {
			return string(token)
		} else if c == '\r' {
			_, _ = rw.ReadByte()
			return string(token)
		} else {
			token = append(token, c)
		}
	}
}

func nextInt() int {
	ans, _ := strconv.Atoi(nextString())
	return ans
}

func nextInt64() int64 {
	ans, _ := strconv.ParseInt(nextString(), 10, 64)
	return ans
}

func abs(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}

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

func goleft(vi int, p, t int64) int {
	vj := vi
	for ; vj < m && abs(p - vis[vj]) <= t; vj++ {
		p, t = vis[vj], t - abs(p - vis[vj])
	}
	return vj
}

func goright(vi int, p, t int64) int {
	if p <= vis[vi] || vi == m-1 {
		return 0
	}
	vj := vi
	for ; vj < m && vis[vj] < p; vj++ {}
	for ; vj < m && (vis[vj]-p) + (vis[vj]-vis[vi]) <= t; vj++ {}
	return vj
}

func can(time int64) bool {
	vi := 0
	for i := 0; i < n && vi < m; i++ {
		vi = max(goleft(vi, pos[i], time), goright(vi, pos[i], time))
	}
	return vi == m
}

func main() {
	defer rw.Flush()
	n, m = nextInt(), nextInt()
	pos = make([]int64, n)
	vis = make([]int64, m)
	for i := 0; i < n; i++ {
		pos[i] = nextInt64()
	}
	for i := 0; i < m; i++ {
		vis[i] = nextInt64()
	}
	if can(0) {
		fmt.Fprintln(rw, 0)
		return
	}
	lastFalse := int64(0)
	for t := int64(1) << 34; t > 0; t = t / 2 {
		if !can(lastFalse + t) {
			lastFalse += t
		}
	}
	fmt.Fprintln(rw, lastFalse+1)
}


Theofanis really likes sequences of positive integers, thus his teacher (Yeltsa Kcir) gave him a problem about a sequence that consists of only special numbers.Let's call a positive number special if it can be written as a sum of different non-negative powers of $$$n$$$. For example, for $$$n = 4$$$ number $$$17$$$ is special, because it can be written as $$$4^0 + 4^2 = 1 + 16 = 17$$$, but $$$9$$$ is not.Theofanis asks you to help him find the $$$k$$$-th special number if they are sorted in increasing order. Since this number may be too large, output it modulo $$$10^9+7$$$.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$; $$$1 \le k \le 10^9$$$).

For each test case, print one integer — the $$$k$$$-th special number in increasing order modulo $$$10^9+7$$$.

package main

import "fmt"

func main() {
	var T int
	fmt.Scan(&T)
	for i := 0; i < T; i++ {
		solve()
	}
}

func solve() {
	mod := 1e9 + 7
	var n, k int
	fmt.Scan(&n, &k)
	var res, base int64
	res, base = 0, 1
	for k > 0 {
		if k&1 == 1 {
			res = (res + base) % int64(mod)

		}
		k /= 2
		base = (base * int64(n)) % int64(mod)
	}
	fmt.Println(res % int64(mod))
}

package main

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

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

func scan(a ...interface{}) {
	fmt.Fscan(in, a...)
}
func printf(f string, a ...interface{}) {
	fmt.Fprintf(out, f, a...)
}

func solve() {
	var n, k int64
	scan(&n, &k)
	mod := int64(1e9) + 7
	var ans, x int64 = 0, 1
	for k > 0 {
		ans += x * (k % 2)
		ans %= mod
		x %= mod
		x *= n
		k /= 2
	}
	printf("%d\n", ans)
}
func main() {
	defer out.Flush()
	n := 0
	scan(&n)
	for i := 0; i < n; i++ {
		solve()
	}
}

package main

import (
	"fmt"
	"math"
)

func main() {
	var t int
	fmt.Scanf("%d\n", &t)
	for ; t > 0; t-- {
		var n, k int
		var u int64
		e := int64(1)
		m := int64(1000000007)
		fmt.Scanf("%d %d\n", &n, &k)
		for ; k > 0; k /= 2 {
			if math.Mod(float64(k), 2.0) == 1 {
				u = (u + e) % m
			}
			e = (e * int64(n)) % m
		}
		fmt.Printf("%d\n", u)
	}
}

package main

import (
	"fmt"
)

func main() {
	var t int
	fmt.Scanf("%d\n", &t)
	for ; t > 0; t-- {
		var n, k int
		fmt.Scanf("%d %d\n", &n, &k)
		var kBinary string = fmt.Sprintf("%b", k)
		var u int64
		e := int64(1)
		for i := len(kBinary) - 1; i >= 0; i-- {
			if kBinary[i] == '1' {
				u += e
				u %= int64(1000000007)
			}
			e *= int64(n)
			e %= int64(1000000007)
		}
		fmt.Printf("%d\n", u)
	}
}

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
}

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, k int64

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

		var ans int64

		pp := k
		init := int64(1)
		for pp > 0 {
			if pp%2 == 1 {
				ans = ans + init
				ans = ans % 1000000007
				pp = pp / 2
			} else {
				pp = pp / 2

			}
			init = init * n
			init = init % 1000000007

		}
		fmt.Println(ans)

	}

}

package main

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

func main() {
	r := bufio.NewReaderSize(os.Stdin, 1024*1024)

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

		res := int64(0)
		x := int64(1)
		for i := 0; i < 32; i++ {
			if k&(1<<i) != 0 {
				res += x
				res %= 1000000007
			}
			x *= int64(n)
			x %= 1000000007
		}

		fmt.Println(res)
	}
}

package main

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

func solve() {
	f := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	N := 0
	fmt.Fscan(f, &N)
	for i := 0; i < N; i++ {
		n, k, tot, pl := int64(0), int64(0), int64(0), int64(1)
		fmt.Fscan(f, &n, &k)
		p := strconv.FormatInt(k, 2)
		for j := len(p) - 1; j >= 0; j-- {
			if string(p[j]) == "1" {
				tot += pl
			}
			pl *= n
			pl %= (1e9 + 7)
		}
		fmt.Fprintln(w, tot%(1e9+7))
	}
	w.Flush()
}

func main() {
	solve()
}

package main

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

/*

 */

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

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

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

		num := int64(0)
		power := int64(0)

		for k != 0 {
			if k&1 != 0 {
				bigNum := big.NewInt(0)
				bigNum.Exp(big.NewInt(n), big.NewInt(power), big.NewInt(1_000_000_007))

				num += bigNum.Int64()
				num %= 1_000_000_007
			}

			k >>= 1
			power++
		}

		fmt.Fprintln(writer, num)
	}
}

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 divCeil(num, div int64) int64 {
	quot := num / div
	if num%div != 0 {
		quot++
	}
	return quot
}

//goland:noinspection GoUnusedFunction
func min(nums ...int) int {
	minimum := nums[0]
	for _, num := range nums {
		if num < minimum {
			minimum = num
		}
	}
	return minimum
}

//goland:noinspection GoUnusedFunction
func max(nums ...int) int {
	maximum := nums[0]
	for _, num := range nums {
		if num > maximum {
			maximum = num
		}
	}
	return maximum
}

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

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, k int64
		fmt.Fscan(in, &n, &k)

		const mod = 1e9 + 7
		var c int64
		for p := int64(1); k > 0; k >>= 1 {
			c = (c + k&1*p) % mod
			p = p * n % mod
		}
		fmt.Fprintln(out, c)
	}
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1594B(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	const mod int64 = 1e9 + 7

	var T, n, k int64
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &k)
		ans := int64(0)
		for p := int64(1); k > 0; k >>= 1 {
			ans = (ans + k&1*p) % mod
			p = p * n % mod
		}
		Fprintln(out, ans)
	}
}

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

package main

import(
	"fmt"
)
const mod =1e9+7
func main() {
	var t,n,k,ans,d int64
	fmt.Scan(&t)
	for t>0{
		t--
		fmt.Scan(&n,&k)
		ans=0
		d=1
		for k>0{
			if k%2==1{
				ans=(ans+d)%mod
			}
			k=k/2
			d=(d*n)%mod
		}
		fmt.Println(ans)
	}
}

package main

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

const MOD = 1e9 + 7

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

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

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

func _int() int {
	s := _string()
	i := 0
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int(r - '0')
	}
	return i
}

func _int64() int64 {
	s := _string()
	i := int64(0)
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int64(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int64(r - '0')
	}
	return i
}

func _string() string {
	if !scanner.Scan() {
		panic(scanner.Err())
	}
	return scanner.Text()
}

func main() {
	defer writer.Flush()
	for t := _int(); t > 0; t-- {
		n := _int64()
		k := _int64()
		printf("%d\n", sol(n, k))
	}
}

func sol(n, k int64) int64 {
	ret := int64(0)
	curr := int64(1)
	for k > 0 {
		if k&1 == 1 {
			ret += curr
		}
		curr *= n
		curr %= MOD
		k >>= 1
	}
	return ret % MOD
}

package main

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

func main()  {
	in := bufio.NewReader(os.Stdin)
	//out := bufio.NewWriter(os.Stdout)
	mod := int64(1000000007)
	var t int
	var n ,k int64
	fmt.Fscanf(in, "%d\n", &t)
	for i:= 0;i < t;i++{

		fmt.Fscanf(in, "%d %d\n", &n,&k)
		d := int64(1)
		ans := int64(0)
		for k > 0{
			if (k%2) == 1{
				ans = (ans + d)%mod
			}
			d = (d*n)%mod
			k /= 2
		}
		fmt.Printf("%v\n",ans)
	}

}

package main

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

var N, K int64

const MOD int64 = 1000000007

var v []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, &K)

		ans := solve()

		fmt.Println(ans)
	}
}

func solve() int64 {
	if K == 1{
		return binpow(N, 0, MOD)
	} else if K == 2 {
		return binpow(N, 1, MOD)
	} else if K == 3 {
		return binpow(N, 0, MOD) + binpow(N, 1, MOD)
	}

	var sum int64

	for ; K > 0; {
		var i int64
		for i = 0; i <= 40; i++ {
			var t int64
			t = 1 << uint(i)

			if t <= K {
				continue
			}
			break
		}
		var d int64
		d = i - 1

		t := binpow(N, d, MOD)

		sum += t

		sum = sum % MOD

		K -= (1 << uint(d))
	}

	return sum % MOD

}

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

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

func binpow(a int64, b int64, m int64) int64 {
	a %= m
	var res int64 = 1
	for ; b > 0;
	{
		if b & 1 == 1 {
			res = res * a % m
		}

		a = a * a % m
		b >>= 1
	}
	return res
}

package main

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

func main()  {
	var input io.Reader = bufio.NewReader(os.Stdin)
	var t int64
	fmt.Fscan(input, &t)
	for t > 0 {
		var n int64
		var k int64
		fmt.Fscan(input, &n)
		fmt.Fscan(input, &k)
		mod:=1000000007
		fmt.Println(getAns(n,k, int64(mod)))
		t--
	}
}

func getAns(n int64, k int64, mod int64) int64 {
	ans:=int64(0)
	temp:=int64(1)
	for k>0 {
		if k%2==1 {
			ans+=temp
		}
		k/=2
		temp*=n
		temp%=mod
	}
	return ans%mod
}

package main

import "fmt"

func main() {
	var T int
	fmt.Scan(&T)
	for i := 0; i < T; i++ {
		solve()
	}
}

func solve() {
	mod := 1e9 + 7
	var n, k int
	fmt.Scan(&n, &k)
	res, base := 0, 1
	for k > 0 {
		if k&1 == 1 {
			res += (base % int(mod))
			res = res % int(mod)
		}
		k /= 2
		base *= n
	}
	fmt.Println(res % int(mod))
}

package main

import "fmt"

const (
	mod = 1e9 + 7
)

func main() {
	var T int
	fmt.Scan(&T)
	for i := 0; i < T; i++ {
		solve()
	}
}

func solve() {
	var n, k int
	fmt.Scan(&n, &k)
	res, base := 0, 1
	for k > 0 {
		if k&1 == 1 {
			res += (base % int(mod))
			res = res % int(mod)
		}
		k /= 2
		base *= n
	}
	fmt.Println(res % int(mod))
}

package main

import "fmt"

const (
	mod = 1e9 + 7
)

func main() {
	var T int
	fmt.Scan(&T)
	for i := 0; i < T; i++ {
		solve()
	}
}

func solve() {
	var n, k int
	fmt.Scan(&n, &k)
	res, base := 0, 1
	for k > 0 {
		if k%2 == 1 {
			res += base
					}
		k /= 2
		base *= n
	}
	fmt.Println(res % int(mod))
}

package main

import "fmt"

const (
	mod = 1e9 + 7
)

func main() {
	var T int
	fmt.Scan(&T)
	for i := 0; i < T; i++ {
		solve()
	}
}

func solve() {
	var n, k int
	fmt.Scan(&n, &k)
	res, base := 0, 1
	for k > 0 {
		if k%2 == 1 {
			res += base
			res = res % int(mod)
		}
		k /= 2
		base *= n
	}
	fmt.Println(res % int(mod))
}

package main

import "fmt"

const (
	mod = 1e9 + 7
)

func main() {
	var T int
	fmt.Scan(&T)
	for i := 0; i < T; i++ {
		solve()
	}
}

func solve() {
	var n, k int
	fmt.Scan(&n, &k)
	res, base := 0, 1
	for k > 0 {
		if k%2 == 1 {
			res += base
		}
		k /= 2
		base *= n
	}
	fmt.Println(res % mod)
}

package main

import "fmt"

const (
	mod = 1e9 + 7
)

func main() {
	var T int
	fmt.Scan(&T)
	for i := 0; i < T; i++ {
		solve()
	}
}

func solve() {
	var n, k int
	fmt.Scan(&n, &k)
	res, base := 0, 1
	for k > 0 {
		if k%2 == 1 {
			res += base
		}
		k /= 2
		base *= n
	}
	fmt.Println(res)
}

package main

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

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

func scan(a ...interface{}) {
	fmt.Fscan(in, a...)
}
func printf(f string, a ...interface{}) {
	fmt.Fprintf(out, f, a...)
}

func solve() {
	var n, k int
	scan(&n, &k)
	mod := int(1e9) + 7
	x, ans := 1, 0
	for k > 0 {
		ans += x * (k % 2)
		ans %= mod
		x *= n
		k /= 2
	}
	printf("%d\n", ans)
}
func main() {
	defer out.Flush()
	n := 0
	scan(&n)
	for i := 0; i < n; i++ {
		solve()
	}
}

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
}

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, k int

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

		var ans int

		p := k
		init := 1
		for p > 0 {

			if p%2 == 1 {
				ans = ans + init
				ans = ans % 1000000007
				p = p / 2
			} else {
				p = p / 2

			}
			init = init * n
			init = init % 1000000007

		}
		fmt.Println(ans)

	}

}

package main

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

func main() {
	r := bufio.NewReaderSize(os.Stdin, 1024*1024)

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

		res := 0
		x := 1
		for i := 0; i < 32; i++ {
			if k&(1<<i) != 0 {
				res += x
				res %= 1000000007
			}
			x *= n
			x %= 1000000007
		}

		fmt.Println(res)
	}
}


Spring has come, and the management of the AvtoBus bus fleet has given the order to replace winter tires with summer tires on all buses.You own a small bus service business and you have just received an order to replace $$$n$$$ tires. You know that the bus fleet owns two types of buses: with two axles (these buses have $$$4$$$ wheels) and with three axles (these buses have $$$6$$$ wheels).You don't know how many buses of which type the AvtoBus bus fleet owns, so you wonder how many buses the fleet might have. You have to determine the minimum and the maximum number of buses that can be in the fleet if you know that the total number of wheels for all buses is $$$n$$$.

The first line contains an integer $$$t$$$ ($$$1 \le t \le 1\,000$$$) — the number of test cases. The following lines contain description of test cases. The only line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 10^{18}$$$) — the total number of wheels for all buses.

For each test case print the answer in a single line using the following format. Print two integers $$$x$$$ and $$$y$$$ ($$$1 \le x \le y$$$) — the minimum and the maximum possible number of buses that can be in the bus fleet. If there is no suitable number of buses for the given $$$n$$$, print the number $$$-1$$$ as the answer.

NoteIn the first test case the total number of wheels is $$$4$$$. It means that there is the only one bus with two axles in the bus fleet.In the second test case it's easy to show that there is no suitable number of buses with $$$7$$$ wheels in total.In the third test case the total number of wheels is $$$24$$$. The following options are possible:  Four buses with three axles.  Three buses with two axles and two buses with three axles.  Six buses with two axles. So the minimum number of buses is $$$4$$$ and the maximum number of buses is $$$6$$$.

package main

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

type CF1679A struct {
	sc        *bufio.Reader
	split     []string
	index     int
	separator string
}

func (in *CF1679A) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1679A) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

func (in *CF1679A) NextInt() int {
	in.load()
	val, _ := strconv.Atoi(strings.TrimSpace(in.split[in.index]))
	in.index++
	return val
}

func (in *CF1679A) NextInt64() int64 {
	in.load()
	val, _ := strconv.ParseInt(strings.TrimSpace(in.split[in.index]), 10, 64)
	in.index++
	return val
}

func (in *CF1679A) NextString() string {
	in.load()
	val := strings.TrimSpace(in.split[in.index])
	in.index++
	return val
}

/**
Run solve the problem CF1679A
Date: 8/21/2022
User: wotan
URL: https://codeforces.com/problemset/problem/1679/A
Problem: CF1679A
**/
func (in *CF1679A) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt64()
		min := int64(0)
		max := int64(0)
		a := n / 4
		llantasSobrantes := n - (a * 4)
		if llantasSobrantes == 2 || llantasSobrantes == 0 {
			max = a
			min = a
		} else {
			max = 0
		}

		b := n / 6
		llantasSobrantes = n - (b * 6)
		if llantasSobrantes == 2 || llantasSobrantes == 4 {
			min = b + 1
		} else if llantasSobrantes == 0 {
			min = b
		}

		if min == 0 || max == 0 {
			fmt.Println(-1)
		} else {
			fmt.Println(min, max)
		}

	}
}

func NewCF1679A(r *bufio.Reader) *CF1679A {
	return &CF1679A{
		sc:        r,
		split:     []string{},
		index:     0,
		separator: " ",
	}
}

func main() {
	NewCF1679A(bufio.NewReader(os.Stdin)).Run()
}

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

		if n%2 != 0 || n < 4 {
			fmt.Fprintln(out, -1)
		} else {
			a := n / 4
			b := n / 6
			if n%6 != 0 {
				b++
			}
			fmt.Fprintln(out, b, a)

		}
	}
}

package main

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

func main_div2_791_A() {
	_in, _out := os.Stdin, os.Stdout
	in, out := bufio.NewReader(_in), bufio.NewWriter(_out)
	defer out.Flush()
	var cas int = 1
	Fscan(in, &cas)
	var sol func()
	// Coding...beg!
	sol = func() {
		var n int64
		Fscan(in, &n)
		// x * 4 + y * 6 = n
		// x * 2  + y * 3 = n/2
		// x * 2  + y * 2  + y = n/2

		// max min
		// max 4 best
		if n%2 != 0 {
			Fprintln(out, -1)
			return
		}
		var k = n / 2
		if k == 1 {
			Fprintln(out, -1)
			return
		}
		var min, max int64
		if k%2 == 0 {
			max = k / 2
		} else {
			max = 1 + (k-3)/2
		}
		if k%3 == 0 {
			min = k / 3
		} else {
			if k%3 == 2 {
				k -= 2
				min = 1 + k/3
			} else {
				k -= 4
				min = 2 + k/3
			}
		}
		Fprintln(out, min, max)
	}
	// Coding...ove!
	for i := 0; i < cas; i++ {
		sol()
	}
}

func main() {
	main_div2_791_A()
}

// https://codeforces.com/problemset/problem/1679/A

// 1679A - AvtoBus

// Spring has come, and the management of the AvtoBus bus fleet has given the order to replace winter tires with summer tires on all buses.
// You own a small bus service business and you have just received an order to replace n tires. You know that the bus fleet owns two types of buses: with two axles (these buses have 4 wheels) and with three axles (these buses have 6 wheels).
// You don't know how many buses of which type the AvtoBus bus fleet owns, so you wonder how many buses the fleet might have. You have to determine the minimum and the maximum number of buses that can be in the fleet if you know that the total number of wheels for all buses is n.

// Input
// The first line contains an integer t (1≤t≤1000) — the number of test cases. The following lines contain description of test cases.
// The only line of each test case contains one integer n (1≤n≤10^18) — the total number of wheels for all buses.

// Output
// For each test case print the answer in a single line using the following format.
// Print two integers x and y (1≤x≤y) — the minimum and the maximum possible number of buses that can be in the bus fleet.
// If there is no suitable number of buses for the given n, print the number −1 as the answer.

// Example
// input
// 4
// 4
// 7
// 24
// 998244353998244352
// output
// 1 1
// -1
// 4 6
// 166374058999707392 249561088499561088

// Note

// In the first test case the total number of wheels is 4. It means that there is the only one bus with two axles in the bus fleet.
// In the second test case it's easy to show that there is no suitable number of buses with 7 wheels in total.
// In the third test case the total number of wheels is 24. The following options are possible:
//  • Four buses with three axles.
//  • Three buses with two axles and two buses with three axles.
//  • Six buses with two axles.
// So the minimum number of buses is 4 and the maximum number of buses is 6.

// Tutorial

// Let the number of buses with two axles is x and the number of buses with three axles is y. Then the equality 4x+6y=n must be true. If n is odd, there is no answer, because the left part of the equality is always even. Now we can divide each part of the equality by two: 2x+3y=n/2.
// Let's maximize the number of buses. Then we should make x as large as possible. So, we will get 2+…+2+2=n2 if n2 is even, and 2+…+2+3=n/2 otherwise. In both cases the number of buses is ⌊n/2⌋.
// Now let's minimize the number of buses. So, we should make y as large is possible. We will get 3+…+3+3+3=n/2 if n/2 is divisible by 3, 3+…+3+3+2=n/2 if n≡2(mod 3), and 3+…+3+2+2=n/2 if n≡1(mod 3). In all cases the number of buses is ⌈n/3⌉.
// Also don't forget the case n=2 — each bus has at least four wheels, so in this case there is no answer.
// Time complexity: O(1).

package main

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

func main() {
	var reader = bufio.NewReader(os.Stdin)
	var writer = bufio.NewWriter(os.Stdout)
	var c int
	var t uint
	var n uint64

	for c, _ = fmt.Fscan(reader, &t); c == 1; c, _ = fmt.Fscan(reader, &t) {
		for ; t > 0; t-- {
			fmt.Fscan(reader, &n)
			if n&1 == 1 || n < 4 {
				fmt.Fprintln(writer, -1)
			} else {
				fmt.Fprintln(writer, (n+5)/6, n/4)
			}
		}
	}

	writer.Flush()
}

// 00
package main

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

func main() {
	var T int
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscan(reader, &T)
	mins := map[int64]int64{0: 0, 4: 1, 6: 1, 8: 2, 10: 2, 12: 2, 14: 3}
	maxes := map[int64]int64{0: 0, 4: 1, 6: 1, 8: 2, 10: 2, 12: 3, 14: 3}
	for t := 1; t <= T; t++ {
		var n int64
		fmt.Fscan(reader, &n)
		if n%2 == 1 || n == 2 {
			fmt.Println(-1)
		} else {
			if n%12 == 2 {
				fmt.Println((n-14)/6+mins[14], (n-14)/4+maxes[14])
			} else {
				rem := n - (n % 12)
				fmt.Println(rem/6+(mins[n%12]), rem/4+(maxes[n%12]))
			}
		}

	}
}

package main

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

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

func CF1670E(in io.Reader, out io.Writer) {
	r := bufio.NewReader(in)
	w := bufio.NewWriter(out)
	defer w.Flush()
	const (
		four = 4
		six  = 6
	)

	findMax := func(n int64) int64 {
		left := n % four
		if left == 0 {
			return n / four
		}
		for i := n / four; i >= 0; i-- {
			sLeft := (n - i*4) % six
			if sLeft == 0 {
				return i + (n-i*4)/six
			}
		}
		return -1
	}

	findMin := func(n int64) int64 {
		left := n % six
		if left == 0 {
			return n / six
		}
		for i := n / six; i >= 0; i-- {
			sLeft := (n - i*6) % four
			if sLeft == 0 {
				return i + (n-i*6)/four
			}
		}
		return -1
	}

	var T int64
	fmt.Fscan(r, &T)
	for ; T > 0; T-- {
		var n int64
		fmt.Fscan(r, &n)
		if n%2 == 1 {
			fmt.Fprintln(w, "-1")
			continue
		}

		f, s := findMax(n), findMin(n)
		if f == -1 || s == -1 {
			fmt.Fprintln(w, "-1")
			continue
		}
		fmt.Fprintln(w, s, f)
	}

}

package main

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

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

func main() {
	scanner := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	var ntests int
	fmt.Fscan(scanner, &ntests)
	for iter := 0; iter < ntests; iter++ {
		var n int64
		fmt.Fscan(scanner, &n)
		if n%2 == 1 || n == 2 {
			fmt.Fprintln(writer, "-1")
		} else {
			min := n / 4 // max
			max := n / 6 // hmm
			switch n - (6 * max) {
			case 2:
				max++
			case 4:
				max++
			}
			fmt.Fprintln(writer, max, min)
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)
var wrtr = bufio.NewWriterSize(os.Stdout, 10_000_000)
var rdr = bufio.NewScanner(os.Stdin)
func gs() string  { rdr.Scan(); return rdr.Text() }
func gi() int     { i,e := strconv.Atoi(gs()); if e != nil {panic(e)}; return i }
func gi64() int64 { i,e := strconv.ParseInt(gs(),10,64); if e != nil {panic(e)}; return i }
func main() {
	//f1, _ := os.Create("cpu.prof"); pprof.StartCPUProfile(f1); defer pprof.StopCPUProfile()
	defer wrtr.Flush()
	infn := ""; if infn == "" && len(os.Args) > 1 {	infn = os.Args[1] }
	if infn != "" {	f, e := os.Open(infn); if e != nil { panic(e) }; rdr = bufio.NewScanner(f) }
	rdr.Split(bufio.ScanWords); rdr.Buffer(make([]byte,1024),1_000_000_000)
	// PROGRAM STARTS HERE
	T := gi()
	for tt:=1;tt<=T;tt++ {
		N := gi64()
		m1,m2 := int64(0),int64(0)
		if N % 2 == 1 || N < 4 {
			m1 = -1
		} else {
			n1 := N; for n1 % 6 != 0 { n1 -= 4; m1++ }; m1 += n1/6
			n2 := N; for n2 % 4 != 0 { n2 -= 6; m2++ }; m2 += n2/4
		}
		if m1 == -1 { fmt.Fprintln(wrtr,-1) } else { fmt.Fprintf(wrtr,"%v %v\n",m1,m2) }
	}
}

package main

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

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

	min := func(a, b int64) int64 {
		if a < b {
			return a
		}
		return b
	}
	max := func(a, b int64) int64 {
		if a > b {
			return a
		}
		return b
	}

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

		// 4x
		// 6y
		// 4x + 6y = n

		var mn, mx int64 = 1e18, 0
		for i := 0;i <= 10; i++ {
			if int64(i * 4) > n {
				break
			}
			if int64(n - int64(i * 4)) % 6 == 0 {
				j := int64((n - int64(i * 4)) / 6)
				mn = min(mn, int64(int64(i) + j))
				mx = max(mx, int64(int64(i) + j))
			}
		}
		for i := 0;i <= 10; i++ {
			if int64(i * 6) > n {
				break
			}
			if int64(n - int64(i * 6)) % 4 == 0 {
				j := int64((n - int64(i * 6)) / 4)
				mn = min(mn, int64(int64(i) + j))
				mx = max(mx, int64(int64(i) + j))
			}
		}
		if mn == 1e18 {
			fmt.Fprintln(out, -1)
		} else {
			fmt.Fprintln(out, mn, mx)
		}
	}
}

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

package main

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

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

func slove() {
	var n int64
	fmt.Fscan(r, &n)
	if n % 2 == 1 || n <= 3{
		fmt.Fprintln(w, "-1")
		return
	}
	var ll, rr int64
	if n % 6 == 2 {
		ll = 2 + (n - 8) / 6
	} else {
		ll = n / 6
		if n % 6 != 0 {
			ll++
		}
	}
	if n % 4 == 2 {
		rr = 1 + (n - 6) / 4
	} else {
		rr = n / 4
	}
	fmt.Fprintln(w, ll, rr)
}

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

package main

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

type CF1679A struct {
	sc        *bufio.Reader
	split     []string
	index     int
	separator string
}

func (in *CF1679A) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1679A) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

func (in *CF1679A) NextInt() int {
	in.load()
	val, _ := strconv.Atoi(strings.TrimSpace(in.split[in.index]))
	in.index++
	return val
}

func (in *CF1679A) NextInt64() int64 {
	in.load()
	val, _ := strconv.ParseInt(strings.TrimSpace(in.split[in.index]), 10, 64)
	in.index++
	return val
}

func (in *CF1679A) NextString() string {
	in.load()
	val := strings.TrimSpace(in.split[in.index])
	in.index++
	return val
}

/**
Run solve the problem CF1679A
Date: 8/21/2022
User: wotan
URL: https://codeforces.com/problemset/problem/1679/A
Problem: CF1679A
**/
func (in *CF1679A) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt64()
		min := int64(0)
		max := int64(0)
		a := int64(math.Floor(float64(n) / 4.))
		llantasSobrantes := n - (a * 4)
		if llantasSobrantes == 2 || llantasSobrantes == 0 {
			max = a
			min = a
		} else {
			max = 0
		}

		b := int64(math.Floor(float64(n) / 6.))
		llantasSobrantes = n - (b * 6)
		if llantasSobrantes == 2 || llantasSobrantes == 4 {
			min = b + 1
		} else if llantasSobrantes == 0 {
			min = b
		}

		if min == 0 || max == 0 {
			fmt.Println(-1)
		} else {
			fmt.Println(min, max)
		}

	}
}

func NewCF1679A(r *bufio.Reader) *CF1679A {
	return &CF1679A{
		sc:        r,
		split:     []string{},
		index:     0,
		separator: " ",
	}
}

func main() {
	NewCF1679A(bufio.NewReader(os.Stdin)).Run()
}

package main

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

type CF1679A struct {
	sc        *bufio.Reader
	split     []string
	index     int
	separator string
}

func (in *CF1679A) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1679A) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

func (in *CF1679A) NextInt() int {
	in.load()
	val, _ := strconv.Atoi(strings.TrimSpace(in.split[in.index]))
	in.index++
	return val
}

func (in *CF1679A) NextInt64() int64 {
	in.load()
	val, _ := strconv.ParseInt(strings.TrimSpace(in.split[in.index]), 10, 64)
	in.index++
	return val
}

func (in *CF1679A) NextString() string {
	in.load()
	val := strings.TrimSpace(in.split[in.index])
	in.index++
	return val
}

/**
Run solve the problem CF1679A
Date: 8/21/2022
User: wotan
URL: https://codeforces.com/problemset/problem/1679/A
Problem: CF1679A
**/
func (in *CF1679A) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt64()
		if n%2 != 0 || n < 4 {
			fmt.Println(-1)
		} else {
			n /= int64(2)
			max := int64(float64(n) / float64(2))
			min := int64(0)
			if n%3 == 0 {
				min = n / int64(3)
			} else {
				min = n/int64(3) + int64(1)
			}

			fmt.Println(min, max)
		}
	}
}

func NewCF1679A(r *bufio.Reader) *CF1679A {
	return &CF1679A{
		sc:        r,
		split:     []string{},
		index:     0,
		separator: " ",
	}
}

func main() {
	NewCF1679A(bufio.NewReader(os.Stdin)).Run()
}

package main

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

type CF1679A struct {
	sc        *bufio.Reader
	split     []string
	index     int
	separator string
}

func (in *CF1679A) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1679A) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

func (in *CF1679A) NextInt() int {
	in.load()
	val, _ := strconv.Atoi(strings.TrimSpace(in.split[in.index]))
	in.index++
	return val
}

func (in *CF1679A) NextInt64() int64 {
	in.load()
	val, _ := strconv.ParseInt(strings.TrimSpace(in.split[in.index]), 10, 64)
	in.index++
	return val
}

func (in *CF1679A) NextString() string {
	in.load()
	val := strings.TrimSpace(in.split[in.index])
	in.index++
	return val
}

/**
Run solve the problem CF1679A
Date: 8/21/2022
User: wotan
URL: https://codeforces.com/problemset/problem/1679/A
Problem: CF1679A
**/
func (in *CF1679A) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt64()
		if n%2 != 0 || n < 4 {
			fmt.Println(-1)
		} else {
			n /= int64(2)
			max := int64(float64(n) / float64(2))
			min := int64(0)
			if n%3 == 0 {
				min = n / 3
			} else {
				min = n/3 + 1
			}

			fmt.Println(min, max)
		}
	}
}

func NewCF1679A(r *bufio.Reader) *CF1679A {
	return &CF1679A{
		sc:        r,
		split:     []string{},
		index:     0,
		separator: " ",
	}
}

func main() {
	NewCF1679A(bufio.NewReader(os.Stdin)).Run()
}

package main

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

type CF1679A struct {
	sc        *bufio.Reader
	split     []string
	index     int
	separator string
}

func (in *CF1679A) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1679A) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

func (in *CF1679A) NextInt() int {
	in.load()
	val, _ := strconv.Atoi(strings.TrimSpace(in.split[in.index]))
	in.index++
	return val
}

func (in *CF1679A) NextInt64() int64 {
	in.load()
	val, _ := strconv.ParseInt(strings.TrimSpace(in.split[in.index]), 10, 64)
	in.index++
	return val
}

func (in *CF1679A) NextString() string {
	in.load()
	val := strings.TrimSpace(in.split[in.index])
	in.index++
	return val
}

/**
Run solve the problem CF1679A
Date: 8/21/2022
User: wotan
URL: https://codeforces.com/problemset/problem/1679/A
Problem: CF1679A
**/
func (in *CF1679A) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt64()
		if n%2 != 0 || n < 4 {
			fmt.Println(-1)
		} else {
			n /= 2
			max := int64(float64(n) / float64(2))
			min := int64(float64(n) / float64(2))
			if n%3 == 0 {
				min = n / 3
			} else {
				min = n/3 + 1
			}

			fmt.Println(min, max)
		}
	}
}

func NewCF1679A(r *bufio.Reader) *CF1679A {
	return &CF1679A{
		sc:        r,
		split:     []string{},
		index:     0,
		separator: " ",
	}
}

func main() {
	NewCF1679A(bufio.NewReader(os.Stdin)).Run()
}

package main

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

type CF1679A struct {
	sc        *bufio.Reader
	split     []string
	index     int
	separator string
}

func (in *CF1679A) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1679A) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

func (in *CF1679A) NextInt() int {
	in.load()
	val, _ := strconv.Atoi(strings.TrimSpace(in.split[in.index]))
	in.index++
	return val
}

func (in *CF1679A) NextInt64() int64 {
	in.load()
	val, _ := strconv.ParseInt(strings.TrimSpace(in.split[in.index]), 10, 64)
	in.index++
	return val
}

func (in *CF1679A) NextString() string {
	in.load()
	val := strings.TrimSpace(in.split[in.index])
	in.index++
	return val
}

/**
Run solve the problem CF1679A
Date: 8/21/2022
User: wotan
URL: https://codeforces.com/problemset/problem/1679/A
Problem: CF1679A
**/
func (in *CF1679A) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt64()
		if n%2 != 0 || n < 4 {
			fmt.Println(-1)
		} else {
			n /= 2
			max := int64(float64(n) / 2.)
			min := int64(float64(n) / 2.)
			if n%3 == 0 {
				min = n / 3
			} else {
				min = n/3 + 1
			}

			fmt.Println(min, max)
		}
	}
}

func NewCF1679A(r *bufio.Reader) *CF1679A {
	return &CF1679A{
		sc:        r,
		split:     []string{},
		index:     0,
		separator: " ",
	}
}

func main() {
	NewCF1679A(bufio.NewReader(os.Stdin)).Run()
}

package main

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

type CF1679A struct {
	sc        *bufio.Reader
	split     []string
	index     int
	separator string
}

func (in *CF1679A) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1679A) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

func (in *CF1679A) NextInt() int {
	in.load()
	val, _ := strconv.Atoi(strings.TrimSpace(in.split[in.index]))
	in.index++
	return val
}

func (in *CF1679A) NextInt64() int64 {
	in.load()
	val, _ := strconv.ParseInt(strings.TrimSpace(in.split[in.index]), 10, 64)
	in.index++
	return val
}

func (in *CF1679A) NextString() string {
	in.load()
	val := strings.TrimSpace(in.split[in.index])
	in.index++
	return val
}

/**
Run solve the problem CF1679A
Date: 8/21/2022
User: wotan
URL: https://codeforces.com/problemset/problem/1679/A
Problem: CF1679A
**/
func (in *CF1679A) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt64()
		if n%2 != 0 || n < 4 {
			fmt.Println(-1)
		} else {
			n /= 2
			max := int64(math.Floor(float64(n) / 2.))
			min := int64(float64(n) / 2.)
			if n%3 == 0 {
				min = n / 3
			} else {
				min = (n / 3) + 1
			}

			fmt.Println(min, max)
		}
	}
}

func NewCF1679A(r *bufio.Reader) *CF1679A {
	return &CF1679A{
		sc:        r,
		split:     []string{},
		index:     0,
		separator: " ",
	}
}

func main() {
	NewCF1679A(bufio.NewReader(os.Stdin)).Run()
}

package main

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

type CF1679A struct {
	sc        *bufio.Reader
	split     []string
	index     int
	separator string
}

func (in *CF1679A) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1679A) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

func (in *CF1679A) NextInt() int {
	in.load()
	val, _ := strconv.Atoi(strings.TrimSpace(in.split[in.index]))
	in.index++
	return val
}

func (in *CF1679A) NextInt64() int64 {
	in.load()
	val, _ := strconv.ParseInt(strings.TrimSpace(in.split[in.index]), 10, 64)
	in.index++
	return val
}

func (in *CF1679A) NextString() string {
	in.load()
	val := strings.TrimSpace(in.split[in.index])
	in.index++
	return val
}

/**
Run solve the problem CF1679A
Date: 8/21/2022
User: wotan
URL: https://codeforces.com/problemset/problem/1679/A
Problem: CF1679A
**/
func (in *CF1679A) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt64()
		if n%2 != 0 || n < 4 {
			fmt.Println(-1)
		} else {
			n /= 2
			max := int64(math.Floor(float64(n) / 2.))
			min := int64(math.Ceil(float64(n) / 2.))
			if n%3 == 0 {
				min = n / 3
			} else {
				min = (n / 3) + 1
			}

			fmt.Println(min, max)
		}
	}
}

func NewCF1679A(r *bufio.Reader) *CF1679A {
	return &CF1679A{
		sc:        r,
		split:     []string{},
		index:     0,
		separator: " ",
	}
}

func main() {
	NewCF1679A(bufio.NewReader(os.Stdin)).Run()
}

package main

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

type CF1679A struct {
  sc        *bufio.Reader
  split     []string
  index     int
  separator string
}

func (in *CF1679A) GetLine() string {
  line, err := in.sc.ReadString('\n')
  if err != nil {
    fmt.Println("error line:", line, " err: ", err)
  }
  in.split = []string{}
  in.index = 0
  return line
}
func (in *CF1679A) load() {
  if len(in.split) <= in.index {
    in.split = strings.Split(in.GetLine(), in.separator)
    in.index = 0
  }
}

func (in *CF1679A) NextInt() int {
  in.load()
  val, _ := strconv.Atoi(strings.TrimSpace(in.split[in.index]))
  in.index++
  return val
}

func (in *CF1679A) NextInt64() int64 {
  in.load()
  val, _ := strconv.ParseInt(strings.TrimSpace(in.split[in.index]), 10, 64)
  in.index++
  return val
}

func (in *CF1679A) NextString() string {
  in.load()
  val := strings.TrimSpace(in.split[in.index])
  in.index++
  return val
}

/**
Run solve the problem CF1679A
Date: 8/21/2022
User: wotan
URL: https://codeforces.com/problemset/problem/1679/A
Problem: CF1679A
**/
func (in *CF1679A) Run() {
  for t := in.NextInt(); t > 0; t-- {
    n := in.NextInt64()
    if n%4 == 0 || n%6 == 0 {
      if n%4 == 0 && n%6 != 0 {
        fmt.Println(n/4, n/4)
      } else if n%6 == 0 && n%4 != 0 {
        fmt.Println(n/6, n/6)
      } else {
        fmt.Println(n/6, n/4)
      }

    } else {
      fmt.Println(-1)
    }
  }
}

func NewCF1679A(r *bufio.Reader) *CF1679A {
  return &CF1679A{
    sc:        r,
    split:     []string{},
    index:     0,
    separator: " ",
  }
}

func main() {
  NewCF1679A(bufio.NewReader(os.Stdin)).Run()
}

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

		if n%2 != 0 || n < 4 {
			fmt.Fprintln(out, -1)
		} else {
			a := n / 4
			b := n / 6
			if n%6 != 0 {
				b = a
			}
			fmt.Fprintln(out, b, a)

		}
	}
}

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

		a := n / 4
		if n%4 != 0 && (a-1)*4+6 != n {
			a = -1
		}

		b := n / 6
		if n%6 != 0 && (b-1)*6+4 != n {
			b = a
		}

		if a <= 0 && b <= 0 {
			fmt.Fprintln(out, -1)
		} else {
			fmt.Fprintln(out, b, a)
		}
	}
}

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

		a := n / 4
		if n%4 != 0 && (a-1)*4+6 != n {
			a = -1
		}

		b := n / 6
		if n%6 != 0 && (b-1)*6+4 != n {
			b = a
		}

		if a < 0 && b < 0 {
			fmt.Fprintln(out, -1)
		} else {
			fmt.Fprintln(out, b, a)
		}
	}
}

package main

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

func main_div2_791_A() {
	_in, _out := os.Stdin, os.Stdout
	in, out := bufio.NewReader(_in), bufio.NewWriter(_out)
	defer out.Flush()
	var cas int = 1
	Fscan(in, &cas)
	var sol func()
	// Coding...beg!
	sol = func() {
		var n int64
		Fscan(in, &n)
		// x * 4 + y * 6 = n
		// x * 2  + y * 3 = n/2
		// x * 2  + y * 2  + y = n/2

		// max min
		// max 4 best
		if n%2 != 0 {
			Fprintln(out, -1)
			return
		}
		var k = n / 2
		if k == 1 {
			Fprintln(out, -1)
			return
		}
		var min, max int64
		if k%2 == 0 {
			max = k / 2
		} else {
			k -= 3
			max = 1 + k/2
		}
		if k%3 == 0 {
			min = k / 3
		} else {
			if k%3 == 2 {
				k -= 2
				min = 1 + k/3
			} else {
				k -= 4
				min = 2 + k/3
			}
		}
		Fprintln(out, min, max)
	}
	// Coding...ove!
	for i := 0; i < cas; i++ {
		sol()
	}
}

func main() {
	main_div2_791_A()
}


All cinema halls in Berland are rectangles with K rows of K seats each, and K is an odd number. Rows and seats are numbered from 1 to K. For safety reasons people, who come to the box office to buy tickets, are not allowed to choose seats themselves. Formerly the choice was made by a cashier, but now this is the responsibility of a special seating program. It was found out that the large majority of Berland's inhabitants go to the cinema in order to watch a movie, that's why they want to sit as close to the hall center as possible. Moreover, a company of M people, who come to watch a movie, want necessarily to occupy M successive seats in one row. Let's formulate the algorithm, according to which the program chooses seats and sells tickets. As the request for M seats comes, the program should determine the row number x and the segment [yl, yr] of the seats numbers in this row, where yr - yl + 1 = M. From all such possible variants as a final result the program should choose the one with the minimum function value of total seats remoteness from the center. Say,  — the row and the seat numbers of the most "central" seat. Then the function value of seats remoteness from the hall center is . If the amount of minimum function values is more than one, the program should choose the one that is closer to the screen (i.e. the row number x is lower). If the variants are still multiple, it should choose the one with the minimum yl. If you did not get yet, your task is to simulate the work of this program.

The first line contains two integers N and K (1 ≤ N ≤ 1000, 1 ≤ K ≤ 99) — the amount of requests and the hall size respectively. The second line contains N space-separated integers Mi from the range [1, K] — requests to the program.

Output N lines. In the i-th line output «-1» (without quotes), if it is impossible to find Mi successive seats in one row, otherwise output three numbers x, yl, yr. Separate the numbers with a space.

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 main() {
  scanner = bufio.NewScanner(os.Stdin)
  scanner.Split(bufio.ScanWords)
  writer := bufio.NewWriter(os.Stdout)
  defer writer.Flush()

  n, k := getI(), getI()
  tails := make([][]int, k+1)
  for x := 1; x <= k; x++ {
    tails[x] = make([]int, k+1)
    for y := 1; y <= k; y++ {
      tails[x][y] = k+1-y
    }
  }
  c := (k+1)/2
  for ni := 0; ni < n; ni++ {
    m := getI()
    bestX, bestY, bestScore := -1, -1, -1
    for x := 1; x <= k; x++ {
      v := c-x
      if v < 0 {
        v *= -1
      }
      for y := 1; y <= k; y++ {
        if tails[x][y] < m {
          continue
        }
        la, ra, lb, rb := c, c, c, c
        L, R := y, y+m-1
        if L < c {
          la, ra = L, c-1
          if ra > R {
            ra = R
          }
        }
        if R > c {
          lb, rb = c+1, R
          if lb < L {
            lb = L
          }
        }
        score := (R+1-L)*v
        score += (ra+1-la)*c - (la+ra)*(ra+1-la)/2
        score += (lb+rb)*(rb+1-lb)/2 - (rb+1-lb)*c
        if bestScore == -1 || score < bestScore {
          bestX, bestY, bestScore = x, y, score
        }
      }
    }
    if bestScore == -1 {
      writer.WriteString("-1\n")
    } else {
      writer.WriteString(fmt.Sprintf("%d %d %d\n", bestX, bestY, bestY+m-1))
      for y := bestY+m-1; y >= bestY; y-- {
        tails[bestX][y] = 0
      }
      for y := bestY-1; y >= 1 && tails[bestX][y] != 0; y-- {
        tails[bestX][y] = bestY-y
      }
    }
  }
}

// https://codeforces.com/contest/10/problem/A
package main

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)

func main() {
	var (
		n, k int
	)

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input := bufio.NewReader(os.Stdin)
	fmt.Fscan(input, &n, &k)
	middle := k / 2
	cost := make([][]int, k)
	used := make([][]bool, k)
	for i := 0; i < k; i++ {
		used[i] = make([]bool, k)
		cost[i] = make([]int, k)
		cost[i][0] = abs(i - middle) + middle
		for j := 1; j < k; j++ {
			cost[i][j] = abs(i - middle) + abs(j - middle)
			cost[i][j] += cost[i][j - 1]
		}
	}

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

		mincost := int(1e9)
		minrow := -1
		minfrom := 0
		for row := 0; row < k; row++ {
			for s := 0; s + group - 1 < k; s++ {
				se := s + group - 1
				clean := true
				for cc := s; cc <= se; cc++ {
					if used[row][cc] {
						clean = false
						break
					}
				}
				if !clean {
					continue
				}

				currcost := cost[row][se]
				if s > 0 {
					currcost -= cost[row][s - 1]
				}

				if currcost < mincost {
					mincost = currcost
					minrow = row
					minfrom = s
				}
			}
		}
		if minrow < 0 {
			fmt.Println(-1)
		} else {
			fmt.Println(minrow + 1, minfrom + 1, minfrom + group)
			for cc := minfrom; cc < minfrom + group; cc++ {
				used[minrow][cc] = true
			}
		}
	}
}

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

// https://codeforces.com/contest/10/problem/A
package main

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)
type crows struct {
	lf, rf int
}

func main() {
	var (
		n, k int
	)

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input := bufio.NewReader(os.Stdin)
	fmt.Fscan(input, &n, &k)
	rows := make([]*crows, k)
	middle := k / 2 +1
	for i := 0; i < n; i++ {
		var group int
		fmt.Fscan(input, &group)
		if i < k {
			rows[i] = &crows{
				lf: (k - group) / 2,
				rf: k - group - ((k - group) / 2),
			}
			fmt.Println(i + 1, rows[i].lf + 1, rows[i].lf + group)
			continue
		}

		maxl := -1
		maxrow := -1
		minr := k + 1
		minrow := -1
		for row := 0; row < k; row++ {
			if rows[row].lf >= group {
				if maxl < rows[row].lf - group + 1 {
					maxrow = row
					maxl = rows[row].lf - group + 1
				}
			}

			if rows[row].rf >= group {
				if minr > k - rows[row].rf + 1 {
					minrow = row
					minr = k - rows[row].rf + 1
				}
			}
		}

		if maxrow < 0 && minrow < 0 {
			fmt.Println(-1)
		}  else {
			if maxrow >= 0 && minrow >= 0 {
				if middle - maxl < minr - middle {
					fmt.Println(maxrow + 1, maxl, rows[maxrow].lf)
					rows[maxrow].lf -= group
				} else {
					fmt.Println(maxrow + 1, minr, k - rows[minrow].rf + group)
					rows[minrow].rf -= group
				}
			} else if maxrow >= 0 {
				fmt.Println(maxrow + 1, maxl, rows[maxrow].lf)
					rows[maxrow].lf -= group
			} else {
				fmt.Println(maxrow + 1, minr, k - rows[minrow].rf + group)
					rows[minrow].rf -= group
			}
		}
	}
}

// https://codeforces.com/contest/10/problem/A
package main

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)

func main() {
	var (
		n, k int
	)

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input := bufio.NewReader(os.Stdin)
	fmt.Fscan(input, &n, &k)
	middle := k / 2 +1
	cost := make([][]int, k)
	used := make([][]bool, k)
	for i := 0; i < k; i++ {
		cost[i] = make([]int, k)
		used[i] = make([]bool, k)
		for j := 0; j < k; j++ {
			cost[i][j] = abs(i - middle) + (j - middle)
			if i > 0 {
				cost[i][j] += cost[i - 1][j]
			}
		}
	}

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

		mincost := int(1e9)
		minrow := -1
		minfrom := 0
		for row := 0; row < k; row++ {
			for s := 0; s <= k - group; s++ {
				se := s + group - 1
				clean := true
				for cc := s; cc <= se; cc++ {
					if used[row][cc] {
						clean = false
						break
					}
				}
				if !clean {
					continue
				}

				if mincost > cost[row][s + group - 1] - cost[row][s] {
					mincost = cost[row][s + group - 1] - cost[row][s]
					minrow = row
					minfrom = s
				}
			}
		}
		if minrow < 0 {
			fmt.Println(-1)
		} else {
			fmt.Println(minrow + 1, minfrom + 1, minfrom + group)
			for cc := minfrom; cc < minfrom + group; cc++ {
				used[minrow][cc] = true
			}
		}
	}
}

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


A sequence of non-negative integers $$$a_1, a_2, \dots, a_n$$$ is called growing if for all $$$i$$$ from $$$1$$$ to $$$n - 1$$$ all ones (of binary representation) in $$$a_i$$$ are in the places of ones (of binary representation) in $$$a_{i + 1}$$$ (in other words, $$$a_i \:\&\: a_{i + 1} = a_i$$$, where $$$\&$$$ denotes bitwise AND). If $$$n = 1$$$ then the sequence is considered growing as well.For example, the following four sequences are growing:   $$$[2, 3, 15, 175]$$$ — in binary it's $$$[10_2, 11_2, 1111_2, 10101111_2]$$$;  $$$[5]$$$ — in binary it's $$$[101_2]$$$;  $$$[1, 3, 7, 15]$$$ — in binary it's $$$[1_2, 11_2, 111_2, 1111_2]$$$;  $$$[0, 0, 0]$$$ — in binary it's $$$[0_2, 0_2, 0_2]$$$. The following three sequences are non-growing:   $$$[3, 4, 5]$$$ — in binary it's $$$[11_2, 100_2, 101_2]$$$;  $$$[5, 4, 3]$$$ — in binary it's $$$[101_2, 100_2, 011_2]$$$;  $$$[1, 2, 4, 8]$$$ — in binary it's $$$[0001_2, 0010_2, 0100_2, 1000_2]$$$. Consider two sequences of non-negative integers $$$x_1, x_2, \dots, x_n$$$ and $$$y_1, y_2, \dots, y_n$$$. Let's call this pair of sequences co-growing if the sequence $$$x_1 \oplus y_1, x_2 \oplus y_2, \dots, x_n \oplus y_n$$$ is growing where $$$\oplus$$$ denotes bitwise XOR.You are given a sequence of integers $$$x_1, x_2, \dots, x_n$$$. Find the lexicographically minimal sequence $$$y_1, y_2, \dots, y_n$$$ such that sequences $$$x_i$$$ and $$$y_i$$$ are co-growing.The sequence $$$a_1, a_2, \dots, a_n$$$ is lexicographically smaller than the sequence $$$b_1, b_2, \dots, b_n$$$ if there exists $$$1 \le k \le n$$$ such that $$$a_i = b_i$$$ for any $$$1 \le i < k$$$ but $$$a_k < b_k$$$.

The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$). Then $$$t$$$ test cases follow. The first line of each test case contains an integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — length of the sequence $$$x_i$$$. The second line contains $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$0 \le x_i < 2^{30}$$$) — elements of the sequence $$$x_i$$$. It is guaranteed that the sum of $$$n$$$ overall all test cases doesn't exceed $$$2 \cdot 10^5$$$.

For each test case, print $$$n$$$ integers $$$y_1, y_2, \dots, y_n$$$ ($$$0 \le y_i < 2^{30}$$$) — lexicographically minimal sequence such that such that it's co-growing with given sequence $$$x_i$$$.

package main

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

const N int = 2e5 + 10

var a, b [N]int

func solve(num int, res int) (ans int, sum int) {
	for j := 0; ; j++ {
		x, y := res>>j, num>>j
		if x == 0 && y == 0 {
			break
		}

		if x&1 == 1 && y&1 == 0 {
			ans += 1 << j
			sum += 1 << j
		} else if y&1 == 1 {
			sum += 1 << j
		}
	}
	return
}

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

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

		//b[1] = 0
		res := a[1]
		b[1] = 0
		for i := 2; i <= n; i++ {
			b[i], res = solve(a[i], res)
		}

		for i := 1; i < n; i++ {
			Fprint(out, b[i], " ")
		}
		Fprint(out, b[n], " ")
	}
}

package main

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

const N int = 2e5 + 10

var a, b [N]int

func solve(num int, res int) (ans int, sum int) {
	for j := 0; ; j++ {
		x, y := res>>j, num>>j
		if x == 0 && y == 0 {
			break
		}

		if x&1 == 1 && y&1 == 0 {
			ans += 1 << j
			sum += 1 << j
		} else if y&1 == 1 {
			sum += 1 << j
		}
	}
	return
}

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

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

		b[1] = 0
		res := a[1]
		for i := 2; i <= n; i++ {
			b[i], res = solve(a[i], res)
		}

		for i := 1; i < n; i++ {
			Fprint(out, b[i], " ")
		}
		Fprint(out, b[n], " ")
	}
}

package main

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

const N int = 2e5 + 10

var a, b [N]int

func solve(num int, res int) (ans int, sum int) {
	for j := 0; ; j++ {
		x, y := res>>j, num>>j
		if x == 0 && y == 0 {
			break
		}

		if x&1 == 1 && y&1 == 0 {
			ans += 1 << j
			sum += 1 << j
		} else if y&1 == 1 {
			sum += 1 << j
		}
	}
	return
}

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

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

		b[1] = 0
		res := a[1]
		for i := 2; i <= n; i++ {
			b[i], res = solve(a[i], res)
		}

		for i := 1; i < n; i++ {
			Fprintf(out, "%d ", b[i])
		}
		Fprintf(out, "%d\n", b[n])
	}
}

package main

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

const N int = 2e5 + 10

var a, b [N]int

func solve(num int, res int) (ans int, sum int) {
	for j := 0; ; j++ {
		x, y := res>>j, num>>j
		if x == 0 && y == 0 {
			break
		}

		if x&1 == 1 && y&1 == 0 {
			ans += 1 << j
			sum += 1 << j
		} else if y&1 == 1 {
			sum += 1 << j
		}
	}
	return
}

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

	T := 0
	fmt.Fscanf(in, "%d\n", &T)
	for ; T > 0; T-- {
		var n int
		fmt.Fscanf(in, "%d\n", &n)
		for i := 1; i <= n; i++ {
			fmt.Fscanf(in, "%d", &a[i])
		}
		fmt.Fscanf(in, "\n")

		b[1] = 0
		res := a[1]
		for i := 2; i <= n; i++ {
			b[i], res = solve(a[i], res)
		}

		for i := 1; i < n; i++ {
			fmt.Fprintf(out, "%d ", b[i])
		}
		fmt.Fprintf(out, "%d\n", b[n])
	}
}

package main

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

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

	// Get t
	t, _ := GetInt(input)

	for i:=0; i<t; i++ {
		// Get n
		input, _ = reader.ReadString('\n')
		var n int
		n, _ = GetInt(input)

		// Get numbers
		input, _ = reader.ReadString('\n')
		var prev int
		prev, input = GetInt(input)
		fmt.Printf("%d ", 0)
		for i:=1; i<n; i++ {
			var num int
			num, input = GetInt(input)
			prev = prev | num
			y := prev ^ num
			fmt.Printf("%d ", y)
		}
		fmt.Println()
	}
}

// func abs(val float64) float64 {
//     if val >= 0.0 {
//         return val
//     }
//     return -val
// }

func GetInt(s string) (result int, output string) {
	iter, n := 0, len(s)
	for iter < n && s[iter] >= '0' && s[iter] <= '9' {
		iter++
	}
	result, _ = strconv.Atoi(s[:iter])
	// Skip the redundant characters
	for iter < n && (s[iter] < '0' || s[iter] > '9') {
		iter++
	}
	if iter == n {
		output = ""

	} else {
		output = s[iter:]
	}
	return
}

func GetInt64(s string) (result int64, output string) {
	iter, n := 0, len(s)
	for iter < n && s[iter] >= '0' && s[iter] <= '9' {
		iter++
	}
	result, _ = strconv.ParseInt(s[:iter], 10, 64)
	// Skip the redundant characters
	for iter < n && (s[iter] < '0' || s[iter] > '9') {
		iter++
	}
	if iter == n {
		output = ""

	} else {
		output = s[iter:]
	}
	return
}

package main

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

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

	// Get t
	t, _ := GetInt(input)

	for i:=0; i<t; i++ {
		// Get n
		input, _ = reader.ReadString('\n')
		var n int
		n, _ = GetInt(input)

		// Get numbers
		x := make([]int, n)
		input, _ = reader.ReadString('\n')
		for i:=0; i<n; i++ {
			x[i], input = GetInt(input)
		}

		y := make([]int, n)
		prev := x[0]
		for i:=1; i<n; i++ {
			prev = x[i] | prev
			y[i] = prev ^ x[i]
		}

		for _, val := range y {
			fmt.Printf("%d ", val)
		}
		fmt.Println()
	}
}

// func abs(val float64) float64 {
//     if val >= 0.0 {
//         return val
//     }
//     return -val
// }

func GetInt(s string) (result int, output string) {
	iter, n := 0, len(s)
	for iter < n && s[iter] >= '0' && s[iter] <= '9' {
		iter++
	}
	result, _ = strconv.Atoi(s[:iter])
	// Skip the redundant characters
	for iter < n && (s[iter] < '0' || s[iter] > '9') {
		iter++
	}
	if iter == n {
		output = ""

	} else {
		output = s[iter:]
	}
	return
}

func GetInt64(s string) (result int64, output string) {
	iter, n := 0, len(s)
	for iter < n && s[iter] >= '0' && s[iter] <= '9' {
		iter++
	}
	result, _ = strconv.ParseInt(s[:iter], 10, 64)
	// Skip the redundant characters
	for iter < n && (s[iter] < '0' || s[iter] > '9') {
		iter++
	}
	if iter == n {
		output = ""

	} else {
		output = s[iter:]
	}
	return
}

package main

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

func main() {
	var T int
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	fmt.Fscan(os.Stdin,&T)
	for T>0 {
		T--
		var n int
		fmt.Fscan(in,&n)
		V := make([]int,n)
		res := make([]int,n)
		for i:=0;i<n;i++ {
			fmt.Fscan(in,&V[i])
		}
		cur := 0
		for i:=0;i<n;i++ {
			for j:=0;j<30;j++ {
				if((cur&(1<<j))!=0&&(V[i]&(1<<j))==0) {
					res[i]^=(1<<j)
				}
			}
			cur=V[i]^res[i]
		}
		for i:=0;i<n;i++ {
			fmt.Fprintf(out,"%d ",res[i])
		}
		fmt.Fprintln(out)
	}
}

package main

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

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

	var t int
	var n int
	arr := make([]int64, 2e5+10)
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n)
		for i := 0; i < n; i++ {
			Fscan(in, &arr[i])
		}
		Fprintf(out, "%d ", 0)
		it := arr[0]
		for i := 1; i < n; i++ {
			it |= arr[i]
			Fprintf(out, "%d ", it^arr[i])
		}
		Fprintln(out)
	}
}

func main() {
	CF1547D(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 < T; t++ {
		var n int
		fmt.Fscan(in, &n)

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

		y := make([]int, n)
		for i := 1; i < n; i++ {
			d := x[i-1] & x[i]
			y[i] = d ^ x[i-1]
			x[i] ^= y[i]
		}

		for _, v := range y {
			fmt.Fprintf(out, "%d ", v)
		}
		fmt.Fprintln(out)
	}
}

package main

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

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

	var tcs int
	_, _ = fmt.Fscan(in, &tcs)

	for tc := 0; tc < tcs; tc++ {
		var n int
		_, _ = fmt.Fscan(in, &n)

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

		b := make([]int, n)
		for i := 1; i < n; i++ {
			c := a[i-1] ^ b[i-1]
			b[i] = c ^ (c & a[i])
		}

		for i := 0; i < n; i++ {
			_, _ = fmt.Fprintf(out, "%d ", b[i])
		}

		_, _ = fmt.Fprintln(out)
	}

	_ = out.Flush()
}

func abs(n int) int {
	if n < 0 {
		n = -n
	}

	return n
}

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
}

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, 4096), 1e9)
}

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

func sint64() int64 {
	s := sstring()
	i := int64(0)
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int64(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int64(r - '0')
	}
	return i
}

func sint64s(n int64) []int64 {
	a := make([]int64, n)
	for i := range a {
		a[i] = sint64()
	}
	return a
}

func sstring() string {
	if !scanner.Scan() {
		panic(scanner.Err())
	}
	return scanner.Text()
}

func main() {
	defer writer.Flush()
	for t := sint64(); t > 0; t-- {
		n := sint64()
		x := sint64s(n)
		res := sol(n, x)
		for _, r := range res {
			printf("%d ", r)
		}
		printf("\n")
	}
}

func sol(n int64, x []int64) []int64 {
	ret := make([]int64, n)
	for i := 1; i < len(ret); i++ {
		prev := x[i-1] ^ ret[i-1]
		if x[i]|prev == x[i] {
			continue // 0
		}
		ret[i] = (x[i] | prev) ^ x[i]
	}
	return ret
}

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([]int, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		b := make([]int, n)
		for i := 1; i < n; i++ {
			if a[i-1] & a[i] == a[i-1] {
				continue
			}
			xor := a[i-1]^a[i]
			need := a[i-1] & xor
			b[i] = need
			a[i] ^=need
		}
		for _, v := range b {
			Fprint(out, v, " ")
		}
		Fprintln(out)
	}

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

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

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


In Aramic language words can only represent objects.Words in Aramic have special properties:   A word is a root if it does not contain the same letter more than once.  A root and all its permutations represent the same object.  The root $$$x$$$ of a word $$$y$$$ is the word that contains all letters that appear in $$$y$$$ in a way that each letter appears once. For example, the root of "aaaa", "aa", "aaa" is "a", the root of "aabb", "bab", "baabb", "ab" is "ab".  Any word in Aramic represents the same object as its root. You have an ancient script in Aramic. What is the number of different objects mentioned in the script?

The first line contains one integer $$$n$$$ ($$$1 \leq n \leq 10^3$$$) — the number of words in the script. The second line contains $$$n$$$ words $$$s_1, s_2, \ldots, s_n$$$ — the script itself. The length of each string does not exceed $$$10^3$$$. It is guaranteed that all characters of the strings are small latin letters.

Output one integer — the number of different objects mentioned in the given ancient Aramic script.

NoteIn the first test, there are two objects mentioned. The roots that represent them are "a","ab".In the second test, there is only one object, its root is "amer", the other strings are just permutations of "amer".

package main

import (
	"fmt"
	"sort"
	"bufio"
	"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 main() {
	var manuscripts int

	scanf("%d\n", &manuscripts)
	ciphers := make([]string, manuscripts)

	for i := range ciphers {
		scanf("%s", &ciphers[i])
	}

	used := make(map[string]bool)

	for i := range ciphers {
		letterUsed := make(map[byte]bool)
		for j := range ciphers[i] {
			letterUsed[ciphers[i][j]] = true
		}

		var letters []byte
		for j := range letterUsed {
			letters = append(letters, j)
		}

		sort.Slice(letters, func(i, j int) bool { return letters[i] < letters[j] })
		used[string(letters)] = true
	}

	fmt.Printf("%d", len(used))
}

package main

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

func main() {
	var n int
	var line string
	fmt.Scanf("%d\n", &n)
	reader := bufio.NewReader(os.Stdin)
	line, _ = reader.ReadString('\n')
	arrSlova := strings.Split(strings.TrimSpace(line), " ")
	for i := range arrSlova{
		arrSlova[i]=delBuk(arrSlova[i])
	}
	sort.Strings(arrSlova)
	var m = make(map[string]int)
	for i:=0;i<n;i++{
		m[arrSlova[i]]++
	}
	var result int
	for range m{
		result++
	}
	fmt.Println(result)
}

func delBuk(in string) string{
	var m = make(map[byte]int)
	sl := len(in)
	for i:=0;i<sl;i++{
		m[in[i]]++
	}
	var result []byte
	for k := range m{
		result = append(result, k)
	}
	for j := 1; j < len(result); j++ {
		key := result[j]
		i := j - 1
		for i >= 0 && result[i] > key {
			result[i+1] = result[i]
			i = i - 1
		}
		result[i+1] = key
	}
	return string(result)
}

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 main() {
	defer writer.Flush()

	var n int
	scanf("%d\n", &n)
	s := make([]string, n)
	for i := range s {
		scanf("%s", &s[i])
	}
	obj := make(map[string]bool)
	for i := range s {
		letters := make(map[byte]bool)
		for j := range s[i] {
			letters[s[i][j]] = true
		}
		var word []byte
		for j := byte('a'); j <= 'z'; j++ {
			if letters[j] {
				word = append(word, j)
			}
		}
		obj[string(word)] = true
	}
	printf("%d", len(obj))
}

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 main() {
	defer writer.Flush()

	var n int
	scanf("%d\n", &n)
	s := make([]string, n)
	for i := range s {
		scanf("%s", &s[i])
	}
	obj, letters := make(map[string]bool), [26]bool{false}
	for i := range s {
		for j := range s[i] {
			letters[s[i][j]-'a'] = true
		}
		var word []byte
		for j := range letters {
			if letters[j] {
				word = append(word, byte(j+'a'))
			}
		}
		obj[string(word)] = true
		for j := range letters {
			letters[j] = false
		}
	}
	printf("%d", len(obj))
}

package main

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

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 scanlnInt(a interface{})           { fmt.Fscanf(reader, "%d\n", a) }
func scanChar(a interface{})            { fmt.Fscanf(reader, "%c", a) }
func scanString(a interface{})          { fmt.Fscanf(reader, "%s", a) }

func main() {
	defer writer.Flush()

	var n int
	scanlnInt(&n)
	s := make([]string, n)
	for i := range s {
		scanString(&s[i])
	}
	obj := make(map[string]bool)
	for i := range s {
		letters := make(map[byte]bool)
		for j := range s[i] {
			letters[s[i][j]] = true
		}
		var word []byte
		for j := range letters {
			word = append(word, j)
		}
		sort.Slice(word, func(i, j int) bool { return word[i] < word[j] })
		obj[string(word)] = true
	}

	printf("%d", len(obj))
}

package main

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

func main() {
	var (
		n int
		word string
		wordN int
		b []byte
	)

	b = make([]byte, 0, 1024)

	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()

	n, _ = strconv.Atoi(scanner.Text())

	var m = make(map[string]bool)
	var letters = make([]bool, 26)

	cnt := 0
	for i := 0; i < n; i++ {
		scanner.Scan()
		word = scanner.Text()
		wordN = len(word)

		// clear slice
		for j := range letters {
		   letters[j] = false
		}

		for j := 0; j < wordN; j++ {
			letters[word[j] - 'a'] = true
		}

		// clear slice
		b = b[:0]

		for j := 0; j < 26; j++ {
			if letters[j] {
				b = append(b, byte('a' + j))
			}
		}

		word = string(b)

		if m[word] {
			continue
		}

		cnt++

		m[word] = true
	}

	fmt.Println(cnt)
}

package main

import (
	"fmt"
)

func main() {
	var (
		n int
		s []byte
	)

	var meet = make([]bool, 24)

	fmt.Scanln(&n)

	cnt := 0
	for i := 0; i < n; i++ {
		fmt.Scan(&s)

		hasNew := false
		for _, ch := range s {
			pos := ch - 'a'

			if !meet[pos] {
				hasNew = true
			}

			meet[pos] = true
		}

		if hasNew {
			cnt++
		}
	}

	fmt.Println(cnt)
}