/*_x000D_
  Java solution of problem Eilanden_x000D_
  for NKP2005_x000D_
  author: P.G.Kluit_x000D_
  date  : May 2005_x000D_
_x000D_
 */_x000D_
_x000D_
import java.io.*;_x000D_
import java.util.*;_x000D_
_x000D_
public class ProblemC1 {_x000D_
    private static String infile = "c.in";_x000D_
_x000D_
    public static void main (String [] args){_x000D_
        //Stopwatch watch = new Stopwatch();_x000D_
        try{_x000D_
        //   for (int m = 1; m <=5; m++){_x000D_
          // infile = "pgkTestEilanden" + m + ".txt";_x000D_
           if (args.length > 0) // another inputfile can be given_x000D_
              infile = args[0]; // as command line argument_x000D_
_x000D_
_x000D_
_x000D_
           InputStream is = new FileInputStream(infile) ;_x000D_
           IntReader ir = new IntReader(is);_x000D_
           int no = ir.read();_x000D_
_x000D_
           for (int k = 0; k < no; k++){_x000D_
                //watch.start();_x000D_
                Problem p = new FastProblem();_x000D_
                p.read(ir);_x000D_
                //System.out.println("grootte: "+  p.aantal);_x000D_
                //watch.stop();_x000D_
                //System.out.println("Inlezen duurde: " + watch.getDuur()/1000 + " seconden");_x000D_
                // System.out.println("case: " + (k + 1));_x000D_
                p.solve();_x000D_
                //watch.stop();_x000D_
                //System.out.println("Rekenen duurde: " + watch.getDuur()/1000 + " seconden");_x000D_
                //System.out.println(p.aantal + " "+  p.aantal* (p.aantal -1) / 2);_x000D_
                System.out.println(p.getSolution() + "");_x000D_
           }_x000D_
           //}_x000D_
_x000D_
        }_x000D_
        catch (IOException iox){_x000D_
            System.err.println(iox.toString());_x000D_
        }_x000D_
    }_x000D_
}_x000D_
_x000D_
class Problem{_x000D_
_x000D_
    long answer;_x000D_
    int aantal;_x000D_
    int [][] punten;_x000D_
_x000D_
    public void solve(){ // quadratic algoritm_x000D_
       for (int count = 0; count < aantal; count ++){_x000D_
          int x = punten[count][0];_x000D_
          int y = punten[count][1];_x000D_
          for (int punt = 0; punt < count; punt++)_x000D_
             if (punten[punt][0] >= x && punten [punt][1] >= y)_x000D_
                answer++;_x000D_
             else if(punten[punt][0] <= x && punten [punt][1] <= y)_x000D_
                answer++;_x000D_
       }_x000D_
    }_x000D_
_x000D_
    public long getSolution(){_x000D_
       return answer;_x000D_
    }_x000D_
_x000D_
    public void read(IntReader ir){_x000D_
       aantal = ir.read();_x000D_
       punten = new int [aantal] [2];_x000D_
       for (int count = 0; count < aantal; count ++){_x000D_
          int x = ir.read();_x000D_
          punten[count][0] = x;_x000D_
          int y = ir.read();_x000D_
          punten[count][1] = y;_x000D_
       }_x000D_
    }_x000D_
_x000D_
    public void dump(){_x000D_
       for (int k = 0; k < aantal; k++)_x000D_
          System.out.println(punten[k][0] + " " + punten[k][1]);_x000D_
   }_x000D_
     public void dump2(){}_x000D_
}_x000D_
_x000D_
_x000D_
class FastProblem extends Problem{_x000D_
    private Punt [] points;_x000D_
    int rijen = 0;_x000D_
    public void solve(){ // nlogn algoritme_x000D_
       heapsort();_x000D_
       collect();_x000D_
       mergesort();_x000D_
    }_x000D_
_x000D_
    private void merge(int [] left, int [] right, int [] target){_x000D_
       int leftindex = 0;_x000D_
       int rightindex = 0;_x000D_
       int targetindex = 0;_x000D_
       while (leftindex < left.length && rightindex < right.length){_x000D_
          if (left[leftindex] <= right[rightindex]){_x000D_
             int rs = right.length - rightindex;_x000D_
             answer += rs ;_x000D_
             target[targetindex++] = left[leftindex++];_x000D_
          }_x000D_
          else_x000D_
             target[targetindex++] = right[rightindex++];_x000D_
       }_x000D_
       while (leftindex < left.length)_x000D_
           target[targetindex++] = left[leftindex++];_x000D_
       while (leftindex < left.length && rightindex < right.length)_x000D_
             target[targetindex++] = right[rightindex++];_x000D_
    }_x000D_
_x000D_
    private void mergesort(){_x000D_
       while (rijen > 1){_x000D_
          int naar = 0;_x000D_
          int from = 0;_x000D_
          while (from < rijen){_x000D_
              int other = from + 1;_x000D_
              int [] target;_x000D_
              if (other < rijen){_x000D_
                 target = new int [punten[from].length + punten[other].length];_x000D_
                 merge(punten[from], punten[other], target);_x000D_
                 punten[from] = null;_x000D_
                 punten[other] = null;_x000D_
              }_x000D_
              else_x000D_
                 target = punten[from];_x000D_
              punten[naar++] = target;_x000D_
              from +=2;_x000D_
          }_x000D_
          rijen = naar;_x000D_
       }_x000D_
    }_x000D_
_x000D_
    private void collect(){_x000D_
      punten = new int [aantal][];_x000D_
      int begin = aantal;_x000D_
      int eind ;_x000D_
      while (begin > 0){_x000D_
         eind = begin - 1;_x000D_
         while (eind > 0  && points[eind].y > points[eind + 1].y)_x000D_
            eind--;_x000D_
         eind++;_x000D_
         int lengte = begin - eind +1;_x000D_
         answer += (long) lengte *(lengte -1) / 2;_x000D_
         int [] target = new int [lengte];_x000D_
         int t = 0;_x000D_
         for (int k = begin; k >= eind; k--)_x000D_
            target[t++] = points[k].y;_x000D_
         punten[rijen++] = target;_x000D_
         begin = eind - 1;_x000D_
      }_x000D_
      points = null;_x000D_
    }_x000D_
_x000D_
    private void sift(int k, int eind){_x000D_
       //System.out.println(" sift  " + k + " " + eind);_x000D_
       int l;_x000D_
       if (2 * k + 1 <= eind && points[2 * k + 1].before(points[2 * k]))_x000D_
         l = 2 * k + 1;_x000D_
       else_x000D_
         l = 2 * k;_x000D_
       if (l <= eind && points[l].before(points[k])){_x000D_
          swap(k, l);_x000D_
          sift(l,eind);_x000D_
       }_x000D_
    }_x000D_
_x000D_
    private void swap(int k, int l){_x000D_
        Punt hulp = points[k];_x000D_
        points[k] = points[l];_x000D_
        points[l] = hulp;_x000D_
    }_x000D_
_x000D_
    private void heapsort(){_x000D_
       for (int k = aantal/2 + 1 ; k >= 1; k--)_x000D_
          sift(k,aantal);_x000D_
       //dump();_x000D_
       for (int eind = aantal; eind > 1; eind--){_x000D_
          swap(1,eind);_x000D_
          sift(1,eind - 1);_x000D_
          //dump();_x000D_
       }_x000D_
    }_x000D_
_x000D_
    public void read(IntReader ir){_x000D_
       aantal = ir.read();_x000D_
       points = new Punt [aantal + 1] ;_x000D_
       for (int count = 1; count <= aantal; count ++){_x000D_
          int x = ir.read();_x000D_
          int y = ir.read();_x000D_
          points[count] = new Punt (x,y);_x000D_
       }_x000D_
    }_x000D_
_x000D_
    public void dump(){_x000D_
       System.out.println("---");_x000D_
       for (int k = 1; k <= aantal; k++)_x000D_
          System.out.print(points[k] + " ");_x000D_
       System.out.println();_x000D_
       System.out.println("answer = : " + answer);_x000D_
       System.out.println("----");_x000D_
   }_x000D_
_x000D_
   public void dump2(){_x000D_
      int m = 0;_x000D_
      while (m < aantal && punten[m] != null){_x000D_
        for (int k = 0; k < punten[m].length; k++)_x000D_
          System.out.print(punten[m][k] + " ");_x000D_
        System.out.println();_x000D_
        m ++;_x000D_
      }_x000D_
      System.out.println("answer = : " + answer);_x000D_
   }_x000D_
}_x000D_
_x000D_
class Punt{_x000D_
   int x, y;_x000D_
   public Punt(int x, int y){_x000D_
      this.x = x;_x000D_
      this.y = y;_x000D_
   }_x000D_
_x000D_
   public boolean before(Punt that){_x000D_
      return this.x < that.x ||_x000D_
             (this.x == that.x && this.y < that.y);_x000D_
   }_x000D_
_x000D_
   public String toString(){_x000D_
       return "(" + x + ", " + y + ")";_x000D_
   }_x000D_
}_x000D_
_x000D_
class IntReader extends StreamTokenizer{_x000D_
 	public IntReader(InputStream is){_x000D_
	 	super(new BufferedReader(new InputStreamReader(is)));_x000D_
	}_x000D_
_x000D_
	public int read(){_x000D_
		try{_x000D_
			int tokentype = nextToken();_x000D_
		}_x000D_
		catch(IOException iox){_x000D_
			System.out.println(iox);_x000D_
		}_x000D_
		return (int) nval;_x000D_
	}_x000D_
}_x000D_

