On vacations n pupils decided to go on excursion and gather all together. They need to overcome the path with the length l meters. Each of the pupils will go with the speed equal to v1. To get to the excursion quickly, it was decided to rent a bus, which has seats for k people (it means that it can't fit more than k people at the same time) and the speed equal to v2. In order to avoid seasick, each of the pupils want to get into the bus no more than once.Determine the minimum time required for all n pupils to reach the place of excursion. Consider that the embarkation and disembarkation of passengers, as well as the reversal of the bus, take place immediately and this time can be neglected.

The first line of the input contains five positive integers n, l, v1, v2 and k (1 ≤ n ≤ 10 000, 1 ≤ l ≤ 109, 1 ≤ v1 < v2 ≤ 109, 1 ≤ k ≤ n) — the number of pupils, the distance from meeting to the place of excursion, the speed of each pupil, the speed of bus and the number of seats in the bus.

Print the real number — the minimum time in which all pupils can reach the place of excursion. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.

NoteIn the first sample we should immediately put all five pupils to the bus. The speed of the bus equals 2 and the distance is equal to 10, so the pupils will reach the place of excursion in time 10 / 2 = 5.

package main

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

var bufin *bufio.Reader
var bufout *bufio.Writer

var n, k int
var l, v1, v2 float64

func main() {
	bufin = bufio.NewReader(os.Stdin)
	bufout = bufio.NewWriter(os.Stdout)

	fmt.Fscan(bufin, &n, &l, &v1, &v2, &k)

	g := (n + k - 1) / k
	d := l * (v1 + v2) / (2*v1*float64(g) + v2 - v1)
	t := d/v2 + (l-d)/v1

	fmt.Fprintln(bufout, t)

	defer bufout.Flush()
}

package main

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

func main() {
	in := bufio.NewScanner(os.Stdin)
	in.Split(bufio.ScanWords)

	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	in.Scan()
	n, _ := strconv.Atoi(in.Text())

	in.Scan()
	l, _ := strconv.ParseFloat(in.Text(), 64)

	in.Scan()
	v1, _ := strconv.ParseFloat(in.Text(), 64)

	in.Scan()
	v2, _ := strconv.ParseFloat(in.Text(), 64)

	in.Scan()
	k, _ := strconv.Atoi(in.Text())

	for n%k != 0 {
		n++
	}

	a := float64(n / k)
	d := l * (v1 + v2) / (2*v1*a + v2 - v1)
	res := d/v2 + (l-d)/v1
	out.WriteString(strconv.FormatFloat(res, 'f', -1, 64))
}

package main

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

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

func init() {
	in.Split(bufio.ScanWords)
}
func exit() {
	out.Flush()
}

// custom
func nextInt() int {
	in.Scan()
	ret, _ := strconv.Atoi(in.Text())
	return ret
}

/*
 */

var n *big.Rat
var l *big.Rat
var vp *big.Rat
var vb *big.Rat
var rongliang *big.Rat
var vhebp *big.Rat
var vchabp *big.Rat
var hehe *big.Rat
var haha *big.Rat
var NTime *big.Rat
var initdiff *big.Rat
var lpos *big.Rat
var inittime *big.Rat
var onetime *big.Rat
var backtime *big.Rat
var chasetime *big.Rat

func initAll() {
	vhebp = big.NewRat(0, 1)
	vchabp = big.NewRat(0, 1)
	hehe = big.NewRat(0, 1)
	haha = big.NewRat(0, 1)
	initdiff = big.NewRat(0, 1)
	NTime = big.NewRat(0, 1)
	lpos = big.NewRat(0, 1)
	inittime = big.NewRat(0, 1)
	onetime = big.NewRat(0, 1)
	backtime = big.NewRat(0, 1)
	chasetime = big.NewRat(0, 1)

	hehe.Add(vp, vb)
	vhebp.Set(hehe)
	hehe.Sub(vb, vp)
	vchabp.Set(hehe)

	var ntime, _ = hehe.Sub(n, rongliang).Quo(hehe, rongliang).Float64()
	NTime.SetFloat64(math.Ceil(ntime))
}
func cost(pos *big.Rat) *big.Rat {
	inittime.Quo(pos, vb)
	lpos.Mul(inittime, vp)
	initdiff.Sub(pos, lpos)
	backtime.Quo(initdiff, vhebp)
	hehe.Mul(backtime, vb)
	haha.Mul(backtime, vp)
	hehe.Add(hehe, haha)
	chasetime.Quo(hehe, vchabp)
	onetime.Add(backtime, chasetime)
	hehe.Mul(onetime, NTime)
	hehe.Mul(vp, hehe)
	hehe.Add(hehe, pos)
	hehe.Sub(hehe, l)
	return hehe.Abs(hehe)
}
func answer(pos *big.Rat) *big.Rat {
	inittime.Quo(pos, vb)
	lpos.Mul(inittime, vp)
	initdiff.Sub(pos, lpos)
	backtime.Quo(initdiff, vhebp)
	hehe.Mul(backtime, vb)
	haha.Mul(backtime, vp)
	hehe.Add(hehe, haha)
	chasetime.Quo(hehe, vchabp)
	onetime.Add(backtime, chasetime)
	hehe.Mul(onetime, NTime)
	return hehe.Add(inittime, hehe)
}
func main() {
	defer exit()

	n = big.NewRat(int64(nextInt()), 1)
	l = big.NewRat(int64(nextInt()), 1)
	vp = big.NewRat(int64(nextInt()), 1)
	vb = big.NewRat(int64(nextInt()), 1)
	rongliang = big.NewRat(int64(nextInt()), 1)
	initAll()

	if rongliang.Cmp(n) >= 0 {
		fmt.Fprintf(out, "%s\n", l.Quo(l, vb).FloatString(10))
		return
	}
	var cp = big.NewRat(1, 10000000000)
	var left = big.NewRat(1, 1000000000)
	var right = big.NewRat(0, 1)
	right.Set(l)
	var diff = big.NewRat(0, 1)
	var s_1_3 = big.NewRat(0, 1)
	var c13 = big.NewRat(0, 1)
	var s_1_2 = big.NewRat(0, 1)
	var c12 = big.NewRat(0, 1)
	var san = big.NewRat(3, 1)
	var er = big.NewRat(2, 1)
	for {
		if diff.Sub(right, left).Cmp(cp) < 0 {
			break
		}
		s_1_3.Quo(diff, san)
		s_1_3.Add(left, s_1_3)

		s_1_2.Quo(diff, er)
		s_1_2.Add(left, s_1_2)
		c13.Set(cost(s_1_3))
		c12.Set(cost(s_1_2))

		if c13.Cmp(c12) > 0 {
			left.Set(s_1_3)
		} else {
			right.Set(s_1_2)
		}
	}
	t := answer(left)
	fmt.Fprintf(out, "%s\n", t.FloatString(10))
}

package main

import "fmt"

const eps = 1e-8

func can(a, b, v1, v2, mi, l float64, it int) bool {
	for i := 0; i < it && mi > 0; i++ {
		t1 := (b - a) / (v1 + v2)
		c := a + t1*v1
		d := (v1*c - v2*l + v1*v2*(mi-t1)) / (v1 - v2)
		t2 := (d - c) / v2

		a = c + t2*v1
		b = d
		mi -= t1 + t2
	}
	return mi > 0
}

func solve(it, l, v1, v2 int) float64 {
	lo, hi := float64(0), float64(l)/float64(v1)
	if v1 >= v2 {
		return hi
	}
	for hi-lo > eps {
		mi := (lo + hi) / 2
		if mi-lo < eps {
			return hi
		}
		if can(0, 0, float64(v1), float64(v2), mi, float64(l), it) {
			hi = mi
		} else {
			lo = mi
		}
	}
	return hi
}

func main() {
	var n, l, v1, v2, k int
	fmt.Scanf("%d%d%d%d%d", &n, &l, &v1, &v2, &k)
	fmt.Println(solve((n+k-1)/k, l, v1, v2))
}

package main

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

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

func init() {
	in.Split(bufio.ScanWords)
}
func exit() {
	out.Flush()
}

// custom
func nextInt() int {
	in.Scan()
	ret, _ := strconv.Atoi(in.Text())
	return ret
}

/*
 */

var n *big.Rat
var l *big.Rat
var vp *big.Rat
var vb *big.Rat
var rongliang *big.Rat
var vhebp *big.Rat
var vchabp *big.Rat
var hehe *big.Rat
var haha *big.Rat
var NTime *big.Rat
var initdiff *big.Rat
var lpos *big.Rat
var inittime *big.Rat
var onetime *big.Rat
var backtime *big.Rat
var chasetime *big.Rat

func initAll() {
	vhebp = big.NewRat(0, 1)
	vchabp = big.NewRat(0, 1)
	hehe = big.NewRat(0, 1)
	haha = big.NewRat(0, 1)
	initdiff = big.NewRat(0, 1)
	NTime = big.NewRat(0, 1)
	lpos = big.NewRat(0, 1)
	inittime = big.NewRat(0, 1)
	onetime = big.NewRat(0, 1)
	backtime = big.NewRat(0, 1)
	chasetime = big.NewRat(0, 1)

	hehe.Add(vp, vb)
	vhebp.Set(hehe)
	hehe.Sub(vb, vp)
	vchabp.Set(hehe)

	var ntime, _ = hehe.Sub(n, rongliang).Quo(hehe, rongliang).Float64()
	NTime.SetFloat64(math.Ceil(ntime))
}
func cost(pos *big.Rat) *big.Rat {
	inittime.Quo(pos, vb)
	lpos.Mul(inittime, vp)
	initdiff.Sub(pos, lpos)
	backtime.Quo(initdiff, vhebp)
	hehe.Mul(backtime, vb)
	haha.Mul(backtime, vp)
	hehe.Add(hehe, haha)
	chasetime.Quo(hehe, vchabp)
	onetime.Add(backtime, chasetime)
	hehe.Mul(onetime, NTime)
	hehe.Mul(vp, hehe)
	hehe.Add(hehe, pos)
	hehe.Sub(hehe, l)
	return hehe.Abs(hehe)
}
func answer(pos *big.Rat) *big.Rat {
	inittime.Quo(pos, vb)
	lpos.Mul(inittime, vp)
	initdiff.Sub(pos, lpos)
	backtime.Quo(initdiff, vhebp)
	hehe.Mul(backtime, vb)
	haha.Mul(backtime, vp)
	hehe.Add(hehe, haha)
	chasetime.Quo(hehe, vchabp)
	onetime.Add(backtime, chasetime)
	hehe.Mul(onetime, NTime)
	return hehe.Add(inittime, hehe)
}
func main() {
	defer exit()

	n = big.NewRat(int64(nextInt()), 1)
	l = big.NewRat(int64(nextInt()), 1)
	vp = big.NewRat(int64(nextInt()), 1)
	vb = big.NewRat(int64(nextInt()), 1)
	rongliang = big.NewRat(int64(nextInt()), 1)
	initAll()

	if rongliang.Cmp(n) >= 0 {
		fmt.Fprintf(out, "%s\n", l.Quo(l, vb).FloatString(10))
		return
	}
	var cp = big.NewRat(1, 1000000000)
	var left = big.NewRat(1, 100000000)
	var right = big.NewRat(0, 1)
	right.Set(l)
	var diff = big.NewRat(0, 1)
	var s_1_3 = big.NewRat(0, 1)
	var c13 = big.NewRat(0, 1)
	var s_1_2 = big.NewRat(0, 1)
	var c12 = big.NewRat(0, 1)
	var san = big.NewRat(3, 1)
	var er = big.NewRat(2, 1)
	for {
		if diff.Sub(right, left).Cmp(cp) < 0 {
			break
		}
		s_1_3.Quo(diff, san)
		s_1_3.Add(left, s_1_3)

		s_1_2.Quo(diff, er)
		s_1_2.Add(left, s_1_2)
		c13.Set(cost(s_1_3))
		c12.Set(cost(s_1_2))

		if c13.Cmp(c12) > 0 {
			left.Set(s_1_3)
		} else {
			right.Set(s_1_2)
		}
	}
	t := answer(left)
	fmt.Fprintf(out, "%s\n", t.FloatString(10))
}

package main

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

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

func init() {
	in.Split(bufio.ScanWords)
}
func exit() {
	out.Flush()
}

// custom
func nextInt() int {
	in.Scan()
	ret, _ := strconv.Atoi(in.Text())
	return ret
}

/*
 */

var n int
var l int
var vp int
var vb int
var rongliang int

func cost(pos float64) float64 {
	// the number of the left pupils
	leftnum := n - rongliang
	// when bus stop to go back, where are the left pupils
	lpos := (pos / float64(vb)) * float64(vp)
	// when bus stop to go back, where are the in-bus pupils
	rpos := pos
	for leftnum > 0 {
		p_meet_b_t := (rpos - lpos) / float64(vp+vb)
		p_move := p_meet_b_t * float64(vp)
		l_now_pos := lpos + p_move
		r_now_pos := rpos + p_move
		b_zhui_t := (r_now_pos - l_now_pos) / float64(vb-vp)
		zong_t := p_meet_b_t + b_zhui_t
		lpos = lpos + float64(vp)*zong_t
		rpos = rpos + float64(vp)*zong_t
		leftnum -= rongliang
	}
	var re = float64(l) - rpos
	if re > 0 {
		return re
	}
	return -re
}
func answer(pos float64) float64 {
	var zongt = pos / float64(vb)
	// the number of the left pupils
	leftnum := n - rongliang
	// when bus stop to go back, where are the left pupils
	lpos := (pos / float64(vb)) * float64(vp)
	// when bus stop to go back, where are the in-bus pupils
	rpos := pos
	for leftnum > 0 {
		p_meet_b_t := (rpos - lpos) / float64(vp+vb)
		p_move := p_meet_b_t * float64(vp)
		l_now_pos := lpos + p_move
		r_now_pos := rpos + p_move
		b_zhui_t := (r_now_pos - l_now_pos) / float64(vb-vp)
		zong_t := p_meet_b_t + b_zhui_t
		lpos = lpos + float64(vp)*zong_t
		rpos = rpos + float64(vp)*zong_t
		leftnum -= rongliang
		zongt += zong_t
	}

	return zongt
}
func main() {
	defer exit()

	n = nextInt()
	l = nextInt()
	vp = nextInt()
	vb = nextInt()
	rongliang = nextInt()

	if rongliang >= n {
		fmt.Fprintf(out, "%f\n", float64(l)/float64(vb))
		return
	}
	var left = 0.0000001
	var right = float64(l) - 0.0000001
	var t float64
	for {
		if right-left < 0.0000002 {
			break
		}
		s_1_3 := left + (right-left)/3

		s_1_2 := left + (right-left)/2
		var c_s_1_3 = cost(s_1_3)
		var c_s_1_2 = cost(s_1_2)
		if c_s_1_3 < c_s_1_2 {
			right = s_1_2
		} else {
			left = s_1_3
		}
	}
	t = answer(left)
	fmt.Fprintf(out, "%f\n", t)
}


Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs.  Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) × B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l ≤ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r.

