N, Q = map(int, input().split()) query = [list(map(int, input().split())) for _ in range(Q)] ans = [0]*Q for i in range(Q): s, t = query[i] if s == 0: ans[i] = str(bin(t)).count('1') continue ct = 0 ind = t.bit_length()-1 while (s >> ind) & 1 and (t >> ind) & 1: s -= 1 << ind t -= 1 << ind ind -= 1 if s == 0: ans[i] = str(bin(t)).count('1') continue for j in range(ind): if (s >> j) & 1: ct += 1 s += 1 << j for j in range(ind-1, -1, -1): if (t >> j) & 1: ct += 1 ans[i] = ct print(*ans, sep='\n')