import java.util.ArrayList; import java.util.Scanner; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * * @author kontris */ public class most { static ArrayList pole; static Scanner sc = new Scanner(System.in); public static void main(String args[]) { int count = sc.nextInt(); for (int q = 0; q < count; q++) { int k = sc.nextInt(); int rozmery[][] = new int[k][3]; int min = Integer.MAX_VALUE; for (int i = 0; i < k; i++) { rozmery[i][0] = sc.nextInt(); rozmery[i][2] = sc.nextInt(); rozmery[i][1] = rozmery[i][2] - rozmery[i][0]; if (rozmery[i][1] < min) { min = rozmery[i][1]; } } pole = new ArrayList<>(k); int pocet = 0; for (int i = 0; i < k; i++) { if (rozmery[i][1] == min) { pole.add(i); pocet++; } for (int j = i + 1; j < k; j++) { if (rozmery[j][2] <= rozmery[i][0]) { if (rozmery[i][1] != min) { pole.add(i); pocet++; } if (!pole.contains(j)) { pole.add(j); pocet++; } } } } for (int i = 0; i < pocet - 1; i++) { for (int j = i + 1; j < pocet; j++) { int x = rozmery[pole.get(j)][2] - rozmery[pole.get(i)][0] + pole.get(j) - pole.get(i); if (x < min) { min = x; } } } System.out.println("K prechodu reky je treba " + min + " pontonu."); } } }