Mandalas

Time limit1sMemory limit128 MB

Summary
Given up to 500 circles, count how many regions the arrangement of circles divides the plane into, handling tangency and duplicate circles exactly.
Level

Medium7 of 10

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

Problem

Shasita is drawing mandalas. First she draws several circles, and then she colours the regions that they divide the plane into.

Her younger sister Rice agrees to help: Rice will draw the circles and Shasita will colour them. Shasita does not want to colour a huge number of tiny regions, so she wants to fix in advance how many regions the circles will create. Rice, however, is not good at counting regions, so she asks you for help.

Given a mandala, that is, an arrangement of circles, determine how many regions it divides the plane into.

Input

The first line contains the number of test cases. Each test case has the following format:

  • one line with the integer nn (0≤n≤5000 \le n \le 500), the number of circles;
  • nn lines follow, each with three integers xix_i, yiy_i, rir_i describing the ii-th circle, whose centre is (xi,yi)(x_i, y_i) and whose radius is rir_i, with ∣xi∣,∣yi∣≤1000|x_i|, |y_i| \le 1000 and 0<ri≤10000 < r_i \le 1000.

Output

For each test case, output a single integer on its own line: the number of regions into which the circles divide the plane R2\mathbb{R}^2.

Examples4

  1. Example 1

    Input
    3
    1
    5 7 10
    2
    0 0 5
    10 0 5
    3
    0 0 7
    10 10 7
    0 10 8
    
    Expected output
    2
    3
    6
    
  2. Example 2

    Input
    1
    0
    
    Expected output
    1
    
  3. Example 3

    Input
    1
    2
    0 0 3
    0 0 5
    
    Expected output
    3
    
  4. Example 4

    Input
    1
    2
    0 0 5
    6 0 5
    
    Expected output
    4