The first line of input contains three integers A, B and n (1 ≤ A, B ≤ 106, 1 ≤ n ≤ 105). Next n lines contain information about queries. i-th line contains integers l, t, m (1 ≤ l, t, m ≤ 106) for i-th query.

For each query, print its answer in a single line.

package main

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

var sc = bufio.NewScanner(os.Stdin)

func next() string {
	sc.Split(bufio.ScanWords)
	if !sc.Scan() {
		panic("could not scan a word from the reader")
	}
	return sc.Text()
}

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

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

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

func main() {
	A, B, N := nextLong(), nextLong(), nextInt()

	solve := func() int64 {
		L, T, M := nextLong(), nextLong(), nextLong()
		L--
		Sl := A + B*L
		lb, ub := L, (T-A)/B
		if ub < lb {
			return -1
		}
		if lb == ub {
			return lb + 1
		}
		ub++
		for ub-lb > 1 {
			d := ub - lb
			m := lb + d/2
			Sr := A + B*m
			s := (Sr + Sl) * (m - L + 1) / 2
			if s > min(M, m-L+1)*T {
				ub = m
			} else {
				lb = m
			}
		}
		return lb + 1
	}

	for i := 0; i < N; i++ {
		fmt.Println(solve())
	}
}


During a recent research Berland scientists found out that there were n cities in Ancient Berland, joined by two-way paths. Any two cities are joined by no more than one path. No path joins a city with itself. According to a well-known tradition, the road network was built so that it would be impossible to choose three cities from each of which one can get to any other one directly. That is, there was no cycle exactly as long as 3. Unfortunately, the road map has not been preserved till nowadays. Now the scientists are interested how much developed a country Ancient Berland was. Help them - find, what maximal number of roads could be in the country. You also have to restore any of the possible road maps.

