Fashion Police (Large)

With J jackets, P pants, S shirts and a limit K on repeats of any two-garment pair, list a longest set of outfits and report its size.

Medium7MathCombinatoricsGreedyImplementationNo attempts yetTime limit5sMemory limit512 MB

Problem

You are so excited about the 2016 Code Jam World Finals that you just moved to New York. You have brought along JJ different jackets (numbered 1 through JJ), PP different pairs of pants (numbered 1 through PP), and SS different shirts (numbered 1 through SS). You have at least as many shirts as pairs of pants, and at least as many pairs of pants as jackets. (JPSJ \le P \le S.)

Every day, you pick one jacket, one pair of pants, and one shirt to wear as an outfit. You wash all of your garments every night, so all of your garments are available to use each day.

In New York, the Fashion Police officers are always watching and keeping track of what everyone wears every day. If they find out that you have worn the exact same outfit twice, you will immediately be taken to the Fashion Jail on 5th Avenue for a mandatory makeover; you definitely want to avoid that! You will also immediately be taken to Fashion Jail if they find out that you have worn the same two-garment combination more than KK times in total. A combination is a particular jacket worn with a particular pair of pants, a particular jacket worn with a particular shirt, or a particular pair of pants worn with a particular shirt. For example, in the set of outfits (jacket 1, pants 2, shirt 3) and (jacket 1, pants 1, shirt 3), the combination (jacket 1, shirt 3) appears twice, whereas the combination (pants 1, shirt 3) appears only once.

You wear one outfit per day. Find the largest possible number of days you can avoid being taken to Fashion Jail, and produce a list of outfits to use each day.

Input

The first line of the input gives the number of test cases, TT. TT test cases follow; each consists of one line with four integers JJ, PP, SS, and KK.

Limits

  • 1T1001 \le T \le 100
  • 1JPS1 \le J \le P \le S
  • 1K101 \le K \le 10
  • S10S \le 10

Output

For each test case, first output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the maximum number of days you can avoid being taken to Fashion Jail. Then output y more lines, each with three integers: the numbers of the jacket, pants, and shirt (in that order) for one day's outfit.

To make the answer unique, the outfit (a,b,c)(a, b, c) (jacket aa, pants bb, shirt cc) is in the list exactly when

(cab+1)modS<min(K,S)(c - a - b + 1) \bmod S < \min(K, S)

where the result of mod\bmod is taken in the range 00 to S1S - 1. Output every outfit that meets this condition, sorted in ascending lexicographic order (compare the jacket number, then the pants number, then the shirt number). The list built by this rule is a longest list that keeps you out of Fashion Jail.

Hint

In Case #1, even though the Fashion Police officers have set a lenient value of K=10K = 10, you can form only one outfit, so you can avoid Fashion Jail for only one day.

In Case #2, adding any other outfit would send you to Fashion Jail:

  • Adding 1 1 3 would use the combination (jacket 1, pants 1) more than 2 times.
  • Adding 1 2 1 would use the combination (jacket 1, pants 2) more than 2 times.

In this case, any set of 5 outfits includes at least one fashion violation.

Unlike JJ, PP, and SS, the numbers of the jacket, pants, and shirt within one outfit do not have to be in nondecreasing order.

In Case #3, you have only one jacket and pants combination, which you must keep reusing, so no matter which shirts you wear, you cannot form more than K=2K = 2 different outfits.

In Case #4, several maximum sets of outfits exist, but the output rule requires 1 1 1 and 1 2 2.