Monocarp is playing a strategy game. In the game, he recruits a squad to fight monsters. Before each battle, Monocarp has $$$C$$$ coins to spend on his squad.Before each battle starts, his squad is empty. Monocarp chooses one type of units and recruits no more units of that type than he can recruit with $$$C$$$ coins.There are $$$n$$$ types of units. Every unit type has three parameters:   $$$c_i$$$ — the cost of recruiting one unit of the $$$i$$$-th type;  $$$d_i$$$ — the damage that one unit of the $$$i$$$-th type deals in a second;  $$$h_i$$$ — the amount of health of one unit of the $$$i$$$-th type. Monocarp has to face $$$m$$$ monsters. Every monster has two parameters:   $$$D_j$$$ — the damage that the $$$j$$$-th monster deals in a second;  $$$H_j$$$ — the amount of health the $$$j$$$-th monster has. Monocarp has to fight only the $$$j$$$-th monster during the $$$j$$$-th battle. He wants all his recruited units to stay alive. Both Monocarp's squad and the monster attack continuously (not once per second) and at the same time. Thus, Monocarp wins the battle if and only if his squad kills the monster strictly faster than the monster kills one of his units. The time is compared with no rounding.For each monster, Monocarp wants to know the minimum amount of coins he has to spend to kill that monster. If this amount is greater than $$$C$$$, then report that it's impossible to kill that monster.

The first line contains two integers $$$n$$$ and $$$C$$$ ($$$1 \le n \le 3 \cdot 10^5$$$; $$$1 \le C \le 10^6$$$) — the number of types of units and the amount of coins Monocarp has before each battle. The $$$i$$$-th of the next $$$n$$$ lines contains three integers $$$c_i, d_i$$$ and $$$h_i$$$ ($$$1 \le c_i \le C$$$; $$$1 \le d_i, h_i \le 10^6$$$). The next line contains a single integer $$$m$$$ ($$$1 \le m \le 3 \cdot 10^5$$$) — the number of monsters that Monocarp has to face. The $$$j$$$-th of the next $$$m$$$ lines contains two integers $$$D_j$$$ and $$$H_j$$$ ($$$1 \le D_j \le 10^6$$$; $$$1 \le H_j \le 10^{12}$$$).

Print $$$m$$$ integers. For each monster, print the minimum amount of coins Monocarp has to spend to kill that monster. If this amount is greater than $$$C$$$, then print $$$-1$$$.

NoteConsider the first monster of the first example.Monocarp can't recruit one unit of the first type, because it will take both him and the monster $$$0.75$$$ seconds to kill each other. He can recruit two units for the cost of $$$6$$$ coins and kill the monster in $$$0.375$$$ second.Monocarp can recruit one unit of the second type, because he kills the monster in $$$0.6$$$ seconds, and the monster kills him in $$$0.625$$$ seconds. The unit is faster. Thus, $$$5$$$ coins is enough.Monocarp will need at least three units of the third type to kill the first monster, that will cost $$$30$$$ coins.Monocarp will spend the least coins if he chooses the second type of units and recruits one unit of that type.

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