The first line contains an integer n (1 ≤ n ≤ 100) — the number of cities in Berland.

On the first line must be printed number m — the maximal number of roads in Berland. Then print m lines containing two numbers each — the numbers of cities that the given road joins. The cities are numbered with integers from 1 to n. If there are several variants of solving the problem, print any of them.

package main

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

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

	var n int
	Fscan(in, &n)
	Fprintln(out, n*n/4)
	for i := 1; i < n; i++ {
		for j := i + 1; j <= n; j++ {
			if i&1 != j&1 {
				Fprintln(out, i, j)
			}
		}
	}
}

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


Ridhiman challenged Ashish to find the maximum valued subsequence of an array $$$a$$$ of size $$$n$$$ consisting of positive integers. The value of a non-empty subsequence of $$$k$$$ elements of $$$a$$$ is defined as $$$\sum 2^i$$$ over all integers $$$i \ge 0$$$ such that at least $$$\max(1, k - 2)$$$ elements of the subsequence have the $$$i$$$-th bit set in their binary representation (value $$$x$$$ has the $$$i$$$-th bit set in its binary representation if $$$\lfloor \frac{x}{2^i} \rfloor \mod 2$$$ is equal to $$$1$$$). Recall that $$$b$$$ is a subsequence of $$$a$$$, if $$$b$$$ can be obtained by deleting some(possibly zero) elements from $$$a$$$.Help Ashish find the maximum value he can get by choosing some subsequence of $$$a$$$.

The first line of the input consists of a single integer $$$n$$$ $$$(1 \le n \le 500)$$$ — the size of $$$a$$$. The next line consists of $$$n$$$ space-separated integers — the elements of the array $$$(1 \le a_i \le 10^{18})$$$.

Print a single integer — the maximum value Ashish can get by choosing some subsequence of $$$a$$$.

NoteFor the first test case, Ashish can pick the subsequence $$$\{{2, 3}\}$$$ of size $$$2$$$. The binary representation of $$$2$$$ is 10 and that of $$$3$$$ is 11. Since $$$\max(k - 2, 1)$$$ is equal to $$$1$$$, the value of the subsequence is $$$2^0 + 2^1$$$ (both $$$2$$$ and $$$3$$$ have $$$1$$$-st bit set in their binary representation and $$$3$$$ has $$$0$$$-th bit set in its binary representation). Note that he could also pick the subsequence $$$\{{3\}}$$$ or $$$\{{2, 1, 3\}}$$$.For the second test case, Ashish can pick the subsequence $$$\{{3, 4\}}$$$ with value $$$7$$$.For the third test case, Ashish can pick the subsequence $$$\{{1\}}$$$ with value $$$1$$$.For the fourth test case, Ashish can pick the subsequence $$$\{{7, 7\}}$$$ with value $$$7$$$.

package main

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

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

	n := readNum(reader)

	A := make([]uint64, n)

	s, _ := reader.ReadBytes('\n')
	var pos int = -1

	for i := 0; i < n; i++ {
		pos = readUint64(s, pos+1, &A[i])
	}

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

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

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

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

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

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

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

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

	return i
}

func solve(n int, A []uint64) uint64 {
	var best uint64

	for i := 0; i < n; i++ {
		best = max(best, A[i])
	}

	for i := 0; i < n; i++ {
		for j := i + 1; j < n; j++ {
			best = max(best, A[i]|A[j])
		}
	}

	for i := 0; i < n; i++ {
		for j := i + 1; j < n; j++ {
			for k := j + 1; k < n; k++ {
				best = max(best, A[i]|A[j]|A[k])
			}
		}
	}

	return best
}

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

package main

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

