Problem:
Score : 600 points

Problem Statement

Ringo Mart, a convenience store, sells apple juice.

On the opening day of Ringo Mart, there were A cans of juice in stock in the morning.
Snuke buys B cans of juice here every day in the daytime.
Then, the manager checks the number of cans of juice remaining in stock every night.
If there are C or less cans, D new cans will be added to the stock by the next morning.

Determine if Snuke can buy juice indefinitely, that is, there is always B or more cans of juice in stock when he attempts to buy them.
Nobody besides Snuke buy juice at this store.

Note that each test case in this problem consists of T queries.

Constraints

1 \leq T \leq 300

1 \leq A, B, C, D \leq 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

T
A_1 B_1 C_1 D_1
A_2 B_2 C_2 D_2
:
A_T B_T C_T D_T

In the i-th query, A = A_i, B = B_i, C = C_i, D = D_i.

Output

Print T lines. The i-th line should contain Yes if Snuke can buy apple juice indefinitely in the i-th query; No otherwise.

Sample Input 1

14
9 7 5 9
9 7 6 9
14 10 7 12
14 10 8 12
14 10 9 12
14 10 7 11
14 10 8 11
14 10 9 11
9 10 5 10
10 10 5 10
11 10 5 10
16 10 5 10
1000000000000000000 17 14 999999999999999985
1000000000000000000 17 15 999999999999999985

Sample Output 1

No
Yes
No
Yes
Yes
No
No
Yes
No
Yes
Yes
No
No
Yes

In the first query, the number of cans of juice in stock changes as follows: (D represents daytime and N represents night.)

9
→D 2
→N 11
→D 4
→N 13
→D 6
→N 6
→D x

In the second query, the number of cans of juice in stock changes as follows:

9
→D 2
→N 11
→D 4
→N 13
→D 6
→N 15
→D 8
→N 8
→D 1
→N 10
→D 3
→N 12
→D 5
→N 14
→D 7
→N 7
→D 0
→N 9
→D 2
→N 11
→D …

and so on, thus Snuke can buy juice indefinitely.

Sample Input 2

24
1 2 3 4
1 2 4 3
1 3 2 4
1 3 4 2
1 4 2 3
1 4 3 2
2 1 3 4
2 1 4 3
2 3 1 4
2 3 4 1
2 4 1 3
2 4 3 1
3 1 2 4
3 1 4 2
3 2 1 4
3 2 4 1
3 4 1 2
3 4 2 1
4 1 2 3
4 1 3 2
4 2 1 3
4 2 3 1
4 3 1 2
4 3 2 1

Sample Output 2

No
No
No
No
No
No
Yes
Yes
No
No
No
No
Yes
Yes
Yes
No
No
No
Yes
Yes
Yes
No
No
No

Solution in Scala (2.11.7) (judge verdict: Wrong Answer)

import java.io.StringReader
import java.nio.charset.StandardCharsets
import java.nio.file.{Files, Paths}

import scala.io.StdIn
import scala.language.higherKinds
import scala.reflect.ClassTag

object Main {
  import StdInScanner._
  def main(args: Array[String]): Unit = {
    val scanner = for {
      t <- readAnInt
      qs <- readLongMatrix(t)
    } yield (t, qs)
    val (t, qs) = scanner.run
    for (q <- qs) {
      println(solve(q(0), q(1), q(2), q(3)))
    }
  }

  def solve(a: Long, b: Long, c: Long, d: Long): String = {
    val night0 = a - b
    val diff = d - b
    if (diff < 0 || night0 < 0) "No"
    else if (diff == 0) {
      if (night0 < c) "Yes"
      else "No"
    }
    else {
      val n = (c - night0) / diff + 1
      val dayMin = night0 + diff * n
      if (dayMin < b) {
        "No"
      }
      else {
        "Yes"
      }
    }
  }
}


case class StdInScanner[A](scan: StdIn.type => A) {
  // transform read result by "f"
  def map[B](f: A => B): StdInScanner[B] =
    StdInScanner(in => f(scan(in)))

  // run this to read A, then obtain new scanner from A using "f", then run the new scanner
  def flatMap[B](f: A => StdInScanner[B]): StdInScanner[B] =
    StdInScanner(in => f(scan(in)).scan(in))

  // run entire scanner and get result
  def run: A = scan(StdIn)

  // run entire scanner on the text file
  def runOnFile(filename: String): A =
    Console.withIn(Files.newBufferedReader(Paths.get(filename), StandardCharsets.UTF_8))(this.scan(StdIn))

  // run entire scanner on the String
  def runOnString(str: String): A =
    Console.withIn(new StringReader(str))(this.scan(StdIn))
}

