#437. Photoshop Layers

Photoshop Layers

Description

Pixels in a digital picture can be represented with three integers (R,G,B)(R,G,B) in the range 00 to 255255 that indicate the intensity of the red, green, and blue colors. The color of a pixel can be expressed as a six-digit hexadecimal capital string. For example, (R=100,G=255,B=50)(R=100,G=255,B=50) can be expressed as ''64FF32\texttt{64FF32}''.

There are nn layers in Photoshop workstation, labeled by 1,2,,n1,2,\dots,n from bottom to top. The screen will display these layers from bottom to top. In this problem, you only need to handle the case that the color of all the pixels in a layer are the same. The color of the ii-th layer is ci=(Ri,Gi,Bi)c_i=(R_i,G_i,B_i), the blending mode of the ii-th layer is mim_i (mi{1,2}m_i\in\{1,2\}):

  • If mi=1m_i=1, the blending mode of this layer is ''Normal''. Assume the previous color displayed on the screen is (Rp,Gp,Bp)(R_p,G_p,B_p), now the new color will be (Ri,Gi,Bi)(R_i,G_i,B_i).
  • If mi=2m_i=2, the blending mode of this layer is ''Linear Dodge''. Assume the previous color displayed on the screen is (Rp,Gp,Bp)(R_p,G_p,B_p), now the new color will be (min(Rp+Ri,255)(\min(R_p+R_i,255), min(Gp+Gi,255)\min(G_p+G_i,255), min(Bp+Bi,255))\min(B_p+B_i,255)).

You will be given qq queries. In the ii-th query, you will be given two integers lil_i and rir_i (1lirin1\leq l_i\leq r_i\leq n). Please write a program to compute the final color displayed on the screen if we only keep all the layers indexed within [li,ri][l_i,r_i] without changing their order. Note that the color of the background is (R=0,G=0,B=0)(R=0,G=0,B=0).

Format

Input

The first line contains a single integer TT (1T101 \leq T \leq 10), the number of test cases. For each test case:

The first line of the input contains two integers nn and qq (1n,q1000001 \leq n,q \leq 100\,000), denoting the number of layers and the number of queries.

In the next nn lines, the ii-th line contains an integer mim_i and a six-digit hexadecimal capital string cic_i, describing the ii-th layer.

In the next qq lines, the ii-th line contains two integers lil_i and rir_i (1lirin1\leq l_i\leq r_i\leq n), describing the ii-th query.

Output

For each query, print a single line containing a six-digit hexadecimal capital string, denoting the final displayed color.

Samples

1
5 5
1 64C832
2 000100
2 010001
1 323C21
2 32C8C8
1 2
1 3
2 3
2 4
2 5
64C932
65C933
010101
323C21
64FFE9