Recently you went to a fair. There were a lots of rides and stalls. You enjoyed the fair a lot, but the most entertaining part was to play ThrowBall. In ThrowBall, you need to pay Rs 100 as the fee, and then throw N identical balls into K distinct boxes, such that there is at least one ball in each box. You tried your best but couldn’t throw all balls properly, so you lost your money. But the shopkeeper was generous, he gave you a last chance. He asked you a question, which if solved correctly will give your money back.

The question is **What was the distinct number of ways in which you could throw those balls into the boxes?**

As the answer could be very large, print it modulo M, where M is a prime number.

**Input:**

The first line of input contains an integer T, denoting the number of test cases. The only line of each test case contains 3 space seperated integers N,K and M as described above.

**Output:**

For each test case output a single integer corresponding to the answer of the question asked by the shopkeeper, in new line.

**Constraints:**

1<= T <=100

1<= N <= 10^{18}

1<= K <= 10^{18}

1<= M <= 10^{6}

**Example:
Input:**

2

13 9 7

5 5 7

5

1

In first test case distinct number of ways of throwing 13 identical balls into 9 distinct boxes is 495.

So the answer modulo 7 is 5.

Author: tanujyadav97

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.