Derek is forming palindromic strings of length lesser than or equal to N, with first K characters of lowercase English language, such that each character in a string doesn’t appear more than twice.

Help Derek in finding out the total number of such strings formed.

**Input:**

The first line of input contains a single integer T denoting the number of test cases. Then T test cases follow. Each test case consists of two integers N and K as described in the problem statement.

**Output:**

For each test case, print in a new line, the number of such strings formed. As the results can be very large, print your answer modulo 10^9+7.

**Constraints:**

1 <= T <= 100

1 <= K <= 26

1 <= N <= 52

2*K >= N

**Example:**

**Input:**

2

3 2

4 3

**Output:**

6

18

**Explanation:**

For the **1st sample test case**:

The possible strings are: “a”, “b”, “aa”, “bb”, “aba”, “bab”. Hence, the answer is 6.

For the **2nd sample test case**:

The possible strings are: "a", "b", "c", "aa", "bb", "cc", "aba", "aca", "bab", "bcb", "cac", "cbc", "abba", "acca", "baab", "bccb", "caac", "cbbc". Hence, the answer is 18.

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.

narahc321 | 170 |

agness | 163 |

lonecoder | 153 |

2016uec1342 | 142 |

HuHu | 140 |

lonecoder | 1088 |

nikhil_sojan | 1082 |

KshatriyaYash | 1066 |

mazumderrohit8 | 928 |

SumitSingh27 | 832 |

blackshadows | 5327 |

Ibrahim Nash | 5215 |

akhayrutdinov | 5111 |

mb1973 | 4504 |

Quandray | 4444 |

Login to report an issue on this page.