Cooling Down the Task Runner
A task runner works in numbered time slots 1, 2, 3, …. In every slot it either executes exactly one task or stays idle. Every task takes exactly one slot and has a type, an uppercase letter from A to Z.
After a task of some type runs, the runner has to cool down before it can run another task of the same type: if two tasks of the same type run in slots i < j, then at least n slots must lie strictly between them, that is j − i ≥ n + 1. Tasks of different types never restrict each other, and tasks may be executed in any order.
You are given how many tasks of each type must be executed. Find the smallest T such that all tasks can be executed in slots 1 … T. When the number of tasks is small, also print one schedule achieving it.
Input
The first line contains t (1 ≤ t ≤ 10^4), the number of test cases. Each test case consists of two lines:
- the cooldown
n(0 ≤ n ≤ 10^5); - 26 integers
c_A, c_B, …, c_Z(0 ≤ c_i ≤ 10^6), the number of tasks of each type.
In every test case the total number of tasks m = c_A + … + c_Z is at least 1. The sum of m over all test cases does not exceed 10^6.
Output
For each test case print the minimum number of slots T on its own line.
If m ≤ 1000, then print m more lines describing one optimal schedule: each line contains a slot number s and a letter, meaning a task of that type runs in slot s. The lines must be ordered by strictly increasing s, every s must satisfy 1 ≤ s ≤ T, each type must appear exactly as many times as it has tasks, and the cooldown rule must hold. Any optimal schedule is accepted.
If m > 1000, print only T.
Notes
In the first example the first test case has n = 2 with three A and three B tasks. A schedule A B . A B . A B (dots are idle slots) uses 8 slots. Fewer is impossible: three tasks of one type span at least 7 slots (first to last), and the first A and the first B cannot share slot 1, so one of the two types finishes no earlier than slot 8. In the second test case there is no cooldown, so the runner is never idle.
The answer may not fit in a 32-bit integer.