Cartoons

Tanya likes cartoons. She knows that $n$ new cartoons will be released in his favorite cinema: the $i$-th of them will be airing from the day $a_i$ to the day $b_i$ ($1 \le a_i \le b_i \le 10^9$).

The cinema has a special offer: there is a huge discount every day when only one cartoon is airing.

Tanya doesn’t care which cartoon she will watch but she’d like to save some money. That’s why she asks you to find any day $x$ when only one cartoon will be airing. Formally: find $x$ such that there is exactly one $i$ ($1 \le i \le n$) with $a_i \le x \le b_i$. If there are several possible answers, print any of them. If there is no such day, print -1.Input

The first line contains single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The following are descriptions of the $t$ test cases.

The first line of each test case contains a single integer $n$ ($1 \le n \le 2000$) — the number of cartoons.

In the next $n$ lines, the cartoons themselves are described, one per line, by a pair of integers $a_i$, $b_i$ ($1 \le a_i \le b_i \le 10^9$) — the first and last airing days for the $i$-th cartoon.

It is guaranteed that the sum of the values $n$ for all test cases in the input does not exceed $2000$.Output

Print $t$ answers to given test cases in the order in which they appear in the input: the $i$-th answer is such $x$, that only one cartoon will be airing on day $x$ or -1 if there are no such days.Exampleinput

5
1
1 1
3
2 1000000000
2 500000000
500000002 1000000000
3
1 2
3 4
1 4
2
4 11
4 9
3
1 5
10 10
1 5

output

1
500000001
-1
10
10

Note

In the third test case: at day $1$ and $2$, first and third cartoons will be airing, and days $3$ and $4$, second and third cartoons will be airing. So, there is no day when only one cartoon will be airing.

In the fourth test case, $11$ is also a possible answer.

Solution:

private fun readLn() = readLine()!! // string line
private fun readInt() = readLn().toInt() // single int
private fun readLong() = readLn().toLong() // single long
private fun readDouble() = readLn().toDouble() // single double
private fun readStrings() = readLn().split(" ") // list of strings
private fun readInts() = readStrings().map { it.toInt() } // list of ints
private fun readLongs() = readStrings().map { it.toLong() } // list of longs
private fun readDoubles() = readStrings().map { it.toDouble() } // list of doubles

var n = 0
var a = IntArray(0)
var b = IntArray(0)
var ans = 0

fun test(x: Int) {
var cnt = 0
for (i in 0 until n) {
if (a[i] <= x && x <= b[i]) {
            cnt += 1
        }
    }
    if (cnt == 1) {
        ans = x
    }
}

fun main(args: Array<String>) {
var t = readInt()
while (t-- > 0) {
n = readInt()
a = IntArray(n)
b = IntArray(n)
for (i in 0 until n) {
var (x, y) = readInts()
a[i] = x
b[i] = y
}
ans = -1
for (i in 0 until n) {
test(a[i])
test(b[i])
test(a[i] - 1)
test(b[i] + 1)
}
println(ans)
}
}