var checked []bool
var edges [][]int
var cnt int
var a, b, c, d, e, f []int
var children [][]int

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	t := int64(1)
	//fmt.Fscanf(in, "%d\n", &t)
	for tt := int64(0); tt < t; tt++ {
		n := 0
		fmt.Fscanf(in, "%d\n", &n)
		a := make([]int64, n)
		for i := 0; i < n; i++ {
			fmt.Fscanf(in, "%d", &a[i])
		}
		fmt.Fscanf(in, "\n")
		ans := a[0]
		if n == 2 {
			ans = ans | a[1]
		}
		for i := 0; i < n; i++ {
			for j := i + 1; j < n; j++ {
				for k := j + 1; k < n; k++ {
					if a[i]|a[j]|a[k] > ans {
						ans = a[i] | a[j] | a[k]
					}
				}
			}
		}
		fmt.Fprintf(out, "%d\n", ans)
	}
}

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

func countOne(cur int) {
	checked[cur] = true
	d[cur] = b[cur]
	e[cur] = 1
	f[cur] = c[cur]
	for _, v := range edges[cur] {
		if !checked[v] {
			countOne(v)
			d[cur] += d[v]
			e[cur] += e[v]
			f[cur] += f[v]
			children[cur] = append(children[cur], v)
		}
	}
}

package main

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

const maxN = 500

func main() {
	scanner := bufio.NewScanner(os.Stdin)

	buff := make([]byte, 2*1024*1024)

	scanner.Buffer(buff,  1024*1024)

	scanner.Scan()

	scanner.Text()

	scanner.Scan()

	line := scanner.Text()

	nums := MustParseFields(line)

	ans := solve4(nums)

	fmt.Println(ans)
}

func solve4(nums []int64) int64 {
	var sum int64

	if len(nums) == 1 {
		return nums[0]
	}

	if len(nums) == 2 {
		return nums[0] | nums[1]
	}

	for i := 0; i < len(nums); i++ {
		for j := i + 1; j < len(nums); j++ {
			for k:= j + 1; k < len(nums); k++ {
				s := nums[i] | nums[j] | nums[k]
				if s > sum {
					sum = s
				}
			}
		}
	}

	return sum
}

func MustParseFields(str string) []int64 {
	parts := strings.Fields(str)

	var ints []int64

	for i := 0; i < len(parts); i++ {
		v, err := strconv.ParseInt(parts[i], 10, 64);

		if err != nil {
			panic(err)
		}
		ints = append(ints, v)
	}
	return ints
}

/*
URL:
https://codeforces.com/contest/1365/problem/E
*/

package main

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

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

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

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

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

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

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

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

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

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

var (
	n int
	A []int64
)

func main() {
	n = ReadInt()
	A = ReadInt64Slice(n)

	if n == 1 {
		fmt.Println(A[0])
		return
	}

	if n == 2 {
		ans := A[0] | A[1]
		fmt.Println(ans)
		return
	}

	ans := int64(0)
	for i := 0; i < n; i++ {
		for j := i + 1; j < n; j++ {
			for k := j + 1; k < n; k++ {
				a, b, c := A[i], A[j], A[k]
				v := a | b | c

				ChMax(&ans, v)
			}
		}
	}

	fmt.Println(ans)
}

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

// NthBit returns nth bit value of an argument.
// n starts from 0.
func NthBit(num int64, nth int) int64 {
	return num >> uint(nth) & 1
}

// OnBit returns the integer that has nth ON bit.
// If an argument has nth ON bit, OnBit returns the argument.
func OnBit(num int64, nth int) int64 {
	return num | (1 << uint(nth))
}

// OffBit returns the integer that has nth OFF bit.
// If an argument has nth OFF bit, OffBit returns the argument.
func OffBit(num int64, nth int) int64 {
	return num & ^(1 << uint(nth))
}

// PopCount returns the number of ON bit of an argument.
func PopCount(num int64) int {
	res := 0

	for i := 0; i < 70; i++ {
		if ((num >> uint(i)) & 1) == 1 {
			res++
		}
	}

	return res
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return res
}

