You are given an undirected graph consisting of n vertices and n edges. It is guaranteed that the given graph is connected (i. e. it is possible to reach any vertex from any other vertex) and there are no self-loops and multiple edges in the graph.

Your task is to calculate the number of simple paths of length at least 1 in the given graph. Note that paths that differ only by their direction are considered the same (i. e. you have to calculate the number of undirected paths). For example, paths [1, 2, 3] and [3, 2, 1] are considered the same.

You have to answer t independent test cases.

Recall that a path in the graph is a sequence of vertices v_1, v_2, …, v_k such that each pair of adjacent (consecutive) vertices in this sequence is connected by an edge. The length of the path is the number of edges in it. A simple path is such a path that all vertices in it are distinct.

Input

The first line of the input contains one integer t (1 ≤ t ≤ 2 ⋅ 10^4) — the number of test cases. Then t test cases follow.

The first line of the test case contains one integer n (3 ≤ n ≤ 2 ⋅ 10^5) — the number of vertices (and the number of edges) in the graph.

The next n lines of the test case describe edges: edge i is given as a pair of vertices u_i, v_i (1 ≤ u_i, v_i ≤ n, u_i ≠ v_i), where u_i and v_i are vertices the i-th edge connects. For each pair of vertices (u, v), there is at most one edge between u and v. There are no edges from the vertex to itself. So, there are no self-loops and multiple edges in the graph. The graph is undirected, i. e. all its edges are bidirectional. The graph is connected, i. e. it is possible to reach any vertex from any other vertex by moving along the edges of the graph.

It is guaranteed that the sum of n does not exceed 2 ⋅ 10^5 (∑ n ≤ 2 ⋅ 10^5).

Output

For each test case, print one integer: the number of simple paths of length at least 1 in the given graph. Note that paths that differ only by their direction are considered the same (i. e. you have to calculate the number of undirected paths).

Example

Input


3
3
1 2
2 3
1 3
4
1 2
2 3
3 4
4 2
5
1 2
2 3
1 3
2 5
4 3


Output


6
11
18

Note

Consider the second test case of the example. It looks like that:

<image>

There are 11 different simple paths:

  1. [1, 2]; 
  2. [2, 3]; 
  3. [3, 4]; 
  4. [2, 4]; 
  5. [1, 2, 4]; 
  6. [1, 2, 3]; 
  7. [2, 3, 4]; 
  8. [2, 4, 3]; 
  9. [3, 2, 4]; 
  10. [1, 2, 3, 4]; 
  11. [1, 2, 4, 3]. 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;


public class Main {
	static final int INF=Integer.MAX_VALUE;
	static final long LINF =Long.MAX_VALUE;
	
	static ArrayList<Integer>[] graph;
	static boolean[] iscycle;
	static boolean[] visited;
	static int[] cnt;
	
	public static void main(String[] args) {
		FastScanner fs = new FastScanner();
		Map<Integer,Integer> mp = new TreeMap<>();
		Set<Integer> set = new TreeSet<Integer>();
		Queue<Integer> que= new LinkedList<Integer>();
		PriorityQueue<pii<Integer,Integer>> pq = new PriorityQueue<>();
		
		int Q=fs.nextInt();
		for(int q=0;q<Q;q++) {
			int n=fs.nextInt();
			graph=new ArrayList[n];
			
			for(int i=0;i<n;i++) graph[i]=new ArrayList<Integer>();
			int[] degree = new int[n];
			
			for(int i=0;i<n;i++) {
				int a=fs.nextInt(),b=fs.nextInt();
				a--;
				b--;
				graph[a].add(b);
				graph[b].add(a);
				degree[a]++;
				degree[b]++;
			}
			
			
			visited= new boolean[n];
			Arrays.fill(visited, false);
			iscycle= new boolean[n];
			Arrays.fill(iscycle, true);
			
			for(int i=0;i<n;i++) if(degree[i]==1) {
				 que.add(i);
			}
			
			
			cnt=new int[n];
			for(int i=0;i<n;i++) cnt[i]=1;
			
			while(que.isEmpty()==false) {
				
				int here=que.remove();
				if(degree[here]==0) continue;
				iscycle[here]=false;
				
				for(int nxt:graph[here]) {
					if(degree[nxt]<=1) continue;
					degree[nxt]--;
					if(degree[nxt]==1) que.add(nxt);
					cnt[nxt]+=cnt[here];
				}
			}
			
//			debug(iscycle);
			
			long ans=0;
			
			for(int i=0;i<n;i++) { 
				 if(iscycle[i]==false) continue;
				 long c= cnt[i];
				 ans+=c*(c-1)/2;
			}
			
			for(int i=0;i<n;i++) { 
				 if(iscycle[i]==false) continue;
				 long c= cnt[i];
				 ans+=c*(n-c);
			}
			
			//분리된 트리들의 노드의 갯수의 tot-here and combinatoric calc
			
			
			System.out.println(ans);
		}
		
	
	}
	
	/***********************************************************************/
	
	static int gcd(int a,int b) {
		if(b!=0) return gcd(b,a%b);
		else return a;
	}
	
	static long gcd(long a,long b) {
		if(b!=0) return gcd(b,a%b);
		else return a;
	}
	
	static void debug(boolean[] a) {
		for(int i=0;i<a.length;i++) System.out.print(a[i]+" ");
		System.out.println();
	}

	static void debug(int[] a) {
		for(int i=0;i<a.length;i++) System.out.print(a[i]+" ");
		System.out.println();
	}
	
	static void debug(long[] a) {
		for(int i=0;i<a.length;i++) System.out.print(a[i]+" ");
		System.out.println();
	}
	
