IDO - The Idol

Original problem statement (in Polish) can be found here.

Somewhere in the rainforests of South America, among the deserted ruins, two human figures were carefully traversing through a narrow corridor made of stone. Serpentine vines and thick spider webs, shimmering in the sunlight, were making their journey more difficult.

That's not what Archibald the Archelogist was worrying about, though. While Huacho, his companion, was clearing the path with a machete, Archibald was carefully examining the floor. It was covered with square-shaped stone tiles - they were adjacent to each other, and one such tile was as wide as the corridor itself. Some strange looking symbols were carved in the walls near the bottom - seasoned archeologist recognized them at once as La-Og-Mhtir numerals. He studied various records of this civilization for many years now - after all, that's the whole reason he ended up in that jungle in the first place.

The journey was taking forever. Archibald was getting increasingly worried - except for a small incident with the natives, everything was going pretty smoothly. He expected deadly traps and hazards at the very least - supposedly no one got ouf those ruins alive. But so far they did not spot anything interesting - apart from that corridor they were traversing. It was tiled with amazing precision and regularity, but the numbers carved above the tiles seemed entirely coincidental. Yet La-Og-Mhtir people were never leaving anything to chance.

"Over there, Señor!" shouted Huacho, interrupting Archibald's contemplations.

They finally reached the end of the corridor, and found themselves at the entrance to a small, circular room. In the middle of it, a golden figurine in a shape of a human head was placed on the dais. It was also holding a hand near its mouth, which made it look as if it was pondering on something important.

"It's The Idol of Ingenuity," said Archibald quietly. "Just like the records said".

"No reason to wait, Señor, let's take it and leave!" Huacho rushed in the direction of the dais, and Archibald immediately stopped him.

"Careful!" said Archibald quietly, and started looking more closely at the walls. He identified La-Og-Mhtirs' alphabet without difficulty. The inscriptions looked like some kind of warning, and he began to ponder on its exact meaning, but a sudden shout interrupted his thoughts.

"Adios, Señor!"

His companion was already sprinting through the corridor, with the statuette under his arm. Suddenly, huge spikes launched from under the plate he was standing on, making ear-piercing noise. Archibald winced, but the noise continued. More spikes were appearing at regular intervals from under various plates, while some of them were retracting. The archelogist observed all of this very carefully, but couldn't spot any pattern.

Suddenly, he recalled the numbers carved over the plates, and everything became clear.

The noise has stopped. Huacho was lying dead on the floor, but apart from that, the corridor was empty. Archibald once again read the warning carved on the wall, gave a thought to it for a moment, and stepped on the first plate with confidence.

Input

The first line contains a single integer t, denoting the number of testcases. Then, descriptions of the testcases follow.

First line of the description consists of two integers n and m (1 <= n <= 109, 0 <= m <= 105). They denote that the corridor consists of n plates, and the spikes will launch m times in total.

Then, m lines follow. One such line consists of two integers pi and ci (1 <= pi <= n, 0 <= ci <= 109). It means that ci seconds after triggering the mechanism, the spikes will launch from under the plate number pi, counting from the start of the corridor (from the room with the idol). Spikes will retract after one second (unless they are scheduled to launch next second).

Every pair of numbers pi, ci appears at most once. The pairs are sorted lexicographically.

Output

For every testcase you should output one integer in a separate line - the minimal number of seconds that Archibald needs to safely cross the corridor. You should count the time it takes to reach the plate number n, starting from the plate number 1. The mechanism is triggered the moment Archibald steps on the first plate for the first time. Every second, he can decide to go one plate forward, one plate backward or stay in place. One second later he will be at that plate. The journey is safe if for every second, there are no spikes on the plate Archibald is currently standing on (including the last plate). Going back into the chamber with the idol doesn't help, because the mechanism will reset. The mechanism is designed in such a way that it is always possible to cross the corridor.

Example

Input:

2
3 5
1 1
2 2
3 2
3 3
3 4
3 0

Output:

5
2

Explanation

In the first testcase, after stepping on the first plate, Archibald will step on the plate number 2 (because after one second the spikes will launch from under the first plate). Then, he goes back to plate number 1 (after 2 second from the start there will be spikes on the plates 2 and 3, so he cannot go forward or stay in place). Then, he jumps onto plate number two, stays in place, and finally steps on plate number 3. All of this takes 5 seconds.

In the second testcase there are no traps, so Archibald can reach the last plate in two jumps. (In this testcase Huacho actually escaped with the idol!)


Added by:Piotr Jagiełło
Date:2017-04-29
Time limit:3s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All
Resource:PIZZA 2017 qualifying round

hide comments
2017-04-30 10:08:10 Vipul Srivastava
@ Piotr can u give me some hint where I am going wrong please..

Piotr: sorry, I honestly don't understand your solution



Last edit: 2017-05-23 19:00:36
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.