// Strtoi is a wrapper of strconv.Atoi().
// If strconv.Atoi() returns an error, Strtoi calls panic.
func Strtoi(s string) int {
	if i, err := strconv.Atoi(s); err != nil {
		panic(errors.New("[argument error]: Strtoi only accepts integer string"))
	} else {
		return i
	}
}

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

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

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

	return string(res)
}

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

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

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

	return string(res)
}

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

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1365E(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var n, ans int64
	Fscan(in, &n)
	a := make([]int64, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	if n == 1 {
		ans = a[0]
	} else if n == 2 {
		ans = a[0] | a[1]
	} else {
		for i, v := range a {
			for j, w := range a[i+1:] {
				for _, u := range a[j+1:] {
					if v|w|u > ans {
						ans = v | w | u
					}
				}
			}
		}
	}
	Fprint(out, ans)
}

func main() { CF1365E(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()
	scan := func(x ...interface{}) (n int, err error) {
		return fmt.Fscan(in, x...)
	}
	println := func(x ...interface{}) (n int, err error) {
		return fmt.Fprintln(out, x...)
	}

	var n int
	scan(&n)
	a := make([]int64, n)
	for i := range a {
		scan(&a[i])
	}
	ans := int64(0)
	for i := range a {
		for j := range a {
			for k := range a {
				tripleOr := a[i] | a[j] | a[k]
				if tripleOr > ans {
					ans = tripleOr
				}
			}
		}
	}
	println(ans)
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	scan := func(x ...interface{}) (n int, err error) {
		return fmt.Fscan(in, x...)
	}
	println := func(x ...interface{}) (n int, err error) {
		return fmt.Fprintln(out, x...)
	}

	var n int
	scan(&n)
	a := make([]int64, n)
	for i := range a {
		scan(&a[i])
	}
	ans := int64(0)
	for i := range a {
		for j := 0; j <= i; j++ {
			for k := 0; k <= j; k++ {
				tripleOr := a[i] | a[j] | a[k]
				if tripleOr > ans {
					ans = tripleOr
				}
			}
		}
	}
	println(ans)
}

package main

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

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

	n := readNum(reader)

	A := make([]uint64, n)

	s, _ := reader.ReadBytes('\n')
	var pos int = -1

	for i := 0; i < n; i++ {
		pos = readUint64(s, pos+1, &A[i])
	}

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

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

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

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

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

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

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

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

	return i
}

func solve(n int, A []uint64) uint64 {
	sort.Sort(Nums(A))

	var res uint64
	cnt := make([]int, 64)
	var k int

	update := func(num uint64, v int) {
		for i := 0; i < 64; i++ {
			if num>>i&1 == 1 {
				cnt[i] += v
			}
		}
	}

	check := func(pos int) bool {
		kk := max(1, k-2)

		for i := 63; i >= pos; i-- {
			if cnt[i] > 0 && cnt[i] < kk {
				return false
			}
		}
		return true
	}

	for i, pos := 0, 63; i < n && pos >= 0; pos-- {
		ii := i
		for ii < n && A[ii]>>pos&1 == 1 {
			ii++
		}

		for j := i; j < ii; j++ {
			update(A[j], 1)
			k++
			if !check(pos) {
				update(A[j], -1)
				k--
				break
			}
		}

		if cnt[pos] >= max(1, k-2) {
			res |= 1 << pos
		}

		i = ii
	}

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

type Nums []uint64

func (nums Nums) Len() int {
	return len(nums)
}

func (nums Nums) Less(i, j int) bool {
	return nums[i] > nums[j]
}

func (nums Nums) Swap(i, j int) {
	nums[i], nums[j] = nums[j], nums[i]
}

package main

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

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

	n := readNum(reader)

	A := make([]uint64, n)

	s, _ := reader.ReadBytes('\n')
	var pos int = -1

	for i := 0; i < n; i++ {
		pos = readUint64(s, pos+1, &A[i])
	}

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

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

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

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

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

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

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

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

	return i
}

func solve(n int, A []uint64) uint64 {
	sort.Sort(Nums(A))

	var res uint64
	cnt := make([]int, 64)
	var k int

	update := func(num uint64, v int) {
		for i := 0; i < 64; i++ {
			if num>>i&1 == 1 {
				cnt[i] += v
			}
		}
	}

	check := func(pos int) bool {
		kk := max(1, k-2)

		for i := 63; i >= pos; i-- {
			if cnt[i] > 0 && cnt[i] < kk {
				return false
			}
		}
		return true
	}

	for i, pos := 0, 63; i < n && pos >= 0; pos-- {
		ii := i
		for ii < n && A[ii]>>pos&1 == 1 {
			ii++
		}

		for j := i; j < ii; j++ {
			update(A[j], 1)
			k++
			if !check(pos) {
				update(A[j], -1)
				k--
				break
			}
		}

		if cnt[pos] > 0 {
			res |= 1 << pos
		}

		i = ii
	}

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

type Nums []uint64

func (nums Nums) Len() int {
	return len(nums)
}

func (nums Nums) Less(i, j int) bool {
	return nums[i] > nums[j]
}

func (nums Nums) Swap(i, j int) {
	nums[i], nums[j] = nums[j], nums[i]
}

package main

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

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

	n := readNum(reader)

	A := make([]uint64, n)

	s, _ := reader.ReadBytes('\n')
	var pos int = -1

	for i := 0; i < n; i++ {
		pos = readUint64(s, pos+1, &A[i])
	}

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

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

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

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

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

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

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

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

	return i
}

func solve(n int, A []uint64) uint64 {
	sort.Sort(Nums(A))

	cnt := make([]int, 64)
	var k int

	update := func(num uint64, v int) {
		for i := 0; i < 64; i++ {
			if num>>i&1 == 1 {
				cnt[i] += v
			}
		}
	}

	check := func(pos int) bool {
		kk := max(1, k-2)

		for i := 63; i >= pos; i-- {
			if cnt[i] > 0 && cnt[i] < kk {
				return false
			}
		}
		return true
	}

	for i, pos := 0, 63; i < n && pos >= 0; pos-- {
		ii := i
		for ii < n && A[ii]>>pos&1 == 1 {
			ii++
		}

		for j := i; j < ii; j++ {
			update(A[j], 1)
			k++
			if !check(pos) {
				update(A[j], -1)
				k--
				break
			}
		}
		i = ii
	}

	var res uint64

	for i := 0; i < 64; i++ {
		if cnt[i] >= max(1, k-2) {
			res |= 1 << i
		}
	}

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

type Nums []uint64

func (nums Nums) Len() int {
	return len(nums)
}

func (nums Nums) Less(i, j int) bool {
	return nums[i] > nums[j]
}

func (nums Nums) Swap(i, j int) {
	nums[i], nums[j] = nums[j], nums[i]
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	scan := func(x ...interface{}) (n int, err error) {
		return fmt.Fscan(in, x...)
	}
	println := func(x ...interface{}) (n int, err error) {
		return fmt.Fprintln(out, x...)
	}

	var n int
	scan(&n)
	a := make([]int64, n)
	for i := range a {
		scan(&a[i])
	}
	ans := int64(0)
	for i := range a {
		for j := 0; j < i; j++ {
			for k := 0; k < j; k++ {
				tripleOr := a[i] | a[j] | a[k]
				if tripleOr > ans {
					ans = tripleOr
				}
			}
		}
	}
	println(ans)
}


Polycarp analyzes the prices of the new berPhone. At his disposal are the prices for $$$n$$$ last days: $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ is the price of berPhone on the day $$$i$$$.Polycarp considers the price on the day $$$i$$$ to be bad if later (that is, a day with a greater number) berPhone was sold at a lower price. For example, if $$$n=6$$$ and $$$a=[3, 9, 4, 6, 7, 5]$$$, then the number of days with a bad price is $$$3$$$ — these are days $$$2$$$ ($$$a_2=9$$$), $$$4$$$ ($$$a_4=6$$$) and $$$5$$$ ($$$a_5=7$$$).Print the number of days with a bad price.You have to answer $$$t$$$ independent data sets.

The first line contains an integer $$$t$$$ ($$$1 \le t \le 10000$$$) — the number of sets of input data in the test. Input data sets must be processed independently, one after another. Each input data set consists of two lines. The first line contains an integer $$$n$$$ ($$$1 \le n \le 150000$$$) — the number of days. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$), where $$$a_i$$$ is the price on the $$$i$$$-th day. It is guaranteed that the sum of $$$n$$$ over all data sets in the test does not exceed $$$150000$$$.

Print $$$t$$$ integers, the $$$j$$$-th of which should be equal to the number of days with a bad price in the $$$j$$$-th input data set.

package main

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

type in struct {
	Scanner *bufio.Scanner
}

func (in *in) s() string {
	in.Scanner.Scan()
	return in.Scanner.Text()
}
func (in *in) i() int64 {
	return in.ibase(10)
}
func (in *in) ibase(base int) int64 {
	i, _ := strconv.ParseInt(in.s(), base, 64)
	return i
}
func (in *in) f() float64 {
	i, _ := strconv.ParseFloat(in.s(), 64)
	return i
}
func (in *in) ss(n int64) []string {
	a := make([]string, n)
	for i := range a {
		a[i] = in.s()
	}
	return a
}
func (in *in) is(n int64) []int64 {
	a := make([]int64, n)
	for i := range a {
		a[i] = in.i()
	}
	return a
}
func (in *in) fs(n int64) []float64 {
	a := make([]float64, n)
	for i := range a {
		a[i] = in.f()
	}
	return a
}
func (in *in) imat(h int64, w int64) [][]int64 {
	a := make([][]int64, h)
	for y := range a {
		a[y] = make([]int64, w)
		for x := range a[y] {
			a[y][x] = in.i()
		}
	}
	return a
}

func stdin() in {
	scn := bufio.NewScanner(os.Stdin)
	scn.Split(bufio.ScanWords)
	max := 1048576
	buf := make([]byte, max)
	scn.Buffer(buf, max)
	return in{Scanner: scn}
}

func main() {
	in := stdin()
	t := in.i()
	for i := int64(0); i < t; i++ {
		n := in.i()
		arr := in.is(n)
		mi := arr[n-1]
		ans := 0
		for j := n - 2; j >= 0; j-- {
			if arr[j] <= mi {
				mi = arr[j]
			} else {
				ans++
			}
		}
		fmt.Println(ans)
	}
}

package main

import (
	"bufio"
	"container/heap"
	"fmt"
	"os"
	"strconv"
)

func main() {
	sc := NewScanner()
	T := sc.NextInt()
	for p := 0; p < T; p++ {
		N := sc.NextInt()
		A := sc.NextIntArray()
		pq := make(PriorityQueue, N)
		i := 0
		for _, priority := range A {
			pq[i] = &Item{
				value:    priority,
				priority: -priority,
				index:    i,
			}
			i++
		}
		heap.Init(&pq)

		count := 0
		delMap := map[int]int{}
		for _, v := range A {
			delMap[v]++

		retry:
			if len(pq) == 0 {
				break
			}
			Q := heap.Pop(&pq).(*Item)
			if _, ok := delMap[Q.value]; ok {
				delMap[Q.value]--
				if delMap[Q.value] == 0 {
					delete(delMap, Q.value)
				}
				goto retry
			}
			//fmt.Println(Q.value)
			if Q.value < v {
				count++
			}

			heap.Push(&pq, Q)
		}
		fmt.Println(count)
	}

}

// https://golang.org/pkg/container/heap/

// An Item is something we manage in a priority queue.
type Item struct {
	value    int // The value of the item; arbitrary.
	priority int // The priority of the item in the queue.
	// The index is needed by update and is maintained by the heap.Interface methods.
	index int // The index of the item in the heap.
}

// A PriorityQueue implements heap.Interface and holds Items.
type PriorityQueue []*Item

func (pq PriorityQueue) Len() int { return len(pq) }

func (pq PriorityQueue) Less(i, j int) bool {
	// We want Pop to give us the highest, not lowest, priority so we use greater than here.
	return pq[i].priority > pq[j].priority
}

func (pq PriorityQueue) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
	pq[i].index = i
	pq[j].index = j
}