//System.out.println("Inlezen duurde: " + watch.getDuur()/1000 + " seconden");_x000D_
/*_x000D_
  Java solution of problem Eilanden_x000D_
  for NKP2005_x000D_
  author: P.G.Kluit_x000D_
  date  : May 2005_x000D_
_x000D_
 */_x000D_
_x000D_
import java.io.*;_x000D_
import java.util.*;_x000D_
_x000D_
public class ProblemC1 {_x000D_
    private static String infile = "c.in";_x000D_
_x000D_
    public static void main (String [] args){_x000D_
        //Stopwatch watch = new Stopwatch();_x000D_
        try{_x000D_
        //   for (int m = 1; m <=5; m++){_x000D_
          // infile = "pgkTestEilanden" + m + ".txt";_x000D_
           if (args.length > 0) // another inputfile can be given_x000D_
              infile = args[0]; // as command line argument_x000D_
_x000D_
_x000D_
_x000D_
           InputStream is = new FileInputStream(infile) ;_x000D_
           IntReader ir = new IntReader(is);_x000D_
           int no = ir.read();_x000D_
_x000D_
           for (int k = 0; k < no; k++){_x000D_
                //watch.start();_x000D_
                Problem p = new FastProblem();_x000D_
                p.read(ir);_x000D_
                //System.out.println("grootte: "+  p.aantal);_x000D_
                //watch.stop();_x000D_
                //System.out.println("Inlezen duurde:<SUF>
                // System.out.println("case: " + (k + 1));_x000D_
                p.solve();_x000D_
                //watch.stop();_x000D_
                //System.out.println("Rekenen duurde: " + watch.getDuur()/1000 + " seconden");_x000D_
                //System.out.println(p.aantal + " "+  p.aantal* (p.aantal -1) / 2);_x000D_
                System.out.println(p.getSolution() + "");_x000D_
           }_x000D_
           //}_x000D_
_x000D_
        }_x000D_
        catch (IOException iox){_x000D_
            System.err.println(iox.toString());_x000D_
        }_x000D_
    }_x000D_
}_x000D_
_x000D_
class Problem{_x000D_
_x000D_
    long answer;_x000D_
    int aantal;_x000D_
    int [][] punten;_x000D_
_x000D_
    public void solve(){ // quadratic algoritm_x000D_
       for (int count = 0; count < aantal; count ++){_x000D_
          int x = punten[count][0];_x000D_
          int y = punten[count][1];_x000D_
          for (int punt = 0; punt < count; punt++)_x000D_
             if (punten[punt][0] >= x && punten [punt][1] >= y)_x000D_
                answer++;_x000D_
             else if(punten[punt][0] <= x && punten [punt][1] <= y)_x000D_
                answer++;_x000D_
       }_x000D_
    }_x000D_
_x000D_
    public long getSolution(){_x000D_
       return answer;_x000D_
    }_x000D_
_x000D_
    public void read(IntReader ir){_x000D_
       aantal = ir.read();_x000D_
       punten = new int [aantal] [2];_x000D_
       for (int count = 0; count < aantal; count ++){_x000D_
          int x = ir.read();_x000D_
          punten[count][0] = x;_x000D_
          int y = ir.read();_x000D_
          punten[count][1] = y;_x000D_
       }_x000D_
    }_x000D_
_x000D_
    public void dump(){_x000D_
       for (int k = 0; k < aantal; k++)_x000D_
          System.out.println(punten[k][0] + " " + punten[k][1]);_x000D_
   }_x000D_
     public void dump2(){}_x000D_
}_x000D_
_x000D_
_x000D_
class FastProblem extends Problem{_x000D_
    private Punt [] points;_x000D_
    int rijen = 0;_x000D_
    public void solve(){ // nlogn algoritme_x000D_
       heapsort();_x000D_
       collect();_x000D_
       mergesort();_x000D_
    }_x000D_
_x000D_
    private void merge(int [] left, int [] right, int [] target){_x000D_
       int leftindex = 0;_x000D_
       int rightindex = 0;_x000D_
       int targetindex = 0;_x000D_
       while (leftindex < left.length && rightindex < right.length){_x000D_
          if (left[leftindex] <= right[rightindex]){_x000D_
             int rs = right.length - rightindex;_x000D_
             answer += rs ;_x000D_
             target[targetindex++] = left[leftindex++];_x000D_
          }_x000D_
          else_x000D_
             target[targetindex++] = right[rightindex++];_x000D_
       }_x000D_
       while (leftindex < left.length)_x000D_
           target[targetindex++] = left[leftindex++];_x000D_
       while (leftindex < left.length && rightindex < right.length)_x000D_
             target[targetindex++] = right[rightindex++];_x000D_
    }_x000D_
_x000D_
    private void mergesort(){_x000D_
       while (rijen > 1){_x000D_
          int naar = 0;_x000D_
          int from = 0;_x000D_
          while (from < rijen){_x000D_
              int other = from + 1;_x000D_
              int [] target;_x000D_
              if (other < rijen){_x000D_
                 target = new int [punten[from].length + punten[other].length];_x000D_
                 merge(punten[from], punten[other], target);_x000D_
                 punten[from] = null;_x000D_
                 punten[other] = null;_x000D_
              }_x000D_
              else_x000D_
                 target = punten[from];_x000D_
              punten[naar++] = target;_x000D_
              from +=2;_x000D_
          }_x000D_
          rijen = naar;_x000D_
       }_x000D_
    }_x000D_
_x000D_
    private void collect(){_x000D_
      punten = new int [aantal][];_x000D_
      int begin = aantal;_x000D_
      int eind ;_x000D_
      while (begin > 0){_x000D_
         eind = begin - 1;_x000D_
         while (eind > 0  && points[eind].y > points[eind + 1].y)_x000D_
            eind--;_x000D_
         eind++;_x000D_
         int lengte = begin - eind +1;_x000D_
         answer += (long) lengte *(lengte -1) / 2;_x000D_
         int [] target = new int [lengte];_x000D_
         int t = 0;_x000D_
         for (int k = begin; k >= eind; k--)_x000D_
            target[t++] = points[k].y;_x000D_
         punten[rijen++] = target;_x000D_
         begin = eind - 1;_x000D_
      }_x000D_
      points = null;_x000D_
    }_x000D_
_x000D_
    private void sift(int k, int eind){_x000D_
       //System.out.println(" sift  " + k + " " + eind);_x000D_
       int l;_x000D_
       if (2 * k + 1 <= eind && points[2 * k + 1].before(points[2 * k]))_x000D_
         l = 2 * k + 1;_x000D_
       else_x000D_
         l = 2 * k;_x000D_
       if (l <= eind && points[l].before(points[k])){_x000D_
          swap(k, l);_x000D_
          sift(l,eind);_x000D_
       }_x000D_
    }_x000D_
_x000D_
    private void swap(int k, int l){_x000D_
        Punt hulp = points[k];_x000D_
        points[k] = points[l];_x000D_
        points[l] = hulp;_x000D_
    }_x000D_
_x000D_
    private void heapsort(){_x000D_
       for (int k = aantal/2 + 1 ; k >= 1; k--)_x000D_
          sift(k,aantal);_x000D_
       //dump();_x000D_
       for (int eind = aantal; eind > 1; eind--){_x000D_
          swap(1,eind);_x000D_
          sift(1,eind - 1);_x000D_
          //dump();_x000D_
       }_x000D_
    }_x000D_
_x000D_
    public void read(IntReader ir){_x000D_
       aantal = ir.read();_x000D_
       points = new Punt [aantal + 1] ;_x000D_
       for (int count = 1; count <= aantal; count ++){_x000D_
          int x = ir.read();_x000D_
          int y = ir.read();_x000D_
          points[count] = new Punt (x,y);_x000D_
       }_x000D_
    }_x000D_
_x000D_
    public void dump(){_x000D_
       System.out.println("---");_x000D_
       for (int k = 1; k <= aantal; k++)_x000D_
          System.out.print(points[k] + " ");_x000D_
       System.out.println();_x000D_
       System.out.println("answer = : " + answer);_x000D_
       System.out.println("----");_x000D_
   }_x000D_
_x000D_
   public void dump2(){_x000D_
      int m = 0;_x000D_
      while (m < aantal && punten[m] != null){_x000D_
        for (int k = 0; k < punten[m].length; k++)_x000D_
          System.out.print(punten[m][k] + " ");_x000D_
        System.out.println();_x000D_
        m ++;_x000D_
      }_x000D_
      System.out.println("answer = : " + answer);_x000D_
   }_x000D_
}_x000D_
_x000D_
class Punt{_x000D_
   int x, y;_x000D_
   public Punt(int x, int y){_x000D_
      this.x = x;_x000D_
      this.y = y;_x000D_
   }_x000D_
_x000D_
   public boolean before(Punt that){_x000D_
      return this.x < that.x ||_x000D_
             (this.x == that.x && this.y < that.y);_x000D_
   }_x000D_
_x000D_
   public String toString(){_x000D_
       return "(" + x + ", " + y + ")";_x000D_
   }_x000D_
}_x000D_
_x000D_
class IntReader extends StreamTokenizer{_x000D_
 	public IntReader(InputStream is){_x000D_
	 	super(new BufferedReader(new InputStreamReader(is)));_x000D_
	}_x000D_
_x000D_
	public int read(){_x000D_
		try{_x000D_
			int tokentype = nextToken();_x000D_
		}_x000D_
		catch(IOException iox){_x000D_
			System.out.println(iox);_x000D_
		}_x000D_
		return (int) nval;_x000D_
	}_x000D_
}_x000D_
