import Control.Monad.State (State, evalState, gets, modify)
import Data.Map (Map, lookup, insert, empty)
import Data.Maybe (maybe)
import Data.List
import qualified Data.ByteString.Char8 as B
import Control.Monad
import Data.Bits
import Data.Array
import Data.Ord

main :: IO ()
main = breads >>= main'
    where
        main' [n, k] = replicateM n breads >>= print . solve n k . sortBy (flip (comparing head))

solve :: Int -> Int -> [[Int]] -> Int
solve n k xs = run calc (0, 0)
    where
        arr = listArray (0, n - 1) xs
        calc f (i, w)
            | i >= n = return 0
            | w .|. a > k = f (i + 1, w)
            | w .&. a == a = (+ b) <$> f (i + 1, w)
            | otherwise = max <$> f (i + 1, w) <*> ((+ b) <$> f (i + 1, w .|. a))
            where
                [a, b] = arr ! i


breads :: IO [Int]
breads = unfoldr uff <$> B.getLine
    where
        uff b = check <$> B.readInt b
        check (a, b)
            | B.null b = (a, b)
            | otherwise = (a, B.tail b)


type Memoizer a b = a -> State (Map a b) b

run :: (Ord a) => (Memoizer a b -> Memoizer a b) -> a -> b
run f = flip evalState empty . memoizeFix f

memoizeFix :: (Ord a) => (Memoizer a b -> Memoizer a b) -> Memoizer a b
memoizeFix f x = gets (Data.Map.lookup x) >>= maybe (f (memoizeFix f) x >>= ((<$) <*> (modify . Data.Map.insert x))) return
