description: Monocarp is the coach of the Berland State University programming teams. He decided to compose a problemset for a training session for his teams.
  
  Monocarp has n problems that none of his students have seen yet. The i-th problem has a topic a_i (an integer from 1 to n) and a difficulty b_i (an integer from 1 to n). All problems are different, that is, there are no two tasks that have the same topic and difficulty at the same time.
  
  Monocarp decided to select exactly 3 problems from n problems for the problemset. The problems should satisfy at least one of two conditions (possibly, both):
  
    * the topics of all three selected problems are different; 
    * the difficulties of all three selected problems are different. 
  
  
  
  Your task is to determine the number of ways to select three problems for the problemset.
  
  Input
  
  The first line contains a single integer t (1 ≤ t ≤ 50000) — the number of testcases.
  
  The first line of each testcase contains an integer n (3 ≤ n ≤ 2 ⋅ 10^5) — the number of problems that Monocarp have.
  
  In the i-th of the following n lines, there are two integers a_i and b_i (1 ≤ a_i, b_i ≤ n) — the topic and the difficulty of the i-th problem.
  
  It is guaranteed that there are no two problems that have the same topic and difficulty at the same time.
  
  The sum of n over all testcases doesn't exceed 2 ⋅ 10^5.
  
  Output
  
  Print the number of ways to select three training problems that meet either of the requirements described in the statement.
  
  Example
  
  Input
  
  
  2
  4
  2 4
  3 4
  2 1
  1 3
  5
  1 5
  2 4
  3 3
  4 2
  5 1
  
  
  Output
  
  
  3
  10
  
  Note
  
  In the first example, you can take the following sets of three problems:
  
    * problems 1, 2, 4; 
    * problems 1, 3, 4; 
    * problems 2, 3, 4. 
  
  
  
  Thus, the number of ways is equal to three.
solution: import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.util.ArrayList;
  import java.util.HashMap;
  import java.util.Scanner;
  
  public class D {
      public static long count(ArrayList<ArrayList<Integer>> a) {
          HashMap<ArrayList<Integer>, Long> counts=new HashMap<>();
          for (ArrayList<Integer> x : a) {
              counts.put(x, counts.getOrDefault(x,0L)+1);
          }
          long sum = 0;
          long pairwise = 0;  // sum_i sum_j (i=/=j) s[i]*s[j]
          for (ArrayList<Integer> x : counts.keySet()) {
              sum += counts.get(x);
          }
          for (ArrayList<Integer> x : counts.keySet()) {
              pairwise += counts.get(x) * (sum-counts.get(x));
          }
          pairwise /= 2;
          long ans=0;
          for (ArrayList<Integer> x : counts.keySet()) {
              ans += (pairwise - (counts.get(x) * (sum-counts.get(x))))*counts.get(x);
              //System.out.println(x+" "+counts.get(x)+" "+(pairwise - (counts.get(x) * (sum-counts.get(x)))));
          }
          //System.out.println(counts);
          //System.out.println(sum+" "+pairwise+" "+(ans/3));
          return ans/3;
      }
      public static long count1d(ArrayList<Integer> a) {
          HashMap<Integer, Long> counts=new HashMap<>();
          for (int x : a) {
              counts.put(x, counts.getOrDefault(x,0L)+1);
          }
          long sum = a.size();
          long pairwise = 0;  // sum_i sum_j (i=/=j) s[i]*s[j]
          for (int x : counts.keySet()) {
              pairwise += counts.get(x) * (sum-counts.get(x));
          }
          pairwise /= 2;
          long ans=0;
          for (int x : counts.keySet()) {
              ans += (pairwise - (counts.get(x) * (sum-counts.get(x))))*counts.get(x);
              //System.out.println(x+" "+counts.get(x)+" "+(pairwise - (counts.get(x) * (sum-counts.get(x)))));
          }
          //System.out.println(counts);
          //System.out.println(sum+" "+pairwise+" "+(ans/3));
          return ans/3;
      }
      public static long count2d(ArrayList<ArrayList<Integer>> a) {
          HashMap<ArrayList<Integer>, Long> counts=new HashMap<>();
          for (ArrayList<Integer> x : a) {
              counts.put(x, counts.getOrDefault(x,0L)+1);
          }
          HashMap<Integer, Long> d1hash=new HashMap<>();
          HashMap<Integer, Long> d2hash=new HashMap<>();
          for (ArrayList<Integer> x : a) {
              d1hash.put(x.get(0), d1hash.getOrDefault(x.get(0),0L)+1);
              d2hash.put(x.get(1), d2hash.getOrDefault(x.get(1),0L)+1);
          }
          long sum = a.size();
  
          // for any x, # of elements with different d1 and d2
          // = sum-d1hash[x]-d2hash[x]+counts[x]
  
          HashMap<ArrayList<Integer>, Long> hashPairs=new HashMap<>();
          HashMap<Integer, Long> d1hashPairs=new HashMap<>();
          HashMap<Integer, Long> d2hashPairs=new HashMap<>();
          long pairwise = 0;  // sum_i sum_j (i=/=j) s[i]*s[j]
          for (ArrayList<Integer> x : counts.keySet()) {
              long pairs =counts.get(x) * (sum-d1hash.get(x.get(0))-d2hash.get(x.get(1))+counts.get(x));
              pairwise += pairs;
              hashPairs.put(x, hashPairs.getOrDefault(x, 0L)+pairs);
              d1hashPairs.put(x.get(0), d1hashPairs.getOrDefault(x.get(0), 0L)+pairs);
              d2hashPairs.put(x.get(1), d2hashPairs.getOrDefault(x.get(1), 0L)+pairs);
          }
          pairwise /= 2;
          long ans=0;
          for (ArrayList<Integer> x : counts.keySet()) {
              // Subtract pairs that involve some y whose d1 and/or d2 are the same as x
              ans += (
                      pairwise
                      - d1hashPairs.get(x.get(0))
                      - d2hashPairs.get(x.get(1))
                      + hashPairs.get(x)
                      + (d1hash.get(x.get(0))-1)*(d2hash.get(x.get(1))-1)   // # pairs (y,z) where y[0]==x[0] and z[1]==x[1]
                      ) * counts.get(x);
  
              //ans += (pairwise - (counts.get(x) * (sum-counts.get(x))))*counts.get(x);
              //System.out.println(x+" "+counts.get(x)+" "
              //        + pairwise + " " + d1hashPairs.get(x.get(0)) + " "
              //        + d2hashPairs.get(x.get(1)) + " " + hashPairs.get(x)
              //);
          }
          //System.out.println(counts);
          //System.out.println(sum+" "+pairwise+" "+(ans/3));
          //System.out.println(hashPairs+" "+d1hashPairs+" "+d2hashPairs);
          return ans/3;
      }
      public static void main(String[] args) throws IOException {
          BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
          int t = Integer.parseInt(in.readLine());
          for (int ti = 0; ti < t; ti++) {
              int n=Integer.parseInt(in.readLine());
              ArrayList<Integer> top=new ArrayList<>(), dif=new ArrayList<>();
              ArrayList<ArrayList<Integer>> td=new ArrayList<>();
              for (int i=0;i<n;i++) {
                  String[] l=in.readLine().split(" ");
                  int a=Integer.parseInt(l[0]),b=Integer.parseInt(l[1]);
                  top.add(a);
                  dif.add(b);
                  ArrayList<Integer> l3=new ArrayList<>();
                  l3.add(a);
                  l3.add(b);
                  td.add(l3);
              }
              //System.out.println(count(top));
              System.out.println(count1d(top)-count2d(td)+count1d(dif));
          }
      }
  }
  