main = do
  s <- getContents
  putStr . concat . map eachDataSet . dataSets . lines $ s

eachDataSet = showResult 
                . map fst . filter ((>= 1000000) . snd)
                . accumulate
                . map (map read . words)
showResult [] = "NA\n"
showResult is = unlines . map show $ is

-- accumulate [[101,10,3],[102,20,1],[101,30,2]] == [(101,90),(102,20)]
accumulate :: [[Int]] -> [(Int,Int)]
accumulate = foldl accum [] . map amount
  where
    amount [i,p,q] = (i, p * q)
    accum [] t = [t]
    accum (t:ts) (i,a) | fst t == i = (i, snd t + a) : ts
                       | otherwise  = t : accum ts (i,a)

-- dataSets ["2","aa","b","1","cc","0"] == [["aa","b"],["cc"]]
dataSets :: [String] -> [[String]]
dataSets (x:xs)
  | n == 0    = []
  | otherwise = take n xs : dataSets (drop n xs)
  where n = read x