Internet Problem Solving Contest

IPSC 2017

Problem Q – Queue skipping

Recently there was a rumor that on Monday the meat store will actually have some meat. It’s Monday, half past one in the morning. The entire town is already waiting for the store to open. More precisely, there are n people waiting in a line. The people are numbered 1 through n in line order, with person 1 being the one currently closest to the door of the store.

During the next few hours, at some moments one of the people will use some trick in order to skip to the beginning of the queue. Examples of such tricks include:

Problem specification

You are given the sequence of people skipping to the front of the queue. After all those events, the meat store will open. People will enter the store one at a time. Inside, they will learn that the meat didn’t arrive (again!), so they will go home empty-handed.

Find out who will waste the most time waiting for nothing. In other words, find out who will be the last person in the queue after all the skipping to the front is over.

Input specification

The first line of the input file contains an integer t specifying the number of test cases. Each test case is preceded by a blank line.

Each test case starts with a line containing two positive integers n and e: the number of people and the number of events. The rest of the test case consists of e lines, each containing a single positive integer: the number of a person who just moved to the front of the queue.

In the easy subproblem Q1 you may assume that t = 100 and 1 ≤ n, e ≤ 100.

In the hard subproblem Q2 you may assume that t = 100 and 1 ≤ n, e ≤ 105.

Note that you cannot download the input for subproblem Q2 directly. Instead, we have provided a small Python 2 program that will generate the file q2.in when executed.


In the real contest, some large input files may be provided in the same way as the input q2.in in this practice problem. Please make sure you are able to generate it.

Output specification

For each test case output a single line with a single positive integer: the number of the last person in the queue when the meat store opened.




3 4

7 1



In the first example test case, the queue changed as follows:

In the second example test case, person 1 skipping to the front of the queue doesn’t change the queue at all. In particular, the last person in the queue is still person 7.