/**
 *    author:   K.R.Surrya  
 *    created:  24.11.2020 20:40:30
**/
//~ ~~ ~~~~ ~~~~~~~~   
#include<bits/stdc++.h>
using namespace std; 
 
#define int                             long long 
 
using pii                  =            pair<int, int>;
typedef long long                       ll; 
typedef pair<ll,ll>                     pll;
#define mp                              make_pair
#define in                              insert
#define ff                              first
#define ss                              second
#define arr                             array
#define vi                              vector<int>
#define vvi                             vector<vector<int>>
#define vii                             vector<pair<int, int>>
#define pb                              push_back
#define ALL(x)                          begin(x),end(x)
#define inf                             numeric_limits<int>::max()
#define MOD                             1000000007
#define sz(x)                           ((int)x.size())
#define                                 FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define                                 IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define                                 rep(i, n) FOR(i,0,n)
#define                                 irep(i, n) IFOR(i,0,n)
#define                                 f_ip   ios_base::sync_with_stdio(false); cin.tie(NULL);
#define restore(a,x)                    fill(ALL(a),x)
#define unq(a)                          a.resize(distance(a.begin(),unique(begin(a),end(a))));
#define lastBit(n)                      31-__builtin_clz(n) + 1 //32 bit integers, 1-based indexing.
#define firstBit(n)                     __builtin_ctz((n&(-n))) + 1
#define n2b(n, b)                       bitset<b>(n).to_string()
#define precise(x)                      cout << fixed << showpoint << setprecision(x);
struct fast_ios                         { fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;
template<typename T>                    istream &operator>>(istream &is, vector<T> &vec){ for (auto &v : vec) is >> v; return is; }
template<typename T>                    bool chmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; }
template<typename T>                    bool chmin(T &m, const T q) { if (q < m) {m = q; return true;} else return false; }
mt19937                                 rng(chrono::steady_clock::now().time_since_epoch().count());


        /* ~~Directions~~ */
       /* ~~~~~~~~~~~~~~~ */
const int dx[8] = {0, 1, -1, 0, 1, 1, -1, -1};
const int dy[8] = {1, 0, 0, -1, 1, -1, -1, 1};


        /* ~~Useful Functions~~ */
      /*~~~~~~~~~~~~~~~~~~~~~~*/
inline int add(int a, int b,int mod) { return ((a % mod) + (b % mod)) % mod; }
inline int sub(int a, int b,int mod) { return ((a % mod) - (b % mod) + mod) % mod; }
inline int mul(int a, int b,int mod) { return ((a % mod) * (b % mod)) % mod; }

        /* ~~Debugging~~ */         
      /*~~~~~~~~~~~~~~~~~*/       
