A Pie for a Pie
Bessie and Elsie have each baked n pies. Each of the 2n pies has a tastiness value according to Bessie and a (possibly different) tastiness value according to Elsie.
Bessie is thinking about giving one of her pies to Elsie. If Elsie receives a pie from Bessie, she will feel obligated to give one of her pies to Bessie. So as to not appear stingy nor flamboyant, Elsie will try to pick a pie that is at least as tasty (in Elsie's eyes) as the pie she received, but no more than d units tastier. Such a pie may not exist, in which case Elsie will adopt a pseudonym and exile herself to Japan.
But if Elsie does give Bessie a pie in return, Bessie will similarly try to give Elsie a pie which is at least as tasty but no more than d units tastier (in Bessie's eyes) as the pie Elsie just gave her. Should this be impossible, Bessie too will exile herself. Otherwise she will give her chosen pie to Elsie. This cycle will continue until one of the cows is exiled, an unhappy outcome, or one of the cows receives a pie which she accords a tastiness value of 0, in which case the gift exchange will end and both cows will be happy.
Note that a pie may not be gifted twice, nor can either cow return a pie gifted to her.
For each of the n pies Bessie could select as her initial gift to Elsie, determine the minimum number of pies that could possibly be gifted in the resulting exchange before the cows are happy.
Input
The first line contains the two integers n (1 ≤ n ≤ 10^5
) and d (0 ≤ d ≤ 10^9
). The next 2n lines contain two space-separated integers each, respectively denoting the value of a particular pie according to Bessie, and the value of that pie according to Elsie.
The first n lines refer to Bessie's pies, and the remaining n lines refer to Elsie's pies.
It is guaranteed that all tastiness values are in the range [0, 10^9
].
Output
There should be n lines in the output. Line i should contain a single integer: the minimum number of pies that could be gifted in a happy gift exchange started with Bessie's pie i. If no gift exchange starting with pie i is happy, then line i should contain the single integer −1 instead.