n,q = map(int,input().split()) for i in range(q): s,t = map(int,input().split()) if s == 0: print(bin(t).count('1')) continue ans = 0 while (s).bit_length() != (t).bit_length(): s += s & -s ans += 1 if ans != 0: ans += bin(t).count('1') - 1 else: ls = bin(s) lt = bin(t) for i in range(len(ls)): if ls[i] != lt[i]: ind = i break p = 1 << (len(ls) - ind - 1) if s % p != 0: while s // p != t // p: s += s & -s ans += 1 ls = bin(s) lt = bin(t) for i in range(ind,len(ls)): if ls[i] != lt[i]: ans += 1 print(ans)