require 'prime'
l = [0]
cnt = 1
Prime.each do |prime|
  while cnt < prime do
    l << l.last
    cnt += 1
  end
  l << l.last+1
  cnt += 1
  break if prime > 1_000_000
end
while n = gets.to_i
  break if n == 0
  x = -n
  n.times do
    p, m = gets.split.map(&:to_i)
    a = p > m-1 ? p - m - 1: 0
    b = p + m < 1_000_000 ? p + m : 1_000_000 - 1
    x += l[b] - l[a]
  end
  p x
end