func (pq *PriorityQueue) Push(value interface{}) {
	n := len(*pq)
	item := value.(*Item)
	item.index = n
	*pq = append(*pq, item)
}

func (pq *PriorityQueue) Pop() interface{} {
	old := *pq
	n := len(old)
	item := old[n-1]
	item.index = -1 // for safety
	*pq = old[0 : n-1]
	return item
}

// update modifies the priority and value of an Item in the queue.
func (pq *PriorityQueue) update(item *Item, value int, priority int) {
	item.value = value
	item.priority = priority
	heap.Fix(pq, item.index)
}

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

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

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

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

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

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

	return mp
}

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

package main
import "fmt"
import "bufio"
import "os"

var input *bufio.Reader

func main(){
	input = bufio.NewReader(os.Stdin)
	var T int
	fmt.Fscanf(input, "%d\n", &T)
	for ; T>0; T-- {
		var n int
		fmt.Fscanf(input, "%d\n", &n)
		arr := make([]int, n)
		for i:=0; i<n; i++ {
			fmt.Fscanf(input, "%d", &arr[i])
		}
		fmt.Fscanf(input, "\n");
		ans := 0
		min := arr[n-1]
		for i:=n-2; i>=0; i-- {
			if arr[i]>min {
				ans++
			} else {
				min=arr[i]
			}
		}
		fmt.Println(ans)
	}
}

package main

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

// reusable

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 abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}

func valueCounts(arr []int) map[int]int {
	dict := make(map[int]int)
	for _, num := range arr {
		dict[num] = dict[num] + 1
	}
	return dict
}

func minArray(arr []int) int {
	sort.Ints(arr)
	return arr[0]
}

// solution

func readInput() []int {
	var n int
	scanf("%d\n", &n)
	input := make([]int, n)
	for i := 0; i < n-1; i++ {
		scanf("%d", &input[i])
	}
	scanf("%d\n", &input[n-1])
	return input
}

func solve(arr []int) int {
	badDays := 0
	if len(arr) == 1 {
		return 0
	}
	minPrice := arr[len(arr)-1]
	for i := len(arr) - 2; i >= 0; i-- {
		if minPrice < arr[i] {
			badDays++
		} else {
			minPrice = arr[i]
		}
	}
	return badDays
}

func main() {
	defer writer.Flush()
	var t int
	scanf("%d\n", &t)
	for i := 0; i < t; i++ {
		input := readInput()
		printf("%d\n", solve(input))
	}
}

package main

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

func main() {

	//stdin, err := os.Open("in.txt")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdin.Close()
	stdin := os.Stdin
	reader := bufio.NewReaderSize(stdin, 1024*1024)

	//stdout, err := os.Create("test.txt")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdout.Close()
	stdout := os.Stdout
	writer := bufio.NewWriterSize(stdout, 1024*1024)

	var q int
	fmt.Fscanf(reader, "%d\n", &q)

	for i := 0; i < q; i++ {
		var n int
		fmt.Fscanf(reader, "%d\n", &n)

		a, b := make([]int, n, n), make([]int, n, n)
		for j := 0; j < n; j++ {
			fmt.Fscanf(reader, "%d", &a[j])
		}
		b[n-1] = a[n-1]
		for j := n - 2; j >= 0; j-- {
			if a[j] < b[j+1] {
				b[j] = a[j]
			} else {
				b[j] = b[j+1]
			}
		}

		var c int
		for j := 0; j < n; j++ {
			if b[j] < a[j] {
				c++
			}
		}

		fmt.Fprintf(writer, "%d\n", c)

		fmt.Fscanf(reader, "\n")
	}

	writer.Flush()
}

// Codeforces 1.12.6
// AtCoder 1.6
// Paiza 1.7.3

package main

import (
	"bufio"
	"container/heap"
	"fmt"
	"os"
	"strconv"
	"strings"
)

type in struct {
	Scanner *bufio.Scanner
}

func (in *in) s() string {
	in.Scanner.Scan()
	return in.Scanner.Text()
}
func (in *in) i() int64 {
	return in.ibase(10)
}
func (in *in) ibase(base int) int64 {
	i, _ := strconv.ParseInt(in.s(), base, 64)
	return i
}
func (in *in) f() float64 {
	i, _ := strconv.ParseFloat(in.s(), 64)
	return i
}
func (in *in) ss(n int64) []string {
	a := make([]string, n)
	for i := range a {
		a[i] = in.s()
	}
	return a
}
func (in *in) is(n int64) []int64 {
	a := make([]int64, n)
	for i := range a {
		a[i] = in.i()
	}
	return a
}
func (in *in) fs(n int64) []float64 {
	a := make([]float64, n)
	for i := range a {
		a[i] = in.f()
	}
	return a
}
func (in *in) imat(h int64, w int64) [][]int64 {
	a := make([][]int64, h)
	for y := range a {
		a[y] = make([]int64, w)
		for x := range a[y] {
			a[y][x] = in.i()
		}
	}
	return a
}