	static<F,S> void debug_pii(pii<F,S>[] a) {
		for(int i=0;i<a.length;i++) System.out.print("("+a[i].fs+","+a[i].sc+")");
		System.out.println();
	}
	
	static<T> void debug(List<T> a) {
		for(int i=0;i<a.size();i++) System.out.print(a.get(i)+" ");
		System.out.println();
	}
	
	static<F,S> void debug_pii(List<pii<F,S>> a) {
		for(int i=0;i<a.size();i++) System.out.print("("+a.get(i).fs+","+a.get(i).sc+")");
		System.out.println();
	}

	static void swap(int[] a,int idx1,int idx2) {
		int tmp=a[idx1];
		a[idx1]=a[idx2];
		a[idx2]=tmp;
	}
	
	static void sort(int[] a) {
		List<Integer> l=new ArrayList<>();
		for (int i:a) l.add(i);	
		Collections.sort(l); 
		for (int i=0; i<a.length; i++) a[i]=l.get(i);
	}
	
	static void rsort(int[] a) {
		List<Integer> l=new ArrayList<>();
		for (int i:a) l.add(i);
		Collections.sort(l,Collections.reverseOrder()); 
		for (int i=0; i<a.length; i++) a[i]=l.get(i);
	}
	
	static void sort(List<Integer> a) {
		Collections.sort(a);
	}
	
	static void rsort(List<Integer> a) {
		Collections.sort(a,Collections.reverseOrder());
	}
	
	static void sort_pii(List<pii<Integer,Integer>> a) {
		Collections.sort(a);
	}
	
	static void rsort_pii(List<pii<Integer,Integer>> a) {
		Collections.sort(a,Collections.reverseOrder());
	}
	
	static int lowerbound(int[] a,int x) {
		int s=0,e=a.length;
		while(s<e) {
			int mid = (s+e)/2;
			if(a[mid]>=x) e=mid;
			else s=mid+1;
		}
		return e;
	}
	
	static int lowerbound(List<Integer> a,int x) {
		int s=0,e=a.size();
		while(s<e) {
			int mid=(s+e)/2;
			if(a.get(mid)>=x) e=mid;
			else s=mid+1;
		}
		return e;
	}
	
	static class FastScanner {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer("");
		String next() {
			while (!st.hasMoreTokens())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		}
		
		int nextInt() {
			return Integer.parseInt(next());
		}
		
		int[] readArray(int n) {
			int[] a=new int[n];
			for (int i=0; i<n; i++) a[i]=nextInt();
			return a;
		}
		
		ArrayList<Integer> readArrayList(int n){
			ArrayList<Integer> a =new ArrayList<Integer>();
			for(int i=0;i<n;i++) a.add(nextInt());
			return a;
		}
		
		long nextLong() {
			return Long.parseLong(next());
		}
		
		long[] readArray_long(int n) {
			long[] a=new long[n];
			for(int i=0;i<n;i++) a[i]=nextLong();
			return a;
		}	
	}
	
	static class pii<F,S> implements Comparable<pii<F,S>>{
		final F fs;
		final S sc;
		
		public pii(F fs,S sc){
			this.fs=fs;
			this.sc=sc;
		}
		
		static <F,S> pii<F,S> of(F fs,S sc){
			return new pii<F,S>(fs,sc);
		}
		
		public int compareTo(pii<F,S> o) {
			if((int)this.fs<(int)o.fs) return -1;
			else if((int)this.fs==(int)o.fs) {
				if((int)this.sc<=(int)o.sc) return -1;
				else return 1;
			}
			return 1;
		}

		
	}
	
	static class ppi<F,S,S2>{
		final F fs;
		final S sc;
		final S2 sc2;
		
		public ppi(F fs,S sc,S2 sc2) {
			this.fs=fs;
			this.sc=sc;
			this.sc2=sc2;
		}
		
		static <F,S,S2> ppi<F,S,S2> of(F fs,S sc,S2 sc2){
			return new ppi<F,S,S2>(fs,sc,sc2);
		}
	}
	
	static class pipi<F,S,F2,S2>{
		final F fs;
		final S sc;
		final F fs2;
		final S2 sc2;
		
		public pipi(F fs,S sc,F fs2,S2 sc2) {
			this.fs=fs;
			this.sc=sc;
			this.fs2=fs2;
			this.sc2=sc2;
		}
		
		static <F,S,F2,S2> pipi<F,S,F2,S2> of(F fs,S sc,F fs2,S2 sc2){
			return new pipi<F,S,F2,S2>(fs,sc,fs2,sc2);
		}
	}
	
	static int abs(int a) {
		return Math.abs(a);
	}
	static long abs(long a) {
		return Math.abs(a);
	}
	
	static int min(int a, int b) {
		return Math.min(a, b);
	}
	static long min(long a,long b) {
		return Math.min(a, b);
	}
	
	static int max(int a, int b) {
		return Math.max(a, b);
	}
	
	static long max(long a,long b) {
		return Math.max(a, b);
	}

	static int max(int[] a) {
		int mx=a[0];
		for(int i=1;i<a.length;i++) if(mx<a[i]) mx=a[i];
		return mx;
	}
	
	static int min(int[] a) {
		int mn=a[0];
		for(int i=1;i<a.length;i++) if(mn>a[i]) mn=a[i];
		return mn;
	}
	
	static int max(List<Integer> a) {
		int mx=a.get(0);
		for(int i=1;i<a.size();i++) if(mx>a.get(i)) mx=a.get(i); 
		return mx;
	}
	
	static int min(List<Integer> a) {
		int mn=a.get(0);
		for(int i=1;i<a.size();i++) if(mn<a.get(i)) mn=a.get(i); 
		return mn;
	}
	
}

