Under construction

Still being built. Things may change

Mysterious Button

Time limit8sMemory limit512 MB

Summary
For each query, count the button press sequences from room 1 that end by escaping from room d and collect exactly e coins.
Level

Hard8 of 10

Topics
Dynamic programming, Matrix, Graph
Solved
No attempts yet

Problem

You decide to earn coins in a dungeon on the outskirts of a town. The dungeon has NN rooms, numbered 1 to NN. It contains three kinds of mysterious buttons: coin buttons, escape buttons, and warp buttons.

  • Each room has exactly one coin button. You can press a coin button any number of times, and each press gives you 1 coin.
  • Each room has exactly one escape button. Pressing it leaves the dungeon immediately and ends the adventure.
  • There are MM warp buttons in total. The ii-th warp button is in room aia_i. Pressing it teleports you to room bib_i and gives you cic_i coins. Here ai<bia_i < b_i and 1≤ci≤31 \le c_i \le 3. A room may have several warp buttons, and some rooms may have none.

You can move between rooms only by pressing buttons, and you cannot press several buttons at once. All buttons are distinguishable from each other.

You have explored this dungeon QQ times. If your memory is correct, the jj-th adventure started in room 1 and ended by pressing the escape button in room djd_j, collecting exactly eje_j coins in total. For each adventure, find the number of ways to press buttons that match this record. Two button sequences are different if they have different total numbers of presses, or if there is some integer kk such that the kk-th pressed button differs. Since the answer can be large, print it modulo 109+710^9+7.

Your memory may be wrong, in which case no such sequence exists. Then print 0.

Input

The input consists of multiple datasets in the following format.

N M
a1 b1 c1
a2 b2 c2
...
aM bM cM
Q
d1 e1
d2 e2
...
dQ eQ

The first line of each dataset has NN and MM, with 1≤N,M≤20001 \le N, M \le 2000. The next MM lines describe the warp buttons. Line ii contains aia_i, bib_i, and cic_i, with 1≤ai<bi≤N1 \le a_i < b_i \le N and 1≤ci≤31 \le c_i \le 3. Line M+2M+2 contains QQ, with 1≤Q≤20001 \le Q \le 2000. The next QQ lines each contain djd_j and eje_j, with 1≤dj≤N1 \le d_j \le N and 1≤ej≤1091 \le e_j \le 10^9.

The input ends with a line containing two zeros. There are at most 50 datasets in total.

Output

For each dataset, output QQ lines. Line jj contains the number of ways to press buttons that start in room 1, end by pressing the escape button in room djd_j, and collect exactly eje_j coins in total, modulo 109+710^9+7.

Examples1

  1. Example 1

    Input
    4 9
    1 2 1
    1 2 2
    1 3 1
    1 3 2
    1 3 3
    2 3 1
    2 3 2
    1 4 1
    3 4 2
    8
    1 5
    2 5
    3 5
    4 5
    1 1234567
    2 1234567
    3 1234567
    4 1234567
    4 8
    1 2 1
    1 2 1
    1 2 3
    3 4 3
    1 4 2
    2 4 1
    2 4 3
    2 4 3
    8
    1 3
    2 6
    3 8
    4 12
    1 31415926
    2 53589793
    3 23846
    4 26433832
    0 0
    
    Expected output
    1
    9
    37
    21
    1
    2469133
    307629943
    60012504
    1
    16
    0
    424
    1
    160769377
    0
    43581113