import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * * @author kuric10 */ public class Most { private static int pocTC,row,min, akt, temp, pom,rozdiel; private static int pole[][] = new int[10000002][2]; /** * @param args the command line arguments */ public static void main(String[] args)throws IOException{ // TODO code application logic here BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); String s,poleS[]; pocTC = Integer.parseInt(input.readLine()); for (int i = 0; i < pocTC; i++) { row = Integer.parseInt(input.readLine()); min = Integer.MAX_VALUE; for (int j = 0; j < row; j++) { poleS = input.readLine().split(" "); pole[j][0] = Integer.parseInt(poleS[0]); pole[j][1] = Integer.parseInt(poleS[1]); rozdiel = pole[j][1]-pole[j][0]; if(min>rozdiel){ min = rozdiel; } } for (int j = 0; j < row; j++) { //hore for (int k = j-1; k >=Math.max(0, j-min-1); k--) { temp = Math.abs(j-k)+Math.abs(pole[j][0]-pole[k][1]); // System.out.println(Math.abs(pole[j][1]-pole[k][0])); // System.out.println("hore"+pole[j][0]+" "+pole[j][1]+" - "+pole[k][0]+" "+pole[k][1]+" temp "+temp); // System.out.println(""); if(temp < min){ min = temp; // System.out.println("j "+j+" "+pole[j][0]); } } //dole for (int k = j+1; k