require 'prime'

ps = Prime.take_while {|q| q <= 1_000_000 }
loop do
  n = gets.to_i
  break if n.zero?
  p n.times.map {
    p, m = gets.split.map(&:to_i)
    a, b = p - m, p + m + 1
    (ps.bsearch_index {|q| q >= b } || ps.size) - (ps.bsearch_index {|q| q >= a } || ps.size) - 1
  }.inject 0, :+
end