import Data.List
import qualified Data.ByteString.Char8 as B
import Control.Monad
import Data.Bits

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

solve :: Int -> [[Int]] -> Int
solve k xs = foldl' ff 0 ks
    where
        ks = k : [ shift (shift k (-i - 1)) (i + 1) + shift 1 i - 1 | i <- [0..30], shift 1 i .&. k /= 0]
        ff acc n = max acc $ foldl' ff2 0 xs
            where
                ff2 acc2 [a, b]
                    | m .&. complement a == m = acc2 + b
                    | otherwise = acc2
                    where
                        m = complement n

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)
