let input ='';

process.stdin.resume();
process.stdin.setEncoding('utf-8');
process.stdin.on('data',(data)=>input+=data);

process.stdin.on('end',()=>{
   let count=0;
   input = input.split('\n');
   const arr = input[1].split(' ').map((item) => parseInt(item))
   arr.sort((a,b)=>b-a)

   for(let i = 0; i < arr.length-1;i++){
       count+=arr[i]-arr[i+1] - 1
   }
   console.log(count)
}
)

const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

let l = 0;
rl.on('line', (input) => {
    if (l === 0) { l++ }
    else {
        rl.close();
        let arr = input.split(" ").map(x => parseInt(x))
        arr.sort(function sortNumber(a, b) {
            return a - b;
        })
        let cnt = 0
        for (let i = 0; i < arr.length - 1; i++) {
            if ((arr[i] + 1) !== arr[i+1]) {
                cnt+=arr[i+1]-arr[i]
                cnt--
            }
        }
        console.log(cnt)
        return 0;
    }
})

//Don't have to see. start------------------------------------------
var read = require('readline').createInterface({
  input: process.stdin, output: process.stdout
});
var obj; var inLine = [];
read.on('line', function(input){inLine.push(input);});
read.on('close', function(){
  obj = init(inLine);
  console.error('\n');
  var start = Date.now();
  Main();
  var end = Date.now() - start;
  myerr('time : ' + end + 'ms');
  myerr('memory : ' + Math.round(process.memoryUsage().heapTotal / 1024) + 'KB');
});
function makeClone(obj){return (obj instanceof Set) ? new Set(Array.from(obj)) : JSON.parse(JSON.stringify(obj));}
function nextInt(){return myconv(next(),1);} function nextStrArray(){return myconv(next(),2);}
function nextIntArray(){return myconv(next(),4);} function nextCharArray(){return myconv(next(),6);}
function next(){return obj.next();} function hasNext(){return obj.hasNext();}
function init(input){
  return {
    list : input, index : 0, max : input.length,
    hasNext : function(){return (this.index < this.max);},
    next : function(){if(this.hasNext()){return this.list[this.index++];}else{throw 'ArrayIndexOutOfBoundsException ‚There is no more input';}}
  };
}
function myout(s){console.log(s);}
function myerr(s){console.error('debug:' + require('util').inspect(s,false,null));}
//param "no" is
//unknown or outlier : return i. 1: parseInt.
//2: split space. 4: split space and parseInt.
//6: split 1 character. 7: split 1 character and parseInt.
//8: join space. 9: join nextline. 0: join no character.
function myconv(i,no){try{switch(no){case 1:return parseInt(i);case 2:return i.split(' ');case 4:return i.split(' ').map(Number);case 6:return i.split('');case 7:return i.split('').map(Number);case 8:return i.join(' ');case 9:return i.join('\n');case 0:return i.join('');default:return i;}}catch(e){return i;}}

//Don't have to see. end------------------------------------------
function Main(){
  var N = nextInt();
  var output = 0;
  var list = nextIntArray();
  list.sort(function(a,b){
  	return a - b;
  });
  for(var i = 0; i < N - 1; i++){
    output += list[i + 1] - list[i] - 1;
  }
  myout(output);
}

var n = Number( readline() );
var arr = readline().split(' ');
var min = +arr[0];
var max = +arr[0];

for (var i = 0; i < arr.length; i++) {
	min = Math.min(min, arr[i]);
	max = Math.max(max, arr[i]);
}

var result = max - min + 1 - n;
print(result);

const readline = require('readline');
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  terminal: false
});
let c = 0;

rl.on('line', (d) => {
  if (c === 0) {
    c++;
    return;
  }

  const arr = d.split(' ').map(Number).sort((a, b) => a - b);
  const min = arr[0];
  const max = arr[arr.length - 1];
  const ans = max - min - (arr.length - 1);

  console.log(ans);
  c++;
});

var n = +readline(),min = Number.MAX_VALUE, max = -1;
readline().split(' ').forEach(x=>{x = +x;min = Math.min(min,x);max = Math.max(max,x)});
print(Math.abs(max-min+1-n));

var n = Number(readline());

var input = readline().split(' ');
var result = 0;

input.sort(function(a,b) {
  return a-b;
});

for(var i=0; i<n-1; i++) {
  result += input[i+1]-input[i]-1;
}

print(result);

"use strict";

var length = parseInt(readline());

var maxNumber = void 0,
  minNumber = void 0;
