Mana Collection

아직 제출이 없습니다시간 제한5초메모리 제한1024 MB

문제

Bessie has recently taken an interest in magic and needs to collect mana for a very important spell. Bessie has NN (1N181\le N\le 18) mana pools, the iith of which accumulates m_im\_i mana per second (1m_i1081\le m\_i\le 10^8). The pools are linked by a collection of MM (0MN(N1)0\le M\le N(N-1)) directed edges (a_i,b_i,t_i)(a\_i,b\_i,t\_i), meaning that she can travel from a_ia\_i to b_ib\_i in t_it\_i seconds (1a_i,b_iN1\le a\_i, b\_i\le N, a_ib_ia\_i\neq b\_i, 1t_i1091\le t\_i\le 10^9). Whenever Bessie is present at a pool, she can collect all the mana stored at that location, emptying it. At time 00, all mana pools are empty, and Bessie can select any pool to start at.

Answer QQ (1Q21051\le Q\le 2\cdot 10^5) queries, each specified by two integers ss and ee (1s1091\le s\le 10^9, 1eN1\le e\le N). For each query, determine the maximum amount of mana Bessie can collect in ss seconds if she must be at mana pool ee at the end of the ssth second.

입력

First line contains NN and MM.

Next line contains m_1,m_2,,m_Nm\_1,m\_2,\dots, m\_N.

Next MM lines contain a_i,b_i,t_ia\_i,b\_i,t\_i. No ordered pair (a_i,b_i)(a\_i,b\_i) appears more than once in the input.

Next line contains QQ.

Next QQ lines contain two integers ss and ee.

출력

QQ lines, one for each query.