Everybody May Get Lost in Space

Time limit1sMemory limit512 MB

Summary
Given three balls in 3D space, compute the exact volume of their union, rounded to six decimals.
Level

Medium7 of 10

Topics
Geometry, Math, Implementation, Intervals
Solved
No attempts yet

Problem

A space shuttle is drifting somewhere in empty space, and its exact position is unknown. To help locate it, the shuttle is equipped with three independent navigation systems.

Each system reports the shuttle's position as coordinates (x,y,z)(x, y, z) together with a bound bb on its measurement error. If a system is working correctly, the shuttle's true position lies within distance bb of the reported point: it is some (x′,y′,z′)(x', y', z') with (x−x′)2+(y−y′)2+(z−z′)2≤b\sqrt{(x - x')^2 + (y - y')^2 + (z - z')^2} \le b. In other words, each system restricts the shuttle to a solid ball of radius bb centered at its reported point (x,y,z)(x, y, z).

At least one of the three systems is guaranteed to be working, but any of the others may be broken and report arbitrary, meaningless values. Since we do not know which systems are reliable, the shuttle may be located at any point that lies inside at least one of the three balls: if a point lies in some system's ball, then that system alone being the working one is a consistent explanation.

Compute the total volume of the region of all possible positions of the shuttle, which is the volume of the union of the three balls.

Input

The first line contains a single positive integer ZZ (Z≤10000Z \le 10000), the number of test cases. Each test case is given on three lines. Each of those lines describes one measurement with four integers xx, yy, zz, and bb separated by single spaces, where −109≤x,y,z≤109-10^9 \le x, y, z \le 10^9 and 1≤b≤1091 \le b \le 10^9. Here (x,y,z)(x, y, z) is the reported position and bb is the error bound, i.e. the radius of that system's ball.

Output

For each test case, print on its own line the volume of the union of the three balls, rounded to exactly six digits after the decimal point.

Examples5

  1. Example 1

    Input
    2
    0 0 0 10
    19 0 0 10
    23 0 0 10
    0 0 0 10
    12 0 0 10
    18 0 0 10
    
    Expected output
    9602.016146
    9334.718971
    
  2. Example 2

    Input
    1
    0 0 0 5
    0 0 0 5
    0 0 0 5
    
    Expected output
    523.598776
    
  3. Example 3

    Input
    1
    0 0 0 6
    5 0 0 6
    40 0 0 4
    
    Expected output
    1705.623012
    
  4. Example 4

    Input
    1
    0 0 0 3
    30 0 0 4
    0 30 0 5
    
    Expected output
    904.778684
    
  5. Example 5

    Input
    1
    0 0 0 10
    1 1 0 3
    15 0 0 8
    
    Expected output
    6215.169826