Gotham has been attacked by Joker . Bruce Wayne has deployed automatic machine gun at each tower of Gotham.

All the towers in Gotham are in straight line.

You are given no of towers 'n' followed by height of 'n' towers.

For every tower(p), find the height of the closest tower (towards the right), greater than the height of tower(p).

Now , Print sum of all such heights (mod 1000000001).

Note : If for a tower(k) , no such tower exsits then take its height as 0.

**Input:**

First line of the input contains t, the number of test cases. First line of each test case contains 'n' denoting no of towers.

This is followed by 'n' spaced integers h1,h2.....h(n) representing height of towers.

**Output:**

On a single line, output the sum(mod 1000000001).

**Constraints:**

1<=t<=100

1<=n<=18000

0<=a[i]<=100000

**Example:**

**Input:**

1

9

112 133 161 311 122 512 1212 0 19212

**Output:**

41265

**Explanation :**

nextgreater(112) : 133

nextgreater(133) : 161

nextgreater(161) : 311

nextgreater(311) : 512

nextgreater(122) : 512

nextgreater(512) : 1212

nextgreater(1212) : 19212

nextgreater(0) : 19212

nextgreater(19212) : 0

add = 133+161+311+512+512+1212+19212+19212+0 = 41265.

Expected Time complexity - **O(n).**

Author: Chunky_2808

lakshmi_pandey | 72 |

abbatta7 | 68 |

Ashish Kumar Vaishy | 62 |

aman19 | 58 |

Kumar Gaurav Singh | 58 |

saumitra13325 | 612 |

ashujack | 551 |

lakshmi_pandey | 544 |

aman19 | 538 |

piyushmittal25 | 514 |

blackshadows | 5249 |

akhayrutdinov | 5111 |

Ibrahim Nash | 5087 |

Quandray | 4354 |

sanjay05 | 3668 |

Login to report an issue on this page.