import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int s = Integer.parseInt(br.readLine()); PrintWriter pw = new PrintWriter(System.out); for (int i = 0; i < s; i++) { String[] sa = br.readLine().split(" "); int x = Integer.parseInt(sa[0]); int y = Integer.parseInt(sa[1]); int xy = x + y; List list = divList(xy); int ans = 0; for (int o : list) { long a = o - 1; long bc = xy / o; long y2 = y * a - x; long c2 = a * a - 1; if (y2 > 0 && y2 % c2 == 0 && y2 / c2 < bc) { ans++; } } pw.println(ans); } br.close(); pw.flush(); } static List divList(int n) { List list = new ArrayList<>(); int end = (int) Math.sqrt(n); for (int i = 2; 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; } }