399. Next Greater Element



Given an array, find the Next Greater Element (NGE) for each element. The Next Greater Element for an element x is the first greater element on the right side of x in the array. If there is no greater element, consider the next greater element as -1.

Input Format

The input consists of multiple test cases. The first line of each test case contains a single integer T, denoting the number of test cases. The following lines contain the test case data. Each test case begins with an integer N, denoting the size of the array. The next line contains N space-separated integers, representing the elements of the array.

Output Format

For each index, print the array element and its next greater element separated by a comma, each pair in a new line.



2 4 11 13 21 3 5 11 9 13 21 3


11,13 13,21 21,-1 3,-1 11,13 9,13 13,21 21,-1 3,-1


1 <= T <= 50 1 <= N <= 10^5

View Submissions