readline()
  .split(" ")
  .map(function(value, index) {
    value = parseInt(value);

    if (index === 0) {
      maxNumber = value;
      minNumber = value;
    }

    if (maxNumber < value) maxNumber = value;
    if (minNumber > value) minNumber = value;
  });

write(maxNumber - minNumber + 1 - length + "\n");

var keyNumber = +readline();
var keyCurrent = readline().split(' ').sort(function(a, b){return a - b});
var first = keyCurrent[0],
    end = keyCurrent[keyNumber - 1];

//write(keyCurrent)
write((end - first + 1) - keyNumber)

var n = Number(readline());
var a = readline().split(' ').sort((a,b)=>{return a-b}).map(e=>parseInt(e));

var res = a[a.length-1] - a[0] + 1;
res = res -n
print(res)

const stdin = process.stdin;
stdin.setEncoding( 'utf8' );
stdin.on( 'data', function( rawInput ){
    const inputLines = rawInput.split('\n');
    const n = parseInt(inputLines[0]);
    const indices = splitAndConvert(inputLines[1], n);
    const result = solve(indices, n);
    console.log(result);
});

function splitAndConvert(stringOfNumbers, numOfValid) {
	return stringOfNumbers.split(' ').slice(0, numOfValid).map(function(num) {
		return parseInt(num);
	});
}

function solve(indices, n) {
    const max = Math.max.apply(null, indices);
    const min = Math.min.apply(null, indices);
    const expectedN = max - min + 1;
    return expectedN - n;
}

const input = [];

const RL = require('readline').createInterface({
   input : process.stdin,
   output : process.stdout
});

RL.on('line', (line) => {
    input.push(line);
});

function fact(num)
{
    if(num === 0)
        return 1;
    return num * fact(num-1);
}

RL.on('close', () => {
    const n = parseInt(input[0], 10);
    const data = []; input[1].split(' ').forEach( i => data.push(parseInt(i, 10)) );
    data.sort((a, b) => a-b);
    let answer = 0;
    for(let i = 1; i < n; i += 1) {
        if(data[i-1]+1 !== data[i]) {
            answer += data[i] - data[i-1] - 1;
        }
    }
    console.log(answer);
});

readline()
var input = readline().split(' ').sort(function(a,b){return a-b;});

 var calc = 0;
 function myFunction(a, b) {return b - a;}

for(var x=0;x<input.length ;x++)
{
var r =  (myFunction(input[x],input[x+1]) - 1)
if (r > 0){calc+=r}

}
print(calc);

'use strict'
process.stdin.on('data', data => {
  data = data.toString().split('\n')
  data.shift()
  let arr = data.shift().split(' ').map(a => parseInt(a))
  arr = arr.sort((a, b) => a - b)
  let ans = 0
  for (let i = 1; i < arr.length; i++) {
    ans += arr[i] - arr[i - 1] - 1
  }
  console.log(ans)
})

var n = Number(readline());
var a = readline().split(' ').sort((a,b)=>{return a-b}).map(e=>parseInt(e));
var res = a[a.length-2] - n;

print(res)

const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

let l = 0;
rl.on('line', (input) => {
    if (l === 0) { l++ }
    else {
        rl.close();
        let arr = input.split(" ").map(x => parseInt(x))
        let cnt = 0
        for (let i = 0; i < arr.length - 1; i++) {
            if ((arr[i] + 1) !== arr[i]) {
                cnt++
            }
        }
        console.log(cnt)
        return 0;
    }
})

const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

let l = 0;
rl.on('line', (input) => {
    if (l === 0) { l++ }
    else {
        rl.close();
        let arr = input.split(" ").map(x => parseInt(x))
        let cnt = 0
        for (let i = 0; i < arr.length - 1; i++) {
            if ((arr[i] + 1) !== arr[i+1]) {
                cnt++
            }
        }
        console.log(cnt)
        return 0;
    }
})

const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

let l = 0;
rl.on('line', (input) => {
    if (l === 0) { l++ }
    else {
        rl.close();
        let arr = input.split(" ").map(x => parseInt(x))
        arr.sort()
        let cnt = 0
        for (let i = 0; i < arr.length - 1; i++) {
            if ((arr[i] + 1) !== arr[i+1]) {
                cnt++
            }
        }
        console.log(cnt)
        return 0;
    }
})

const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

let l = 0;
rl.on('line', (input) => {
    if (l === 0) { l++ }
    else {
        rl.close();
        let arr = input.split(" ").map(x => parseInt(x))
        arr.sort()
        let cnt = 0
        for (let i = 0; i < arr.length - 1; i++) {
            if ((arr[i] + 1) !== arr[i+1]) {
                cnt+=arr[i+1]-arr[i]
            }
        }
        console.log(cnt)
        return 0;
    }
})


