{-# LANGUAGE BangPatterns #-}
import Control.Arrow
import Data.List
import qualified Data.ByteString.Char8 as B
import Control.Monad
import Control.Monad.Primitive
import qualified Data.Vector as V 
import qualified Data.Vector.Mutable as VM
import Control.Monad.ST
import Data.Foldable

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

solve :: Int -> Int -> [Int] -> [[Int]] -> Int
solve n m ps xys = runST solveST
    where
        solveST :: ST s Int
        solveST = initTree >>= count
        initTree :: ST s (UFTree s)
        initTree = makeTreeM n >>= (\t -> t <$ mapM_ (\[x, y] -> unionNodesM (x - 1) (y - 1) t) xys)
        count uft = foldlM ff 0 $ zip [0..] ps
            where
                ff acc (i, p) = check acc <$> isSameM i (p - 1) uft 
                check acc b
                    | b = acc + 1
                    | otherwise = acc
            

data UFTree s = UFTree !(VM.MVector s (Int, Int))

makeTreeM :: (PrimMonad m) => Int -> m (UFTree (PrimState m))
makeTreeM n = UFTree <$> V.unsafeThaw vec
    where
        vec = V.fromListN n $ zip (repeat 0) [0..]

findRootM :: (PrimMonad m) => Int -> UFTree (PrimState m) -> m Int
findRootM v t@(UFTree vs) = VM.unsafeRead vs v >>= checkRoot
    where
        checkRoot (_, root)
            | v == root = return v
            | otherwise = findRootM root t
            
unionNodesM :: (PrimMonad m) => Int -> Int -> UFTree (PrimState m) -> m ()
unionNodesM a b t@(UFTree vs) = mapM (flip findRootM t) [a, b] >>= checkRoot
    where
        checkRoot [c, d]
            | c == d = return ()
            | otherwise = mapM (VM.unsafeRead vs) [c, d] >>= checkRank c d 
        checkRank c d [ra, rb]
            | ra < rb = VM.unsafeModify vs (second (const d)) c
            | ra > rb = VM.unsafeModify vs (second (const c)) d
            | otherwise = VM.unsafeModify vs (\(x, _) -> (x + 1, c)) d
            
isSameM :: (PrimMonad m) => Int -> Int -> UFTree (PrimState m) -> m Bool
isSameM a b t = (==) <$> findRootM a t <*> findRootM b t
        

breads :: IO [Int]
breads = unfoldr (\s -> second (maybe B.empty snd . B.uncons) <$> B.readInt s) <$> B.getLine