object StdInScanner {
  /* pure(read nothing) scanners */
  // read nothing, and return specified "a"
  def apply[A](a: A): StdInScanner[A] = StdInScanner(_ => a)
  // read nothing, and return ()
  def unit: StdInScanner[Unit] = StdInScanner(())

  /* read single line -> produce single value */
  // read a line
  def readALine: StdInScanner[String] = StdInScanner(s => s.readLine().trim)
  // read a line as Int
  def readAnInt: StdInScanner[Int] = StdInScanner(s => s.readInt())
  // read a line as Long
  def readALong: StdInScanner[Long] = StdInScanner(s => s.readLong())

  def readAPair[A, B](fa: String => A, fb: String => B): StdInScanner[(A, B)] =
    readTokens.map(v => (fa(v(0)), fb(v(1))))

  def readIntPair: StdInScanner[(Int, Int)] =
    readAPair(_.toInt, _.toInt)

  def readATriple[A, B, C](fa: String => A, fb: String => B, fc: String => C): StdInScanner[(A, B, C)] =
    readTokens.map(v => (fa(v(0)), fb(v(1)), fc(v(2))))

  def readIntTriple: StdInScanner[(Int, Int, Int)] =
    readATriple(_.toInt, _.toInt, _.toInt)

  def readTwoPairs[A, B](fa: String => A, fb: String => B): StdInScanner[((A, B), (A, B))] =
    readTokens.map(v => ((fa(v(0)), fb(v(1))), (fa(v(2)), fb(v(3)))))

  def readTwoIntPairs: StdInScanner[((Int, Int), (Int, Int))] =
    readTwoPairs(_.toInt, _.toInt)

  /* read single line -> produce multiple values */
  // read tokens from a line, then map them with "f"
  def readTokensWithMap[A](f: String => A): StdInScanner[Vector[A]] =
    readALine.map(_.split(" ").toVector.map(f))

  def readTokens: StdInScanner[Vector[String]] =
    readTokensWithMap(identity)

  def readIntTokens: StdInScanner[Vector[Int]] =
    readTokensWithMap(_.toInt)

  def readLongTokens: StdInScanner[Vector[Long]] =
    readTokensWithMap(_.toLong)

  /* read multiple lines -> produce one value per line */
  def readLines(n: Int): StdInScanner[Vector[String]] =
    StdInScanner(s => Vector.fill(n)(s.readLine().trim))

  def readLinesWithMap[A](n: Int, f: String => A): StdInScanner[Vector[A]] =
    StdInScanner(s => Vector.fill(n)(f(s.readLine())))

  def readInts(n: Int): StdInScanner[Vector[Int]] =
    StdInScanner(s => Vector.fill(n)(s.readInt()))

  def readBigInts(n: Int): StdInScanner[Vector[BigInt]] =
    StdInScanner(s => Vector.fill(n)(BigInt(s.readLine())))

  def readPairs[A, B](n: Int)(fa: String => A, fb: String => B): StdInScanner[Vector[(A, B)]] =
    readLines(n).map(_.map{ line =>
      val t1 :: t2 :: _ = line.split(" ").toList
      (fa(t1), fb(t2))
    })
  def readIntPairs(n: Int): StdInScanner[Vector[(Int, Int)]] =
    readPairs(n)(_.toInt, _.toInt)

  def readTriples[A, B, C](n: Int)(fa: String => A, fb: String => B, fc: String => C): StdInScanner[Vector[(A, B, C)]] =
    readLines(n).map(_.map{ line =>
      val t1 :: t2 :: t3 :: _ = line.split(" ").toList
      (fa(t1), fb(t2), fc(t3))
    })

  def readIntTriples(n: Int): StdInScanner[Vector[(Int, Int, Int)]] =
    readTriples(n)(_.toInt, _.toInt, _.toInt)

  /* read multiple lines -> produce multiple value per line */
  // "2D Matrix of A" is Vector of "Vector of A"
  type Matrix[A] = Vector[Vector[A]]

  // read a table of "rows", then map each elements with "f"
  def readTableWithMap[A: ClassTag](rows: Int, f: String => A): StdInScanner[Matrix[A]] =
    readLines(rows).map{ lns =>
      lns.map(ln => ln.split(" ").toVector.map(tkn => f(tkn)))
    }
  def readTable(rows: Int): StdInScanner[Matrix[String]] =
    readTableWithMap(rows, identity)

  def readIntMatrix(rows: Int): StdInScanner[Matrix[Int]] =
    readTableWithMap(rows, _.toInt)

  def readLongMatrix(rows: Int): StdInScanner[Matrix[Long]] =
    readTableWithMap(rows, _.toLong)
}