var c = 0, ans = 0;
readline().replace(/heavy|metal/g, function(a) {
	if (a === 'heavy') c++;
	else ans += c;
	return '';
});
print(ans);

;(function () {

	print(function (s) {

		var t = [];

		['heavy', 'metal'].forEach(function (e, i) {
			var pos = -1;
			while (true) {
				pos = s.indexOf(e, pos + 1);
				if (pos !== -1) t.push([i, pos]);
				else break;
			}
		});

		t = t.sort(function (a, b) { return a[1] - b[1]; }).map(function (e) { return e[0]; });

		var l = 0;
		for (var i = 0, _i = t.length; i < _i; i++) {
			if (t[i] === 0) l++;
			else t[i] = l;
		}

		return t.reduce(function (p, e) { return p + e; }, 0) || 0;

	}(readline()));

}.call(this));

var c = 0, ans = 0;
readline().replace(/(heavy)|(metal)/g, function(_, a, b) {
	if (a) c++;
	else ans += c;
});
print(ans);

var c = 0, ans = 0;
readline().replace(/heavy|metal/g, function(a) {
	if (a === 'heavy') c++;
	else ans += c;
});
print(ans);


const THRESHOLD = Math.pow(2, 9);

const main = () => {
  readline();

  const A = readline()
    .split(" ")
    .map((i) => Number(i));
  const B = readline()
    .split(" ")
    .map((i) => Number(i));

  var minResult = Infinity;

  for (var result = 0; result < THRESHOLD; result++) {
    var canReachResult = true;

    for (var i = 0; i < A.length; i++) {
      var canReachCurrentResult = false;

      for (var j = 0; j < B.length; j++) {
        if (((A[i] & B[j]) | result) === result) {
          canReachCurrentResult = true;
          break;
        }
      }

      if (!canReachCurrentResult) {
        canReachResult = false;
        break;
      }
    }

    if (canReachResult) {
      minResult = Math.min(minResult, result);
    }
  }

  return minResult;
};

print(main());

var readline=require("readline");
var rl=readline.createInterface({
    input:process.stdin,
    output:process.stdout
});
var arr=[],a,b,n,m;
rl.on('line',function(inp){
    arr.push(inp);
    var len=arr.length;
    if(len===1){
        n=parseInt(arr[0].split(' ')[0]);
        m=parseInt(arr[0].split(' ')[1]);
    }else if(len===2){
        a=arr[1].split(' ');
        for(var i=0;i<n;i++) a[i]=parseInt(a[i]);
    }else if(len===3){
        b=arr[2].split(' ');
        for(var i=0;i<m;i++) b[i]=parseInt(b[i]);
        var C=Math.pow(2,9),vis=new Array(201);
        for(var i=0;i<201;i++) vis[i]=new Array(C+1).fill(0);
        for(var i=0;i<n;i++){
            for(var j=0;j<m;j++){
                var ab=a[i]&b[j];
                for(var k=0;k<C+1;k++){
                    if(i>0&&vis[i-1][k]) vis[i][k|ab]=1;
                }
                if(i===0) vis[i][ab]=1;
            }
        }
        for(var i=0;i<=C;i++){
            if(vis[n-1][i]){
                console.log(i);
                process.exit(0);
            }
        }
    }
});

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readLine() {
    return inputString[currentLine++];
}

function main() {
  const [n, m] = readLine().split(/\s/).map(Number)
  const as = readLine().split(/\s/).map(Number)
  const bs = readLine().split(/\s/).map(Number)
  const cCandidates = []
  let max = 0
  for (const a of as) {
    const cCandidate = bs.map(b => a & b)
    cCandidates.push(cCandidate)
    max = Math.max(max, Math.min.apply(null, cCandidate))
  }
  const cs = []
  for (const cCandidate of cCandidates) {
    cs.push(cCandidate.sort((a, b) => (a | max) - (b | max))[0])
  }
  console.log(cs.reduce((acc, curr) => acc | curr))
}

const main = () => {
  readline();

  const A = readline()
    .split(" ")
    .map((i) => Number(i));
  const B = readline()
    .split(" ")
    .map((i) => Number(i));

  var result = 0;

  for (var i = 0; i < A.length; i++) {
    var currMin = A[i] & B[0];

    for (var j = 1; j < B.length; j++) {
      if ((result | (A[i] & B[j])) < (result | currMin)) {
        currMin = A[i] & B[j];
      }
    }

    result |= currMin;
  }

  return result;
};

print(main());


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 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) 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(' '))
})()