

def solve():
    n = int(input())
    g = [[] for _ in range(n)]

    for _ in range(n - 1):
        a, b = [int(x) for x in input().split()]
        a -= 1
        b -= 1
        g[a].append(b)
        g[b].append(a)

    ds = dict()

    def dfs1(g, a, fr):
        res = 0
        for b in g[a]:
            if b == fr:
                continue
            x = dfs1(g, b, a)
            ds[(a, b)] = x
            res = max(res, x)

        return res + 1

    dfs1(g, 0, -1)
    # print(ds)
    ans = 0

    def dfs2(g, a, fr, d_fr):
        nonlocal ans
        # print(a, fr, d_fr)
        cd = [(d_fr, fr)]
        for b in g[a]:
            if b == fr:
                continue
            cd.append((ds[(a, b)], b))

        cd.sort()
        cd.reverse()
        # print(cd)
        cur = (cd[1][0] + 1) if (len(cd) > 1) else 1
        cur = min(cur, cd[0][0] - 1)
        # print(cur)
        ans += cur + 1

        for b in g[a]:
            if b == fr:
                continue
            x = cd[0][0]
            if cd[0][1] == b:
                x = cd[1][0]
            dfs2(g, b, a, x + 1)

    dfs2(g, 0, -1, 0)
    print(ans + 1)


if __name__ == '__main__':
    t = int(input())
    for _ in range(t):
        solve()
