This page is still under construction.

Parts of this page are still being built. What you see may change.

Just Buy Your Drinks, Please

Time limit3sMemory limit1024 MB

Summary
Each query asks for the highest taste threshold t such that liquids with d_i >= t can supply at least L liters within budget g, respecting per-liquid caps.
Level

Hard8 of 10

Topics
Binary search, Sorting, Greedy, Prefix sum
Solved
No attempts yet

Problem

Jaehyun has taken up a hobby of mixing various liquids to make drinks.

The mart sells nn liquids numbered 0,1,…,n−10, 1, \ldots, n - 1. Liquid ii has a taste of did_i and costs pip_i per liter. A single bottle of the drink Jaehyun makes may use at most lil_i liters of liquid ii. (Breaking this rule may put your health in serious danger.)

A bizarre rumor spread that drinking Jaehyun's beverage lets you trade health for problem-solving skill, so mm people came to Jaehyun's house to drink one bottle each. Person jj wants the liquid used to make one bottle to cost at most gjg_j and the volume to be at least LjL_j liters. Under these conditions, person jj wants to maximize the taste of the drink. Here, the taste of a drink is the minimum among the tastes of the liquids that make it up.

For each person, output the taste of the drink that person will drink. If you cannot serve a drink, output -1.

Input

The first line contains two integers n,mn, m (1≤n,m≤100 0001 \le n, m \le 100\,000).

Then nn lines follow, each with three integers di,pi,lid_i, p_i, l_i. (1≤di,pi,li≤1051 \le d_i, p_i, l_i \le 10^5)

Then mm lines follow, each with two integers gi,Lig_i, L_i. (1≤gi,Li≤10181 \le g_i, L_i \le 10^{18})

Output

Output the answers in mm lines. On line ii, output the taste of the drink person ii will drink. If you cannot serve a drink, output -1.

Examples2

  1. Example 1

    Input
    3 4
    1 3 5
    2 1 3
    3 2 5
    6 3
    5 3
    10 10
    20 10
    
    Expected output
    3
    2
    -1
    1
    
  2. Example 2

    Input
    10 10
    71203 70943 96030
    2907 3366 43446
    59057 58730 29943
    20030 19971 5151
    54659 54133 16206
    61347 60820 58102
    73802 73343 32955
    49325 49519 51020
    53790 53260 12493
    60357 60341 33443
    1039324449 4656
    396371768 1370
    1385376577 1519
    1468730283 4687
    1728094263 8256
    124371528 7039
    1505613387 3776
    1556326425 4864
    1231069280 3853
    305372610 8028
    
    Expected output
    73802
    73802
    73802
    73802
    73802
    2907
    73802
    73802
    73802
    20030