import math

class Circle:
    def __init__(self, x, y, r):
        self.x = x
        self.y = y
        self.r = r

def angle_between(p1, p2):
    return math.atan2(p2.y - p1.y, p2.x - p1.x)

def external_tangent_angle(c1, c2):
    dx = c2.x - c1.x
    dy = c2.y - c1.y
    d = math.hypot(dx, dy)
    if d < abs(c1.r - c2.r):  # circles are inside one another, invalid
        return None
    angle = math.atan2(dy, dx)
    #print("angle ", angle*(180/3.1415926))
    offset = math.asin((c1.r - c2.r) / d)
    #return angle - offset, angle + offset
    ret = angle + offset
    if(ret < 0): 
        ret += 2*math.pi
    return ret

def tangent_length(c1, c2):
    d = math.hypot(c2.x - c1.x, c2.y - c1.y)
    return math.sqrt(d**2 - (c1.r - c2.r)**2)

def ccw_angle(from_angle, to_angle):
    """Returns smallest positive CCW angle from 'from_angle' to 'to_angle'"""
    while to_angle < from_angle:
        to_angle += 2 * math.pi
    return to_angle - from_angle

def convex_hull_circles(circles):
    n = len(circles)
    if n == 1:
        return 2 * math.pi * circles[0].r

    # Find leftmost circle (by leftmost point)
    start = min(circles, key=lambda c: (c.x - c.r, c.y))
    #print("start ",start.x, start.y)
    hull = []
    turns = []
    cur = start
    start_angle = math.pi*1.5
    angle = start_angle
    first_angle = None

    while True:
        best = None
        best_angle = None
        best_tangent = None

        for c in circles:
            if c == cur:
                continue
            ta = external_tangent_angle(cur, c)
            #print(cur.x, cur.y, c.x, c.y, tangents[0]*(180/3.1415926), tangents[1]*(180/3.1415926))
            turn = ccw_angle(angle, ta)
            #print(turn)
            if best is None or turn < best_angle:
                best = c
                best_angle = turn
                best_tangent = ta
            #print("best ", best_angle*(180/3.1415926))

        #print("best tangent ",best_tangent, " first was ",first_angle)
        angle = best_tangent
        oldcur = cur
        cur = best
        if first_angle == None:
            first_angle = angle
        else:
            hull.append(oldcur)
            turns.append(best_angle)
            if angle == first_angle:
                if oldcur == start:
                    break


    #print("total turns ",sum(turns))
    # Now compute length of the polygon
    total_length = 0
    for i in range(len(hull)):
        #print(i,hull[i].x, hull[i].y, hull[i].r)
        c1 = hull[i]
        c2 = hull[(i+1) % len(hull)]
        #print(c2.x,c2.y,c2.r)
        d = math.hypot(c2.x - c1.x, c2.y - c1.y)
        if d < abs(c1.r - c2.r):
            continue  # skip invalid (contained) cases
        total_length += c1.r * turns[i]
        #print("arc ",c1.r * turns[i])
        total_length += tangent_length(c1, c2)
        #print("tangent ",tangent_length(c1, c2))
    return total_length

# Input/output
def solve():
    import sys
    input = sys.stdin.read
    data = input().splitlines()
    idx = 0
    T = int(data[idx]); idx += 1
    results = []

    for _ in range(T):
        N = int(data[idx]); idx += 1
        circles = []
        for _ in range(N):
            x, y, r = map(int, data[idx].split()); idx += 1
            circles.append(Circle(x, y, r))
        result = convex_hull_circles(circles)
        print(f"{result:.10f}")

if __name__ == "__main__":
    solve()