#define LOCAL
#define sim template <class c
#define ris return *this
#define dor > deb &operator<<
#define eni(x)                                                                  \
    sim > typename enable_if<sizeof dud<c>(0) x 1, deb &>::type operator<<(c i) \
    {
sim > struct rge
{
    c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct deb
{
#ifdef LOCAL
    ~deb()
    {
        cerr << endl;
    }
    eni(!=) cerr << boolalpha << i;
    ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d)
{
    ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d)
{
    *this << "[";
    for (auto it = d.b; it != d.e; ++it)
        *this << ", " + 2 * (it == d.b) << *it;
    ris << "]";
}
#else
    sim dor(const c &)
    {
        ris;
    }
#endif
}
;
#define what(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
// -DLOCAL

#define out2d(x) cout << #x << " is :- \n"; rep(i,sz(x)) rep(j,sz(x[i]))  cout << x[i][j] << (j<sz(x[i])-1?' ':'\n'); cout << '\n'; 

int find(int src,vi &leader)
{
    if(src == leader[src])
    {
        return src;
    }

    return leader[src] = find(leader[src],leader);
}
void unite(int x,int y,vi &leader,vi &rank,vi &size)
{
    int px = find(x,leader);
    int py = find(y,leader);

    int tSize = size[px] + size[py];
    if(px!=py)
    {
        if(rank[py]<rank[px])
        {
            leader[py] = px;
            size[px] = tSize;
        } else if(rank[px]<rank[py])
        {
            leader[px] = py;
            size[py] = px;
        } else
        {
            leader[py] = px;
            size[px] = tSize;
            ++rank[px];
        }
    }
}
    /* ~~driver~~ */
    /*~~~~~~~~~~~~*/
vvi adj;
vi subCnt;
vi lvl;
vi viz;//*

int answer = 0LL;
int cycLen = 0LL;
int n;


int dfs1(int src,int par)
{
    bool isLeaf = true;
    int sum = 0;
    for(auto child:adj[src]) if(child!=par)
    {
        isLeaf = false;
        lvl[child] = lvl[src]+1;
        sum += dfs1(child,src);
    }

    if(isLeaf)
    {
        subCnt[src] = 1;
        answer += (n-1);
        answer += 1-1;

        return 1; //inclusive of the element ;;;
    }

    subCnt[src] = (sum+1LL);
    answer += (subCnt[src]-1);  //bottom
    answer += (n-subCnt[src]); // top

    return (sum+1LL);
}

deque<int> d;
vi cnodes;

void dfs2(int src,int par)
{

    if(cycLen!=0)
    {
        return ;
    }
    viz[src] = 1;
    
    // deb() << what(src);
    d.pb(src);
    for(auto child:adj[src]) if(child!=par)
    {
        if(!viz[child])
        {   
            dfs2(child,src);
        } else
        {
            if(lvl[src]>lvl[child])
            {
                //cycle point ;;
                while(sz(d) >= 1LL) //standard cycle detection code ;;;
                {
                    int curr = *d.rbegin(); d.pop_back();
                    // if(lvl[curr]<lvl[child])            break;
                    cnodes.pb(curr);

                    if(curr == child)
                    {
                        break;
                    }
                }

                // deb() << what(cnodes); 
                cycLen = sz(cnodes);
                return ;
            } else //second time after the dfs call ;;; (0,3) --> (3,0) pairs ;;;
            {

            }
        }
    }
    if(sz(d)>=1LL) //** SIGSEGV
    d.pop_back();
}

void dfs3(int src,set<int> &care,vi &viz2,int &csum) 
{
    viz2[src]=1;
    for(auto child:adj[src])
    {
        if(care.count(child) == 0 && viz2[child] == 0)
        {
            ++csum;
            dfs3(child,care,viz2,csum);
        }
    }
    viz2[src]=0;
}

void solve() {

     d.clear();
     cnodes.clear();

     cin>>n;
     adj = vvi(n);
     subCnt = vi(n);
     lvl = vi(n);
     viz = vi(n,0);
     answer = 0LL;
     cycLen = 0LL;

     vi leader(n);
     iota(ALL(leader),0LL);
     vi size(n,1LL);
     vi rank(n,1LL);

     //0-based indexing ;;;
     pii left;

     int cnt =0 ;
     rep(_,n)
     {
         int u,v;cin>>u>>v;
         --u,--v; //changed to 0-based indexing ;;;

         int pu = find(u,leader);int pv = find(v,leader);
         if(pu!=pv)
         {
             adj[u].pb(v);
             adj[v].pb(u);
             unite(u,v,leader,rank,size);
         } else
         {
             ++cnt;
             left = mp(u,v);
         }
     }

     lvl[0] = 1;
     int nu = dfs1(0,-1);
     assert(answer%2 == 0);
     answer = (answer/2);

     int lu = left.ff;
     int lv = left.ss;
     adj[lu].pb(lv);adj[lv].pb(lu);
     
     dfs2(0,-1);
     assert(cycLen>=3LL);

     int fac = (cycLen-1LL);

     int temp = ( (n-cycLen)*fac + (fac*(fac+1))/2LL ); 
     //deb() << what(cycLen) << what(answer) << what(temp);
     answer = answer + temp;

     //new_fac;
     set<int> co; //alright ;;;
     for(auto ele:cnodes)
     {
         co.in(ele);
     }
     vi comp;
    //  deb() << what(cnodes);
     vi viz2(n,0);

     for(int i=0;i<sz(cnodes);i++)
     {
         int csum =0 ;
         int cc = cnodes[i];
        
         
         dfs3(cc,co,viz2,csum);

         if(csum>=1)
         {
              comp.pb(csum);
         }
     }
     //deb() << what(comp);
     int cfac = 0LL;

    int sum = 0LL;
    for(int i=sz(comp)-1;i>=0;i--) //sum of all pair product ;;;
    {
        cfac += (sum*comp[i]);
        sum += comp[i];
    }
    
     //deb() << what(cfac);
     answer = (answer + cfac);
     cout << answer << '\n';
}

signed main()
{   
     #ifndef ONLINE_JUDGE
     //freopen("input.txt", "r", stdin);
     //freopen("output.txt", "w", stdout);
     #endif

     int T=1;
     cin>>T;
     for(int tt=1;tt<=T;++tt) {
       //cout << "Case #" << tt << ": "; 
         solve();
     }

}

 /* Eat -- Sleep ~~ CODE ~~ Repeat */
