Given an array **A** of **N** length. We need to calculate the next greater element for each element in given array. If next greater element is not available in given array then we need to fill ‘_’ at that index place.

**Input:**

The first line contains an integer **T**, the number of test cases. For each test case, the first line contains an integer **n**, the size of the array. Next line contains n space separated integers denoting the elements of the array.

**Output:**

For each test case, the output is an array that displays next greater element to element at that index.

**Constraints:**

1 <= T <= 100

1 <= N <= 100

-10^{6} <= A_{i} <= 10^{6}

**Example:
Input**

2

9

6 3 9 8 10 2 1 15 7

4

13 6 7 12

**Output**:

7 6 10 9 15 3 2 _ 8

_ 7 12 13

**Explanation:
Testcase 1:** Here every element of the array has next greater element but at index 7, 15 is the greatest element of given array and no other element is greater from 15 so at the index of 15 we fill with '_'.

Author: Vanshika_pec

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

d_Coder00 | 134 |

dungeon_master1299 | 126 |

Astikeysingh | 120 |

rajendrashekhawat063 | 115 |

kaustavbhattachaarya | 111 |

Pulkit__Sharma__ | 646 |

Anirban166 | 531 |

abducodes | 501 |

Found_me | 454 |

Yo_Nehru | 404 |

blackshadows | 5331 |

Ibrahim Nash | 5219 |

akhayrutdinov | 5111 |

mb1973 | 4929 |

Quandray | 4559 |

Login to report an issue on this page.