def floor(n)
  n |= n >> 1
  n |= n >> 2
  n |= n >> 4
  n |= n >> 8
  n |= n >> 16
  (n + 1) >> 1
end

def f(a, b)
  mask = -floor(b - a)

  c = b & mask
  if b == c
    a, b = -b, -a 
    c = b & mask
  end
  b == c ? 1 : f(a, c) + f(c, b)
end

n = STDIN.gets.to_i
n.times do
  s, d = STDIN.gets.split.map(&:to_i)
  puts f(s, d)
end