func stdin() in {
	scn := bufio.NewScanner(os.Stdin)
	scn.Split(bufio.ScanWords)
	max := 1048576
	buf := make([]byte, max)
	scn.Buffer(buf, max)
	return in{Scanner: scn}
}

type lm struct{}

func (*lm) max(a ...int64) int64 {
	t := a[0]
	for _, v := range a {
		if t < v {
			t = v
		}
	}
	return t
}
func (*lm) min(a ...int64) int64 {
	t := a[0]
	for _, v := range a {
		if t > v {
			t = v
		}
	}
	return t
}
func (*lm) sum(a ...int64) int64 {
	t := int64(0)
	for _, v := range a {
		t += v
	}
	return t
}
func (*lm) abs(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}
func (lm *lm) gcd(a, b int64) int64 {
	// returns greatest common divisor.
	if a < b {
		a, b = b, a
	}
	if b == 0 {
		return a
	}
	return lm.gcd(b, a%b)
}
func (lm *lm) lcm(a, b int64) int64 {
	// returns least common multiple.
	gcd := lm.gcd(a, b)
	return gcd * (a / gcd) * (b / gcd)
}

type su struct{}

func (*su) iarr(arr []int64) string {
	// int slice to string.
	ret := make([]string, len(arr))
	for i, x := range arr {
		ret[i] = strconv.FormatInt(x, 10)
	}
	return strings.Join(ret, " ")
}
func (*su) ilns(arr []int64) string {
	// int slice to string.
	ret := make([]string, len(arr))
	for i, x := range arr {
		ret[i] = strconv.FormatInt(x, 10)
	}
	return strings.Join(ret, "\n")
}
func (su *su) imat(matrix [][]int64) string {
	// int slice matrix to string.
	ret := make([]string, len(matrix))
	for i := range matrix {
		ret[i] = su.iarr(matrix[i])
	}
	return strings.Join(ret, "\n")
}
func (*su) bool(b bool) str {
	if b {
		return "Yes"
	}
	return "No"
}

type str string

func (s str) upper() string { return strings.ToUpper(string(s)) }
func (s str) lower() string { return strings.ToLower(string(s)) }

type runes []rune

func (p runes) Len() int           { return len(p) }
func (p runes) Less(i, j int) bool { return p[i] < p[j] }
func (p runes) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }

type comps [][]int64

func (p comps) Len() int { return len(p) }
func (p comps) Less(i, j int) bool {
	for x := range p[0] {
		// Yes, every row in matrix must have the same length.
		if p[i][x] < p[j][x] {
			return true
		}
	}
	return false
}

func (p comps) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p *comps) Push(arr interface{}) {
	x := arr.([]int64)
	*p = append(*p, x)
}
func (p *comps) Pop() interface{} {
	// https://golang.org/pkg/container/heap/
	var ret []int64
	n := len(*p)
	ret, *p = (*p)[n-1], (*p)[0:n-1]
	return ret
}
func (p *comps) pop() []int64 {
	x := heap.Pop(p).([]int64)
	return x
}

func main() {
	// utilities.
	const zr int64 = int64(0)
	const mod int64 = int64(1000000007)
	in, lm, su := stdin(), lm{}, su{}
	_, _, _ = in, lm, su

	// --- read here.
	t := in.i()

	// --- solve here.
	ans := make([]int64, 0)
	for i := zr; i < t; i++ {
		sans := zr
		n := in.i()
		a := in.is(n)
		mn := a[n-1]
		for i := n - 2; i >= 0; i-- {
			if mn < a[i] {
				sans++
			} else {
				mn = a[i]
			}
		}

		ans = append(ans, sans)
	}

	printable := su.ilns(ans)
	// --- print here.
	fmt.Println(printable)
}

package main

import (
	"bufio"
	"os"
	"runtime"
	"strconv"
)

/* solution */

func solve() {

	t := readInt()

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

}

func solveOne() {

	n := readInt()
	a := make([]int, n)

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

	ans := 0
	min := a[n-1]
	for i := n - 2; i >= 0; i-- {
		if a[i] > min {
			ans++
		} else {
			min = a[i]
		}
	}

	printInt(ans)

}

/* stubs */

func main() {

	var f *os.File
	if runtime.GOROOT() == "C:\\Soft\\Go" {
		f, _ = os.Open("input.txt")
		defer f.Close()
	} else {
		f = os.Stdin
	}

	writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	reader = bufio.NewScanner(f)

	const maxCapacity = 1024 * 1024
	buf := make([]byte, maxCapacity)
	reader.Buffer(buf, maxCapacity)

	reader.Split(bufio.ScanWords)

	solve()

}

var reader *bufio.Scanner
var writer *bufio.Writer

func read() string {
	reader.Scan()
	return reader.Text()
}

func readInt() int {
	reader.Scan()
	ret, _ := strconv.Atoi(reader.Text())
	return ret
}

func print(s string) {
	writer.WriteString(s)
	writer.WriteString("\n")
}

func printInt(i int) {
	print(strconv.Itoa(i))
}

func printInt64(i int64) {
	print(strconv.FormatInt(i, 10))
}

func readInt64() int64 {
	reader.Scan()
	ret, _ := strconv.ParseInt(reader.Text(), 10, 64)
	return ret
}

package main

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

func badDaysCounter(a []int) (cnt int) {
	min := a[len(a)-1]
	for i := len(a) - 1; i >= 0; i-- {
		if a[i] > min {
			cnt++
		} else {
			min = a[i]
		}
	}
	return cnt
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	var t int
	fmt.Fscan(in, &t)
	a := make([]int, 150000, 150000)
	for ; t > 0; t-- {
		var n int
		fmt.Fscan(in, &n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
		}
		fmt.Fprintln(out, badDaysCounter(a[0:n]))
	}
	out.Flush()
}

package main

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

func badDaysCounter(a []int) (cnt int) {
	min := a[len(a)-1]
	for i := len(a) - 1; i >= 0; i-- {
		if a[i] > min {
			cnt++
		} else {
			min = a[i]
		}
	}
	return cnt
}

func main() {
	in := bufio.NewReader(os.Stdin)
	var t int
	fmt.Fscan(in, &t)
	a := make([]int, 150000, 150000)
	for ; t > 0; t-- {
		var n int
		fmt.Fscan(in, &n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
		}
		fmt.Println(badDaysCounter(a[0:n]))
	}
}

package main

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

func badDaysCounter(a []int) (cnt int) {
	min := a[len(a)-1]
	for i := len(a) - 1; i >= 0; i-- {
		if a[i] > min {
			cnt++
		} else {
			min = a[i]
		}
	}
	return cnt
}