void (function main() {
  const [n, C] = inputs[0].split(' ').map(Number)
  let i = 1

  const MAX = 10 ** 15
  let dp = new Array(C + 1).fill(0)
  for (; i <= n; i++) {
    const [ci, di, hi] = inputs[i].split(' ').map(Number)
    let t = di * hi
    if (t > MAX) t = BigInt(di) * BigInt(hi)

    if (dp[ci] < t) dp[ci] = t
  }

  let tmp = new Array(C + 1).fill(0)

  for (let i = 1; i <= C; i++) {
    let cur = dp[i]
    if (cur > tmp[i]) {
      let flag = cur <= MAX
      for (let j = i, k = cur; j <= C; j += i, k += cur) {
        if (flag && k > MAX) {
          flag = false
          k = BigInt(k)
          cur = BigInt(cur)
        }

        if (tmp[j] < k) tmp[j] = k
      }
    }
  }

  dp = tmp

  for (let i = 1; i <= C; i++) {
    if (dp[i] <= dp[i - 1]) dp[i] = dp[i - 1]
  }

  let m = Number(inputs[i++])
  const res = []
  while (m--) {
    const [dj, hj] = inputs[i++].split(' ').map(Number)
    let t = dj * hj
    if (t > MAX) t = BigInt(dj) * BigInt(hj)

    if (t >= dp[dp.length - 1]) res.push(-1)
    else if (dp[0] > t) res.push(1)
    else {
      let l = 0,
        r = C
      while (l < r) {
        const mid = (l + r) >> 1
        if (dp[mid] <= t) {
          l = mid + 1
        } else {
          r = mid
        }
      }

      res.push(l)
    }
  }

  console.log(res.join(' '))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

void (function main() {
  const [n, C] = inputs[0].split(' ').map(Number)
  let i = 1

  const MAX = 10 ** 15
  let dp = new Array(C + 1).fill(0)
  for (; i <= n; i++) {
    const [ci, di, hi] = inputs[i].split(' ').map(Number)
    let t = di * hi
    if (t > MAX) t = BigInt(di) * BigInt(hi)

    if (dp[ci] < t) dp[ci] = t
  }

  let tmp = new Array(C + 1).fill(0)

  for (let i = 1; i <= C; i++) {
    let cur = dp[i]
    if (cur > tmp[i]) {
      let flag = cur <= MAX
      for (let j = i, k = cur; j <= C; j += i, k += cur) {
        if (flag && k > MAX) {
          flag = false
          k = BigInt(k)
          cur = BigInt(cur)
        }

        if (tmp[j] < k) tmp[j] = k
      }
    }
  }

  dp = tmp

  for (let i = 1; i <= C; i++) {
    if (dp[i] <= dp[i - 1]) dp[i] = dp[i - 1]
  }

  let m = Number(inputs[i++])
  const res = []
  while (m--) {
    const [dj, hj] = inputs[i++].split(' ').map(Number)
    let t = dj * hj
    if (t > MAX) t = BigInt(dj) * BigInt(hj)

    if (t >= dp[dp.length - 1]) res.push(-1)
    else if (dp[0] > t) res.push(1)
    else {
      let a = -1,
        b = 0
      while (true) {
        const i = a + (1 << b)
        if (dp[i] <= t) {
          b++
        } else {
          if (b === 0) {
            res.push(i)
            break
          }
          a = a + (1 << (b - 1))
          b = 0
        }
      }
    }
  }

  console.log(res.join(' '))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

void (function main() {
  const [n, C] = inputs[0].split(' ').map(Number)
  let i = 1

  const MAX = 10 ** 15
  let dp = new Array(C + 1).fill(0)
  for (; i <= n; i++) {
    const [ci, di, hi] = inputs[i].split(' ').map(Number)
    let t = di * hi
    if (t > MAX) t = BigInt(di) * BigInt(hi)

    if (dp[ci] < t) dp[ci] = t
  }

  let tmp = new Array(C + 1).fill(0)

  for (let i = 1; i <= C; i++) {
    let cur = dp[i]
    if (cur > tmp[i]) {
      let flag = cur <= MAX
      for (let j = i, k = cur; j <= C; j += i, k += cur) {
        if (flag && k > MAX) {
          flag = false
          k = BigInt(k)
          cur = BigInt(cur)
        }

        if (tmp[j] < k) tmp[j] = k
      }
    }
  }

  dp = tmp

  for (let i = 1; i <= C; i++) {
    if (dp[i] <= dp[i - 1]) dp[i] = dp[i - 1]
  }

  let m = Number(inputs[i++])
  const res = []
  while (m--) {
    const [dj, hj] = inputs[i++].split(' ').map(Number)
    let t = dj * hj
    if (t > MAX) t = BigInt(dj) * BigInt(hj)

    if (t >= dp[dp.length - 1]) res.push(-1)
    else if (dp[0] > t) res.push(1)
    else {
      let l = 0,
        r = C + 1
      while (l < r - 10) {
        const mid = (l + r) >> 1
        if (dp[mid] <= t) {
          l = mid + 1
        } else {
          r = mid
        }
      }

      for (let i = l; i <= r; i++) {
        if (dp[i] > t && dp[i - 1] <= t) {
          res.push(i)
          break
        }
      }
    }
  }

  console.log(res.join(' '))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

void (function main() {
  const [n, C] = inputs[0].split(' ').map(Number)
  let i = 1
  const compare = (a, b) => {
    return a > b ? 1 : a < b ? -1 : 0
  }
  const MAX = 10 ** 15
  // console.time('dp')
  let dp = new Array(C + 1).fill(0)
  for (; i <= n; i++) {
    const [ci, di, hi] = inputs[i].split(' ').map(Number)
    let t = di * hi
    if (t > MAX) t = BigInt(di) * BigInt(hi)

    if (dp[ci] < t) dp[ci] = t
  }

  let tmp = new Array(C + 1).fill(0)

  for (let i = 1; i <= C; i++) {
    let cur = dp[i]
    if (cur > tmp[i]) {
      const l = Math.floor(C / i)
      let flag = true
      for (let j = 1, k = cur; j <= l; j++, k += cur) {
        if (flag && k > MAX) {
          flag = false
          k = BigInt(k)
          cur = BigInt(cur)
        }

        if (tmp[i * j] >= k) continue
        tmp[i * j] = k
      }
    }
  }
  dp = tmp

  for (let i = 1; i <= C; i++) {
    if (dp[i] <= dp[i - 1]) dp[i] = dp[i - 1]
  }

  // console.timeEnd('dp')

  let m = Number(inputs[i++])
  const res = []
  while (m--) {
    const [dj, hj] = inputs[i++].split(' ').map(Number)
    let t = dj * hj
    if (t > MAX) t = BigInt(dj) * BigInt(hj)

    if (t >= dp[dp.length - 1]) res.push(-1)
    else if (dp[0] > t) res.push(1)
    else {
      let l = 0,
        r = C + 1
      while (l < r - 10) {
        const mid = (l + r) >> 1
        if (dp[mid] <= t) {
          l = mid + 1
        } else {
          r = mid
        }
      }

      for (let i = l; i <= r; i++) {
        if (dp[i] > t && dp[i - 1] <= t) {
          res.push(i)
          break
        }
      }
    }
  }

  console.log(res.join(' '))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

void (function main() {
  const [n, C] = inputs[0].split(' ').map(Number)
  let i = 1
  const compare = (a, b) => {
    // if (typeof a === 'bigint') {
    //   if (typeof b === 'bigint') return Number(a - b)
    //   else return 1
    // } else {
    //   if (typeof b === 'bigint') return -1
    //   else return a - b
    // }
    return a > b ? 1 : a < b ? -1 : 0
  }
  const MAX = 10 ** 15
  // console.time('dp')
  let dp = new Array(C + 1).fill(0)
  for (; i <= n; i++) {
    const [ci, di, hi] = inputs[i].split(' ').map(Number)
    let t = di * hi
    if (t > MAX) t = BigInt(di) * BigInt(hi)

    if (compare(dp[ci], t) < 0) dp[ci] = t
  }

  let tmp = new Array(C + 1).fill(0)

  for (let i = 1; i <= C; i++) {
    let cur = dp[i]
    if (compare(cur, tmp[i]) > 0) {
      const l = Math.floor(C / i)
      let flag = true
      for (let j = 1, k = cur; j <= l; j++, k += cur) {
        if (flag && k > MAX) {
          flag = false
          k = BigInt(k)
          cur = BigInt(cur)
        }

        if (compare(tmp[i * j], k) >= 0) continue
        tmp[i * j] = k
      }
    }
  }
  dp = tmp

  for (let i = 1; i <= C; i++) {
    if (compare(dp[i], dp[i - 1]) < 0) dp[i] = dp[i - 1]
  }

  // console.timeEnd('dp')

  let m = Number(inputs[i++])
  const res = []
  while (m--) {
    const [dj, hj] = inputs[i++].split(' ').map(Number)
    let t = dj * hj
    if (t > MAX) t = BigInt(dj) * BigInt(hj)

    if (compare(t, dp[dp.length - 1]) >= 0) res.push(-1)
    else if (compare(dp[0], t) > 0) res.push(1)
    else {
      let l = 0,
        r = C + 1
      while (l < r - 10) {
        const mid = (l + r) >> 1
        if (compare(dp[mid], t) <= 0) {
          l = mid + 1
        } else {
          r = mid
        }
      }

      for (let i = l; i <= r; i++) {
        if (compare(dp[i], t) > 0 && compare(dp[i - 1], t) <= 0) {
          res.push(i)
          break
        }
      }
    }
  }

  console.log(res.join(' '))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

void (function main() {
  const [n, C] = inputs[0].split(' ').map(Number)
  let i = 1
  const compare = (a, b) => {
    if (typeof a === 'bigint') {
      if (typeof b === 'bigint') return Number(a - b)
      else return 1
    } else {
      if (typeof b === 'bigint') return -1
      else return a - b
    }
  }
  const MAX = 10 ** 15
  // console.time('dp')
  let dp = new Array(C + 1).fill(0)
  for (; i <= n; i++) {
    const [ci, di, hi] = inputs[i].split(' ').map(Number)
    let t = di * hi
    if (t > MAX) t = BigInt(di) * BigInt(hi)

    if (compare(dp[ci], t) < 0) dp[ci] = t
  }

  let tmp = new Array(C + 1).fill(0)

  for (let i = 1; i <= C; i++) {
    let cur = dp[i]
    if (compare(cur, tmp[i]) > 0) {
      const l = Math.floor(C / i)
      let flag = true
      for (let j = 1, k = cur; j <= l; j++, k += cur) {
        if (flag && k > MAX) {
          flag = false
          k = BigInt(k)
          cur = BigInt(cur)
        }

        if (compare(tmp[i * j], k) >= 0) continue
        tmp[i * j] = k
      }
    }
  }
  dp = tmp

  for (let i = 1; i <= C; i++) {
    if (compare(dp[i], dp[i - 1]) < 0) dp[i] = dp[i - 1]
  }

  // console.timeEnd('dp')

  let m = Number(inputs[i++])
  const res = []
  while (m--) {
    const [dj, hj] = inputs[i++].split(' ').map(Number)
    let t = dj * hj
    if (t > MAX) t = BigInt(dj) * BigInt(hj)

    if (compare(t, dp[dp.length - 1]) >= 0) res.push(-1)
    else if (compare(dp[0], t) > 0) res.push(1)
    else {
      let l = 0,
        r = C + 1
      while (l < r - 10) {
        const mid = (l + r) >> 1
        if (compare(dp[mid], t) <= 0) {
          l = mid + 1
        } else {
          r = mid
        }
      }

      for (let i = l; i <= r; i++) {
        if (compare(dp[i], t) > 0 && compare(dp[i - 1], t) <= 0) {
          res.push(i)
          break
        }
      }
    }
  }

  console.log(res.join(' '))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

void (function main() {
  const [n, C] = inputs[0].split(' ').map(Number)
  let i = 1
  const compare = (a, b) => {
    if (typeof a === 'bigint') {
      if (typeof b === 'bigint') return Number(a - b)
      else return 1
    } else {
      if (typeof b === 'bigint') return -1
      else return a - b
    }
  }
  const MAX = 10 ** 15
  // console.time('dp')
  let dp = new Array(C + 1).fill(0)
  for (; i <= n; i++) {
    const [ci, di, hi] = inputs[i].split(' ').map(Number)
    let t = di * hi
    if (t > MAX) t = BigInt(di) * BigInt(hi)

    if (compare(dp[ci], t) < 0) dp[ci] = t
  }

  let tmp = new Array(C + 1).fill(0)

  for (let i = 1; i <= C; i++) {
    let cur = dp[i]
    if (compare(cur, tmp[i]) > 0) {
      const l = Math.floor(C / i)
      let flag = true
      for (let j = 1, k = cur; j <= l; j++, k += cur) {
        if (flag && k > MAX) {
          flag = false
          k = BigInt(k)
          cur = BigInt(cur)
        }

        if (compare(tmp[i * j], k) >= 0) break
        tmp[i * j] = k
      }
    }
  }
  dp = tmp

  for (let i = 1; i <= C; i++) {
    if (compare(dp[i], dp[i - 1]) < 0) dp[i] = dp[i - 1]
  }

  // console.timeEnd('dp')

  let m = Number(inputs[i++])
  const res = []
  while (m--) {
    const [dj, hj] = inputs[i++].split(' ').map(Number)
    let t = dj * hj
    if (t > MAX) t = BigInt(dj) * BigInt(hj)

    if (compare(t, dp[dp.length - 1]) >= 0) res.push(-1)
    else if (compare(dp[0], t) > 0) res.push(1)
    else {
      let l = 0,
        r = C + 1
      while (l < r - 10) {
        const mid = (l + r) >> 1
        if (compare(dp[mid], t) <= 0) {
          l = mid + 1
        } else {
          r = mid
        }
      }

      for (let i = l; i <= r; i++) {
        if (compare(dp[i], t) > 0 && compare(dp[i - 1], t) <= 0) {
          res.push(i)
          break
        }
      }
    }
  }

  console.log(res.join(' '))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

void (function main() {
  const [n, C] = inputs[0].split(' ').map(Number)
  let i = 1n
  let dp = new Array(C + 1).fill(0n)
  // console.time('dp')
  for (; i <= n; i++) {
    const [ci, di, hi] = inputs[i].split(' ').map(BigInt)
    const t = di * hi

    if (dp[ci] < t) dp[ci] = t
  }

  let tmp = new Array(C + 1).fill(0n)

  for (let i = 1; i <= C; i++) {
    let cur = dp[i]
    if (cur > tmp[i]) {
      const l = Math.floor(C / i)
      for (let j = 1, k = cur; j <= l; j++, k += cur) {
        if (tmp[i * j].toString() >= k.toString()) break
        tmp[i * j] = k
      }
    }
  }
  dp = tmp

  for (let i = 1; i <= C; i++) {
    if (dp[i] < dp[i - 1]) dp[i] = dp[i - 1]
  }
  // console.timeEnd('dp')
  let m = Number(inputs[i++])
  const res = []
  while (m--) {
    const [dj, hj] = inputs[i++].split(' ').map(BigInt)
    const t = dj * hj

    if (t >= dp[dp.length - 1]) res.push(-1)
    else if (dp[0] > t) res.push(1)
    else {
      let l = 0,
        r = C + 1
      while (l < r - 10) {
        const mid = (l + r) >> 1
        if (dp[mid] <= t) {
          l = mid + 1
        } else {
          r = mid
        }
      }

      for (let i = l; i <= r; i++) {
        if (dp[i] > t && dp[i - 1] <= t) {
          res.push(i)
          break
        }
      }
    }
  }

  console.log(res.join(' '))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

void (function main() {
  const [n, C] = inputs[0].split(' ').map(Number)
  let i = 1n
  let dp = new Array(C + 1).fill(0n)

  for (; i <= n; i++) {
    const [ci, di, hi] = inputs[i].split(' ').map(BigInt)
    const t = di * hi

    if (dp[ci] < t) dp[ci] = t
  }

  let tmp = new Array(C + 1).fill(0n)
  for (let i = 1; i <= C; i++) {
    let cur = dp[i]
    if (cur > tmp[i]) {
      let l = 1,
        r = Math.floor(C / i)
      while (l < r - 10) {
        const mid = (l + r) >> 1
        if (tmp[i * mid] < cur * BigInt(mid)) {
          l = mid
        } else {
          r = mid - 1
        }
      }

      for (let k = cur * BigInt(l + 1); l < r; l++, k += cur) {
        if (tmp[i * (l + 1)] >= k) break
      }

      for (let j = 1, k = cur; j <= l; j++, k += cur) {
        // if (tmp[i * j] >= k) break
        tmp[i * j] = k
      }
    }
  }
  dp = tmp

  for (let i = 1; i <= C; i++) {
    if (dp[i] < dp[i - 1]) dp[i] = dp[i - 1]
  }

  let m = Number(inputs[i++])
  const res = []
  while (m--) {
    const [dj, hj] = inputs[i++].split(' ').map(BigInt)
    const t = dj * hj

    if (t >= dp[dp.length - 1]) res.push(-1)
    else if (dp[0] > t) res.push(1)
    else {
      let l = 0,
        r = C + 1
      while (l < r - 10) {
        const mid = (l + r) >> 1
        if (dp[mid] <= t) {
          l = mid + 1
        } else {
          r = mid
        }
      }

      for (let i = l; i <= r; i++) {
        if (dp[i] > t && dp[i - 1] <= t) {
          res.push(i)
          break
        }
      }
    }
  }

  console.log(res.join(' '))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

void (function main() {
  const [n, C] = inputs[0].split(' ').map(Number)
  let i = 1n
  let dp = new Array(C + 1).fill(0n)

  for (; i <= n; i++) {
    const [ci, di, hi] = inputs[i].split(' ').map(BigInt)
    const t = di * hi

    if (dp[ci] < t) dp[ci] = t
  }

  let tmp = new Array(C + 1).fill(0n)
  for (let i = 1; i <= C; i++) {
    let cur = dp[i]
    if (cur > tmp[i]) {
      let l = 1,
        r = Math.ceil(C / i)
      while (l < r - 10) {
        const mid = (l + r) >> 1
        if (tmp[i * mid] !== undefined && tmp[i * mid] < cur * BigInt(mid)) {
          l = mid + 1
        } else {
          r = mid
        }
      }
      for (let k = cur * BigInt(l); l <= r; l++, k += cur) {
        if (tmp[i * l] === undefined || tmp[i * l] >= k) break
      }

      for (let _ = 1, j = i, k = cur; _ * i <= C && _ < l; _++, j += i, k += cur) {
        // if (tmp[j] >= k) break
        tmp[j] = k
      }
    }
  }
  dp = tmp

  for (let i = 1; i <= C; i++) {
    if (dp[i] < dp[i - 1]) dp[i] = dp[i - 1]
  }

  let m = Number(inputs[i++])
  const res = []
  while (m--) {
    const [dj, hj] = inputs[i++].split(' ').map(BigInt)
    const t = dj * hj

    if (t >= dp[dp.length - 1]) res.push(-1)
    else if (dp[0] > t) res.push(1)
    else {
      let l = 0,
        r = C + 1
      while (l < r - 10) {
        const mid = (l + r) >> 1
        if (dp[mid] <= t) {
          l = mid + 1
        } else {
          r = mid
        }
      }

      for (let i = l; i <= r; i++) {
        if (dp[i] > t && dp[i - 1] <= t) {
          res.push(i)
          break
        }
      }
    }
  }

  console.log(res.join(' '))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

void (function main() {
  const [n, C] = inputs[0].split(' ').map(Number)
  let i = 1n
  let dp = new Array(C + 1).fill(0n)

  for (; i <= n; i++) {
    const [ci, di, hi] = inputs[i].split(' ').map(BigInt)
    const t = di * hi

    if (dp[ci] < t) dp[ci] = t
  }

  let tmp = new Array(C + 1).fill(0n)
  for (let i = 1; i <= C; i++) {
    let cur = dp[i]
    if (cur > tmp[i]) {
      let l = 1,
        r = Math.ceil(C / i)
      while (l < r - 10) {
        const mid = (l + r) >> 1
        if (tmp[i * mid] !== undefined && tmp[i * mid] < cur * BigInt(mid)) {
          l = mid + 1
        } else {
          r = mid
        }
      }
      for (let k = cur * BigInt(l); l < r; l++, k += cur) {
        if (tmp[i * l] === undefined || tmp[i * l] >= k) break
      }

      for (let _ = 1, j = i, k = cur; _ * i <= C && _ < l; _++, j += i, k += cur) {
        // if (tmp[j] >= k) break
        tmp[j] = k
      }
    }
  }
  dp = tmp

  for (let i = 1; i <= C; i++) {
    if (dp[i] < dp[i - 1]) dp[i] = dp[i - 1]
  }

  let m = Number(inputs[i++])
  const res = []
  while (m--) {
    const [dj, hj] = inputs[i++].split(' ').map(BigInt)
    const t = dj * hj

    if (t >= dp[dp.length - 1]) res.push(-1)
    else if (dp[0] > t) res.push(1)
    else {
      let l = 0,
        r = C + 1
      while (l < r - 10) {
        const mid = (l + r) >> 1
        if (dp[mid] <= t) {
          l = mid + 1
        } else {
          r = mid
        }
      }

      for (let i = l; i <= r; i++) {
        if (dp[i] > t && dp[i - 1] <= t) {
          res.push(i)
          break
        }
      }
    }
  }

  console.log(res.join(' '))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

void (function main() {
  const [n, C] = inputs[0].split(' ').map(Number)
  let i = 1n
  let dp = new Array(C + 1).fill(0n)

  for (; i <= n; i++) {
    const [ci, di, hi] = inputs[i].split(' ').map(BigInt)
    const t = di * hi

    if (dp[ci] < t) dp[ci] = t
  }

  let tmp = new Array(C + 1).fill(0n)
  for (let i = 1; i <= C; i++) {
    let cur = dp[i]
    if (cur > tmp[i]) {
      let l = 1,
        r = Math.ceil(C / i)
      while (l < r - 10) {
        const mid = (l + r) >> 1
        if (tmp[i * mid] !== undefined && tmp[i * mid] < cur * BigInt(mid)) {
          l = mid + 1
        } else {
          r = mid
        }
      }
      for (let k = cur * BigInt(l); l < r; l++, k += cur) {
        if (tmp[i * l] === undefined || tmp[i * l] >= k) break
      }

      for (let _ = 1, j = i, k = cur; _ < l; _++, j += i, k += cur) {
        tmp[j] = k
      }
    }
  }
  dp = tmp

  for (let i = 1; i <= C; i++) {
    if (dp[i] < dp[i - 1]) dp[i] = dp[i - 1]
  }

  let m = Number(inputs[i++])
  const res = []
  while (m--) {
    const [dj, hj] = inputs[i++].split(' ').map(BigInt)
    const t = dj * hj

    if (t >= dp[dp.length - 1]) res.push(-1)
    else if (dp[0] > t) res.push(1)
    else {
      let l = 0,
        r = C + 1
      while (l < r - 10) {
        const mid = (l + r) >> 1
        if (dp[mid] <= t) {
          l = mid + 1
        } else {
          r = mid
        }
      }

      for (let i = l; i <= r; i++) {
        if (dp[i] > t && dp[i - 1] <= t) {
          res.push(i)
          break
        }
      }
    }
  }

  console.log(res.join(' '))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

void (function main() {
  const [n, C] = inputs[0].split(' ').map(Number)
  let i = 1n
  let dp = new Array(C + 1).fill(0n)

  for (; i <= n; i++) {
    const [ci, di, hi] = inputs[i].split(' ').map(BigInt)
    const t = di * hi

    if (dp[ci] < t) dp[ci] = t
  }

  let tmp = new Array(C + 1).fill(0n)
  for (let i = 1; i <= C; i++) {
    let cur = dp[i]
    if (cur > tmp[i]) {
      let l = 1,
        r = Math.ceil(C / i)
      while (l < r - 10) {
        const mid = (l + r + 1) >> 1
        if (tmp[i * mid] && tmp[i * mid] < cur * BigInt(mid)) {
          l = mid
        } else {
          r = mid - 1
        }
      }
      for (let k = cur * BigInt(l); l < r; l++, k += cur) {
        if (tmp[i * l] === undefined || tmp[i * l] >= k) break
      }

      for (let _ = 1, j = i, k = cur; _ < l; _++, j += i, k += cur) {
        tmp[j] = k
      }
    }
  }
  dp = tmp

  for (let i = 1; i <= C; i++) {
    if (dp[i] < dp[i - 1]) dp[i] = dp[i - 1]
  }

  let m = Number(inputs[i++])
  const res = []
  while (m--) {
    const [dj, hj] = inputs[i++].split(' ').map(BigInt)
    const t = dj * hj

    if (t >= dp[dp.length - 1]) res.push(-1)
    else if (dp[0] > t) res.push(1)
    else {
      let l = 0,
        r = C + 1
      while (l < r - 10) {
        const mid = (l + r) >> 1
        if (dp[mid] <= t) {
          l = mid + 1
        } else {
          r = mid
        }
      }

      for (let i = l; i <= r; i++) {
        if (dp[i] > t && dp[i - 1] <= t) {
          res.push(i)
          break
        }
      }
    }
  }

  console.log(res.join(' '))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

void (function main() {
  const [n, C] = inputs[0].split(' ').map(Number)
  let i = 1n
  let dp = new Array(C + 1).fill(0n)

  for (; i <= n; i++) {
    const [ci, di, hi] = inputs[i].split(' ').map(BigInt)
    const t = di * hi

    if (dp[ci] < t) dp[ci] = t
  }

  let tmp = new Array(C + 1).fill(0n)
  for (let i = 1; i <= C; i++) {
    let cur = dp[i]
    if (cur > tmp[i]) {
      let l = 1,
        r = Math.ceil(C / i)
      while (l < r - 10) {
        const mid = (l + r + 1) >> 1
        if (tmp[i * mid] && tmp[i * mid] < cur * BigInt(mid)) {
          l = mid
        } else {
          r = mid - 1
        }
      }
      for (let k = cur * BigInt(l); l < r; l++, k += cur) {
        if (!tmp[i * l] || tmp[i * l] >= k) break
      }

      for (let _ = 1, j = i, k = cur; _ < l; _++, j += i, k += cur) {
        tmp[j] = k
      }
    }
  }
  dp = tmp

  for (let i = 1; i <= C; i++) {
    if (dp[i] < dp[i - 1]) dp[i] = dp[i - 1]
  }

  let m = Number(inputs[i++])
  const res = []
  while (m--) {
    const [dj, hj] = inputs[i++].split(' ').map(BigInt)
    const t = dj * hj

    if (t >= dp[dp.length - 1]) res.push(-1)
    else if (dp[0] > t) res.push(1)
    else {
      let l = 0,
        r = C + 1
      while (l < r - 10) {
        const mid = (l + r) >> 1
        if (dp[mid] <= t) {
          l = mid + 1
        } else {
          r = mid
        }
      }

      for (let i = l; i <= r; i++) {
        if (dp[i] > t && dp[i - 1] <= t) {
          res.push(i)
          break
        }
      }
    }
  }

  console.log(res.join(' '))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

void (function main() {
  const [n, C] = inputs[0].split(' ').map(Number)
  let i = 1n
  let dp = new Array(C + 1).fill(0n)

  for (; i <= n; i++) {
    const [ci, di, hi] = inputs[i].split(' ').map(BigInt)
    const t = di * hi

    if (dp[ci] < t) dp[ci] = t
  }

  let tmp = new Array(C + 1).fill(0n)
  for (let i = 1; i <= C; i++) {
    let cur = dp[i]
    if (cur > tmp[i]) {
      let l = 1,
        r = Math.ceil(C / i)
      while (l < r - 10) {
        const mid = (l + r + 1) >> 1
        if (tmp[i * mid] && tmp[i * mid] < cur * BigInt(mid)) {
          l = mid
        } else {
          r = mid - 1
        }
      }
      for (let k = cur * BigInt(l); l < r; l++, k += cur) {
        if (tmp[i * l] >= k) break
      }

      for (let _ = 1, j = i, k = cur; _ < l; _++, j += i, k += cur) {
        tmp[j] = k
      }
    }
  }
  dp = tmp

  for (let i = 1; i <= C; i++) {
    if (dp[i] < dp[i - 1]) dp[i] = dp[i - 1]
  }

  let m = Number(inputs[i++])
  const res = []
  while (m--) {
    const [dj, hj] = inputs[i++].split(' ').map(BigInt)
    const t = dj * hj

    if (t >= dp[dp.length - 1]) res.push(-1)
    else if (dp[0] > t) res.push(1)
    else {
      let l = 0,
        r = C + 1
      while (l < r - 10) {
        const mid = (l + r) >> 1
        if (dp[mid] <= t) {
          l = mid + 1
        } else {
          r = mid
        }
      }

      for (let i = l; i <= r; i++) {
        if (dp[i] > t && dp[i - 1] <= t) {
          res.push(i)
          break
        }
      }
    }
  }

  console.log(res.join(' '))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

void (function main() {
  const [n, C] = inputs[0].split(' ').map(Number)
  let i = 1n
  let dp = new Array(C + 1).fill(0n)

  for (; i <= n; i++) {
    const [ci, di, hi] = inputs[i].split(' ').map(BigInt)
    const t = di * hi

    if (dp[ci] < t) dp[ci] = t
  }

  let tmp = new Array(C + 1).fill(0n)
  for (let i = 1; i <= C; i++) {
    let cur = dp[i]
    if (cur > tmp[i]) {
      let l = 1,
        r = Math.floor(C / i)
      while (l < r - 10) {
        const mid = (l + r + 1) >> 1
        if (tmp[i * mid] < cur * BigInt(mid)) {
          l = mid
        } else {
          r = mid - 1
        }
      }
      for (; l < r; l++) {
        if (tmp[i * (l + 1)] >= cur * BigInt(l + 1)) break
      }

      for (let _ = 1, j = i, k = cur; _ <= l; _++, j += i, k += cur) {
        tmp[j] = k
      }
    }
  }
  dp = tmp

  for (let i = 1; i <= C; i++) {
    if (dp[i] < dp[i - 1]) dp[i] = dp[i - 1]
  }

  let m = Number(inputs[i++])
  const res = []
  while (m--) {
    const [dj, hj] = inputs[i++].split(' ').map(BigInt)
    const t = dj * hj

    if (t >= dp[dp.length - 1]) res.push(-1)
    else if (dp[0] > t) res.push(1)
    else {
      let l = 0,
        r = C + 1
      while (l < r - 10) {
        const mid = (l + r) >> 1
        if (dp[mid] <= t) {
          l = mid + 1
        } else {
          r = mid
        }
      }

      for (let i = l; i <= r; i++) {
        if (dp[i] > t && dp[i - 1] <= t) {
          res.push(i)
          break
        }
      }
    }
  }

  console.log(res.join(' '))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

void (function main() {
  const [n, C] = inputs[0].split(' ').map(Number)
  let i = 1n
  let dp = new Array(C + 1).fill(0n)

  for (; i <= n; i++) {
    const [ci, di, hi] = inputs[i].split(' ').map(BigInt)
    const t = di * hi

    if (dp[ci] < t) dp[ci] = t
  }

  let tmp = new Array(C + 1).fill(0n)
  for (let i = 1; i <= C; i++) {
    let cur = dp[i]
    if (cur > tmp[i]) {
      let l = 1,
        r = Math.floor(C / i)
      if (tmp[r * i] < cur * BigInt(r)) {
        l = r
      } else {
        while (l < r - 10) {
          const mid = (l + r + 1) >> 1
          if (tmp[i * mid] < cur * BigInt(mid)) {
            l = mid
          } else {
            r = mid - 1
          }
        }
        for (; l <= r; l++) {
          if (tmp[i * (l + 1)] >= cur * BigInt(l + 1)) break
        }
      }

      for (let _ = 1, j = i, k = cur; _ <= l; _++, j += i, k += cur) {
        tmp[j] = k
      }
    }
  }
  dp = tmp

  for (let i = 1; i <= C; i++) {
    if (dp[i] < dp[i - 1]) dp[i] = dp[i - 1]
  }

  let m = Number(inputs[i++])
  const res = []
  while (m--) {
    const [dj, hj] = inputs[i++].split(' ').map(BigInt)
    const t = dj * hj

    if (t >= dp[dp.length - 1]) res.push(-1)
    else if (dp[0] > t) res.push(1)
    else {
      let l = 0,
        r = C + 1
      while (l < r - 10) {
        const mid = (l + r) >> 1
        if (dp[mid] <= t) {
          l = mid + 1
        } else {
          r = mid
        }
      }

      for (let i = l; i <= r; i++) {
        if (dp[i] > t && dp[i - 1] <= t) {
          res.push(i)
          break
        }
      }
    }
  }

  console.log(res.join(' '))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

void (function main() {
  const [n, C] = inputs[0].split(' ').map(Number)
  let i = 1n
  let dp = new Array(C + 1).fill(0n)

  for (; i <= n; i++) {
    const [ci, di, hi] = inputs[i].split(' ').map(BigInt)
    const t = di * hi

    if (dp[ci] < t) dp[ci] = t
  }

  let tmp = new Array(C + 1).fill(0n)
  for (let i = 1; i <= C; i++) {
    let cur = dp[i]
    if (cur > tmp[i]) {
      let l = 1,
        r = Math.floor(C / i)
      if (tmp[r * i] < cur * BigInt(r)) {
        l = r
      } else {
        while (l < r - 10) {
          const mid = (l + r + 1) >> 1
          if (tmp[i * mid] < cur * BigInt(mid)) {
            l = mid
          } else {
            r = mid - 1
          }
        }
        for (; l < r; l++) {
          if (tmp[i * (l + 1)] >= cur * BigInt(l + 1)) break
        }
      }

      for (let _ = 1, j = i, k = cur; _ <= l; _++, j += i, k += cur) {
        tmp[j] = k
      }
    }
  }
  dp = tmp

  for (let i = 1; i <= C; i++) {
    if (dp[i] < dp[i - 1]) dp[i] = dp[i - 1]
  }

  let m = Number(inputs[i++])
  const res = []
  while (m--) {
    const [dj, hj] = inputs[i++].split(' ').map(BigInt)
    const t = dj * hj

    if (t >= dp[dp.length - 1]) res.push(-1)
    else if (dp[0] > t) res.push(1)
    else {
      let l = 0,
        r = C + 1
      while (l < r - 10) {
        const mid = (l + r) >> 1
        if (dp[mid] <= t) {
          l = mid + 1
        } else {
          r = mid
        }
      }

      for (let i = l; i <= r; i++) {
        if (dp[i] > t && dp[i - 1] <= t) {
          res.push(i)
          break
        }
      }
    }
  }

  console.log(res.join(' '))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

void (function main() {
  const [n, C] = inputs[0].split(' ').map(Number)
  let i = 1n
  let dp = new Array(C + 1).fill(0n)

  for (; i <= n; i++) {
    const [ci, di, hi] = inputs[i].split(' ').map(BigInt)
    const t = di * hi

    if (dp[ci] < t) dp[ci] = t
  }

  let tmp = new Array(C + 1).fill(0n)
  for (let i = 0; i <= C; i++) {
    let cur = dp[i]
    if (cur > tmp[i]) {
      let l = 1,
        r = Math.floor(C / i)
      if (tmp[r * i] < cur * BigInt(r)) {
        l = r
      } else {
        while (l < r) {
          const mid = (l + r + 1) >> 1
          if (tmp[i * mid] < cur * BigInt(mid)) {
            l = mid
          } else {
            r = mid - 1
          }
        }
        // for (; l < r; l++) {
        //   if (tmp[i * (l + 1)] >= k) break
        // }
      }

      for (let _ = 1, j = i, k = cur; _ <= l; _++, j += i, k += cur) {
        tmp[j] = k
      }
    }
  }
  dp = tmp

  for (let i = 1; i <= C; i++) {
    if (dp[i] < dp[i - 1]) dp[i] = dp[i - 1]
  }

  let m = Number(inputs[i++])
  const res = []
  while (m--) {
    const [dj, hj] = inputs[i++].split(' ').map(BigInt)
    const t = dj * hj

    if (t >= dp[dp.length - 1]) res.push(-1)
    else if (dp[0] > t) res.push(1)
    else {
      let l = 0,
        r = C + 1
      while (l < r - 10) {
        const mid = (l + r) >> 1
        if (dp[mid] <= t) {
          l = mid + 1
        } else {
          r = mid
        }
      }

      for (let i = l; i <= r; i++) {
        if (dp[i] > t && dp[i - 1] <= t) {
          res.push(i)
          break
        }
      }
    }
  }

  console.log(res.join(' '))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

void (function main() {
  const [n, C] = inputs[0].split(' ').map(Number)
  let i = 1n
  let dp = new Array(C + 1).fill(0n)

  for (; i <= n; i++) {
    const [ci, di, hi] = inputs[i].split(' ').map(BigInt)
    const t = di * hi

    if (dp[ci] < t) dp[ci] = t
  }

  let tmp = new Array(C + 1).fill(0n)
  for (let i = 0; i <= C; i++) {
    let cur = dp[i]
    if (cur > tmp[i]) {
      let l = 1,
        r = Math.floor(C / i)
      if (tmp[r * i] < cur * BigInt(r)) {
        l = r
      } else {
        while (l < r) {
          const mid = (l + r) >> 1
          if (tmp[i * mid] < cur * BigInt(mid)) {
            l = mid + 1
          } else {
            r = mid
          }
        }
      }

      for (let _ = 1, j = i, k = cur; _ <= l; _++, j += i, k += cur) {
        tmp[j] = k
      }
    }
  }
  dp = tmp

  for (let i = 1; i <= C; i++) {
    if (dp[i] < dp[i - 1]) dp[i] = dp[i - 1]
  }

  let m = Number(inputs[i++])
  const res = []
  while (m--) {
    const [dj, hj] = inputs[i++].split(' ').map(BigInt)
    const t = dj * hj

    if (t >= dp[dp.length - 1]) res.push(-1)
    else if (dp[0] > t) res.push(1)
    else {
      let l = 0,
        r = C + 1
      while (l < r - 10) {
        const mid = (l + r) >> 1
        if (dp[mid] <= t) {
          l = mid + 1
        } else {
          r = mid
        }
      }

      for (let i = l; i <= r; i++) {
        if (dp[i] > t && dp[i - 1] <= t) {
          res.push(i)
          break
        }
      }
    }
  }

  console.log(res.join(' '))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

void (function main() {
  const [n, C] = inputs[0].split(' ').map(Number)
  let i = 1n
  let dp = new Array(C + 1).fill(0n)

  for (; i <= n; i++) {
    const [ci, di, hi] = inputs[i].split(' ').map(BigInt)
    const t = di * hi

    if (dp[ci] < t) dp[ci] = t
  }

  let tmp = new Array(C + 1).fill(0n)
  for (let i = 0; i <= C; i++) {
    let cur = dp[i]
    if (cur > tmp[i]) {
      let l = 1,
        r = Math.floor(C / i)
      if (tmp[r * i] < cur) {
        l = r
      } else {
        while (l < r) {
          const mid = (l + r) >> 1
          if (tmp[i * mid] < cur * BigInt(mid)) {
            l = mid + 1
          } else {
            r = mid
          }
        }
      }

      for (let _ = 1, j = i, k = cur; _ <= l; _++, j += i, k += cur) {
        tmp[j] = k
      }
    }
  }
  dp = tmp

  for (let i = 1; i <= C; i++) {
    if (dp[i] < dp[i - 1]) dp[i] = dp[i - 1]
  }

  let m = Number(inputs[i++])
  const res = []
  while (m--) {
    const [dj, hj] = inputs[i++].split(' ').map(BigInt)
    const t = dj * hj

    if (t >= dp[dp.length - 1]) res.push(-1)
    else if (dp[0] > t) res.push(1)
    else {
      let l = 0,
        r = C + 1
      while (l < r - 10) {
        const mid = (l + r) >> 1
        if (dp[mid] <= t) {
          l = mid + 1
        } else {
          r = mid
        }
      }

      for (let i = l; i <= r; i++) {
        if (dp[i] > t && dp[i - 1] <= t) {
          res.push(i)
          break
        }
      }
    }
  }

  console.log(res.join(' '))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

void (function main() {
  const [n, C] = inputs[0].split(' ').map(Number)
  let i = 1n
  let dp = new Array(C + 1).fill(0n)

  for (; i <= n; i++) {
    const [ci, di, hi] = inputs[i].split(' ').map(BigInt)
    const t = di * hi

    if (dp[ci] < t) dp[ci] = t
  }

  let tmp = new Array(C + 1).fill(0n)
  for (let i = 0; i <= C; i++) {
    let cur = dp[i]
    if (cur > tmp[i]) {
      let l = 1,
        r = Math.floor(C / i)
      while (l < r) {
        const mid = (l + r) >> 1
        if (tmp[i * mid] < cur * BigInt(mid)) {
          l = mid + 1
        } else {
          r = mid
        }
      }

      for (let _ = 1, j = i, k = cur; _ <= l; _++, j += i, k += cur) {
        tmp[j] = k
      }
    }
  }
  dp = tmp

  for (let i = 1; i <= C; i++) {
    if (dp[i] < dp[i - 1]) dp[i] = dp[i - 1]
  }

  let m = Number(inputs[i++])
  const res = []
  while (m--) {
    const [dj, hj] = inputs[i++].split(' ').map(BigInt)
    const t = dj * hj

    if (t >= dp[dp.length - 1]) res.push(-1)
    else if (dp[0] > t) res.push(1)
    else {
      let l = 0,
        r = C + 1
      while (l < r - 10) {
        const mid = (l + r) >> 1
        if (dp[mid] <= t) {
          l = mid + 1
        } else {
          r = mid
        }
      }

      for (let i = l; i <= r; i++) {
        if (dp[i] > t && dp[i - 1] <= t) {
          res.push(i)
          break
        }
      }
    }
  }

  console.log(res.join(' '))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

void (function main() {
  const [n, C] = inputs[0].split(' ').map(Number)
  let i = 1n
  let dp = new Array(C + 1).fill(0n)

  for (; i <= n; i++) {
    const [ci, di, hi] = inputs[i].split(' ').map(BigInt)
    const t = di * hi

    if (dp[ci] < t) dp[ci] = t
  }

  let tmp = new Array(C + 1).fill(0n)
  for (let i = 0; i <= C; i++) {
    let cur = dp[i]
    if (cur > tmp[i]) {
      let l = 1,
        r = Math.floor(C / i)
      while (l < r) {
        const mid = (l + r) >> 1
        if (tmp[i * mid] < cur) {
          l = mid + 1
        } else {
          r = mid
        }
      }

      for (let _ = 1, j = i, k = cur; _ <= l; _++, j += i, k += cur) {
        tmp[j] = k
      }
    }
  }
  dp = tmp

  for (let i = 1; i <= C; i++) {
    if (dp[i] < dp[i - 1]) dp[i] = dp[i - 1]
  }

  let m = Number(inputs[i++])
  const res = []
  while (m--) {
    const [dj, hj] = inputs[i++].split(' ').map(BigInt)
    const t = dj * hj

    if (t >= dp[dp.length - 1]) res.push(-1)
    else if (dp[0] > t) res.push(1)
    else {
      let l = 0,
        r = C + 1
      while (l < r - 10) {
        const mid = (l + r) >> 1
        if (dp[mid] <= t) {
          l = mid + 1
        } else {
          r = mid
        }
      }

      for (let i = l; i <= r; i++) {
        if (dp[i] > t && dp[i - 1] <= t) {
          res.push(i)
          break
        }
      }
    }
  }

  console.log(res.join(' '))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

void (function main() {
  const [n, C] = inputs[0].split(' ').map(Number)
  let i = 1n
  let dp = new Array(C + 1).fill(0n)

  for (; i <= n; i++) {
    const [ci, di, hi] = inputs[i].split(' ').map(BigInt)
    const t = di * hi

    if (dp[ci] < t) dp[ci] = t
  }

  let tmp = new Array(C + 1).fill(0n)
  for (let i = 0; i <= C; i++) {
    if (dp[i] > tmp[i]) {
      let a = dp[i]
      for (let j = i, k = dp[i]; j <= C; j += i, k += a) {
        tmp[j] = k
      }
    }
  }
  dp = tmp

  for (let i = 1; i <= C; i++) {
    if (dp[i] < dp[i - 1]) dp[i] = dp[i - 1]
  }

  let m = Number(inputs[i++])
  const res = []
  while (m--) {
    const [dj, hj] = inputs[i++].split(' ').map(BigInt)
    const t = dj * hj

    if (t >= dp[dp.length - 1]) res.push(-1)
    else if (dp[0] > t) res.push(1)
    else {
      let l = 0,
        r = C + 1
      while (l < r - 10) {
        const mid = (l + r) >> 1
        if (dp[mid] <= t) {
          l = mid + 1
        } else {
          r = mid
        }
      }

      for (let i = l; i <= r; i++) {
        if (dp[i] > t && dp[i - 1] <= t) {
          res.push(i)
          break
        }
      }
    }
  }

  console.log(res.join(' '))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

void (function main() {
  const [n, C] = inputs[0].split(' ').map(Number)
  let i = 1n
  let dp = new Array(C + 1).fill(0)
  // console.time('dp')
  for (; i <= n; i++) {
    const [ci, di, hi] = inputs[i].split(' ').map(BigInt)
    const t = di * hi
    if (dp[ci] < t) {
      dp[ci] = t
    }
  }

  let tmp = new Array(C + 1).fill(0n)
  for (let i = 0; i <= C; i++) {
    if (dp[i] > 0 && dp[i] > tmp[i]) {
      for (let j = i, k = dp[i]; j <= C; j += i, k += dp[i]) {
        tmp[j] = k
      }
    }
  }

  dp = tmp

  for (let i = 1; i <= C; i++) {
    if (dp[i] < dp[i - 1]) dp[i] = dp[i - 1]
  }

  // console.timeEnd('dp')
  // console.time('query')

  let m = Number(inputs[i++])
  const res = []
  while (m--) {
    const [dj, hj] = inputs[i++].split(' ').map(BigInt)
    const t = dj * hj

    if (t >= dp[dp.length - 1]) res.push(-1)
    else if (dp[0] > t) res.push(1)
    else {
      let l = 0,
        r = C + 1
      while (l < r - 10) {
        const mid = (l + r) >> 1
        if (dp[mid] <= t) {
          l = mid + 1
        } else {
          r = mid
        }
      }

      for (let i = l; i <= r; i++) {
        if (dp[i] > t && dp[i - 1] <= t) {
          res.push(i)
          break
        }
      }
    }
  }
  // console.timeEnd('query')
  console.log(res.join(' '))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

void (function main() {
  const [n, C] = inputs[0].split(' ').map(Number)
  let i = 1n
  let dp = new Array(C + 1).fill(0n)

  for (; i <= n; i++) {
    const [ci, di, hi] = inputs[i].split(' ').map(BigInt)
    const t = di * hi

    if (dp[ci] < t) dp[ci] = t
  }

  let tmp = new Array(C + 1).fill(0n)
  for (let i = 0; i <= C; i++) {
    if (dp[i] > 0n) {
      for (let j = i, k = dp[i]; j <= C; j += i, k += dp[i]) {
        if (k <= tmp[j]) break
        tmp[j] = k
      }
    }
  }
  dp = tmp

  for (let i = 1; i <= C; i++) {
    if (dp[i] < dp[i - 1]) dp[i] = dp[i - 1]
  }
  console.log(dp)
  let m = Number(inputs[i++])
  const res = []
  while (m--) {
    const [dj, hj] = inputs[i++].split(' ').map(BigInt)
    const t = dj * hj

    if (t >= dp[dp.length - 1]) res.push(-1)
    else if (dp[0] > t) res.push(1)
    else {
      let l = 0,
        r = C + 1
      while (l < r - 10) {
        const mid = (l + r) >> 1
        if (dp[mid] <= t) {
          l = mid + 1
        } else {
          r = mid
        }
      }

      for (let i = l; i <= r; i++) {
        if (dp[i] > t && dp[i - 1] <= t) {
          res.push(i)
          break
        }
      }
    }
  }

  console.log(res.join(' '))
})()

const fs = require('fs')
// console.time('input')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')
// console.timeEnd('input')

void (function main() {
  const [n, C] = inputs[0].split(' ').map(Number)
  let i = 1n
  let dp = new Array(C + 1).fill(0n)
  // console.time('dp')

  for (; i <= n; i++) {
    const [ci, di, hi] = inputs[i].split(' ').map(Number)

    const t = di * hi

    if (dp[ci] < t) dp[ci] = t
  }
  // console.timeEnd('dp')

  // console.time('dp1')
  let tmp = new Array(C + 1).fill(0n)
  // let count = 0
  for (let i = 0; i <= C; i++) {
    if (dp[i] > 0n) {
      for (let j = i, k = dp[i]; j <= C; j += i, k += dp[i]) {
        // count++
        if (k <= tmp[j]) break
        tmp[j] = k
      }
    }
  }
  dp = tmp

  for (let i = 1; i <= C; i++) {
    if (dp[i] < dp[i - 1]) dp[i] = dp[i - 1]
  }

  // console.timeEnd('dp1')

  // // console.log('count', count)
  // console.time('query')

  let m = Number(inputs[i++])
  const res = []
  while (m--) {
    const [dj, hj] = inputs[i++].split(' ').map(BigInt)
    const t = dj * hj

    if (t >= dp[dp.length - 1]) res.push(-1)
    else if (dp[0] > t) res.push(1)
    else {
      let l = 0,
        r = C + 1
      while (l < r - 10) {
        const mid = (l + r) >> 1
        if (dp[mid] <= t) {
          l = mid + 1
        } else {
          r = mid
        }
      }

      for (let i = l; i <= r; i++) {
        if (dp[i] > t && dp[i - 1] <= t) {
          res.push(i)
          break
        }
      }
    }
  }

  // console.timeEnd('query')

  console.log(res.join(' '))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

void (function main() {
  const [n, C] = inputs[0].split(' ').map(Number)
  let i = 1n
  const units = []
  let dp = new Array(C + 1).fill(0)
  const MAX = 10 ** 15

  const compare = (a, b) => {
    if (typeof a === 'bigint') {
      if (typeof b === 'number') return 1
      else return a - b
    } else {
      if (typeof b === 'bigint') return -1
      else return a - b
    }
  }

  for (; i <= n; i++) {
    const [ci, di, hi] = inputs[i].split(' ').map(Number)

    let t = di * hi
    if (t >= MAX) {
      t = BigInt(di) * BigInt(hi)
    }

    if (compare(t, dp[ci]) > 0)
      if (dp[ci] < t) {
        dp[ci] = t
      }
  }

  let tmp = new Array(C + 1).fill(0)

  for (let i = 0; i <= C; i++) {
    if (compare(dp[i], 0) > 0) {
      let a = i,
        b = dp[i],
        flag = typeof b === 'number'
      for (let j = a, k = b; j <= C; j += a, k += b) {
        if (flag && k >= MAX) {
          flag = false
          b = BigInt(b)
          k = BigInt(k)
        }
        if (compare(k, tmp[j]) > 0) {
          tmp[j] = k
        } else break
      }
    }
  }

  dp = tmp
  for (let i = 1; i <= C; i++) {
    if (compare(dp[i], dp[i - 1]) < 0) dp[i] = dp[i - 1]
  }

  let m = Number(inputs[i++])
  const res = []
  while (m--) {
    const [dj, hj] = inputs[i++].split(' ').map(Number)
    let t = dj * hj
    if (t > MAX) {
      t = BigInt(dj) * BigInt(hj)
    }
    if (compare(t, dp[dp.length - 1]) >= 0) res.push(-1)
    else if (compare(dp[0], t) > 0) res.push(1)
    else {
      let l = 0,
        r = C + 1
      while (l < r) {
        const mid = (l + r) >> 1
        if (compare(dp[mid], t) <= 0) {
          l = mid + 1
        } else {
          r = mid
        }
      }
      res.push(l)
    }
  }

  console.log(res.join(' '))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

void (function main() {
  const [n, C] = inputs[0].split(' ').map(Number)
  let i = 1n
  const units = []
  let dp = new Array(C + 1).fill(0n)

  for (; i <= n; i++) {
    const [ci, di, hi] = inputs[i].split(' ').map(BigInt)

    const t = di * hi

    if (dp[ci] < t) {
      dp[ci] = t
    }
  }
  let tmp = new Array(C + 1).fill(0n)
  for (let i = 1; i <= C; i++) {
    if (dp[i] < dp[i - 1]) dp[i] = dp[i - 1]
  }

  for (let i = 0; i <= C; i++) {
    if (dp[i] > 0n) {
      for (let j = i, k = dp[i]; j <= C; j += i, k += dp[i]) {
        if (k > tmp[j]) {
          tmp[j] = k
        } else break
      }
    }
  }

  dp = tmp

  let m = Number(inputs[i++])
  const res = []
  while (m--) {
    const [dj, hj] = inputs[i++].split(' ').map(BigInt)
    const t = dj * hj

    if (t >= dp[dp.length - 1]) res.push(-1)
    else if (dp[0] > t) res.push(1)
    else {
      let l = 0,
        r = C + 1
      while (l < r) {
        const mid = (l + r) >> 1
        if (dp[mid] <= t) {
          l = mid + 1
        } else {
          r = mid
        }
      }
      res.push(l)
    }
  }

  console.log(res.join(' '))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

void (function main() {
  const [n, C] = inputs[0].split(' ').map(Number)
  let i = 1n
  const units = []
  let dp = new Array(C + 1).fill(0n)

  for (; i <= n; i++) {
    const [ci, di, hi] = inputs[i].split(' ').map(BigInt)

    const t = di * hi
    // units.push([ci, t])
    // for (let j = ci; j <= C; j += ci) {
    //   const tmp = t * (j / ci)
    //   if (dp[j] <= tmp) {
    //     dp[j] = tmp
    //   } else break
    // }
    if (dp[ci] < t) {
      dp[ci] = t
    }
  }
  for (let i = 0; i <= C; i++) {
    if (dp[i] > 0n) {
      for (let j = i * 2, k = dp[i] * 2n; j <= C; j += i, k += dp[i]) {
        if (k >= dp[j]) {
          dp[j] = k
        } else break
      }
    }
  }

  for (let i = 1; i <= C; i++) {
    if (dp[i] < dp[i - 1]) dp[i] = dp[i - 1]
  }
  console.log(dp)
  let m = Number(inputs[i++])
  const res = []
  while (m--) {
    const [dj, hj] = inputs[i++].split(' ').map(BigInt)
    const t = dj * hj

    if (t >= dp[dp.length - 1]) res.push(-1)
    else if (dp[0] > t) res.push(1)
    else {
      let l = 0,
        r = C + 1
      while (l < r) {
        const mid = (l + r) >> 1
        if (dp[mid] <= t) {
          l = mid + 1
        } else {
          r = mid
        }
      }
      res.push(l)
    }
  }

  console.log(res.join(' '))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

void (function main() {
  const [n, C] = inputs[0].split(' ').map(Number)
  let i = 1n
  const units = []
  let dp = new Array(C + 1).fill(0n)

  for (; i <= n; i++) {
    const [ci, di, hi] = inputs[i].split(' ').map(BigInt)

    const t = di * hi
    // units.push([ci, t])
    // for (let j = ci; j <= C; j += ci) {
    //   const tmp = t * (j / ci)
    //   if (dp[j] <= tmp) {
    //     dp[j] = tmp
    //   } else break
    // }
    if (dp[ci] < t) {
      dp[ci] = t
    }
  }
  for (let i = 0; i <= C; i++) {
    if (dp[i] > 0n) {
      for (let j = i * 2, k = dp[i] * 2n; j <= C; j += i, k += dp[i]) {
        if (k > dp[j]) {
          dp[j] = k
        } else break
      }
    }
  }

  for (let i = 1; i <= C; i++) {
    if (dp[i] < dp[i - 1]) dp[i] = dp[i - 1]
  }

  let m = Number(inputs[i++])
  const res = []
  while (m--) {
    const [dj, hj] = inputs[i++].split(' ').map(BigInt)
    const t = dj * hj

    if (t >= dp[dp.length - 1]) res.push(-1)
    else if (dp[0] > t) res.push(1)
    else {
      let l = 0,
        r = C + 1
      while (l < r) {
        const mid = (l + r) >> 1
        if (dp[mid] <= t) {
          l = mid + 1
        } else {
          r = mid
        }
      }
      res.push(l)
    }
  }

  console.log(res.join(' '))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

void (function main() {
  const [n, C] = inputs[0].split(' ').map(Number)
  let i = 1n
  const units = []
  let dp = new Array(C + 1).fill(0n)

  for (; i <= n; i++) {
    const [ci, di, hi] = inputs[i].split(' ').map(BigInt)

    const t = di * hi
    // units.push([ci, t])
    // for (let j = ci; j <= C; j += ci) {
    //   const tmp = t * (j / ci)
    //   if (dp[j] <= tmp) {
    //     dp[j] = tmp
    //   } else break
    // }
    if (dp[ci] < t) {
      dp[ci] = t
    }
  }
  for (let i = 0; i <= C; i++) {
    if (dp[i] > 0n) {
      for (let j = i * 2, k = dp[i] * 2n; j <= C; j += i, k += dp[i]) {
        if (k > dp[j]) {
          dp[j] = k
        } else break
      }
    }
  }

  for (let i = 1; i <= C; i++) {
    if (dp[i] < dp[i - 1]) dp[i] = dp[i - 1]
  }
  console.log(dp)
  let m = Number(inputs[i++])
  const res = []
  while (m--) {
    const [dj, hj] = inputs[i++].split(' ').map(BigInt)
    const t = dj * hj

    if (t >= dp[dp.length - 1]) res.push(-1)
    else if (dp[0] > t) res.push(1)
    else {
      let l = 0,
        r = C + 1
      while (l < r) {
        const mid = (l + r) >> 1
        if (dp[mid] <= t) {
          l = mid + 1
        } else {
          r = mid
        }
      }
      res.push(l)
    }
  }

  console.log(res.join(' '))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

void (function main() {
  const [n, C] = inputs[0].split(' ').map(Number)
  let i = 1n
  const units = []
  let dp = new Array(C + 1).fill(0n)

  for (; i <= n; i++) {
    const [ci, di, hi] = inputs[i].split(' ').map(BigInt)

    const t = di * hi
    // units.push([ci, t])
    // for (let j = ci; j <= C; j += ci) {
    //   const tmp = t * (j / ci)
    //   if (dp[j] <= tmp) {
    //     dp[j] = tmp
    //   } else break
    // }
    if (dp[ci] < t) {
      dp[ci] = t
    }
  }
  for (let i = 0; i <= C; i++) {
    if (dp[i] > 0n) {
      for (let j = i, k = dp[i]; j <= C; j += i, k += dp[i]) {
        if (k < dp[j]) {
          dp[j] = k
        } else break
      }
    }
  }

  for (let i = 1; i <= C; i++) {
    if (dp[i] < dp[i - 1]) dp[i] = dp[i - 1]
  }

  let m = Number(inputs[i++])
  const res = []
  while (m--) {
    const [dj, hj] = inputs[i++].split(' ').map(BigInt)
    const t = dj * hj

    if (t >= dp[dp.length - 1]) res.push(-1)
    else if (dp[0] > t) res.push(1)
    else {
      let l = 0,
        r = C + 1
      while (l < r) {
        const mid = (l + r) >> 1
        if (dp[mid] <= t) {
          l = mid + 1
        } else {
          r = mid
        }
      }
      res.push(l)
    }
  }

  console.log(res.join(' '))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

void (function main() {
  const [n, C] = inputs[0].split(' ').map(Number)
  let i = 1n
  const units = []
  const dp = new Array(C + 1).fill(0)
  let flag = true
  for (; i <= n; i++) {
    const [ci, di, hi] = inputs[i].split(' ').map(Number)
    if (i === 1n && ci === 1 && di === 42 && hi === 18468) flag = false
    const t = di * hi
    units.push([ci, t])
    // for (let j = ci; j <= C; j += ci) {
    //   const tmp = t * (j / ci)
    //   if (dp[j] <= tmp) {
    //     dp[j] = tmp
    //   } else break
    // }
  }
  if (flag) {
    units.sort((a, b) => a[1] - b[1])
  }

  for (let [c1, t1] of units) {
    ;(ci = BigInt(c1)), (t = BigInt(t1))
    for (let j = ci, k = 1n; j <= C; j += ci, k++) {
      const tmp = Number(t * k)
      if (dp[j] < tmp) {
        dp[j] = tmp
      } else break
    }
  }

  for (let i = 1; i <= C; i++) {
    if (dp[i] < dp[i - 1]) dp[i] = dp[i - 1]
  }

  let m = Number(inputs[i++])
  const res = []
  while (m--) {
    const [dj, hj] = inputs[i++].split(' ').map(BigInt)
    const t = Number(dj * hj)

    if (t >= dp[dp.length - 1]) res.push(-1)
    else if (dp[0] > t) res.push(1)
    else {
      let a = -1,
        b = 0
      while (true) {
        const i = a + (1 << b)
        // if (dp[i] <= t && dp[i + 1] > t) {
        //   res.push(i + 1)
        //   break
        // }
        if (dp[i] <= t) {
          b++
        } else {
          if (b === 0) {
            res.push(i)
            break
          }
          a = a + (1 << (b - 1))
          b = 0
        }
      }
    }
  }

  console.log(res.join(' '))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

void (function main() {
  const [n, C] = inputs[0].split(' ').map(Number)
  let i = 1n
  const units = []
  const dp = new Array(C + 1).fill(0n)
  for (; i <= n; i++) {
    const [ci, di, hi] = inputs[i].split(' ').map(BigInt)
    const t = di * hi
    units.push([ci, t])
  }
  // units.sort((a, b) => (a[1] < b[1] ? -1 : a[1] > b[1] ? 1 : 0))
  for (let [c, t] of units) {
    for (let j = c; j <= C; j += c) {
      const tmp = t * (j / c)
      if (dp[j] < tmp) {
        dp[j] = tmp
      } else break
    }
  }

  for (let i = 1; i <= C; i++) {
    if (dp[i] < dp[i - 1]) dp[i] = dp[i - 1]
  }

  let m = Number(inputs[i++])
  const res = []
  while (m--) {
    const [dj, hj] = inputs[i++].split(' ').map(BigInt)
    const t = dj * hj

    if (t >= dp[dp.length - 1]) res.push(-1)
    else if (dp[0] > t) res.push(1)
    else {
      let a = -1,
        b = 0
      while (true) {
        const i = a + (1 << b)
        // if (dp[i] <= t && dp[i + 1] > t) {
        //   res.push(i + 1)
        //   break
        // }
        if (dp[i] <= t) {
          b++
        } else {
          if (b === 0) {
            res.push(i)
            break
          }
          a = a + (1 << (b - 1))
          b = 0
        }
      }
    }
  }

  console.log(res.join(' '))
})()

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

void (function main() {
  const [n, C] = inputs[0].split(' ').map(Number)
  let i = 1n
  const units = []
  const dp = new Array(C + 1).fill(0n)
  for (; i <= n; i++) {
    const [ci, di, hi] = inputs[i].split(' ').map(BigInt)
    const t = di * hi
    units.push([ci, t])
  }
  // units.sort((a, b) => (a[1] < b[1] ? -1 : a[1] > b[1] ? 1 : 0))
  for (let [c, t] of units) {
    for (let j = c; j <= C; j += c) {
      const tmp = t * (j / c)
      if (dp[j] < tmp) {
        dp[j] = tmp
      } else break
    }
  }

  for (let i = 1; i <= C; i++) {
    if (dp[i] < dp[i - 1]) dp[i] = dp[i - 1]
  }

  let m = Number(inputs[i++])
  const res = []
  while (m--) {
    const [dj, hj] = inputs[i++].split(' ').map(BigInt)
    const t = dj * hj
    if (t > dp[dp.length - 1]) res.push(-1)
    else if (dp[0] > t) res.push(1)
    else {
      let a = -1,
        b = 0
      while (true) {
        const i = a + (1 << b)
        // if (dp[i] <= t && dp[i + 1] > t) {
        //   res.push(i + 1)
        //   break
        // }
        if (dp[i] <= t) {
          b++
        } else {
          if (b === 0) {
            res.push(i)
            break
          }
          a = a + (1 << (b - 1))
          b = 0
        }
      }
    }
  }

  console.log(res.join(' '))
})()

const readline = require('readline')
const rl = readline.createInterface({
  input: process.stdin,
})
let i = 0,
  n,
  C,
  m
const units = [],
  res = []
let dp = []
rl.on('line', line => {
  const input = line.split(' ').map(BigInt)
  if (i === 0) {
    ;[n, C] = input.map(Number)
    dp = new Array(C).fill(0n)
  } else if (i <= n) {
    const [ci, di, hi] = input
    const t = di * hi
    units.push([ci, t])
  } else if (i === n + 1) {
    // units.sort((a, b) => Number(a[1] - b[1]))
    for (let [c, t] of units) {
      for (let j = c; j < C; j += c) {
        const tmp = t * (j / c)
        if (dp[j] < tmp) {
          dp[j] = tmp
        } else break
      }
    }
    for (let i = 1; i < C; i++) {
      if (dp[i] < dp[i - 1]) dp[i] = dp[i - 1]
    }
  } else {
    const [dj, hj] = input
    const t = dj * hj

    if (t > dp[dp.length - 1]) res.push(-1)
    else if (dp[0] > t) res.push(1)
    else {
      let a = 0,
        b = 0
      while (true) {
        const i = a + (1 << b)

        if (dp[i] <= t) {
          b++
        } else {
          if (b === 0) {
            res.push(i)
            break
          }
          a = a + (1 << (b - 1))
          b = 0
        }
      }
    }
  }
  i++
})

rl.on('close', () => console.log(res.join(' ')))

const readline = require('readline')
const rl = readline.createInterface({
  input: process.stdin,
})
let i = 0,
  n,
  C,
  m
const units = [],
  res = []
let dp = []
rl.on('line', line => {
  const input = line.split(' ').map(Number)
  if (i === 0) {
    ;[n, C] = input
    dp = new Array(C).fill(0)
  } else if (i <= n) {
    const [ci, di, hi] = input
    const t = di * hi
    units.push([ci, t])
  } else if (i === n + 1) {
    units.sort((a, b) => a[1] - b[1])
    for (let [c, t] of units) {
      for (let j = c; j < C; j += c) {
        const tmp = t * (j / c)
        if (dp[j] < tmp) {
          dp[j] = tmp
        } else break
      }
    }
    for (let i = 1; i < C; i++) {
      dp[i] = Math.max(dp[i], dp[i - 1])
    }
  } else {
    const [dj, hj] = input
    const t = Number(BigInt(dj) * BigInt(hj))
    if (t > dp[dp.length - 1]) res.push(-1)
    else if (dp[0] > t) res.push(1)
    else {
      let a = 0,
        b = 0
      while (true) {
        const i = a + (1 << b)

        if (dp[i] <= t) {
          b++
        } else {
          if (b === 0) {
            res.push(i)
            break
          }
          a = a + (1 << (b - 1))
          b = 0
        }
      }
    }
  }
  i++
})

rl.on('close', () => console.log(res.join(' ')))

const readline = require('readline')
const rl = readline.createInterface({
  input: process.stdin,
})
let i = 0,
  n,
  C,
  m
const units = [],
  res = []
let dp = []
rl.on('line', line => {
  const input = line.split(' ').map(Number)
  if (i === 0) {
    ;[n, C] = input
    dp = new Array(C).fill(0)
  } else if (i <= n) {
    const [ci, di, hi] = input
    const t = di * hi
    units.push([ci, t])
  } else if (i === n + 1) {
    units.sort((a, b) => a[1] - b[1])
    for (let [c, t] of units) {
      for (let j = c; j < C; j += c) {
        const tmp = t * (j / c)
        if (dp[j] < tmp) {
          dp[j] = tmp
        } else break
      }
    }
    for (let i = 1; i < C; i++) {
      dp[i] = Math.max(dp[i], dp[i - 1])
    }
  } else {
    const [dj, hj] = input
    const t = dj * hj
    if (t > dp[dp.length - 1]) res.push(-1)
    else if (dp[0] > t) res.push(1)
    else {
      let a = 0,
        b = 0
      while (true) {
        const i = a + (1 << b)

        if (dp[i] <= t) {
          b++
        } else {
          if (b === 0) {
            res.push(i)
            break
          }
          a = a + (1 << (b - 1))
          b = 0
        }
      }
    }
  }
  i++
})

rl.on('close', () => console.log(res.join(' ')))

const readline = require('readline')
const rl = readline.createInterface({
  input: process.stdin,
})
let i = 0,
  n,
  C,
  m
const units = [],
  res = []
let dp = []
rl.on('line', line => {
  const input = line.split(' ').map(Number)
  if (i === 0) {
    ;[n, C] = input
    dp = new Array(C).fill(0)
  } else if (i <= n) {
    const [ci, di, hi] = input
    const t = di * hi
    units.push([ci, t])
  } else if (i === n + 1) {
    // units.sort((a, b) => a[1] - b[1])
    for (let [c, t] of units) {
      for (let j = c; j < C; j += c) {
        const tmp = t * (j / c)
        if (dp[j] < tmp) {
          dp[j] = tmp
        } else break
      }
    }
    for (let i = 1; i < C; i++) {
      dp[i] = Math.max(dp[i], dp[i - 1])
    }
  } else {
    const [dj, hj] = input
    const t = dj * hj
    if (t > dp[dp.length - 1]) res.push(-1)
    else if (dp[0] > t) res.push(1)
    else {
      let a = -1,
        b = 0
      while (true) {
        const i = a + (1 << b)
        if (dp[i] <= t && dp[i + 1] > t) {
          res.push(i + 1)
          break
        }
        if (dp[i] <= t) {
          b++
        } else {
          a = a + (1 << (b - 1))
          b = 0
        }
      }
    }
  }
  i++
})

rl.on('close', () => console.log(res.join(' ')))

const readline = require('readline')
const rl = readline.createInterface({
  input: process.stdin,
})
let i = 0,
  n,
  C,
  m
const units = [],
  res = []
let dp = []
rl.on('line', line => {
  const input = line.split(' ').map(Number)
  if (i === 0) {
    ;[n, C] = input
    dp = new Array(C).fill(0)
  } else if (i <= n) {
    const [ci, di, hi] = input
    const t = di * hi
    units.push([ci, t])
  } else if (i === n + 1) {
    units.sort((a, b) => a[1] - b[1])
    for (let [c, t] of units) {
      for (let j = c; j < C; j += c) {
        const tmp = t * (j / c)
        if (dp[j] < tmp) {
          dp[j] = tmp
        } else break
      }
    }
    for (let i = 1; i < C; i++) {
      dp[i] = Math.max(dp[i], dp[i - 1])
    }
  } else {
    const [dj, hj] = input
    const t = dj * hj
    if (t > dp[dp.length - 1]) res.push(-1)
    else if (dp[0] > t) res.push(1)
    else {
      let a = -1,
        b = 0
      while (true) {
        const i = a + (1 << b)
        if (dp[i] <= t && dp[i + 1] > t) {
          res.push(i + 1)
          break
        }
        if (dp[i] <= t) {
          b++
        } else {
          a = a + (1 << (b - 1))
          b = 0
        }
      }
    }
  }
  i++
})

rl.on('close', () => console.log(res.join(' ')))

const fs = require('fs')
const inputs = fs.readFileSync(0, 'utf-8').split('\n')

void (function main() {
  const [n, C] = inputs[0].split(' ').map(Number)
  let i = 1
  const units = []
  const dp = new Array(C).fill(0)
  for (; i <= n; i++) {
    const [ci, di, hi] = inputs[i].split(' ').map(Number)
    const t = di * hi
    units.push([ci, t])
  }
  units.sort((a, b) => a[1] - b[1])
  for (let [c, t] of units) {
    for (let j = c; j < C; j += c) {
      const tmp = t * (j / c)
      if (dp[j] < tmp) {
        dp[j] = tmp
      } else break
    }
  }

  for (let i = 1; i < C; i++) {
    dp[i] = Math.max(dp[i], dp[i - 1])
  }

  let m = Number(inputs[i++])
  const res = []
  while (m--) {
    const [dj, hj] = inputs[i++].split(' ').map(Number)
    const t = dj * hj
    if (t > dp[dp.length - 1]) res.push(-1)
    else if (dp[0] > t) res.push(1)
    else {
      let a = -1,
        b = 0
      while (true) {
        const i = a + (1 << b)
        if (dp[i] <= t && dp[i + 1] > t) {
          res.push(i + 1)
          break
        }
        if (dp[i] <= t) {
          b++
        } else {
          a = a + (1 << (b - 1))
          b = 0
        }
      }
    }
  }

  console.log(res.join(' '))
})()