Asynchronous data analysis with concurrency limit

C# Hard async

Task «Asynchronous data analysis with concurrency limit»

Develop an asynchronous console application that reads from stdin the number N, then N lines, each line containing an integer. The application should process each number using the asynchronous function SimulateWorkAsync(int value), which simulates a delay (value ms) and returns the square of the number. It is necessary to limit the maximum number of concurrently executing tasks to 3. Output the results in the order of task completion (not in input order). If any task completes with an exception (for example, when value < 0), the application should wait for all tasks to complete, output "Error", and exit. Use SemaphoreSlim to manage concurrency.

Input Format

First line — integer N (1 ≤ N ≤ 100). Then N lines, each containing an integer from -10 to 10.

Output Format

For each successful result, output the square of the number on a separate line in the order the tasks were completed. If an error occurs, output only "Error".

Examples

Example 1

INPUT
2 5 2
OUTPUT
4 25

Example 2

INPUT
2 https://httpbin.org/status/200 https://httpbin.org/status/404 5000
OUTPUT
Error

Example 3

INPUT
2 -1 4
OUTPUT
Error
main.cs