We have N cards with each card numbered from 1 to N. All cards are randomly shuffled. We are allowed only operation **moveCard(N)** which moves the card with value N to the top of the pile. You are required to find out the minimum number of **moveCard()** operations required to sort the cards in increasing order.

**Input:**

The first line contains a positive integer T denoting the number of test cases. Each of the test case consists of 2 lines. The first line of each test case contains a single integer N. Next line contains space separated sequence of N integers.ā

**Output:**

For each test case, print in new line the minimum number of operations required.ā

**Constraints:**

1 <= T <= 10^{2}

1 <= N <=10^{7}

1 <= A[i] <= Nā

**Example:
Input:**

2

5

5 1 2 3 4

4

3 4 2 1

**Output:**

4

2

**Explanation:**

**For 1st case: **

5 1 2 3 4 //given sequence

4 5 1 2 3 //moveCard(4)

3 4 5 1 2 //moveCard(3)

2 3 4 5 1 //moveCard(2)

1 2 3 4 5 //moveCard(1)

Hence, minimum 4 operations are required.

**For 2nd case:**

3 4 2 1 //given sequence

2 3 4 1 //moveCard(2)

1 2 3 4 //moveCard(1)

Hence, minimum 2 operations are required.

Author: sajalagrawal

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.

Sulagna | 117 |

RUTVIKHARIPARA | 116 |

janvidavda105190 | 93 |

harshsiddhapura105461 | 93 |

devsinghindra | 80 |

PiyushPandey4 | 614 |

ASWATHAMA | 561 |

akhyasharma01 | 547 |

john_wick | 521 |

jagrit_07 | 443 |

blackshadows | 5362 |

Ibrahim Nash | 5242 |

akhayrutdinov | 5111 |

mb1973 | 4929 |

Quandray | 4598 |

Login to report an issue on this page.