{-# LANGUAGE
  ScopedTypeVariables, BangPatterns, TupleSections, ExplicitForAll,
  LambdaCase, MultiWayIf, Unsafe, RecordWildCards, FlexibleContexts, CPP,
  NoMonomorphismRestriction, GADTs, ImplicitParams,
  RankNTypes, EmptyDataDecls, EmptyCase, ViewPatterns #-}


import Data.Bits
import Data.List
import Data.Maybe
import Data.Tuple
import Data.Ord
import Data.Int
import Data.Word
import Data.Char
import Data.Function
import Data.STRef
import Data.IORef
import Data.Monoid
import System.IO
import Control.Applicative
import Control.Monad
import Control.Monad.Primitive
import Control.Monad.State.Strict
import Control.Monad.ST
-- import Control.Monad.ST.Safe
import Control.DeepSeq
import qualified Data.ByteString.Char8 as BS
import qualified Data.ByteString.Builder as BSB
import qualified Data.ByteString.Lazy.Char8 as BSL
import Data.IntMap (IntMap)
import qualified Data.IntMap.Strict as IMS
import qualified Data.IntMap.Lazy as IML
import Data.IntSet (IntSet)
import qualified Data.IntSet as IS
import qualified Data.Sequence as Seq
import Data.Sequence (Seq)
import qualified Data.Array.IArray as A
import qualified Data.Array.MArray.Safe as A
import qualified Data.Array.MArray as A
import Data.Array (Array)
import Data.Array.Unboxed (UArray)
import Data.Array.IArray (IArray)
import Data.Array.MArray.Safe (MArray)
import Data.Array.IO.Safe (IOArray, IOUArray)
import Data.Array.ST.Safe (STArray, STUArray, runSTArray, runSTUArray)
import qualified Data.Vector as V
import qualified Data.Vector.Mutable as VM
import qualified Data.Vector.Unboxed as VU
import qualified Data.Vector.Unboxed.Mutable as VUM
import qualified Data.Vector.Primitive as VP
import qualified Data.Vector.Primitive.Mutable as VPM
import qualified Data.Vector.Generic as VG
import qualified Data.Vector.Generic.Mutable as VGM
import GHC.Exts (build)

-- import Control.Monad.ST.Unsafe

main :: IO ()
main = do
  [n,k] <- map readInt . words <$> getLine
  p <- VU.map (subtract 1) . VU.unfoldrN n (runStateT $ rIntS) <$> BS.getLine
  q <- VU.map (subtract 1) . VU.unfoldrN n (runStateT $ rIntS) <$> BS.getLine
  let ?n = n
  BSB.hPutBuilder stdout
    $ VU.foldr (\ !j r !sp -> sp <> BSB.intDec j <> r (BSB.char7 ' '))
       (\ !sp -> BSB.char7 '\n') (VU.map (+1) $ query p q k) mempty


query :: (?n :: Int) => Perm -> Perm -> Int -> Perm
query !p_immut !q_immut !k = VU.create $ do
  p <- VU.unsafeThaw p_immut
  q <- VU.unsafeThaw q_immut
  tmp_reg <- VUM.new (?n * 2)
  let (!tmp0,!tmp1) = VUM.splitAt ?n tmp_reg
      (!kd6, !km6) = (k-1) `divMod` 6
      loop !p !q !next 0  = return (p,q,next)
      loop !p !q !next !k = do
        forM_ [0 .. ?n-1] $ \ !i -> do
          !pi <- VUM.unsafeRead p i
          (VUM.write next pi $!) =<< VUM.unsafeRead q i
        loop q next p (k-1)
  (!pp, !qq, !tmp2) <- loop p q tmp0 km6 -- (pp,qq) = F^km6(p,q)
      -- now tmp0,p,q are unusable
      -- Meaningful values are in pp and qq; tmp1 and tmp2 are free.
  forM_ [0 .. ?n-1] $ \ !i -> do -- tmp1 = qq*(qq*pp)^{-1}
    !qpi <- (VUM.unsafeRead qq $!) =<< VUM.unsafeRead pp i
    (VUM.unsafeWrite tmp1 qpi $!) =<< VUM.unsafeRead qq i
  forM_ [0 .. ?n-1] $ \ !i -> do -- tmp2 = tmp1*pp
    (VUM.unsafeWrite tmp2 i $!)
      =<< (VUM.unsafeRead tmp1 $!) =<< VUM.unsafeRead pp i
      -- Values for later use are in tmp2 and pp
      -- tmp1 and qq have meaningful values, but will not be used later,
      -- and thus are free
  let pow !b !tmp !res !e = do
        forM_ [0 .. ?n-1] $ \ !i -> VUM.unsafeWrite res i i
        when (e > 0) $ powloop b tmp res e
      powloop !b !tmp !res !e = do
        when (odd e) $ forM_ [0 .. ?n-1] $ \ !i ->
          (VUM.unsafeWrite res i $!)
          =<< (VUM.unsafeRead b $!) =<< VUM.unsafeRead res i
        let !e1 = e `shiftR` 1
        when (e1 > 0) $ do
          forM_ [0 .. ?n-1] $ \ !i ->
            (VUM.unsafeWrite tmp i $!)
            =<< (VUM.unsafeRead b $!) =<< VUM.unsafeRead b i
          powloop tmp b res e1
  pow tmp2 qq tmp1 kd6 -- tmp1 = (tmp2)^kd6
    -- Now tmp2 and qq are freed and hold meaningless values
  forM_ [0 .. ?n - 1] $ \ !i -> do -- tmp2 = tmp1*pp*(tmp1)^{-1}
    !tmp1i <- VUM.unsafeRead tmp1 i
    (VUM.unsafeWrite tmp2 tmp1i $!)
      =<< (VUM.unsafeRead tmp1 $!) =<< VUM.unsafeRead pp i
  return tmp2
  
  

-- Aut_Set({0,..,n-1})
-- To be used under the constraint (?n :: Int)
-- INVARIANT : VU.length x == ?n for x :: Perm
-- INVARIANT : VU.all (uncurry (==)) $ VU.indexed $ sort x for x :: Perm
type Perm = VU.Vector Int


rInt :: StateT BSL.ByteString Maybe Int
rInt = StateT $ BSL.readInt . BSL.dropWhile (<'!')
rIntS :: StateT BS.ByteString Maybe Int
rIntS = StateT $ BS.readInt . BS.dropWhile (<'!')

#define D(f,r,d) f::Integral a=>a->d;f=fromIntegral;r::String->d;r=read
#define C(f,r,g,h,d) D(f,r,d);g,h::RealFrac a=>a->d;g=floor;h=ceiling
C(_toInteger_,readInteger,floorInteger,ceilInteger,Integer)
C(toInt,readInt,floorInt,ceilInt,Int)
C(toI8,readI8,floorI8,ceilI8,Int8)
C(toI16,readI16,floorI16,ceilI16,Int16)
C(toI32,readI32,floorI32,ceilI32,Int32)
C(toI64,readI64,floorI64,ceilI64,Int64)
C(toWord,readWord,floorWord,ceilWord,Word)
C(toW8,readW8,floorW8,ceilW8,Word8)
C(toW16,readW16,floorW16,ceilW16,Word16)
C(toW32,readW32,floorW32,ceilW32,Word32)
C(toW64,readW64,floorW64,ceilW64,Word64)
D(toDouble,readDouble,Double)
D(toFloat,readFloat,Float)
#undef D
#undef C

{-# INLINE wrA #-}
wrA = A.writeArray
{-# INLINE rdA #-}
rdA = A.readArray
{-# INLINE mdA #-}
mdA arr f i = do
  ai <- rdA arr i
  let fai = f ai 
  wrA arr i fai
  return (ai,fai)
{-# INLINE mdA' #-}
mdA' arr f i = do
  !ai <- rdA arr i
  let !fai = f ai
  wrA arr i fai
  return (ai,fai)
{-# INLINE swapA #-}
swapA arr i j = do
  ai <- rdA arr i
  wrA arr i =<< rdA arr j
  wrA arr j ai


#define N(f,g,a,m)\
  f :: forall e i s. (C(a,m)A.Ix i) => (i,i) -> e -> m (a i e); f=A.newArray;\
  g :: forall e i s. (C(a,m)A.Ix i) => (i,i) -> m (a i e); g=A.newArray_
#define C(a,m)
N(newIOA,newIOA_,IOArray,IO)
N(newSTA,newSTA_,STArray s,ST s)
#undef C
#define C(a,m) MArray (a) e (m), 
N(newIOUA,newIOUA_,IOUArray,IO)
N(newSTUA,newSTUA_,STUArray s,ST s)
#undef C
#undef N



Measured memory_bytes: 10364