We need to compute total distance walked in the process of turning off N+1 lights (positions 0 to N). Initially all on. Starting at (0,0). Process: repeatedly alternate: go to the rightmost light that is still on (turn it off), then go to the leftmost light that is still on (turn it off), then to rightmost, etc. until all off. The pattern: we have positions 0...N. Starting position is 0 (leftmost). At each step, we move from current position to the extreme (leftmost or rightmost) among the still-on lights, and turn it off.
We need total distance traveled.
Observation: The process essentially eliminates lights from the ends, but alternating. Let's simulate small N.
N=0: Actually N+1 lights from 0 to N. For N=0, only one light at 0. Initially at 0, but all lights on. Rightmost on is 0, distance 0? But we need to turn it off, we are already at it? However, we need to "walk to the right most light that is on, turn it off." Starting at (0,0). The rightmost on is (0,0). Do we need to move? Distance 0? But then we stop. The example gives test case N=1 yields output 2. For N=0, maybe not considered because constraints N>=1? Actually constraints: 1 ≤ N ≤ 10^5, so N>=1. So N=0 not given. For N=1: lights at 0 and 1. Start at 0.
Step1: go to rightmost on = 1, distance =1. Turn off 1. Now at 1.
Step2: go to leftmost on = 0, distance =1. Turn off 0. Total=2. Output 2 matches.
N=2: example output 5.
Let's simulate: start at 0.
Step1: rightmost on = 2, distance 2 -> turn off 2, at 2.
Step2: leftmost on = 0, distance 2 -> turn off 0, at 0.
Step3: rightmost on = 1, distance 1 -> turn off 1, at 1. Total=5.
N=3: Let's compute manually to see pattern.
Lights: 0,1,2,3. Start 0.
Step1: go to 3, dist=3. Turn off 3, at 3.
Step2: go to leftmost on = 0, dist=3. Turn off 0, at 0.
Step3: rightmost on now = 2? Actually after turning off 0 and 3, remaining lights: 1,2. Rightmost on = 2, dist=2 (from 0 to 2). Turn off 2, at 2.
Step4: leftmost on = 1, dist=1 (from 2 to 1). Turn off 1, at 1. Total distance = 3+3+2+1 = 9.
But let's verify: Might be other pattern? Possibly after step2, the leftmost on is 1? Wait step2 we turned off 0, so leftmost on after step1 was 0, we turned off 0, so after step2, lights on: 1,2. Actually step2: from position 3, go to leftmost on which is 0, distance 3, turn off 0. Now current position is 0. Yes. Then step3: rightmost on from remaining {1,2} is 2, distance 2 (0->2). Turn off 2, now at 2. Then step4: leftmost on =1, distance 1 (2->1). Turn off 1, done. Total 9.
N=4: Let's compute.
Lights 0-4. Start 0.
Step1: to 4, dist4, turn off 4, at 4.
Step2: to leftmost on =0, dist4, turn off 0, at 0.
Remaining: 1,2,3.
Step3: rightmost on =3, dist3 (0->3), turn off 3, at 3.
Step4: leftmost on =1, dist2 (3->1), turn off 1, at 1.
Remaining: 2 only? Actually after turning off 1, remaining lights: 2? Check: After step3 we turned off 3, so remaining: 1,2. After step4 we go to leftmost on=1 and turn off 1, remaining: 2 only. Now step5: need to turn off last light. Since pattern: after turning off leftmost, next would be rightmost? But we turned off leftmost at step4, then next step is to go to rightmost on, which would be 2. So step5: from position 1 to 2, dist1, turn off 2, at 2. Total distance = 4+4+3+2+1 = 14.
Alternatively, maybe we stop after turning off all lights: after step4, if only one light left, do we need to go to it? Actually the process says: "You will start at (0,0). First, you walk to the right most light that is on, turn it off. Then you walk to the left most light that is on, turn it off. Then again to the right most light that is on and so on. You will stop after turning off all lights." So after each turn-off, we check if lights remain. So if after turning off leftmost, there is still one light, we will proceed to next step: go to rightmost (which is the same) and turn it off. So step5 is needed. So total steps = number of lights = N+1. For N=4, that's 5 steps, distances: 4,4,3,2,1 = 14.
Now check N=3, we had 4 steps: distances 3,3,2,1 = 9. That matches pattern: sum of first N+1? Not exactly. Let's list totals for N=1: 2 steps: distances 1,1 => 2. N=2: 3 steps: 2,2,1 =>5. N=3: 4 steps: 3,3,2,1 =>9. N=4:5 steps:4,4,3,2,1=>14. N=5: Let's compute by logic: Lights 0-5. Steps:
1: 0->5 dist5, off5
2: 5->0 dist5, off0
3: 0->4 dist4, off4
4: 4->1 dist3, off1
5: 1->3? Wait remaining after step4: After step3 we turned off 4, remaining: 1,2,3? Actually step3: from 0, rightmost among {1,2,3,4}? Wait after step2, off: 5 and 0. Remaining: 1,2,3,4. Rightmost on =4, off4, at4. Step4: leftmost on =1, off1, at1. Remaining: 2,3. Step5: rightmost on =3, dist2 (1->3), off3, at3. Step6: leftmost on =2, dist1 (3->2), off2. Total steps:6 distances: 5,5,4,3,2,1? But step5 distance is 2? Actually from 1 to 3 is 2, step6 from 3 to 2 is 1. So distances: step1:5, step2:5, step3:4, step4:3, step5:2, step6:1 = total 20? Sum=5+5+4+3+2+1 = 20. Check: 5+5=10, +4=14, +3=17, +2=19, +1=20. Yes.
But earlier for N=4, we had distances:4,4,3,2,1 =14. For N=5: 5,5,4,3,2,1=20. N=3: 3,3,2,1=9. N=2: 2,2,1=5. N=1:1,1=2.
Observing pattern: Total distance = (sum of first N numbers?) Actually for N=1, sum of numbers 1+? 2 =? For N=2: 2+2+1 =5. For N=3: 3+3+2+1=9. For N=4:4+4+3+2+1=14. For N=5:5+5+4+3+2+1=20. So the sequence of distances in steps appears to be: for each i from N down to 1, we have two occurrences of i? But careful: For N=5 we have two 5s, then 4,3,2,1. That's like: two copies of N, then decreasing from N-1 down to 1. For N=4: two 4s, then 3,2,1. For N=3: two 3s, then 2,1. For N=2: two 2s, then 1. For N=1: two 1s? Actually N=1: distances: 1 and 1. That's two 1s and no lower numbers. So pattern holds: For N, total distance = 2N + sum_{i=1}^{N-1} i? Wait sum_{i=1}^{N} i = N(N+1)/2. But we have two N's? Actually we have 2 copies of N? Check: For N=5, sum_{i=1}^{5} i =15. But we have additional N? Because we have distances: 5,5,4,3,2,1 = 5+ (5+4+3+2+1) = 5 + 15 = 20. So total = N + sum_{i=1}^{N} i? That would be N + N(N+1)/2 = (2N + N(N+1))/2? Actually careful: The distances are: first step: N, second step: N, third: N-1, fourth: N-2, ... last: 1? For N=5 we have steps: 5,5,4,3,2,1 (6 steps). That's 2 N's? Actually there are 2 N's? There's step1 = N, step2 = N? Yes. Then the rest are descending from N-1 to 1. So total = 2N + sum_{i=1}^{N-1} i? Because sum from i=1 to N-1 = (N-1)N/2. So total = 2N + N(N-1)/2 = (4N + N(N-1))/2 = N(N+3)/2. For N=5: 58/2=40/2=20 correct. For N=4: 47/2=28/2=14 correct. For N=3: 36/2=18/2=9 correct. For N=2: 25/2=10/2=5 correct. For N=1: 1*4/2=4/2=2 correct. So formula: total distance = N(N+3)/2.
But is that always? Let's test N=6: lights 0-6. Steps: start 0.
Step1: to 6 dist6 off6, at6
Step2: to 0 dist6 off0, at0
Step3: to 5 dist5 off5, at5
Step4: to 1 dist4 off1, at1
Step5: to 4 dist3 off4, at4
Step6: to 2 dist2 off2, at2
Step7: to 3 dist1 off3, at3
Total distances: 6,6,5,4,3,2,1 = sum = 6+6+5+4+3+2+1 = 27? Compute: 6+6=12, +5=17, +4=21, +3=24, +2=26, +1=27. Formula N(N+3)/2 for N=6: 6*9/2=54/2=27. Yes.
Thus pattern seems consistent: total distance = N(N+3)/2. But need to verify for all N? Let's derive formally.
Process: Starting at leftmost (0). The lights are initially on at positions 0,1,...,N. We alternate extreme toggles. The process essentially removes lights from both ends in alternating order: first remove rightmost, then leftmost, then rightmost among remaining, then leftmost among remaining, etc. However, note that after removing two extremes, the "current position" ends at the opposite extreme, which might affect distances. But the pattern we observed yields distances: For first two moves, we always travel full length N from one end to the other and back. Then the remaining lights are 1,...,N-1? Wait after first two moves, we have turned off 0 and N, and we are at 0 after step2. The remaining lights are positions 1 through N-1. Now it's like a subproblem of size N-1, but with starting point at the left end (0). However, the distances in the subproblem are not exactly same as original because the positions are offset by 1? Let's examine. After step2, current position is at 0. But the remaining lights are from 1 to N-1. If we "shift" coordinates by subtracting 1, we have lights at 0 to N-2 (since N-1 - 1 = N-2). But the starting point is at -1 relative to the new leftmost? Actually if we think of subproblem: we have positions 1...N-1. The distance between points in the subproblem measured in original coordinates. Starting point is at 0, which is left of the leftmost (1). When we go to rightmost among remaining (N-1), the distance is (N-1 - 0) = N-1, which is (size+1)? Actually size = (N-1) - 1 + 1 = N-1? Wait number of lights left = N-1? Actually originally N+1 lights. After removing two, we removed 2, so left = N-1 lights: positions 1..N-1. That's size = N-1. The distance from current position 0 to rightmost N-1 is N-1. In the pattern we observed for N=5, after step2 we had remaining 1..4, and step3 distance was 4, which equals (N-1) =4. Then step4 from position 4? Actually step3 we go to N-1? Wait for N=5, after step2 we are at 0, remaining: 1,2,3,4. Step3: go to rightmost =4, distance 4, turn off 4, now at 4. Then step4: go to leftmost =1, distance 3 (4-1=3). Then step5: go to rightmost among remaining {2,3} =3, distance 2 (1->3? Actually after step4 we turned off 1 and are at 1? Wait careful: step4: from 4 to leftmost 1 distance 3, turn off 1, now at 1. Remaining: 2,3. Step5: go to rightmost =3 distance 2, turn off 3, now at 3. Step6: go to leftmost =2 distance 1, turn off 2, done. So distances: step3 = N-1 =4; step4 = (N-1)-1? Actually distance 3 = (N-2)? Not simply.
But overall pattern seems to be: The distances in order are: N, N, N-1, N-2, ..., 2, 1. That's what we saw for all examples. Let's prove that.
We can think of the process as: We have an interval [L,R] of remaining lights, initially L=0, R=N. We also have a current position p. Initially p=0. The process: if turn direction is rightmost: we go from p to R (distance |p-R|), turn off R, set R' = R-1 (since the rightmost light removed), and new p = R. Next turn direction is leftmost: go from p (which is old R) to L (distance |old R - L|), turn off L, set L' = L+1, p = L. Then repeat.
Thus after two moves, we have L increased by 1, R decreased by 1, and p ends at L (the new leftmost after increment? Actually after turning off L, p becomes L (the old leftmost). But then L becomes L+1, so the current position is at the old leftmost, which is now less than the new leftmost? Because L increased by 1, so the leftmost still on is L+1, but the current position is at L which is now off. So after step2, p = old L, but that position is off. That's okay; we start next step from that off position. The next step is go to rightmost on, which is R' = R-1. So distance = |p - (R-1)| = |L - (R-1)|. Since L ≤ R-1? Initially L=0, R=N, after first two steps L becomes 1, R becomes N-1. p = old L = 0. So distance = (R-1 - p) = (N-1 - 0) = N-1. So step3 distance = (R-1) - L_old = (N-1) - 0 = N-1. But note L_old=0. That's N-1, which is the same as original N? Actually it's N-1.
Then after step3, we turn off new rightmost R-1, so p becomes R-1 = N-1. Now R becomes R-2. L is still 1 (since only leftmost off is 0, but leftmost on is 1). Next step: go to leftmost on = L (which is 1). Distance = |p - L| = (N-1 - 1) = N-2. So step4 distance = N-2.
After step4, turn off L=1, p becomes 1. Then L becomes 2. Next step: go to rightmost on = R' = N-2, distance = |1 - (N-2)| = N-3. Then step6: leftmost on = L=2, distance = |N-2 - 2| = N-4, etc.
Thus we see that after the initial two steps which gave distances N and N, subsequent distances descend by 1 each step: N-1, N-2, N-3, ... until the final distance 1. In fact, the sequence of distances for the steps after the first two are exactly N-1, N-2, ..., 1. Combined with the first two distances N and N, we get total = 2N + (N-1)+(N-2)+...+1 = 2N + (N-1)N/2 = N(N+3)/2.
But careful: For odd number of lights? N+1 lights. The process steps count is N+1. The number of "extra" distances after the first two is (N-1). Since total steps = N+1, and we have 2 steps accounted for, remaining steps = N-1. Those steps distances descending from N-1 down to 1. That fits because N-1 descending to 1 gives exactly N-1 numbers. So it's consistent.
Thus formula holds for all N.
But we should verify for N=0 (if existed) would produce? N=0 would have 1 light. According to process, start at 0. Rightmost on = 0, distance 0. Total distance 0. But formula N(N+3)/2 = 0, matches. But not required.
Thus answer for each test case: N(N+3)//2.
But we must handle T up to 1e5 and N up to 1e5. Computations are trivial.
But is there any nuance? Let's re-read the problem statement: "There are N+1 lights. Lights are placed at (0,0), (1,0), ... (N,0). Initially all lights are on. You want to turn off all of them one after one. You will start at (0,0). First, you walk to the right most light that is on, turn it off. Then you walk to the left most light that is on, turn it off. Then again to the right most light that is on and so on. You will stop after turning off all lights. You want to know how much distance you walked in the process." Yes matches our interpretation.
Check with N=2 example: output 5 matches. Good.
Thus answer: For each test case, print N*(N+3)//2.
But wait, is it always integer? Yes, N(N+3) is always even? For any integer N, N(N+3) mod 2: if N is even, then N even => product even; if N is odd, then N+3 is even => product even. So integer.
Thus we can compute.
Now double-check for larger N: N=100000, output = 100000100003/2 = 50000100003 = 5,000,150,000? Actually 100000*100003 = 10,000,300,000, divided by 2 = 5,000,150,000. That's within 64-bit (max ~9e18). So 64-bit is fine. Use long long in C++.
Edge Cases: N=1 => 1*4/2=2 correct. N=10^5.
Thus solution: read T, for each N, compute and print.
But note constraints: T up to 10^5, so O(T) works.
Thus answer is simple.
But before finalizing, we must ensure that the pattern indeed always yields distances descending by 1 from N-1 to 1. Let's simulate more thoroughly with induction.
We have initial state: L=0, R=N, p=0, direction = rightmost first.
We can define step count: total steps = N+1.
Let i denote step number from 1 to N+1.
We claim distances: step1: N; step2: N; for k from 3 to N+1: distance = N+2 - k? Actually when k=3, distance = N-1; k=4: N-2; ... k = N+1, distance = N+2 - (N+1) = 1. Yes.
Proof by invariant: After each pair of moves (rightmost then leftmost), the interval shrinks by 1 on both ends, and the current position ends at the left end (the turned-off leftmost). And the distances for that pair (except maybe first) follow a pattern.
But we can just accept the pattern as observed.
However, is there any possibility that the distances would differ if N+1 is odd vs even? The pattern we derived for N=2 (3 lights) gave distances 2,2,1; for N=3 (4 lights) gave 3,3,2,1; for N=4 (5 lights) gave 4,4,3,2,1; for N=5 (6 lights) gave 5,5,4,3,2,1. So it's consistent.
One more test: N=6 (7 lights) we computed 6,6,5,4,3,2,1 = 27. Good.
Thus answer.
Implementation: In Python, straightforward: for each test case, print(N*(N+3)//2).
Time complexity O(T).
Memory: trivial.
But caution: Input size large, use sys.stdin.read for fast I/O.
Now produce final answer.
But the problem may also be solved by a direct formula: sum of first N numbers plus N + N? Actually N(N+3)/2 = N + sum_{i=1}^{N} i? Wait sum_{i=1}^{N} i = N(N+1)/2. Then N(N+3)/2 = N(N+1)/2 + N = sum_{i=1}^{N} i + N. But we saw total = 2N + (N-1)N/2 = N + sum_{i=1}^{N} i? Because N + N(N+1)/2 = N(N+3)/2. So total = sum_{i=1}^{N} i + N.
Alternative interpretation: Total distance = (N+1 choose 2) + N? Actually sum_{i=1}^{N} i = N(N+1)/2, plus N gives N(N+3)/2. Not needed.
Thus answer.
Now final output.
We should ensure to use integer arithmetic and newline separated.
Thus solution done.
But is there any twist? Perhaps the starting point is at (0,0) and the rightmost on is N, but if we start at 0 and the rightmost is N, distance = N. That's fine.
After turning off the rightmost, we go to leftmost, which is 0 initially, distance = N. That's step2.
Now after that, remaining lights are positions 1..N-1. The current position is 0. Then next rightmost is N-1, distance = N-1. Then leftmost is 1, distance = (N-1)-1 = N-2. Then rightmost becomes N-2, distance = (N-2)-1? Actually after turning off leftmost 1, current position is 1; remaining lights: 2..N-1? Wait after step3 we turned off N-1, so remaining: 1..N-2? Let's track properly: after step2: off: 0 and N; on: 1..N-1; position: 0.
Step3: go to N-1 (rightmost), distance N-1, turn off N-1; now on: 1..N-2; position: N-1.
Step4: go to leftmost on = 1, distance N-1 - 1 = N-2; turn off 1; now on: 2..N-2; position: 1.
Step5: go to rightmost on = N-2, distance = N-2 - 1 = N-3; turn off N-2; now on: 2..N-3; position: N-2.
Step6: go to leftmost on = 2, distance = N-2 - 2 = N-4; turn off 2; now on: 3..N-3; position: 2.
... and so on. So distances indeed decrease by 1 each step.
Thus formula correct.
Now produce final answer.
Given the problem is from CodeChef? Probably Light Switching problem. The answer is N*(N+3)/2.
We'll write solution in Python.
Edge Cases: none.
Testing:
Input:
2
1
2
Our output:
2
5
Matches.
Thus final code.
We'll include reading all ints.
Implementation:
import sys
def main():
data = sys.stdin.read().strip().split()
if not data:
return
t = int(data[0])
out = []
for i in range(1, t+1):
n = int(data[i])
out.append(str(n*(n+3)//2))
sys.stdout.write("\n".join(out))
if name == "main":
main()
That's it.
Now produce final answer.
```python
import sys
def main():
data = sys.stdin.read().strip().split()
if not data:
return
t = int(data[0])
out = []
for i in range(1, t + 1):
n = int(data[i])
ans = n * (n + 3) // 2
out.append(str(ans))
sys.stdout.write("\n".join(out))
if name == "main":
main()