import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for (int z = 0; z < t; z++) { int a = sc.nextInt(); int b = sc.nextInt(); int d = b - a; int ans = Integer.MAX_VALUE; if (d % a == 0) { ans = 0; } else if (d < a) { ans = -1; } else { int a2 = a % d; List list = divList(d); for (int e : list) { if (a2 <= e) { ans = Math.min(ans, e - a2); } } } System.out.println(ans); } sc.close(); } static List divList(int n) { List list = new ArrayList<>(); int end = (int) Math.sqrt(n); for (int i = 1; i <= end; i++) { if (n % i == 0) { list.add(i); } } int i = end * end == n ? list.size() - 2 : list.size() - 1; for ( ; i >= 0; i--) { list.add(n / list.get(i)); } return list; } }