import Data.List
import Control.Monad
import Data.Bits

foldlState :: (c -> a -> b -> (c, a)) -> c -> a -> [b] -> a
foldlState f s a l = snd $ foldl (\(s,a) -> f s a) (s, a) l

check :: [[Int]] -> Int -> Bool
check q i = foldl step True q
    where step a [x,y] = a && (i .&. (2^(x-1)) == y * (2^(x-1)))

checkAll :: [[[Int]]] -> Int -> Bool
checkAll qs i = foldlState step 0 True qs
    where step k a q = (k + 1, a && (i .&. 2^k == 0 || check q i))

bitCount :: Int -> Int
bitCount 0 = 0
bitCount 1 = 1
bitCount i = (if mod i 2 == 1 then 1 else 0) + bitCount (div i 2)

count :: Int -> [[[Int]]] -> Int
count n qs = foldl max 0 $ map bitCount $ filter (checkAll qs) [0..2^n-1]

main = do
  n <- readLn :: IO Int
  qs <- replicateM n readPerson :: IO [[[Int]]]
  putStrLn $ show $ count n qs
    where readPerson = do
            a <- readLn :: IO Int
            replicateM a (map read . words <$> getLine)