func main() {
	in := bufio.NewReaderSize(os.Stdin, 1024*1024)
	out := bufio.NewWriter(os.Stdout)
	var t int
	fmt.Fscan(in, &t)
	a := make([]int, 150000, 150000)
	for ; t > 0; t-- {
		var n int
		fmt.Fscan(in, &n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
		}
		fmt.Fprintln(out, badDaysCounter(a[0:n]))
	}
	out.Flush()
}

package main

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

func badDaysCounter(a []int) (cnt int) {
	min := a[len(a)-1]
	for i := len(a) - 1; i >= 0; i-- {
		if a[i] > min {
			cnt++
		} else {
			min = a[i]
		}
	}
	return cnt
}

func main() {
	in := bufio.NewReaderSize(os.Stdin, 409600)
	var t int
	fmt.Fscan(in, &t)
	a := make([]int, 150000, 150000)
	for ; t > 0; t-- {
		var n int
		fmt.Fscan(in, &n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
		}
		fmt.Println(badDaysCounter(a[0:n]))
	}
}

package main

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

func badDaysCounter(a []int) (cnt int) {
	min := a[len(a)-1]
	for i := len(a) - 1; i >= 0; i-- {
		if a[i] > min {
			cnt++
		} else {
			min = a[i]
		}
	}
	return cnt
}

func main() {
	in := bufio.NewReader(os.Stdin)
	var t int
	fmt.Fscan(in, &t)
	for ; t > 0; t-- {
		var n int
		fmt.Fscan(in, &n)
		a := make([]int, n, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
		}
		fmt.Println(badDaysCounter(a))
	}
}

package main

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

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

func main() {

	reader := bufio.NewReader(os.Stdin)

	var t, n int

	_, _ = fmt.Fscanln(reader, &t)

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

		fmt.Fscanln(reader, &n)

		arr := make([]int, n)

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

		low, cnt := arr[len(arr)-1], 0

		for j := n - 2; j >= 0; j-- {
			if arr[j] > low {
				cnt += 1
			}
			low = min(low, arr[j])
		}

		fmt.Println(cnt)
	}
}

package main

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

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

	t, _ := strconv.ParseInt(readLine(reader), 10, 64)
	for i := int(t) - 1; i >= 0; i-- {
		n, _ := strconv.ParseInt(readLine(reader), 10, 64)
		aTemp := strings.Split(readLine(reader), " ")
		cnt := 0
		min, _ := strconv.ParseInt(aTemp[int(n)-1], 10, 64)
		for j := int(n) - 2; j >= 0; j-- {
			aItem, _ := strconv.ParseInt(aTemp[j], 10, 64)
			if aItem == min {
				continue
			} else if aItem < min {
				min = aItem
			} else {
				cnt++
			}
		}
		fmt.Println(cnt)
	}
}

func readLine(reader *bufio.Reader) string {
	str, _, err := reader.ReadLine()
	if err == io.EOF {
		return ""
	}

	return strings.TrimRight(string(str), "\r\n")
}

//HOWDY! IT'S ME! A HARD BLACKISH DICK MOVE!
package main

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

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

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

//str := PrintlnInts(X...)

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

func main() {
	for t := ReadInt(); t > 0; t-- {
		n := ReadInt()
		a := ReadIntSlice(n)
		ans := 0
		min := a[n-1]
		for i := n - 2; i >= 0; i-- {
			if a[i] > min {
				ans++
			} else {
				min = a[i]
			}
		}
		fmt.Println(ans)
	}
}

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

func min(args ...int) int {
	if len(args) == 0 {
		panic("empty!")
	}
	min := args[0]
	for i := 1; i < len(args); i++ {
		if args[i] < min {
			min = args[i]
		}
	}
	return min
}

func max(args ...int) int {
	if len(args) == 0 {
		panic("empty!")
	}
	max := args[0]
	for i := 1; i < len(args); i++ {
		if args[i] > max {
			max = args[i]
		}
	}
	return max
}

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

func gcd(args ...int) int {
	if len(args) == 0 {
		panic("empty!")
	}
	gcd := args[0]
	for i := 1; i < len(args); i++ {
		gcd = _gcd(gcd, args[i])
	}
	return gcd
}

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

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

func init() {
	ReadString = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) func() string {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), int(1e+9))
	// Split sets the split function for the Scanner. The default split function is ScanLines.
	// Split panics if it is called after scanning has started.
	r.Split(bufio.ScanWords)

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

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

func readInt64() int64 {
	return readIntNbits(64)
}

func readInt() int {
	return int(readIntNbits(32))
}

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

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

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

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

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

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

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

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

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

	return string(res)
}

package main

import (
	"bufio"
	"container/heap"
	"fmt"
	"os"
	"strconv"
)

func main() {
	sc := NewScanner()
	T := sc.NextInt()
	for p := 0; p < T; p++ {
		N := sc.NextInt()
		A := sc.NextIntArray()
		pq := make(PriorityQueue, N)
		i := 0
		for _, priority := range A {
			pq[i] = &Item{
				value:    priority,
				priority: -priority,
				index:    i,
			}
			i++
		}
		heap.Init(&pq)

		count := 0
		delMap := map[int]int{}
		for _, v := range A {
			delMap[v]++

		retry:
			if len(pq) == 0 {
				break
			}
			Q := heap.Pop(&pq).(*Item)
			if _, ok := delMap[Q.value]; ok {
				delMap[Q.value]--
				if delMap[Q.value] == 0 {
					delete(delMap, Q.value)
				}
				goto retry
			}
			//fmt.Println(Q.value)
			if Q.value < v {
				count++
			}

			if Q.value != v {
				heap.Push(&pq, Q)
			}
		}
		fmt.Println(count)
	}

}

// https://golang.org/pkg/container/heap/

// An Item is something we manage in a priority queue.
type Item struct {
	value    int // The value of the item; arbitrary.
	priority int // The priority of the item in the queue.
	// The index is needed by update and is maintained by the heap.Interface methods.
	index int // The index of the item in the heap.
}

// A PriorityQueue implements heap.Interface and holds Items.
type PriorityQueue []*Item

func (pq PriorityQueue) Len() int { return len(pq) }

func (pq PriorityQueue) Less(i, j int) bool {
	// We want Pop to give us the highest, not lowest, priority so we use greater than here.
	return pq[i].priority > pq[j].priority
}

func (pq PriorityQueue) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
	pq[i].index = i
	pq[j].index = j
}

func (pq *PriorityQueue) Push(value interface{}) {
	n := len(*pq)
	item := value.(*Item)
	item.index = n
	*pq = append(*pq, item)
}

func (pq *PriorityQueue) Pop() interface{} {
	old := *pq
	n := len(old)
	item := old[n-1]
	item.index = -1 // for safety
	*pq = old[0 : n-1]
	return item
}

// update modifies the priority and value of an Item in the queue.
func (pq *PriorityQueue) update(item *Item, value int, priority int) {
	item.value = value
	item.priority = priority
	heap.Fix(pq, item.index)
}

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

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

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

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

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

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

	return mp
}

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