Vera and Modern Art

Each of N paint drops covers a lattice of points with power-of-two steps; answer Q queries for the total colour sum at a given point.

Hard8MathBit manipulationHash mapImplementationNo attempts yetTime limit4sMemory limit1024 MB

Problem

Inspired by the great painter Picowso, Vera decided to make her own masterpiece. Her canvas is an infinite two-dimensional coordinate plane. Vera likes powers of two (1, 2, 4, 8, 16, ...), so she paints points repeatedly with step sizes that are powers of two.

Vera paints NN times. The ii-th time is described by three integers xix_i, yiy_i, viv_i. Let aia_i be the largest power of two not greater than xix_i, and let bib_i be the largest power of two not greater than yiy_i. Vera adds one paint drop of colour viv_i to every point of the form (xi+aip, yi+biq)(x_i + a_i p,\ y_i + b_i q), where pp and qq are non-negative integers. A point may hold several paint drops, and several drops of the same colour.

Vera then asks QQ questions. The jj-th question asks for the colour at the point (rj,cj)(r_j, c_j). The colour at a point is the sum of the colours of all paint drops on it. A point with no paint drop has colour 0.

You are her art assistant, so you have to answer the questions.

Input

The first line contains two integers NN and QQ, separated by one space (1N,Q21051 \le N, Q \le 2 \cdot 10^5).

Each of the next NN lines contains three space-separated integers xix_i, yiy_i, viv_i, describing the paint drops of colour viv_i (1iN1 \le i \le N; 1vi100001 \le v_i \le 10000; 1xi,yi10181 \le x_i, y_i \le 10^{18}).

Each of the next QQ lines contains two space-separated integers rjr_j and cjc_j, describing a question about the point (rj,cj)(r_j, c_j) (1jQ1 \le j \le Q; 1rj,cj10181 \le r_j, c_j \le 10^{18}).

Output

Print QQ lines. Line jj contains one integer, the colour of the point (rj,cj)(r_j, c_j).

Note

In the first example, let colours 1, 2, 3, 4, 5 be red, blue, green, orange, purple. With pp and qq non-negative integers, the drops land as follows.

  • A red drop on every point (1+p, 2+2q)(1 + p,\ 2 + 2q).
  • A blue drop on every point (3+2p, 4+4q)(3 + 2p,\ 4 + 4q).
  • A green drop on every point (4+4p, 5+4q)(4 + 4p,\ 5 + 4q).
  • An orange drop on every point (6+4p, 3+2q)(6 + 4p,\ 3 + 2q).
  • A purple drop on every point (7+4p, 1+q)(7 + 4p,\ 1 + q).

The painting from (0,0)(0, 0) to (11,11)(11, 11) looks like this.

painting

The point (7,8)(7, 8) has a red, a blue and a purple drop, so its colour is 1+2+5=81 + 2 + 5 = 8. The point (5,9)(5, 9) has no drop, so its colour is 0.