Under construction

Still being built. Things may change

Best-of Match Winner

Time limit0.5sMemory limit512 MB

Summary
Count the A/B game result strings where Bob reaches M wins to take the series, and Albert reaches N wins before Bob reaches N, modulo 1e9+7.
Level

Medium6 of 10

Topics
Combinatorics, Math
Solved
No attempts yet

Problem

Albert and Bob often play board games and decide the winner with a best-of series.

An X-win series is a match format where the first person to win X games wins the series, and at most 2X−12X-1 games are played. Board games between the two children never end in a draw. For example, when X=3X = 3, the following results are possible. A means Albert won the game, and B means Bob won the game.

  • AAA: Albert wins the series 3-0. Games 4 and 5 are not needed, so they are not played.
  • BAAA, ABAA, AABA: Albert wins the series 3-1. Game 5 is not needed, so it is not played.
  • BBAAA, BAABA, BABAA, ABBAA, ABABA, AABBA: Albert wins the series 3-2.
  • Besides these 10 results, swapping A and B gives 10 more results where Bob wins the series.

After the matches, Bob says the following. The final winner of the MM-win series is Bob, who won MM games first. However, if the series had been an NN-win series, Albert would have won. In other words, Albert reached NN wins first, but Bob reached MM wins first and became the final winner. Of course, N<MN < M.

For example, when M=2M = 2 and N=1N = 1, the only result is ABB, where Albert wins the first game and Bob wins the other two.

Another example is M=3M = 3 and N=2N = 2, which has three possible results:

  • AABBB: In a 2-win series, Albert would have won at game 2. In a 3-win series, Bob is the final winner.
  • ABABB: In a 2-win series, Albert would have won at game 3. In a 3-win series, Bob is the final winner.
  • BAABB: In a 2-win series, Albert would have won at game 3. In a 3-win series, Bob is the final winner.

Given NN and MM, count the results in which Bob reaches MM wins and becomes the final winner, while Albert reaches NN wins first. The count can be very large, so print it modulo 109+710^9+7.

Input

The first line contains the number of test cases TT.

Each test case is given on one line with two integers NN and MM, separated by a space.

Output

For each test case, print the answer on its own line.

Constraints

  • 1≤T≤101 \le T \le 10
  • 1≤N<M≤1051 \le N < M \le 10^5

Examples1

  1. Example 1

    Input
    5
    1 2
    2 3
    1 3
    2 4
    500 1000
    
    Expected output
    1
    3
    4
    13
    886458341