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

main :: IO ()
main = readLn >>= main'
    where
        main' n = replicateM n breads >>= main'' n . sortBy sf . edit 
        main'' n xs = replicateM n breads >>= print . solve xs . sortBy sf . edit
        edit = fmap (\[a, b] -> (a, b))
        sf (xa, xb) (ya, yb) = compare xl yl `mappend` compare xh yh
            where
                xl = min xa xb
                xh = max xa xb
                yl = min ya yb
                yh = max ya yb

solve :: [(Int, Int)] -> [(Int, Int)] -> Int
solve xxs@((xa, xb) : xs) yys@((ya, yb) : ys)
    | xa < ya && xb < yb = 1 + solve xs ys
    | otherwise = solve xxs ys
solve _ _ = 0


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)