object Main {
  def main(args: Array[String]): Unit = {
    val s = new Main()
    s.solve()
    s.out.flush()
  }
}

class Main {
  import java.io._
  import java.util.StringTokenizer

  import scala.collection.mutable
  import scala.util.Sorting
  import math.{abs, max, min}
  import mutable.{ArrayBuffer, ListBuffer}
  import scala.reflect.ClassTag

  val MOD = 1000000007
  val out = new PrintWriter(System.out)

  def solve(): Unit = {
    val N = ni()
    val A = na(N)
    val posCum = cumSum(A.map(max(0, _)))
    val R = mutable.Map[Int, Int]()
    REP_r(N) { i =>
      if (!R.contains(A(i))) R(A(i)) = i
    }

    case class Ans(l: Int, r: Int, v: Long)
    var ans: Ans = null
    REP(N) { l =>
      val r = R(A(l))
      if (r > l) {
        val v = posCum(r) - posCum(l + 1) + A(l) + A(r)
        if (ans == null || v > ans.v) {
          ans = Ans(l, r, v)
        }
      }
    }

    var total = 0L
    var cnt = 0
    val cut = Array.ofDim[Boolean](N)
    REP(N) { i =>
      if (i < ans.l || i > ans.r || i != ans.l && i != ans.r && A(i) < 0) {
        cut(i) = true
        cnt += 1
      } else {
        total += A(i)
      }
    }

    out.println(s"$total $cnt")
    out.println(cut.zipWithIndex.filter(_._1).map(_._2 + 1).mkString(" "))
  }

  def debug(as: Array[Boolean]): Unit = {
    System.err.println(as.map(x => if(x) "1" else "0").mkString)
  }

  def debug(as: Array[Int]): Unit = {
    System.err.println(as.mkString(" "))
  }

  def debug(as: Array[Long]): Unit = {
    System.err.println(as.mkString(" "))
  }

  def debug(s: String): Unit = {
    System.err.println(s)
  }

  class InputReader(val stream: InputStream) {
    private val reader = new BufferedReader(new InputStreamReader(stream), 32768)
    private var tokenizer: StringTokenizer = _

    def next(): String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new StringTokenizer(reader.readLine)
      tokenizer.nextToken
    }

    def nextInt(): Int = next().toInt
    def nextLong(): Long = next().toLong
    def nextChar(): Char = next().charAt(0)
  }
  val sc = new InputReader(System.in)
  def ni(): Int = sc.nextInt()
  def nl(): Long = sc.nextLong()
  def nc(): Char = sc.nextChar()
  def ns(): String = sc.next()
  def ns(n: Int): Array[Char] = ns().toCharArray
  def na(n: Int, offset: Int = 0): Array[Int] = map(n)(_ => ni() + offset)
  def na2(n: Int, offset: Int = 0): (Array[Int], Array[Int]) = {
    val A1, A2 = Array.ofDim[Int](n)
    REP(n) { i =>
      A1(i) = ni() + offset
      A2(i) = ni() + offset
    }
    (A1, A2)
  }
  def nm(n: Int, m: Int): Array[Array[Int]] = {
    val A = Array.ofDim[Int](n, m)
    REP(n) { i =>
      REP(m) { j =>
        A(i)(j) = ni()
      }
    }
    A
  }
  def nal(n: Int): Array[Long] = map(n)(_ => nl())
  def nm_c(n: Int, m: Int): Array[Array[Char]] = map(n) (_ => ns(m))
  def REP(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = offset
    val N = n + offset
    while(i < N) { f(i); i += 1 }
  }
  def REP_r(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = n - 1 + offset
    while(i >= offset) { f(i); i -= 1 }
  }

  def map[@specialized A: ClassTag](n: Int, offset: Int = 0)(f: Int => A): Array[A] = {
    val res = Array.ofDim[A](n)
    REP(n)(i => res(i) = f(i + offset))
    res
  }

  def sumL(as: Array[Int]): Long = {
    var s = 0L
    REP(as.length)(i => s += as(i))
    s
  }

  def cumSum(as: Array[Int]) = {
    val cum = Array.ofDim[Long](as.length + 1)
    REP(as.length) { i =>
      cum(i + 1) = cum(i) + as(i)
    }
    cum
  }
}

import collection.mutable.HashMap
import java.util._
import java.lang._
import java.io._

/**
 * @author kperikov
 */
object A_331 {

  var out: PrintWriter = null
  var br: BufferedReader = null
  var st: StringTokenizer = null

  def next: String = {
    while (st == null || !st.hasMoreTokens) {
      st = new StringTokenizer(br.readLine)
    }
    return st.nextToken
  }

  def nextInt: Int = {
    return Integer.parseInt(next)
  }

  def nextLong: Long = {
    return Long.parseLong(next)
  }

  def solve = {
    val n = nextInt
    val beauty = new Array[Int](n)
    val map = new HashMap[Int, ArrayList[Int]]
    for (i <- 0 to n - 1) {
      beauty(i) = nextInt
      val some = map.get(beauty(i))
      some match {
        case Some(x) => {
          x add i
          map put(beauty(i), x)
        }
        case None => {
          val x = new ArrayList[Int]()
          x add i
          map put(beauty(i), x)
        }
      }
    }
    //println(map.toString)
    val partialSums = new Array[Long](n + 1)
    val partialAllSums = new Array[Long](n + 1)
    partialSums(0) = 0
    partialSums(n) = 0
    partialAllSums(0) = 0
    partialAllSums(n) = 0
    for (i <- 1 to n) {
      if (beauty(i - 1) > 0) {
        partialSums(i) = partialSums(i - 1) + beauty(i - 1)
      } else {
        partialSums(i) = partialSums(i - 1)
      }
      partialAllSums(i) = partialAllSums(i - 1) + beauty(i - 1)
      //print(partialAllSums(i) + " ")
    }
    //println
    var maxAns = Long.MIN_VALUE
    var begin = -1
    var end = -1
    val it = map.keysIterator
    while (it.hasNext) {
      val key = it.next
      val arr = map.get(key).get
      if (arr.size > 1) {
        val min = arr.get(0)
        val max = arr.get(arr.size - 1) + 1
        val res = partialSums(max - 1) - partialSums(min + 1) + beauty(max - 1) + beauty(min)
        if (res > maxAns) {
          maxAns = res
          begin = min
          end = max - 1
        }
      }
    }
    out.print(maxAns)
    var countToKill = 0
    val killed = new ArrayList[Int]
    for (i <- 0 until begin) {
      countToKill = countToKill + 1
      killed.add(i + 1)
    }
    for (i <- begin + 1 until end) {

      if (beauty(i) < 0) {
        countToKill = countToKill + 1
        killed.add(i + 1)
      }
    }

    for (i <- end + 1 until n) {
      countToKill = countToKill + 1
      killed.add(i + 1)
    }
    out.println(" " + countToKill)
    for (i <- 0 to killed.size - 1) {
      out.print(killed.get(i) + " ")
    }

  }

  def main(args: Array[String]): Unit = {
    br = new BufferedReader(new InputStreamReader(System.in))
    out = new PrintWriter(new BufferedOutputStream(System.out))
    solve
    out.close

  }

}

import collection.immutable.HashMap
import java.util._
import java.lang._
import java.io._

/**
 * @author kperikov
 */
object A_331 {

  var out: PrintWriter = null
  var br: BufferedReader = null
  var st: StringTokenizer = null

  def next: String = {
    while (st == null || !st.hasMoreTokens) {
      st = new StringTokenizer(br.readLine)
    }
    return st.nextToken
  }

  def nextInt: Int = {
    return Integer.parseInt(next)
  }

  def nextLong: Long = {
    return Long.parseLong(next)
  }

  def solve = {
    val n = nextInt
    val beauty = new Array[Int](n)
    var map = new HashMap[Int, ArrayList[Int]]
    for (i <- 0 to n - 1) {
      beauty(i) = nextInt
      val some = map.get(beauty(i))
      some match {
        case Some(x) => {
          x add i
          map = map + (beauty(i) -> x)
        }
        case None => {
          val x = new ArrayList[Int]()
          x add i
          map = map + (beauty(i) -> x)
        }
      }
    }
    //println(map.toString)
    val partialSums = new Array[Long](n + 1)
    val partialAllSums = new Array[Long](n + 1)
    partialSums(0) = 0
    partialSums(n) = 0
    partialAllSums(0) = 0
    partialAllSums(n) = 0
    for (i <- 1 to n) {
      if (beauty(i - 1) > 0) {
        partialSums(i) = partialSums(i - 1) + beauty(i - 1)
      } else {
        partialSums(i) = partialSums(i - 1)
      }
      partialAllSums(i) = partialAllSums(i - 1) + beauty(i - 1)
      //print(partialAllSums(i) + " ")
    }
    //println
    var maxAns = Long.MIN_VALUE
    var begin = -1
    var end = -1
    val it = map.keysIterator
    while (it.hasNext) {
      val key = it.next
      val arr = map.get(key).get
      if (arr.size > 1) {
        val min = arr.get(0)
        val max = arr.get(arr.size - 1) + 1
        val res = partialSums(max - 1) - partialSums(min + 1) + beauty(max - 1) + beauty(min)
        if (res > maxAns) {
          maxAns = res
          begin = min
          end = max - 1
        }
      }
    }
    out.print(maxAns)
    var countToKill = 0
    val killed = new ArrayList[Int]
    for (i <- 0 until begin) {
      countToKill = countToKill + 1
      killed.add(i + 1)
    }
    for (i <- begin + 1 until end) {

      if (beauty(i) < 0) {
        countToKill = countToKill + 1
        killed.add(i + 1)
      }
    }

    for (i <- end + 1 until n) {
      countToKill = countToKill + 1
      killed.add(i + 1)
    }
    out.println(" " + countToKill)
    for (i <- 0 to killed.size - 1) {
      out.print(killed.get(i) + " ")
    }

  }

  def main(args: Array[String]): Unit = {
    br = new BufferedReader(new InputStreamReader(System.in))
    out = new PrintWriter(new BufferedOutputStream(System.out))
    solve
    out.close

  }
}

object Main {
  def main(args: Array[String]): Unit = {
    val s = new Main()
    s.solve()
    s.out.flush()
  }
}

class Main {
  import java.io._
  import java.util.StringTokenizer

  import scala.collection.mutable
  import scala.util.Sorting
  import math.{abs, max, min}
  import mutable.{ArrayBuffer, ListBuffer}
  import scala.reflect.ClassTag

  val MOD = 1000000007
  val out = new PrintWriter(System.out)

  def solve(): Unit = {
    val N = ni()
    val A = na(N)
    val posCum = cumSum(A.map(max(0, _)))
    val R = mutable.Map[Int, Int]()
    REP_r(N) { i =>
      if (!R.contains(A(i))) R(A(i)) = i
    }

    case class Ans(l: Int, r: Int, v: Long)
    var ans: Ans = null
    REP(N) { l =>
      val r = R(A(l))
      if (r > l) {
        val v = posCum(r) - posCum(l + 1) + A(l) * 2
        if (ans == null || v > ans.v) {
          ans = Ans(l, r, v)
        }
      }
    }

    var total = 0L
    var cnt = 0
    val cut = Array.ofDim[Boolean](N)
    REP(N) { i =>
      if (i < ans.l || i > ans.r || A(i) < 0) {
        cut(i) = true
        cnt += 1
      } else {
        total += A(i)
      }
    }

    out.println(s"$total $cnt")
    out.println(cut.zipWithIndex.filter(_._1).map(_._2 + 1).mkString(" "))
  }

  def debug(as: Array[Boolean]): Unit = {
    System.err.println(as.map(x => if(x) "1" else "0").mkString)
  }

  def debug(as: Array[Int]): Unit = {
    System.err.println(as.mkString(" "))
  }

  def debug(as: Array[Long]): Unit = {
    System.err.println(as.mkString(" "))
  }

  def debug(s: String): Unit = {
    System.err.println(s)
  }

  class InputReader(val stream: InputStream) {
    private val reader = new BufferedReader(new InputStreamReader(stream), 32768)
    private var tokenizer: StringTokenizer = _

    def next(): String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new StringTokenizer(reader.readLine)
      tokenizer.nextToken
    }

    def nextInt(): Int = next().toInt
    def nextLong(): Long = next().toLong
    def nextChar(): Char = next().charAt(0)
  }
  val sc = new InputReader(System.in)
  def ni(): Int = sc.nextInt()
  def nl(): Long = sc.nextLong()
  def nc(): Char = sc.nextChar()
  def ns(): String = sc.next()
  def ns(n: Int): Array[Char] = ns().toCharArray
  def na(n: Int, offset: Int = 0): Array[Int] = map(n)(_ => ni() + offset)
  def na2(n: Int, offset: Int = 0): (Array[Int], Array[Int]) = {
    val A1, A2 = Array.ofDim[Int](n)
    REP(n) { i =>
      A1(i) = ni() + offset
      A2(i) = ni() + offset
    }
    (A1, A2)
  }
  def nm(n: Int, m: Int): Array[Array[Int]] = {
    val A = Array.ofDim[Int](n, m)
    REP(n) { i =>
      REP(m) { j =>
        A(i)(j) = ni()
      }
    }
    A
  }
  def nal(n: Int): Array[Long] = map(n)(_ => nl())
  def nm_c(n: Int, m: Int): Array[Array[Char]] = map(n) (_ => ns(m))
  def REP(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = offset
    val N = n + offset
    while(i < N) { f(i); i += 1 }
  }
  def REP_r(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = n - 1 + offset
    while(i >= offset) { f(i); i -= 1 }
  }

  def map[@specialized A: ClassTag](n: Int, offset: Int = 0)(f: Int => A): Array[A] = {
    val res = Array.ofDim[A](n)
    REP(n)(i => res(i) = f(i + offset))
    res
  }

  def sumL(as: Array[Int]): Long = {
    var s = 0L
    REP(as.length)(i => s += as(i))
    s
  }

  def cumSum(as: Array[Int]) = {
    val cum = Array.ofDim[Long](as.length + 1)
    REP(as.length) { i =>
      cum(i + 1) = cum(i) + as(i)
    }
    cum
  }
}

import collection.mutable.HashMap
import java.util._
import java.util

/**
 * @author kperikov
 */
object A_331 {

  def readString = Console.readLine

  def readInts = readString.split(" ").map(_.toInt)

  def main(args: Array[String]): Unit = {
    val n = readInt
    val beauty = readInts
    val map = new HashMap[Int, ArrayList[Integer]]
    for (i <- 0 to n - 1) {
      val some = map.get(beauty(i))
      some match {
        case Some(x) => {
          x add i
          map put(beauty(i), x)
        }
        case None => {
          val x = new ArrayList[Integer]()
          x add i
          map put(beauty(i), x)
        }
      }
    }
    //println(map.toString)
    val partialSums = new Array[Int](n + 1)
    val partialAllSums = new Array[Int](n + 1)
    partialSums(0) = 0
    partialSums(n) = 0
    partialAllSums(0) = 0
    partialAllSums(n) = 0
    for (i <- 1 to n) {
      if (beauty(i - 1) > 0) {
        partialSums(i) = partialSums(i - 1) + beauty(i - 1)
      } else {
        partialSums(i) = partialSums(i - 1)
      }
      partialAllSums(i) = partialAllSums(i - 1) + beauty(i - 1)
      //print(partialAllSums(i) + " ")
    }
    //println
    var maxAns = Int.MinValue
    var begin = -1
    var end = -1
    val it = map.keysIterator
    while (it.hasNext) {
      val key = it.next
      val arr = map.get(key).get
      if (arr.size > 1) {
        Collections.sort(arr)
        val min = arr.get(0)
        val max = arr.get(arr.size - 1) + 1
        val res = partialSums(max - 1) - partialSums(min + 1) + beauty(max - 1) + beauty(min)
        if (res > maxAns) {
          maxAns = res
          begin = min
          end = max - 1
        }
      }
    }
    print(maxAns)
    var countToKill = 0
    val killed = new ArrayList[Int]
    for (i <- 0 until begin) {
      countToKill = countToKill + 1
      killed.add(i + 1)
    }
    for (i <- begin + 1 until end) {
      if (beauty(i) < 0) {
        countToKill = countToKill + 1
        killed.add(i + 1)
      }
    }

    for (i <- end + 1 until n) {
      countToKill = countToKill + 1
      killed.add(i + 1)
    }
    println(" " + countToKill)
    for (i <- 0 to killed.size - 1) {
      print(killed.get(i) + " ")
    }

  }

}

import collection.mutable.HashMap
import java.util._
import java.util

/**
 * @author kperikov
 */
object A_331 {

  def readString = Console.readLine

  def readInts = readString.split(" ").map(_.toInt)

  def main(args: Array[String]): Unit = {
    val n = readInt
    val beauty = readInts
    val map = new HashMap[Int, ArrayList[Integer]]
    for (i <- 0 to n - 1) {
      val some = map.get(beauty(i))
      some match {
        case Some(x) => {
          x add i
          map put(beauty(i), x)
        }
        case None => {
          val x = new ArrayList[Integer]()
          x add i
          map put(beauty(i), x)
        }
      }
    }
    //println(map.toString)
    val partialSums = new Array[Int](n + 1)
    val partialAllSums = new Array[Int](n + 1)
    partialSums(0) = 0
    partialSums(n) = 0
    partialAllSums(0) = 0
    partialAllSums(n) = 0
    for (i <- 1 to n) {
      if (beauty(i - 1) > 0) {
        partialSums(i) = partialSums(i - 1) + beauty(i - 1)
      } else {
        partialSums(i) = partialSums(i - 1)
      }
      partialAllSums(i) = partialAllSums(i - 1) + beauty(i - 1)
      //print(partialAllSums(i) + " ")
    }
    //println
    var maxAns = Int.MinValue
    var begin = -1
    var end = -1
    val it = map.keysIterator
    while (it.hasNext) {
      val key = it.next
      val arr = map.get(key).get
      if (arr.size > 1) {
        Collections.sort(arr)
        val min = arr.get(0)
        val max = arr.get(arr.size - 1) + 1
        val res = partialAllSums(max) - partialAllSums(min)
        val res1 = partialSums(max) - partialSums(min)

        //        println(res)
        if (res > maxAns) {
          maxAns = res
          begin = min
          end = max
        }
        if (res > 0) {
          if (res1 > maxAns) {
            maxAns = res1
            begin = min
            end = max
          }
        }
      }
    }
    print(maxAns)
    var countToKill = 0
    val killed = new ArrayList[Int]
    for (i <- 0 until begin) {
      countToKill = countToKill + 1
      killed.add(i + 1)
    }
    if (end - begin > 2) {
      for (i <- begin + 1 until end) {
        if (beauty(i) < 0) {
          countToKill = countToKill + 1
          killed.add(i + 1)
        }
      }
    }

    for (i <- end until n) {
      countToKill = countToKill + 1
      killed.add(i + 1)
    }
    println(" " + countToKill)
    for (i <- 0 to killed.size - 1) {
      print(killed.get(i) + " ")
    }

  }

}

import collection.mutable.HashMap
import java.util._
import java.util

/**
 * @author kperikov
 */
object A_331 {

  def readString = Console.readLine

  def readInts = readString.split(" ").map(_.toInt)

  def main(args: Array[String]): Unit = {
    val n = readInt
    val beauty = readInts
    val map = new HashMap[Int, ArrayList[Integer]]
    for (i <- 0 to n - 1) {
      val some = map.get(beauty(i))
      some match {
        case Some(x) => {
          x add i
          map put(beauty(i), x)
        }
        case None => {
          val x = new ArrayList[Integer]()
          x add i
          map put(beauty(i), x)
        }
      }
    }
    val partialSums = new Array[Int](n + 1)
    partialSums(0) = 0
    partialSums(n) = 0
    for (i <- 1 to n) {
      if (beauty(i - 1) > 0) {
        partialSums(i) = partialSums(i - 1) + beauty(i - 1)
      } else {
        partialSums(i) = partialSums(i - 1)
      }
      //print(partialSums(i) + " ")
    }
    //println
    var maxAns = -1
    var begin = -1
    var end = -1
    val it = map.keysIterator
    while (it.hasNext) {
      val key = it.next
      val arr = map.get(key).get
      if (arr.size > 1) {
        Collections.sort(arr)
        val min = arr.get(0)
        val max = arr.get(arr.size - 1) + 1
        val res = partialSums(max) - partialSums(min)
//        println(res)
        if (res > maxAns) {
          maxAns = res
          begin = min
          end = max
        }
      }
    }
    print(maxAns)
    var countToKill = 0
    val killed = new ArrayList[Int]
    for (i <- 0 to begin - 1) {
      countToKill = countToKill + 1
      killed.add(i + 1)
    }
    for (i <- begin + 1 to end - 1) {
      if (beauty(i) < 0) {
        countToKill = countToKill + 1
        killed.add(i + 1)
      }
    }

    for (i <- end + 1 to n) {
      countToKill = countToKill + 1
      killed.add(i)
    }
    println(" " + countToKill)
    for (i <- 0 to killed.size - 1) {
      print(killed.get(i) + " ")
    }

  }

}


object Main {
  def main(args: Array[String]): Unit = {
    val s = new Main()
    s.solve()
    s.out.flush()
  }
}

class Main {
  import java.io._
  import java.util.StringTokenizer
  import java.util.Arrays

  import scala.collection.mutable
  import scala.util.Sorting
  import math.{abs, max, min}
  import mutable.{ArrayBuffer, ListBuffer}
  import scala.reflect.ClassTag

  val MOD = 1000000007
  val out = new PrintWriter(System.out)

  def solve(): Unit = {
    val N = ni()
    val C = na(N, -1)

    val P = Array.fill[Int](N, 2)(-1)
    REP(N) { i =>
      val c = C(i)
      if (P(c)(0) == -1) P(c)(0) = i
      else P(c)(0) = min(P(c)(0), i)

      if (P(c)(1) == -1) P(c)(1) = i
      else P(c)(1) = max(P(c)(1), i)
    }

    DEBUG {
      debug(P.map(_(0)))
      debug(P.map(_(1)))
    }

    var l1, l2: Int = 1e9.toInt
    var r1, r2: Int = -1
    REP(N) { i =>
      val l = P(i)(0)
      if (l != -1 && l < l2) {
        l2 = l
        if (l2 < l1) {
          val t = l1
          l1 = l2
          l2 = t
        }
      }

      val r = P(i)(1)
      if (r != -1 && r > r2) {
        r2 = r
        if (r2 > r1) {
          val t = r1
          r1 = r2
          r2 = t
        }
      }
    }

    debug(s"l1:$l1 l2:$l2 r1:$r1 r2$r2")

    val ans = if (C(l1) == C(r1)) {
      max(r1 - l2, r2 - l1)
    } else {
      r1 - l1
    }

    out.println(ans)
  }

  private val oj = System.getenv("ATCODER_DEBUG") == null

  def DEBUG(f: => Unit): Unit = {
    if (!oj){ f }
  }

  def debug(as: Array[Boolean]): Unit = DEBUG {
    debug(as.map(x => if(x) "1" else "0").mkString)
  }

  def debug(as: Array[Int]): Unit = DEBUG {
    debug(as.mkString(" "))
  }

  def debug(as: Array[Long]): Unit = DEBUG {
    debug(as.mkString(" "))
  }

  def debug(s: => String): Unit = DEBUG {
    System.err.println(s)
  }

  def debugL(num: => Long): Unit = DEBUG {
    System.err.println(num)
  }

  class InputReader(val stream: InputStream) {
    private val reader = new BufferedReader(new InputStreamReader(stream), 32768)
    private var tokenizer: StringTokenizer = _

    def next(): String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new StringTokenizer(reader.readLine)
      tokenizer.nextToken
    }

    def nextInt(): Int = next().toInt
    def nextLong(): Long = next().toLong
    def nextChar(): Char = next().charAt(0)
  }
  val sc = new InputReader(System.in)
  def ni(): Int = sc.nextInt()
  def nl(): Long = sc.nextLong()
  def nc(): Char = sc.nextChar()
  def ns(): String = sc.next()
  def ns(n: Int): Array[Char] = ns().toCharArray
  def na(n: Int, offset: Int = 0): Array[Int] = map(n)(_ => ni() + offset)
  def na2(n: Int, offset: Int = 0): (Array[Int], Array[Int]) = {
    val A1, A2 = Array.ofDim[Int](n)
    REP(n) { i =>
      A1(i) = ni() + offset
      A2(i) = ni() + offset
    }
    (A1, A2)
  }
  def nm(n: Int, m: Int): Array[Array[Int]] = {
    val A = Array.ofDim[Int](n, m)
    REP(n) { i =>
      REP(m) { j =>
        A(i)(j) = ni()
      }
    }
    A
  }
  def nal(n: Int): Array[Long] = map(n)(_ => nl())
  def nm_c(n: Int, m: Int): Array[Array[Char]] = map(n) (_ => ns(m))
  def REP(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = offset
    val N = n + offset
    while(i < N) { f(i); i += 1 }
  }
  def REP_r(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = n - 1 + offset
    while(i >= offset) { f(i); i -= 1 }
  }
  def TO(from: Int, to: Int)(f: Int => Unit): Unit = {
    REP(to - from + 1, from) { i =>
      f(i)
    }
  }

  def map[@specialized A: ClassTag](n: Int, offset: Int = 0)(f: Int => A): Array[A] = {
    val res = Array.ofDim[A](n)
    REP(n)(i => res(i) = f(i + offset))
    res
  }

  def sumL(as: Array[Int]): Long = {
    var s = 0L
    REP(as.length)(i => s += as(i))
    s
  }

  def cumSum(as: Array[Int]) = {
    val cum = Array.ofDim[Long](as.length + 1)
    REP(as.length) { i =>
      cum(i + 1) = cum(i) + as(i)
    }
    cum
  }
}

import scala.io.StdIn
object Test extends App {
  val n = StdIn.readInt()
  val arr = StdIn.readLine.split(' ')
  val left = for (i <- 1 to n if arr(0) != arr(i-1)) yield i
  val right = for (i <- 1 to n if arr(n-1) != arr(i-1)) yield i
  println(scala.math.max(left(left.length-1)-1, n-right(0)))
}

import scala.io.StdIn.readLine
import scala.math.max

object A1119 {
  def main(args: Array[String]): Unit = {
    readLine()
    val z = readLine().split(" ")
    println(max(z.lastIndexWhere(_ != z.head), z.length - 1 - z.indexWhere(_ != z.last)))
  }
}

import java.io.FileInputStream

object Main  {
    import scala.io.StdIn.{readInt, readLine}

    def main(args: Array[String]): Unit = {
//        System.setIn(new FileInputStream("in.txt"))
        val n = readInt()
        val lst = readLine.split(" ").map(s => s.toInt)
        var answer = 0;
        for (i <- lst.indices) {
            if (lst(0) != lst(i)) {
                answer = Math.max(answer, i-0)
            }
            if (lst(n-1) != lst(i)) {
                answer = Math.max(answer, n-1-i)
            }
        }

        println(answer)
    }
}


import Utils._, annotation.tailrec, collection.mutable, util._, control._, math.Ordering.Implicits._

object _670C extends CodeForcesApp {
  override def apply(io: IO) = {
    val n = io[Int]
    val counter = map[Int] to 0
    repeat(n) {
      val i = io[Int]
      counter(i) += 1
    }
    val m = io[Int]
    val audio, video = io[Vector, Int](m)
    //debug(counter, audio, video)
    val ans = audio.indices maxBy {i => counter(audio(i)) -> counter(video(i))}
    io += (ans + 1)
  }
}
/****************************[Ignore Template Below]****************************************/
trait CodeForcesApp {
  lazy val isOnlineJudge: Boolean = sys.props.get("ONLINE_JUDGE").exists(_.toBoolean)
  def debug(x: Any): Unit = if (!isOnlineJudge) println(x)
  def apply(io: IO): io.type
  def main(args: Array[String]): Unit = this(new IO(System.in, System.out)).close()
}
object Utils {
  import scala.collection.mutable, mutable.{Map => Dict}
  implicit class GenericExtensions[A](val a: A) extends AnyVal {
    def partialMatch[B](f: PartialFunction[A, B]): Option[B] = f lift a
  }
  implicit class TraversableExtensions[A, C[X] <: Traversable[X]](val t: C[A]) extends AnyVal {
    def onlyElement: A = t.head.ensuring(t.size == 1)
    def groupByIdentifier[K](f: A => K): Map[K, A] = t.groupBy(f).mapValues(_.onlyElement)
    def duplicates(implicit b: C of A): C[A] = {
      val elems = mutable.Set.empty[A]
      (t collect {case i if !elems.add(i) => i}).to[C]
    }
    def sumWith[B](f: A => B)(implicit n: Numeric[B]): B = t.foldLeft(n.zero) {case (p, i) => n.plus(p, f(i))}
    def zipWith[B](f: A => B)(implicit b: C of (A, B)): C[(A, B)] = (t map {i => i -> f(i)}).to[C]
    def mapTo[B](f: A => B)(implicit b: C of (A, B)): Map[A, B] = zipWith(f).toUniqueMap
    def whenNonEmpty[B](f: t.type => B): Option[B] = when(t.nonEmpty)(f(t))  // e.g. grades.whenNonEmpty(_.max)
    def collectFirst[B](f: PartialFunction[A, Option[B]]): Option[B] = t.view collect f collectFirst {case Some(x) => x}
    def toMutableMultiSet: Dict[A, Int] = {
      val c = map[A] to 0
      t.foreach(i => c(i) += 1)
      c
    }
    def toMultiSet: Map[A, Int] = toMutableMultiSet.toMap
    def prefixSum(implicit n: Numeric[A]) = t.scanLeft(n.zero) {case (acc, i) => n.plus(acc, i)}
    def suffixSum(implicit n: Numeric[A]) = t.scanRight(n.zero) {case (i, acc) => n.plus(acc, i)}
  }
  implicit class ArrayExtensions[A <: AnyRef](val a: Array[A]) extends AnyVal {
    def sort(from: Int, until: Int)(implicit cmp: Ordering[A]): Unit = java.util.Arrays.sort(a, from, until, cmp) //sort [from, until)
  }
  implicit class ArrayExtensions2[A](val a: Array[A]) extends AnyVal {
    def sort(from: Int, until: Int)(implicit cmp: Ordering[A]): Unit = a.view(from, until).sorted.copyToArray(a, from)
  }
  implicit class SeqExtensions[A, C[X] <: Seq[X]](val s: C[A]) extends AnyVal {
    def isRotationOf(t: Seq[A]): Boolean = s ++ s containsSlice t
  }
  implicit class PairsExtensions[A, B, C[X] <: Traversable[X]](t: C[(A, B)]) {
    def toUniqueMap: Map[A, B] = t.groupByIdentifier(_._1).mapValues(_._2)
    def toMultiMap(implicit b: C of B): Map[A, C[B]] = t.groupBy(_._1).mapValues(_.map(_._2).to[C])
    def swap(implicit b: C of (B, A)): C[(B, A)] = t.map(_.swap).to[C]
  }
  implicit class MapExtensions[K, V, C[X, Y] <: Map[X, Y]](val m: C[K, V]) extends AnyVal {
    def invert: Map[V, Set[K]] = m.toSet[(K, V)].swap.toMultiMap
    def mapKeys[K2](f: K => K2): Map[K2, V] = m map {case (k, v) => f(k) -> v}
    def toMutable: Dict[K, V] = Dict.empty[K, V] ++ m
  }
  implicit class SetExtensions[A, C[A] <: Set[A]](val s: C[A]) extends AnyVal {
    def notContains(x: A): Boolean = !(s contains x)
    def toMutable = mutable.Set.empty[A] ++ s
  }
  implicit class LongExtensions(val x: Long) extends AnyVal {
    def bitCount: Int = java.lang.Long.bitCount(x)
  }
  implicit class NumericExtensions[A](val x: A)(implicit n: Numeric[A]) {
    import Numeric.Implicits._
    def **(i: Int): A = if (i == 0) n.one else {
      val h = x ** (i/2)
      if (i%2 == 0) h * h else h * h * x
    }
    def nonNegative: Option[A] = when(n.gteq(x, n.zero))(x)
    def whenNegative(f: => A): A = nonNegative getOrElse f   //e.g. students indexOf "Greg" whenNegative Int.MaxValue
  }
  implicit class IntegralExtensions[A](val x: A)(implicit n: Integral[A]) {
    import scala.collection.immutable.NumericRange, Integral.Implicits._
    def mod(y: A): A = x - ((x / y) * y) //(x/y)*y + (x mod y) == x
    def -->(y: A): NumericRange.Inclusive[A] = NumericRange.inclusive(n.min(x, y), n.max(x, y), n.one)
  }
  implicit def toPoint[A](p: (A, A))(implicit n: Numeric[A]) = new java.awt.geom.Point2D.Double(n.toDouble(p._1), n.toDouble(p._2))
  def desc[A: Ordering]: Ordering[A] = asc[A].reverse  //e.g. students.sortBy(_.height)(desc)
  def asc[A](implicit order: Ordering[A]): Ordering[A] = order
  def map[K] = new {
    def to[V](default: V): Dict[K, V] = Dict.empty[K, V] withDefaultValue default // also to(Dict.empty[K2, V])
  }
  def memoize[A, B](f: A => B): collection.Map[A, B] = new mutable.HashMap[A, B]() {
    override def apply(key: A) = getOrElseUpdate(key, f(key))
  }
  @inline def when[A](check: Boolean)(f: => A): Option[A] = if (check) Some(f) else None
  def repeat[U](n: Int)(f: => U): Unit = (1 to n).foreach(_ => f)
  type of[C[_], A] = scala.collection.generic.CanBuild[A, C[A]]
  val mod: Int = (1e9 + 7).toInt
}
/***********************************[Scala I/O]*********************************************/
import Utils._, java.io._, java.util.StringTokenizer

class IO(in: BufferedReader, out: BufferedWriter) extends PrintWriter(out, true) with Iterator[String] {
  def this(reader: Reader, writer: Writer) = this(new BufferedReader(reader), new BufferedWriter(writer))
  def this(in: InputStream, out: OutputStream) = this(new InputStreamReader(in), new OutputStreamWriter(out))

  private[this] val tokenizers = Iterator.continually(in.readLine()).takeWhile(_ != null).map(new StringTokenizer(_)).buffered

  @inline private[this] def tokenizer() = {
    while(tokenizers.nonEmpty && !tokenizers.head.hasMoreTokens) tokenizers.next()
    if (tokenizers.nonEmpty) Some(tokenizers.head) else None
  }

  def apply[A](implicit read: IO.Read[A]): A = read.apply(this)
  def apply[C[_], A: IO.Read](n: Int)(implicit b: C of A): C[A] = Iterator.fill(n)(apply).to[C]

  def till(delim: String): String = tokenizer().get.nextToken(delim)
  def tillEndOfLine(): String = till("\n\r")

  override def next() = tokenizer().get.nextToken()
  override def hasNext = tokenizer().nonEmpty

  var separator = " "
  private[this] var isNewLine = true

  def +=[A](x: A)(implicit write: IO.Write[A]): this.type = {
    write(this)(x)
    this
  }
  def ++=[A: IO.Write](xs: Traversable[A]): this.type = (this += xs.size) appendNewLine xs

  def appendNewLine[A: IO.Write](x: A): this.type = {
    println()
    this += x
  }

  override def print(obj: String) = {
    if (isNewLine) isNewLine = false else super.print(separator)
    super.print(obj)
  }
  override def println() = if (!isNewLine) {
    super.println()
    isNewLine = true
  }
  override def close() = {
    flush()
    in.close()
    super.close()
  }
}
object IO {
  class Read[A](val apply: IO => A) {
    def map[B](f: A => B): Read[B] = new Read(apply andThen f)
  }
  object Read {
    implicit def collection[C[_], A: Read](implicit b: C of A): Read[C[A]]         = new Read(r => r[C, A](r[Int]))
    implicit val string                                       : Read[String]       = new Read(_.next())
    implicit val char                                         : Read[Char]         = string.map(_.toTraversable.onlyElement)
    implicit val boolean                                      : Read[Boolean]      = string.map(_.toBoolean)
    implicit val int                                          : Read[Int]          = string.map(_.toInt)
    implicit val long                                         : Read[Long]         = string.map(_.toLong)
    implicit val bigInt                                       : Read[BigInt]       = string.map(BigInt(_))
    implicit val double                                       : Read[Double]       = string.map(_.toDouble)
    implicit val bigDecimal                                   : Read[BigDecimal]   = string.map(BigDecimal(_))
    implicit def tuple2[A: Read, B: Read]                     : Read[(A, B)]       = new Read(r => (r[A], r[B]))
    implicit def tuple3[A: Read, B: Read, C: Read]            : Read[(A, B, C)]    = new Read(r => (r[A], r[B], r[C]))
    implicit def tuple4[A: Read, B: Read, C: Read, D: Read]   : Read[(A, B, C, D)] = new Read(r => (r[A], r[B], r[C], r[D]))
  }
  trait Write[-A] {
    def apply(out: PrintWriter)(x: A): Unit
  }
  trait DefaultWrite {
    implicit def any[A]: Write[A] = Write(_.toString)
  }
  object Write extends DefaultWrite {
    def apply[A](f: A => String) = new Write[A] {
      override def apply(out: PrintWriter)(x: A) = out.print(f(x))
    }
    implicit val boolean: Write[Boolean] = Write(b => if (b) "YES" else "NO")
    implicit def tuple2[A, B]: Write[(A, B)] = Write {case (a, b) => s"$a $b"}
    implicit def iterable[A](implicit write: Write[A]): Write[Traversable[A]] = new Write[Traversable[A]] {
      override def apply(out: PrintWriter)(xs: Traversable[A]) = {
        xs foreach write(out)
        out.println()
      }
    }
  }
}

object Solution extends App {
  val in = io.Source.stdin.getLines()
  val n = in.next().toInt
  val scientists = in.next().split(' ').map(_.toInt)
  val movies = in.next().toInt
  val languages = in.next().split(' ').map(_.toInt)
  val langSet = languages.toSet
  val subtitles = in.next().split(' ').map(_.toInt)
  val scientistsLanguages = scientists.groupBy(i => i).map(i => (i._1, i._2.length))
  val zipped = languages
    .zip(subtitles)
    .map{ case(l, s) => (scientistsLanguages.getOrElse(l, 0), scientistsLanguages.getOrElse(s, 0)) }
    .zipWithIndex
    .maxBy(_._1)._2
  println(zipped + 1)
}

import Utils._, annotation.tailrec, collection.mutable, util._, control._, math.Ordering.Implicits._

object _670C extends CodeForcesApp {
  override def apply(io: IO) = {
    val prefs = io[Vector[Int]].toMultiSet withDefaultValue 0
    val m = io[Int]
    val audio, video = io[Vector, Int](m)
    val ans = audio.indices maxBy {i => prefs(audio(i)) -> prefs(video(i))}
    io += (ans + 1)
  }
}
/****************************[Ignore Template Below]****************************************/
trait CodeForcesApp {
  lazy val isOnlineJudge: Boolean = sys.props.get("ONLINE_JUDGE").exists(_.toBoolean)
  def debug(x: Any): Unit = if (!isOnlineJudge) println(x)
  def apply(io: IO): io.type
  def main(args: Array[String]): Unit = this(new IO(System.in, System.out)).close()
}
object Utils {
  import scala.collection.mutable, mutable.{Map => Dict}
  implicit class GenericExtensions[A](val a: A) extends AnyVal {
    def partialMatch[B](f: PartialFunction[A, B]): Option[B] = f lift a
  }
  implicit class TraversableExtensions[A, C[X] <: Traversable[X]](val t: C[A]) extends AnyVal {
    def onlyElement: A = t.head.ensuring(t.size == 1)
    def groupByIdentifier[K](f: A => K): Map[K, A] = t.groupBy(f).mapValues(_.onlyElement)
    def duplicates(implicit b: C of A): C[A] = {
      val elems = mutable.Set.empty[A]
      (t collect {case i if !elems.add(i) => i}).to[C]
    }
    def sumWith[B](f: A => B)(implicit n: Numeric[B]): B = t.foldLeft(n.zero) {case (p, i) => n.plus(p, f(i))}
    def zipWith[B](f: A => B)(implicit b: C of (A, B)): C[(A, B)] = (t map {i => i -> f(i)}).to[C]
    def mapTo[B](f: A => B)(implicit b: C of (A, B)): Map[A, B] = zipWith(f).toUniqueMap
    def whenNonEmpty[B](f: t.type => B): Option[B] = when(t.nonEmpty)(f(t))  // e.g. grades.whenNonEmpty(_.max)
    def collectFirst[B](f: PartialFunction[A, Option[B]]): Option[B] = t.view collect f collectFirst {case Some(x) => x}
    def toMutableMultiSet: Dict[A, Int] = {
      val c = map[A] to 0
      t.foreach(i => c(i) += 1)
      c
    }
    def toMultiSet: Map[A, Int] = toMutableMultiSet.toMap
    def prefixSum(implicit n: Numeric[A]) = t.scanLeft(n.zero) {case (acc, i) => n.plus(acc, i)}
    def suffixSum(implicit n: Numeric[A]) = t.scanRight(n.zero) {case (i, acc) => n.plus(acc, i)}
  }
  implicit class ArrayExtensions[A <: AnyRef](val a: Array[A]) extends AnyVal {
    def sort(from: Int, until: Int)(implicit cmp: Ordering[A]): Unit = java.util.Arrays.sort(a, from, until, cmp) //sort [from, until)
  }
  implicit class ArrayExtensions2[A](val a: Array[A]) extends AnyVal {
    def sort(from: Int, until: Int)(implicit cmp: Ordering[A]): Unit = a.view(from, until).sorted.copyToArray(a, from)
  }
  implicit class SeqExtensions[A, C[X] <: Seq[X]](val s: C[A]) extends AnyVal {
    def isRotationOf(t: Seq[A]): Boolean = s ++ s containsSlice t
  }
  implicit class PairsExtensions[A, B, C[X] <: Traversable[X]](t: C[(A, B)]) {
    def toUniqueMap: Map[A, B] = t.groupByIdentifier(_._1).mapValues(_._2)
    def toMultiMap(implicit b: C of B): Map[A, C[B]] = t.groupBy(_._1).mapValues(_.map(_._2).to[C])
    def swap(implicit b: C of (B, A)): C[(B, A)] = t.map(_.swap).to[C]
  }
  implicit class MapExtensions[K, V, C[X, Y] <: Map[X, Y]](val m: C[K, V]) extends AnyVal {
    def invert: Map[V, Set[K]] = m.toSet[(K, V)].swap.toMultiMap
    def mapKeys[K2](f: K => K2): Map[K2, V] = m map {case (k, v) => f(k) -> v}
    def toMutable: Dict[K, V] = Dict.empty[K, V] ++ m
  }
  implicit class SetExtensions[A, C[A] <: Set[A]](val s: C[A]) extends AnyVal {
    def notContains(x: A): Boolean = !(s contains x)
    def toMutable = mutable.Set.empty[A] ++ s
  }
  implicit class BooleanExtensions(val x: Boolean) extends AnyVal {
    def to[A](implicit n: Numeric[A]) = if(x) n.one else n.zero
  }
  implicit class LongExtensions(val x: Long) extends AnyVal {
    def bitCount: Int = java.lang.Long.bitCount(x)
  }
  implicit class NumericExtensions[A](val x: A)(implicit n: Numeric[A]) {
    import Numeric.Implicits._
    def **(i: Int): A = if (i == 0) n.one else {
      val h = x ** (i/2)
      if (i%2 == 0) h * h else h * h * x
    }
    def nonNegative: Option[A] = when(n.gteq(x, n.zero))(x)
    def whenNegative(f: => A): A = nonNegative getOrElse f   //e.g. students indexOf "Greg" whenNegative Int.MaxValue
  }
  implicit class IntegralExtensions[A](val x: A)(implicit n: Integral[A]) {
    import scala.collection.immutable.NumericRange, Integral.Implicits._
    def mod(y: A): A = x - ((x / y) * y) //(x/y)*y + (x mod y) == x
    def -->(y: A): NumericRange.Inclusive[A] = NumericRange.inclusive(n.min(x, y), n.max(x, y), n.one)
  }
  implicit def toPoint[A](p: (A, A))(implicit n: Numeric[A]) = new java.awt.geom.Point2D.Double(n.toDouble(p._1), n.toDouble(p._2))
  def desc[A: Ordering]: Ordering[A] = asc[A].reverse  //e.g. students.sortBy(_.height)(desc)
  def asc[A](implicit order: Ordering[A]): Ordering[A] = order
  def map[K] = new {
    def to[V](default: V): Dict[K, V] = Dict.empty[K, V] withDefaultValue default // also to(Dict.empty[K2, V])
  }
  def memoize[A, B](f: A => B): collection.Map[A, B] = new mutable.HashMap[A, B]() {
    override def apply(key: A) = getOrElseUpdate(key, f(key))
  }
  @inline def when[A](check: Boolean)(f: => A): Option[A] = if (check) Some(f) else None
  def repeat[U](n: Int)(f: => U): Unit = (1 to n).foreach(_ => f)
  type of[C[_], A] = scala.collection.generic.CanBuild[A, C[A]]
  val mod: Int = (1e9 + 7).toInt
}
/***********************************[Scala I/O]*********************************************/
import Utils._, java.io._, java.util.StringTokenizer

class IO(in: BufferedReader, out: BufferedWriter) extends PrintWriter(out, true) with Iterator[String] {
  def this(reader: Reader, writer: Writer) = this(new BufferedReader(reader), new BufferedWriter(writer))
  def this(in: InputStream, out: OutputStream) = this(new InputStreamReader(in), new OutputStreamWriter(out))

  private[this] val tokenizers = Iterator.continually(in.readLine()).takeWhile(_ != null).map(new StringTokenizer(_)).buffered

  @inline private[this] def tokenizer() = {
    while(tokenizers.nonEmpty && !tokenizers.head.hasMoreTokens) tokenizers.next()
    if (tokenizers.nonEmpty) Some(tokenizers.head) else None
  }

  def apply[A](implicit read: IO.Read[A]): A = read.apply(this)
  def apply[C[_], A: IO.Read](n: Int)(implicit b: C of A): C[A] = Iterator.fill(n)(apply).to[C]

  def till(delim: String): String = tokenizer().get.nextToken(delim)
  def tillEndOfLine(): String = till("\n\r")

  override def next() = tokenizer().get.nextToken()
  override def hasNext = tokenizer().nonEmpty

  var separator = " "
  private[this] var isNewLine = true

  def +=[A](x: A)(implicit write: IO.Write[A]): this.type = {
    write(this)(x)
    this
  }
  def ++=[A: IO.Write](xs: Traversable[A]): this.type = (this += xs.size) appendNewLine xs

  def appendNewLine[A: IO.Write](x: A): this.type = {
    println()
    this += x
  }

  override def print(obj: String) = {
    if (isNewLine) isNewLine = false else super.print(separator)
    super.print(obj)
  }
  override def println() = if (!isNewLine) {
    super.println()
    isNewLine = true
  }
  override def close() = {
    flush()
    in.close()
    super.close()
  }
}
object IO {
  class Read[A](val apply: IO => A) {
    def map[B](f: A => B): Read[B] = new Read(apply andThen f)
  }
  object Read {
    implicit def collection[C[_], A: Read](implicit b: C of A): Read[C[A]]         = new Read(r => r[C, A](r[Int]))
    implicit val string                                       : Read[String]       = new Read(_.next())
    implicit val char                                         : Read[Char]         = string.map(_.toTraversable.onlyElement)
    implicit val boolean                                      : Read[Boolean]      = string.map(_.toBoolean)
    implicit val int                                          : Read[Int]          = string.map(_.toInt)
    implicit val long                                         : Read[Long]         = string.map(_.toLong)
    implicit val bigInt                                       : Read[BigInt]       = string.map(BigInt(_))
    implicit val double                                       : Read[Double]       = string.map(_.toDouble)
    implicit val bigDecimal                                   : Read[BigDecimal]   = string.map(BigDecimal(_))
    implicit def tuple2[A: Read, B: Read]                     : Read[(A, B)]       = new Read(r => (r[A], r[B]))
    implicit def tuple3[A: Read, B: Read, C: Read]            : Read[(A, B, C)]    = new Read(r => (r[A], r[B], r[C]))
    implicit def tuple4[A: Read, B: Read, C: Read, D: Read]   : Read[(A, B, C, D)] = new Read(r => (r[A], r[B], r[C], r[D]))
  }
  trait Write[-A] {
    def apply(out: PrintWriter)(x: A): Unit
  }
  trait DefaultWrite {
    implicit def any[A]: Write[A] = Write(_.toString)
  }
  object Write extends DefaultWrite {
    def apply[A](f: A => String) = new Write[A] {
      override def apply(out: PrintWriter)(x: A) = out.print(f(x))
    }
    implicit val boolean: Write[Boolean] = Write(b => if (b) "YES" else "NO")
    implicit def tuple2[A, B]: Write[(A, B)] = Write {case (a, b) => s"$a $b"}
    implicit def iterable[A](implicit write: Write[A]): Write[Traversable[A]] = new Write[Traversable[A]] {
      override def apply(out: PrintWriter)(xs: Traversable[A]) = {
        xs foreach write(out)
        out.println()
      }
    }
  }
}

object Solution extends App {
  val in = io.Source.stdin.getLines()
  val n = in.next().toInt
  val scientists = in.next().split(' ').map(_.toInt)
  val movies = in.next().toInt
  val languages = in.next().split(' ').map(_.toInt)
  val langSet = languages.toSet
  val subtitles = in.next().split(' ').map(_.toInt)
  val scientistsLanguages = scientists.groupBy(i => i).map(i => (i._1, i._2.length))
  val zipped = languages
    .zip(subtitles)
    .map{ case(l, s) => (scientistsLanguages.getOrElse(l, 0), scientistsLanguages.getOrElse(l, 0)) }
    .zipWithIndex
    .sortBy(_._1).last._2
  println(zipped + 1)
}

object Solution extends App {
  val in = io.Source.stdin.getLines()
  val n = in.next().toInt
  val scientists = in.next().split(' ').map(_.toInt)
  val movies = in.next().toInt
  val languages = in.next().split(' ').map(_.toInt)
  val langSet = languages.toSet
  val subtitles = in.next().split(' ').map(_.toInt)
  val scientistsLanguages = scientists.groupBy(i => i).map(i => (i._1, i._2.length))
  val zipped = languages
    .zip(subtitles)
    .map{ case(l, s) => (scientistsLanguages.getOrElse(l, 0), scientistsLanguages.getOrElse(l, 0)) }
    .zipWithIndex
    .maxBy(_._1)._2
  println(zipped + 1)
}


import java.util.Scanner
import java.io.PrintWriter
import java.lang.Long.bitCount
import java.util.Arrays.binarySearch
import scala.annotation.tailrec
import scala.annotation.switch
import scala.annotation.unchecked
import scala.collection.mutable
import scala.collection.mutable.ListBuffer
import scala.collection.mutable.ArrayBuffer
import scala.collection.immutable.TreeSet
import scala.util.Random
import scala.util.control.Breaks._

object P425B extends App {
  val sc = new Scanner(System.in)
  val out = new PrintWriter(System.out)

  def solve(): Int = {
    val N, M, K = sc.nextInt
    val A: Array[Array[Int]] = Array.fill(N, M)(sc.nextInt)

    def a2l(a: Array[Int]): Long = a.foldLeft(0L) { (r, i) => (r << 1) + i }

    def solveSmall(): Int = {
      val cols = A.transpose.map(a2l)
      val patterns = List.range(0, 1 << N)
      val mask = (1 << N) - 1
      patterns.map { pat =>
        val shadow = pat ^ mask
        cols.map { x => bitCount(x ^ pat) min bitCount(x ^ shadow) }.sum
      }.min
    }

    def solveLarge(): Int = {
      val a2ll: Array[Int] => List[Long] = { a =>
        val (a0, a1) = a.splitAt(50)
        List(a0, a1).map(a2l)
      }
      val b = A.map(a2ll)
      val List(mask0, mask1) = a2ll(Array.fill(M)(1))
      val patterns = b.take(K + 1)
      patterns.map { pat =>
        val List(pat0, pat1) = pat
        val List(shadow0, shadow1) = List(pat0 ^ mask0, pat1 ^ mask1)
        b.map { row =>
          val List(x0, x1) = row
          val res0 = bitCount(x0 ^ pat0) + bitCount(x1 ^ pat1)
          val res1 = bitCount(x0 ^ shadow0) + bitCount(x1 ^ shadow1)
          res0 min res1
        }.sum
      }.min

    }

    val res = if (N <= K) solveSmall
              else solveLarge
    if (res <= K) res
    else -1
  }

  out.println(solve)
  out.close
}

import java.util.Scanner
import java.io.PrintWriter
import java.lang.Long.bitCount
import java.util.Arrays.binarySearch
import scala.annotation.tailrec
import scala.annotation.switch
import scala.annotation.unchecked
import scala.collection.mutable
import scala.collection.mutable.ListBuffer
import scala.collection.mutable.ArrayBuffer
import scala.collection.immutable.TreeSet
import scala.util.Random
import scala.util.control.Breaks._

object P425B extends App {
  val sc = new Scanner(System.in)
  val out = new PrintWriter(System.out)

  def solve(): Int = {
    val N, M, K = sc.nextInt
    val A: Array[Array[Int]] = Array.fill(N, M)(sc.nextInt)

    def solveSmall(): Int = {
      val a2i: Array[Int] => Int = a => a.foldLeft(0) { (r, x) => (r << 1) + x }
      val cols = A.transpose.map(a2i)
      val patterns = List.range(0, 1 << N)
      val mask = (1 << N) - 1
      patterns.map { pat =>
        val shadow = pat ^ mask
        cols.map { x => bitCount(x ^ pat) min bitCount(x ^ shadow) }.sum
      }.min
    }

    def solveLarge(): Int = {
      val a2ll: Array[Int] => List[Long] = { a =>
        val (a0, a1) = a.splitAt(50)
        List(a0, a1).map { x => x.foldLeft(0L) { (r, i) => (r << 1) + i } }
      }
      val B = A.map(a2ll)
      val List(mask0, mask1) = a2ll(Array.fill(M)(1))
      val patterns = B.take(K + 1)
      patterns.map { pat =>
        val List(pat0, pat1) = pat
        val List(shadow0, shadow1) = List(pat0 ^ mask0, pat1 ^ mask1)
        B.map { row =>
          val List(x0, x1) = row
          val res0 = bitCount(x0 ^ pat0) + bitCount(x1 ^ pat1)
          val res1 = bitCount(x0 ^ shadow0) + bitCount(x1 ^ shadow1)
          res0 min res1
        }.sum
      }.min

    }

    val res = if (N <= K) solveSmall
              else solveLarge
    if (res <= K) res
    else -1
  }

  out.println(solve)
  out.close
}

import java.util.Scanner
import java.io.PrintWriter
import java.lang.Long.bitCount
import java.util.Arrays.binarySearch
import scala.annotation.tailrec
import scala.annotation.switch
import scala.annotation.unchecked
import scala.collection.mutable
import scala.collection.mutable.ListBuffer
import scala.collection.mutable.ArrayBuffer
import scala.collection.immutable.TreeSet
import scala.util.Random
import scala.util.control.Breaks._

object P425B extends App {
  val sc = new Scanner(System.in)
  val out = new PrintWriter(System.out)

  def solve(): Int = {
    val N, M, K = sc.nextInt
    val A: Array[Array[Int]] = Array.fill(N, M)(sc.nextInt)

    @inline
    def a2l(a: Array[Int]): Long = a.foldLeft(0L) { (r, i) => (r << 1) + i }

    def solveSmall(): Int = {
      val cols = A.transpose.map(a2l)
      val patterns = List.range(0, 1 << N)
      val mask = (1 << N) - 1
      patterns.map { pat =>
        val shadow = pat ^ mask
        cols.map { x => bitCount(x ^ pat) min bitCount(x ^ shadow) }.sum
      }.min
    }

    def solveLarge(): Int = {

      @inline
      def a2ll(a: Array[Int]): List[Long] = {
        val (a0, a1) = a.splitAt(50)
        List(a0, a1).map(a2l)
      }

      val List(mask0, mask1) = a2ll(Array.fill(M)(1))
      val b = A.map(a2ll)
      val patterns = b.take(K + 1)
      patterns.map { pat =>
        val List(pat0, pat1) = pat
        val List(shadow0, shadow1) = List(pat0 ^ mask0, pat1 ^ mask1)
        b.map { row =>
          val List(x0, x1) = row
          val res0 = bitCount(x0 ^ pat0) + bitCount(x1 ^ pat1)
          val res1 = bitCount(x0 ^ shadow0) + bitCount(x1 ^ shadow1)
          res0 min res1
        }.sum
      }.min

    }

    val res = if (N <= K) solveSmall
              else solveLarge
    if (res <= K) res
    else -1
  }

  out.println(solve)
  out.close
}

import java.util.Scanner
import java.io.PrintWriter
import java.lang.Long.bitCount
import java.util.Arrays.binarySearch
import scala.annotation.tailrec
import scala.annotation.switch
import scala.annotation.unchecked
import scala.collection.mutable
import scala.collection.mutable.ListBuffer
import scala.collection.mutable.ArrayBuffer
import scala.collection.immutable.TreeSet
import scala.util.Random
import scala.util.control.Breaks._

object P425B extends App {
  val sc = new Scanner(System.in)
  val out = new PrintWriter(System.out)

  def solve(): Int = {
    val N, M, K = sc.nextInt
    val A: Array[Array[Int]] = Array.fill(N, M)(sc.nextInt)

    def a2l(a: Array[Int]): Long = a.foldLeft(0L) { (r, i) => (r << 1) + i }

    def solveSmall(): Int = {
      val cols = A.transpose.map(a2l)
      val patterns = List.range(0, 1 << N)
      val mask = (1 << N) - 1
      patterns.map { pat =>
        val shadow = pat ^ mask
        cols.map { x => bitCount(x ^ pat) min bitCount(x ^ shadow) }.sum
      }.min
    }

    def solveLarge(): Int = {

      def a2ll(a: Array[Int]): List[Long] = {
        val (a0, a1) = a.splitAt(50)
        List(a0, a1).map(a2l)
      }

      val List(mask0, mask1) = a2ll(Array.fill(M)(1))
      val b = A.map(a2ll)
      val patterns = b.take(K + 1)
      patterns.map { pat =>
        val List(pat0, pat1) = pat
        val List(shadow0, shadow1) = List(pat0 ^ mask0, pat1 ^ mask1)
        b.map { row =>
          val List(x0, x1) = row
          val res0 = bitCount(x0 ^ pat0) + bitCount(x1 ^ pat1)
          val res1 = bitCount(x0 ^ shadow0) + bitCount(x1 ^ shadow1)
          res0 min res1
        }.sum
      }.min

    }

    val res = if (N <= K) solveSmall
              else solveLarge
    if (res <= K) res
    else -1
  }

  out.println(solve)
  out.close
}

import java.util.Scanner
import java.io.PrintWriter
import java.lang.Long.bitCount
import java.util.Arrays.binarySearch
import scala.annotation.tailrec
import scala.annotation.switch
import scala.annotation.unchecked
import scala.collection.mutable
import scala.collection.mutable.ListBuffer
import scala.collection.mutable.ArrayBuffer
import scala.collection.immutable.TreeSet
import scala.util.Random
import scala.util.control.Breaks._

object P425B extends App {
  val sc = new Scanner(System.in)
  val out = new PrintWriter(System.out)

  def countDiff(x: Array[Int], y: Array[Int]): Int = {
    val z = x.map(_ ^ 1)
    List(x, z).map { w =>
      (w, y).zipped.map(_ ^ _).sum
    }.min
  }

  def solveSmall(n: Int, m: Int, k: Int, a: Array[Array[Int]]): Int = {
    val colPatterns: List[Array[Int]] = {
      List.range(0, 1 << n).map { i =>
        Array.tabulate(n) { i : Int => (i >> n) & 1 }
      }
    }
    val b = a.transpose
    colPatterns.map { p =>
      b.map(countDiff(p, _)).sum
    }.min
  }

  def solveLarge(n: Int, m: Int, k: Int, a: Array[Array[Int]]): Int = {
    val colPatterns = a.take(k + 1)
    colPatterns.map { p =>
      a.map(countDiff(p, _)).sum
    }.min
  }

  def solve(): Int = {
    val N, M, K = sc.nextInt
    val A: Array[Array[Int]] = Array.fill(N)(Array.fill(M)(sc.nextInt))

    val res = if (N <= K) solveSmall(N, M, K, A)
              else solveLarge(N, M, K, A)
    if (res <= K) res
    else -1
  }

  out.println(solve)
  out.close
}

import java.util.Scanner
import java.io.PrintWriter
import java.lang.Long.bitCount
import java.util.Arrays.binarySearch
import scala.annotation.tailrec
import scala.annotation.switch
import scala.annotation.unchecked
import scala.collection.mutable
import scala.collection.mutable.ListBuffer
import scala.collection.mutable.ArrayBuffer
import scala.collection.immutable.TreeSet
import scala.util.Random
import scala.util.control.Breaks._

object P425B extends App {
  val sc = new Scanner(System.in)
  val out = new PrintWriter(System.out)

  def solve(): Int = {
    val N, M, K = sc.nextInt

    def line2code(m: Int): Long = {

      @tailrec
      def loop(x: Long, i: Int): Long = {
        if (i == m) x
        else loop((x << 1) + sc.nextLong, i + 1)
      }

      loop(0L, 0)
    }

    val A: List[Long] = List.fill(N)(line2code(M))
    val patterns: List[Long] = if (N <= K) List.range[Long](0L, (1L << M))
                               else A.take(K + 1)

    @inline
    def countChange(x: Long): Int = A.map { y: Long =>
        val mask: Long = (1L << M) - 1
        val a = x ^ y
        val b = x ^ mask ^ y
        bitCount(a) min bitCount(b)
      }.sum

    val res: Int = patterns.map(countChange).min
    if (res <= K) res
    else -1
  }

  out.println(solve)
  out.close
}

import java.util.Scanner
import java.io.PrintWriter
import java.lang.Long.bitCount
import java.util.Arrays.binarySearch
import scala.annotation.tailrec
import scala.annotation.switch
import scala.annotation.unchecked
import scala.collection.mutable
import scala.collection.mutable.ListBuffer
import scala.collection.mutable.ArrayBuffer
import scala.collection.immutable.TreeSet
import scala.util.Random
import scala.util.control.Breaks._

object P425B extends App {
  val sc = new Scanner(System.in)
  val out = new PrintWriter(System.out)

  def solve(): Int = {
    val N, M, K = sc.nextInt
    val A: Array[Array[Int]] = Array.fill(N, M)(sc.nextInt)

    def solveSmall(): Int = {
      val a2i: Array[Int] => Int = a => a.foldLeft(0) { (r, x) => (r << 1) + x }
      val cols = A.transpose.map(a2i)
      val patterns = List.range(0, 1 << N)
      val mask = (1 << N) - 1
      patterns.map { pat =>
        val shadow = pat ^ mask
        cols.map { x => bitCount(x ^ pat) min bitCount(x ^ shadow) }.sum
      }.min
    }

    def solveLarge(): Int = {
      val a2ll: Array[Int] => List[Long] = { a =>
        val (a0, a1) = a.splitAt(50)
        List(a0, a1).map { x => x.foldLeft(0L) { (r, i) => (r << 1) + i } }
      }
      val B = A.map(a2ll)
      val List(mask0, mask1) = a2ll(Array.fill(M)(1))
      val patterns = B.take(K + 1)
      patterns.map { pat =>
        val List(pat0, pat1) = pat
        val List(shadow0, shadow1) = List(pat0 ^ mask0, pat1 ^ mask1)
        B.map { row =>
          val List(x0, x1) = row
          val res0 = bitCount(x0 ^ pat0) + bitCount(x1 ^ pat1)
          val res1 = bitCount(x0 ^ shadow0) + bitCount(x1 ^ shadow1)
          res0 min res1
        }.sum
      }.min

    }

    val res = if (N <= K) solveSmall
              else solveLarge
    res
  }

  out.println(solve)
  out.close
}

import java.util.Scanner
import java.io.PrintWriter
import java.lang.Long.bitCount
import java.util.Arrays.binarySearch
import scala.annotation.tailrec
import scala.annotation.switch
import scala.annotation.unchecked
import scala.collection.mutable
import scala.collection.mutable.ListBuffer
import scala.collection.mutable.ArrayBuffer
import scala.collection.immutable.TreeSet
import scala.util.Random
import scala.util.control.Breaks._

object P425B extends App {
  val sc = new Scanner(System.in)
  val out = new PrintWriter(System.out)

  def countDiff(x: Array[Int], y: Array[Int]): Int = {
    val z = x.map(_ ^ 1)
    List(x, z).map { w =>
      (w, y).zipped.map(_ ^ _).sum
    }.min
  }

  def solveSmall(n: Int, m: Int, k: Int, a: Array[Array[Int]]): Int = {
    val colPatterns: List[Array[Int]] = {
      List.range(0, 1 << n).map { i =>
        Array.tabulate(n) { i : Int => (i >> n) & 1 }
      }
    }
    val b = a.transpose
    colPatterns.map { p =>
      b.map(countDiff(p, _)).sum
    }.min
  }

  def solveLarge(n: Int, m: Int, k: Int, a: Array[Array[Int]]): Int = {
    val colPatterns = a.take(k + 1)
    colPatterns.map { p =>
      a.map(countDiff(p, _)).sum
    }.min
  }

  def solve(): Int = {
    val N, M, K = sc.nextInt
    val A: Array[Array[Int]] = Array.fill(N)(Array.fill(M)(sc.nextInt))

    if (N <= K) solveSmall(N, M, K, A)
    else solveLarge(N, M, K, A)
  }

  out.println(solve)
  out.close
}

import java.util.Scanner
import java.io.PrintWriter
import java.lang.Long.bitCount
import java.util.Arrays.binarySearch
import scala.annotation.tailrec
import scala.annotation.switch
import scala.annotation.unchecked
import scala.collection.mutable
import scala.collection.mutable.ListBuffer
import scala.collection.mutable.ArrayBuffer
import scala.collection.immutable.TreeSet
import scala.util.Random
import scala.util.control.Breaks._

object P425B extends App {
  val sc = new Scanner(System.in)
  val out = new PrintWriter(System.out)

  def solve(): Int = {
    val N, M, K = sc.nextInt

    def line2code(m: Int): Long = {

      @tailrec
      def loop(x: Long, i: Int): Long = {
        if (i == m) x
        else loop((x << 1) + sc.nextLong, i + 1)
      }

      loop(0L, 0)
    }

    val A: List[Long] = List.fill(N)(line2code(M))
    val patterns: List[Long] = if (M <= K) List.range[Long](0L, (1L << M))
                               else A.take(K + 1)

    @inline
    def countChange(x: Long): Int = A.map { y: Long =>
        val mask: Long = (1L << M) - 1
        val a = x ^ y
        val b = x ^ (y ^ mask)
        bitCount(a) min bitCount(b)
      }.min

    val res: Int = patterns.map(countChange).min
    if (res <= K) res
    else -1
  }

  out.println(solve)
  out.close
}

import java.util.Scanner
import java.io.PrintWriter
import java.lang.Long.bitCount
import java.util.Arrays.binarySearch
import scala.annotation.tailrec
import scala.annotation.switch
import scala.annotation.unchecked
import scala.collection.mutable
import scala.collection.mutable.ListBuffer
import scala.collection.mutable.ArrayBuffer
import scala.collection.immutable.TreeSet
import scala.util.Random
import scala.util.control.Breaks._

object P425B extends App {
  val sc = new Scanner(System.in)
  val out = new PrintWriter(System.out)

  def solve(): Int = {
    val N, M, K = sc.nextInt

    def line2code(m: Int): Long = {

      @tailrec
      def loop(x: Long, i: Int): Long = {
        if (i == m) x
        else loop((x << 1) + sc.nextLong, i + 1)
      }

      loop(0L, 0)
    }

    val A: List[Long] = List.fill(N)(line2code(M))
    val patterns: List[Long] = if (M <= K) List.range[Long](0L, (1L << M))
                               else A.take(K + 1)

    @inline
    def countChange(x: Long): Int = A.map { y: Long =>
        val mask: Long = (1L << M) - 1
        val a = x ^ y
        val b = x ^ mask ^ y
        bitCount(a) min bitCount(b)
      }.sum

    val res: Int = patterns.map(countChange).min
    if (res <= K) res
    else -1
  }

  out.println(solve)
  out.close
}


object _1234B1 extends CodeForcesApp {
  import Utils._, annotation._, collection.{mutable, Searching}, util._, control._, math.Ordering.Implicits._, Searching._, Math._

  override def apply(io: IO): io.type = {
    val n, k = io.read[Int]
    val msgs = io.read[Seq, Int](n)

    val ans = boundedSet[Int](k)
    for {
      msg <- msgs if !ans.contains(msg)
    } ans.add(msg)

    io.writeLine(ans.size()).writeAll(ans.toArray.reverse)
  }

  def boundedSet[E](limit: Int) = {
    import java.{util => ju}
    ju.Collections.newSetFromMap[E](new ju.LinkedHashMap[E, java.lang.Boolean]() {
      override def removeEldestEntry(eldest: ju.Map.Entry[E, java.lang.Boolean]) = size() > limit
    })
  }
}
/****************************[Ignore Template Below]****************************************/
trait CodeForcesApp {
  lazy val isOnlineJudge: Boolean = sys.props.get("ONLINE_JUDGE").exists(_.toBoolean)
  def debug(x: => Any): Unit = if (!isOnlineJudge) println(x)
  def apply(io: IO): io.type
  def main(args: Array[String]): Unit = apply(new IO(System.in, System.out)).close()
}
/****************************[Scala Collection Utils]****************************************/
object Utils {
  import scala.collection.mutable

  type of[C[_], A] = scala.collection.generic.CanBuild[A, C[A]]

  def when[A](check: Boolean)(f: => A): Option[A] = if (check) Some(f) else None

  def repeat[U](n: Int)(f: => U): Unit = (1 to n).foreach(_ => f)

  implicit class GenericExtensions[A](a: A) {
    def partialMatch[B](f: PartialFunction[A, B]): Option[B] = f lift a
  }

  implicit class TraversableExtensions[A, C[X] <: Traversable[X]](t: C[A]) {
    def zipWith[B](f: A => B)(implicit b: C of (A, B)): C[(A, B)] = (t map {i => i -> f(i)}).to[C]

    def whenNonEmpty[B](f: t.type => B): Option[B] = when(t.nonEmpty)(f(t))  // e.g. grades.whenNonEmpty(_.max)

    def toMultiSet: Map[A, Int] = t.groupBy(identity).mapValues(_.size)
  }

  implicit class PairExtensions[A, B](p: (A, B)) {
    def key = p._1
    def value = p._2
    def toSeq(implicit ev: B =:= A): Seq[A] = Seq(p.key, ev(p.value))
  }

  implicit class PairsExtensions[A, B, C[X] <: Traversable[X]](t: C[(A, B)]) {
    def toMultiMap(implicit b: C of B): Map[A, C[B]] = t.groupBy(_.key).mapValues(_.map(_.value).to[C]).withDefaultValue(b().result())
  }

  implicit class MapExtensions[K, V, C[X, Y] <: Map[X, Y]](m: C[K, V]) {
    def toMutable: mutable.Map[K, V] = mutable.Map.empty[K, V] ++ m
  }

  implicit class SetExtensions[A, C[A] <: Set[A]](s: C[A]) {
    def notContains(x: A): Boolean = !(s contains x)
    def toMutable = mutable.Set.empty[A] ++ s
  }

  implicit class IterableExtensions[A, C[A] <: Seq[A]](s: C[A]) {
    def indicesWhere(f: A => Boolean): Seq[Int] = s.zipWithIndex.collect {case (a, i) if f(a) => i}
    def deleteAtIndex(i: Int, howMany: Int = 1) = s.patch(i, Nil, howMany)
    def insertAtIndex(i: Int, items: Seq[A]) = s.patch(i, items, 0)
    def findWithIndex(f: A => Boolean): Option[(A, Int)] = s.zipWithIndex find {case (a, _) => f(a)}
  }

  implicit class BooleanExtensions(x: Boolean) {
    def to[A](implicit n: Numeric[A]) = if(x) n.one else n.zero
    def toEnglish = if(x) "Yes" else "No"
  }

  def desc[A](implicit order: Ordering[A]): Ordering[A] = order.reverse  //e.g. students.sortBy(_.height)(desc)

  def map[K] = new {
    def to[V](default: => V): mutable.Map[K, V] = using(_ => default)
    def using[V](f: K => V): mutable.Map[K, V] = new mutable.HashMap[K, V]() {
      override def apply(key: K) = getOrElseUpdate(key, f(key))
    }
  }

  def newMultiMap[K, V] = map[K] to mutable.Set.empty[V]

  def memoize[A, B](f: A => B): A => B = map[A] using f
}
/***********************************[Scala I/O]*********************************************/
import java.io._, java.util.StringTokenizer
import scala.collection.generic.CanBuild

class IO(in: BufferedReader, out: BufferedWriter) extends Iterator[String] with AutoCloseable with Flushable {
  def this(reader: Reader, writer: Writer) = this(new BufferedReader(reader), new BufferedWriter(writer))
  def this(in: InputStream, out: OutputStream) = this(new InputStreamReader(in), new OutputStreamWriter(out))

  private[this] val tokenizers = Iterator.continually(in.readLine()).takeWhile(_ != null).map(new StringTokenizer(_)).buffered
  val printer = new PrintWriter(out, true)

  @inline private[this] def tokenizer() = {
    while(tokenizers.nonEmpty && !tokenizers.head.hasMoreTokens) tokenizers.next()
    tokenizers.headOption
  }

  def read[A](implicit read: IO.Read[A]): A = read.apply(this)
  def read[C[_], A: IO.Read](n: Int)(implicit b: CanBuild[A, C[A]]): C[A] = Iterator.fill(n)(read).to[C]

  def readTill(delim: String): String = tokenizer().get.nextToken(delim)
  def readTillEndOfLine(): String = readTill("\n\r")

  def readAll[A: IO.Read]: (Int, Vector[A]) = {
    val data = read[Vector[A]]
    (data.length, data)
  }

  override def next() = tokenizer().get.nextToken()
  override def hasNext = tokenizer().nonEmpty

  def write(obj: Any): this.type = {
    printer.print(obj)
    this
  }
  def writeLine(): this.type = {
    printer.println()
    this
  }
  def writeLine(obj: Any): this.type = {
    printer.println(obj)
    this
  }
  def writeLines(obj: Traversable[Any]): this.type = writeAll(obj, separator = "\n")
  def writeAll(obj: Traversable[Any], separator: String = " "): this.type = write(obj mkString separator)
  def query[A: IO.Read](question: Any): A = writeLine(question).read

  override def flush() = printer.flush()
  def close() = {
    flush()
    in.close()
    printer.close()
  }
}
object IO {
  class Read[A](val apply: IO => A) {
    def map[B](f: A => B): Read[B] = new Read(apply andThen f)
  }

  object Read {
    implicit def collection[C[_], A: Read](implicit b: CanBuild[A, C[A]]) : Read[C[A]]         = new Read(r => r.read[C, A](r.read[Int]))
    implicit val string                                                   : Read[String]       = new Read(_.next())
    implicit val int                                                      : Read[Int]          = string.map(_.toInt)
    implicit val long                                                     : Read[Long]         = string.map(_.toLong)
    implicit val bigInt                                                   : Read[BigInt]       = string.map(BigInt(_))
    implicit val double                                                   : Read[Double]       = string.map(_.toDouble)
    implicit val bigDecimal                                               : Read[BigDecimal]   = string.map(BigDecimal(_))
    implicit def tuple2[A: Read, B: Read]                                 : Read[(A, B)]       = new Read(r => (r.read[A], r.read[B]))
    implicit def tuple3[A: Read, B: Read, C: Read]                        : Read[(A, B, C)]    = new Read(r => (r.read[A], r.read[B], r.read[C]))
    implicit def tuple4[A: Read, B: Read, C: Read, D: Read]               : Read[(A, B, C, D)] = new Read(r => (r.read[A], r.read[B], r.read[C], r.read[D]))
    implicit val boolean                                                  : Read[Boolean]      = string map {s =>
      s.toLowerCase match {
        case "yes" | "true" | "1" => true
        case "no" | "false" | "0" => false
        case _ => s.toBoolean
      }
    }
  }
}

import scala.collection.mutable
import scala.io.StdIn._

object taskA {
  def main(args: Array[String]): Unit = {
    val Array(n, k) = readLine().split(" ").map(_.toInt)
    val arr :Array[Int] = readLine().split(" ").map(_.toInt)
    val map = scala.collection.mutable.Map[Int, Int]()
    val queue =  mutable.Queue[Int]()

    for(i <- 0 to n - 1) {
      val current_id = arr(i)
      if(!map.contains(current_id) || map(current_id) == 0) {
        if(map.contains(current_id)) map(current_id) += 1
        else map(current_id) = 1
        queue.enqueue(current_id)
        if(queue.size == k + 1) {
          map(queue.dequeue()) -= 1
        }
      }
    }
    println(queue.length)
    queue.reverse.foreach(id => print(id + " "))
  }
}

object _1234B1 extends CodeForcesApp {
  import Utils._, annotation._, collection.{mutable, Searching}, util._, control._, math.Ordering.Implicits._, Searching._, Math._

  override def apply(io: IO): io.type = {
    val n, k = io.read[Int]
    val msgs = io.read[Seq, Int](n)
    val ans = mutable.ArrayBuffer.empty[Int]
    val currentlyTalking = mutable.Set.empty[Int]
    for {
      msg <- msgs
      if currentlyTalking.add(msg)
    } {
      ans.prepend(msg)
      if (ans.length > k) currentlyTalking -= ans.remove(k)
    }

    io.writeLine(ans.length).writeAll(ans)
  }
}
/****************************[Ignore Template Below]****************************************/
trait CodeForcesApp {
  lazy val isOnlineJudge: Boolean = sys.props.get("ONLINE_JUDGE").exists(_.toBoolean)
  def debug(x: => Any): Unit = if (!isOnlineJudge) println(x)
  def apply(io: IO): io.type
  def main(args: Array[String]): Unit = apply(new IO(System.in, System.out)).close()
}
/****************************[Scala Collection Utils]****************************************/
object Utils {
  import scala.collection.mutable

  type of[C[_], A] = scala.collection.generic.CanBuild[A, C[A]]

  def when[A](check: Boolean)(f: => A): Option[A] = if (check) Some(f) else None

  def repeat[U](n: Int)(f: => U): Unit = (1 to n).foreach(_ => f)

  implicit class GenericExtensions[A](a: A) {
    def partialMatch[B](f: PartialFunction[A, B]): Option[B] = f lift a
  }

  implicit class TraversableExtensions[A, C[X] <: Traversable[X]](t: C[A]) {
    def zipWith[B](f: A => B)(implicit b: C of (A, B)): C[(A, B)] = (t map {i => i -> f(i)}).to[C]

    def whenNonEmpty[B](f: t.type => B): Option[B] = when(t.nonEmpty)(f(t))  // e.g. grades.whenNonEmpty(_.max)

    def toMultiSet: Map[A, Int] = t.groupBy(identity).mapValues(_.size)
  }

  implicit class PairExtensions[A, B](p: (A, B)) {
    def key = p._1
    def value = p._2
    def toSeq(implicit ev: B =:= A): Seq[A] = Seq(p.key, ev(p.value))
  }

  implicit class PairsExtensions[A, B, C[X] <: Traversable[X]](t: C[(A, B)]) {
    def toMultiMap(implicit b: C of B): Map[A, C[B]] = t.groupBy(_.key).mapValues(_.map(_.value).to[C]).withDefaultValue(b().result())
  }

  implicit class MapExtensions[K, V, C[X, Y] <: Map[X, Y]](m: C[K, V]) {
    def toMutable: mutable.Map[K, V] = mutable.Map.empty[K, V] ++ m
  }

  implicit class SetExtensions[A, C[A] <: Set[A]](s: C[A]) {
    def notContains(x: A): Boolean = !(s contains x)
    def toMutable = mutable.Set.empty[A] ++ s
  }

  implicit class IterableExtensions[A, C[A] <: Seq[A]](s: C[A]) {
    def indicesWhere(f: A => Boolean): Seq[Int] = s.zipWithIndex.collect {case (a, i) if f(a) => i}
    def deleteAtIndex(i: Int, howMany: Int = 1) = s.patch(i, Nil, howMany)
    def insertAtIndex(i: Int, items: Seq[A]) = s.patch(i, items, 0)
    def findWithIndex(f: A => Boolean): Option[(A, Int)] = s.zipWithIndex find {case (a, _) => f(a)}
  }

  implicit class BooleanExtensions(x: Boolean) {
    def to[A](implicit n: Numeric[A]) = if(x) n.one else n.zero
    def toEnglish = if(x) "Yes" else "No"
  }

  def desc[A](implicit order: Ordering[A]): Ordering[A] = order.reverse  //e.g. students.sortBy(_.height)(desc)

  def map[K] = new {
    def to[V](default: => V): mutable.Map[K, V] = using(_ => default)
    def using[V](f: K => V): mutable.Map[K, V] = new mutable.HashMap[K, V]() {
      override def apply(key: K) = getOrElseUpdate(key, f(key))
    }
  }

  def newMultiMap[K, V] = map[K] to mutable.Set.empty[V]

  def memoize[A, B](f: A => B): A => B = map[A] using f
}
/***********************************[Scala I/O]*********************************************/
import java.io._, java.util.StringTokenizer
import scala.collection.generic.CanBuild

class IO(in: BufferedReader, out: BufferedWriter) extends Iterator[String] with AutoCloseable with Flushable {
  def this(reader: Reader, writer: Writer) = this(new BufferedReader(reader), new BufferedWriter(writer))
  def this(in: InputStream, out: OutputStream) = this(new InputStreamReader(in), new OutputStreamWriter(out))

  private[this] val tokenizers = Iterator.continually(in.readLine()).takeWhile(_ != null).map(new StringTokenizer(_)).buffered
  val printer = new PrintWriter(out, true)

  @inline private[this] def tokenizer() = {
    while(tokenizers.nonEmpty && !tokenizers.head.hasMoreTokens) tokenizers.next()
    tokenizers.headOption
  }

  def read[A](implicit read: IO.Read[A]): A = read.apply(this)
  def read[C[_], A: IO.Read](n: Int)(implicit b: CanBuild[A, C[A]]): C[A] = Iterator.fill(n)(read).to[C]

  def readTill(delim: String): String = tokenizer().get.nextToken(delim)
  def readTillEndOfLine(): String = readTill("\n\r")

  def readAll[A: IO.Read]: (Int, Vector[A]) = {
    val data = read[Vector[A]]
    (data.length, data)
  }

  override def next() = tokenizer().get.nextToken()
  override def hasNext = tokenizer().nonEmpty

  def write(obj: Any): this.type = {
    printer.print(obj)
    this
  }
  def writeLine(): this.type = {
    printer.println()
    this
  }
  def writeLine(obj: Any): this.type = {
    printer.println(obj)
    this
  }
  def writeLines(obj: Traversable[Any]): this.type = writeAll(obj, separator = "\n")
  def writeAll(obj: Traversable[Any], separator: String = " "): this.type = write(obj mkString separator)
  def query[A: IO.Read](question: Any): A = writeLine(question).read

  override def flush() = printer.flush()
  def close() = {
    flush()
    in.close()
    printer.close()
  }
}
object IO {
  class Read[A](val apply: IO => A) {
    def map[B](f: A => B): Read[B] = new Read(apply andThen f)
  }

  object Read {
    implicit def collection[C[_], A: Read](implicit b: CanBuild[A, C[A]]) : Read[C[A]]         = new Read(r => r.read[C, A](r.read[Int]))
    implicit val string                                                   : Read[String]       = new Read(_.next())
    implicit val int                                                      : Read[Int]          = string.map(_.toInt)
    implicit val long                                                     : Read[Long]         = string.map(_.toLong)
    implicit val bigInt                                                   : Read[BigInt]       = string.map(BigInt(_))
    implicit val double                                                   : Read[Double]       = string.map(_.toDouble)
    implicit val bigDecimal                                               : Read[BigDecimal]   = string.map(BigDecimal(_))
    implicit def tuple2[A: Read, B: Read]                                 : Read[(A, B)]       = new Read(r => (r.read[A], r.read[B]))
    implicit def tuple3[A: Read, B: Read, C: Read]                        : Read[(A, B, C)]    = new Read(r => (r.read[A], r.read[B], r.read[C]))
    implicit def tuple4[A: Read, B: Read, C: Read, D: Read]               : Read[(A, B, C, D)] = new Read(r => (r.read[A], r.read[B], r.read[C], r.read[D]))
    implicit val boolean                                                  : Read[Boolean]      = string map {s =>
      s.toLowerCase match {
        case "yes" | "true" | "1" => true
        case "no" | "false" | "0" => false
        case _ => s.toBoolean
      }
    }
  }
}

object B1 extends App {
  import java.io._
  import java.nio.file.{Files, Path}
  import java.util.StringTokenizer

  import scala.annotation.tailrec
  import scala.io.Codec

  /**
   * Faster Scanner in Scala by wrick
   * https://codeforces.com/blog/entry/21074
   *
   * Scala implementation of a faster java.util.Scanner
   * See: http://codeforces.com/blog/entry/7018
   */
  class Scanner(reader: LineNumberReader) extends Iterator[String] with AutoCloseable {
    def this(reader: BufferedReader) = this(new LineNumberReader(reader))
    def this(reader: Reader) = this(new BufferedReader(reader))
    def this(inputStream: InputStream)(implicit codec: Codec) = this(new InputStreamReader(inputStream, codec.charSet))
    def this(path: Path)(implicit codec: Codec) = this(Files.newBufferedReader(path, codec.charSet))
    def this(file: File)(implicit codec: Codec) = this(file.toPath)(codec)
    def this(str: String) = this(new StringReader(str))

    private[this] val tokenizers = Iterator.continually(reader.readLine()).takeWhile(_ != null).map(new StringTokenizer(_)).filter(_.hasMoreTokens)
    private[this] var current: Option[StringTokenizer] = None

    @inline private[this] def tokenizer(): Option[StringTokenizer] = current.find(_.hasMoreTokens) orElse {
      current = if (tokenizers.hasNext) Some(tokenizers.next()) else None
      current
    }

    /**
     * Unlike Java's scanner which returns till end of current line, this actually returns the next line
     * @see line() if you want the Java behaviour
     */
    def nextLine(): String = {
      current = None   // reset
      reader.readLine()
    }
    def lineNumber: Int = reader.getLineNumber
    def line(): String = tokenizer().get.nextToken("\n\r")
    def nextString(): String = next()
    def nextChar(): Char = next().ensuring(_.length == 1).head
    def nextBoolean(): Boolean = next().toBoolean
    def nextByte(radix: Int = 10): Byte = java.lang.Byte.parseByte(next(), radix)
    def nextShort(radix: Int = 10): Short = java.lang.Short.parseShort(next(), radix)
    def nextInt(radix: Int = 10): Int = java.lang.Integer.parseInt(next(), radix)
    def nextLong(radix: Int = 10): Long = java.lang.Long.parseLong(next(), radix)
    def nextBigInt(radix: Int = 10): BigInt = BigInt(next(), radix)
    def nextFloat(): Float = next().toFloat
    def nextDouble(): Double = next().toDouble
    def nextBigDecimal(): BigDecimal = BigDecimal(next())
    override def next() = tokenizer().get.nextToken()
    override def hasNext = tokenizer().nonEmpty
    override def close() = reader.close()
  }

  def read2(): (Int, Seq[Int]) = {
    val in = new Scanner(System.in)
    val n = in.nextInt()
    val k = in.nextInt()
    val letters = (0 until n).map(_ => in.nextInt())
    (k, letters)
  }

  @tailrec
  def res(seq: Vector[Int])(acc: Vector[Int], k: Int): Vector[Int] = seq match {
    case Vector() => acc.reverse
    case x +: xs if acc.contains(x) => res(xs)(acc, k)
    case x +: xs if acc.length < k => res(xs)(acc :+ x, k)
    case x +: xs => res(xs)(acc.drop(1) :+ x, k)
  }

  val (k, letters) = read2()
  val r = res(letters.toVector)(Vector(), k)
  println(r.length)
  println(r mkString " ")
}

object Ex2 extends App {
  import java.io._
  import java.nio.file.{Files, Path}
  import java.util.StringTokenizer

  import scala.io.Codec
  import scala.annotation.tailrec

  /**
   * Faster Scanner in Scala by wrick
   * https://codeforces.com/blog/entry/21074
   *
   * Scala implementation of a faster java.util.Scanner
   * See: http://codeforces.com/blog/entry/7018
   */
  class Scanner(reader: LineNumberReader) extends Iterator[String] with AutoCloseable {
    def this(reader: BufferedReader) = this(new LineNumberReader(reader))
    def this(reader: Reader) = this(new BufferedReader(reader))
    def this(inputStream: InputStream)(implicit codec: Codec) = this(new InputStreamReader(inputStream, codec.charSet))
    def this(path: Path)(implicit codec: Codec) = this(Files.newBufferedReader(path, codec.charSet))
    def this(file: File)(implicit codec: Codec) = this(file.toPath)(codec)
    def this(str: String) = this(new StringReader(str))

    private[this] val tokenizers = Iterator.continually(reader.readLine()).takeWhile(_ != null).map(new StringTokenizer(_)).filter(_.hasMoreTokens)
    private[this] var current: Option[StringTokenizer] = None

    @inline private[this] def tokenizer(): Option[StringTokenizer] = current.find(_.hasMoreTokens) orElse {
      current = if (tokenizers.hasNext) Some(tokenizers.next()) else None
      current
    }

    /**
     * Unlike Java's scanner which returns till end of current line, this actually returns the next line
     * @see line() if you want the Java behaviour
     */
    def nextLine(): String = {
      current = None   // reset
      reader.readLine()
    }
    def lineNumber: Int = reader.getLineNumber
    def line(): String = tokenizer().get.nextToken("\n\r")
    def nextString(): String = next()
    def nextChar(): Char = next().ensuring(_.length == 1).head
    def nextBoolean(): Boolean = next().toBoolean
    def nextByte(radix: Int = 10): Byte = java.lang.Byte.parseByte(next(), radix)
    def nextShort(radix: Int = 10): Short = java.lang.Short.parseShort(next(), radix)
    def nextInt(radix: Int = 10): Int = java.lang.Integer.parseInt(next(), radix)
    def nextLong(radix: Int = 10): Long = java.lang.Long.parseLong(next(), radix)
    def nextBigInt(radix: Int = 10): BigInt = BigInt(next(), radix)
    def nextFloat(): Float = next().toFloat
    def nextDouble(): Double = next().toDouble
    def nextBigDecimal(): BigDecimal = BigDecimal(next())
    override def next() = tokenizer().get.nextToken()
    override def hasNext = tokenizer().nonEmpty
    override def close() = reader.close()
  }

  def read2(): (Int, Seq[Int]) = {
    val in = new Scanner(System.in)
    val n = in.nextInt()
    val k = in.nextInt()
    val letters = (0 until n).map(_ => in.nextInt())
    (k, letters)
  }

  @tailrec
  def res(seq: List[Int], k: Int)(acc: List[Int]): List[Int] = {
    if (acc.length == k) acc
    else seq match {
      case Nil => acc
      case x :: xs if !acc.contains(x) => res(xs, k)(x :: acc)
      case _ :: xs => res(xs, k)(acc)
    }
  }

  val (k, letters) = read2()
  val r = res(letters.toList, k)(Nil)
  println(r mkString " ")
}

object Ex2 extends App {
  import java.io._
  import java.nio.file.{Files, Path}
  import java.util.StringTokenizer

  import scala.io.Codec
  import scala.annotation.tailrec

  /**
   * Faster Scanner in Scala by wrick
   * https://codeforces.com/blog/entry/21074
   *
   * Scala implementation of a faster java.util.Scanner
   * See: http://codeforces.com/blog/entry/7018
   */
  class Scanner(reader: LineNumberReader) extends Iterator[String] with AutoCloseable {
    def this(reader: BufferedReader) = this(new LineNumberReader(reader))
    def this(reader: Reader) = this(new BufferedReader(reader))
    def this(inputStream: InputStream)(implicit codec: Codec) = this(new InputStreamReader(inputStream, codec.charSet))
    def this(path: Path)(implicit codec: Codec) = this(Files.newBufferedReader(path, codec.charSet))
    def this(file: File)(implicit codec: Codec) = this(file.toPath)(codec)
    def this(str: String) = this(new StringReader(str))

    private[this] val tokenizers = Iterator.continually(reader.readLine()).takeWhile(_ != null).map(new StringTokenizer(_)).filter(_.hasMoreTokens)
    private[this] var current: Option[StringTokenizer] = None

    @inline private[this] def tokenizer(): Option[StringTokenizer] = current.find(_.hasMoreTokens) orElse {
      current = if (tokenizers.hasNext) Some(tokenizers.next()) else None
      current
    }

    /**
     * Unlike Java's scanner which returns till end of current line, this actually returns the next line
     * @see line() if you want the Java behaviour
     */
    def nextLine(): String = {
      current = None   // reset
      reader.readLine()
    }
    def lineNumber: Int = reader.getLineNumber
    def line(): String = tokenizer().get.nextToken("\n\r")
    def nextString(): String = next()
    def nextChar(): Char = next().ensuring(_.length == 1).head
    def nextBoolean(): Boolean = next().toBoolean
    def nextByte(radix: Int = 10): Byte = java.lang.Byte.parseByte(next(), radix)
    def nextShort(radix: Int = 10): Short = java.lang.Short.parseShort(next(), radix)
    def nextInt(radix: Int = 10): Int = java.lang.Integer.parseInt(next(), radix)
    def nextLong(radix: Int = 10): Long = java.lang.Long.parseLong(next(), radix)
    def nextBigInt(radix: Int = 10): BigInt = BigInt(next(), radix)
    def nextFloat(): Float = next().toFloat
    def nextDouble(): Double = next().toDouble
    def nextBigDecimal(): BigDecimal = BigDecimal(next())
    override def next() = tokenizer().get.nextToken()
    override def hasNext = tokenizer().nonEmpty
    override def close() = reader.close()
  }

  def read2(): (Int, Seq[Int]) = {
    val in = new Scanner(System.in)
    val n = in.nextInt()
    val k = in.nextInt()
    val letters = (0 until n).map(_ => in.nextInt())
    (k, letters)
  }

  @tailrec
  def res(seq: List[Int], k: Int)(acc: List[Int]): List[Int] = {
    if (acc.length == k) acc
    else seq match {
      case Nil => acc
      case x :: xs if !acc.contains(x) => res(xs, k)(x :: acc)
      case _ :: xs => res(xs, k)(acc)
    }
  }

  val (k, letters) = read2()
  val r = res(letters.toList, k)(Nil)
  println(r.length)
  println(r mkString " ")
}


object Main {

  import java.io._
  import java.util.StringTokenizer

  import scala.collection.mutable
  import scala.collection.mutable.ListBuffer
  def main(args: Array[String]): Unit = {
    solve(System.in,System.out)
  }
  def solve(inputStream: InputStream, out: PrintStream): Unit = {
    val in = new InputReader(inputStream)
    val q = in.nextInt()
    val c = Array.fill(200001)(0)
    val t = Array.fill(200001)(0)
    (1 to q).foreach{ _ =>
      val n = in.nextInt()
      (0 until n).foreach(i => {
        t(i) = in.nextInt()

      })
      c(0) = 0
      var ans = 1
      var moreThan1 = false
      var indexToChange = 0
      (1 until n).foreach(i => {

        if(t(i-1) == t(i)){
          c(i) = c(i-1)
          moreThan1 = true
          indexToChange = i
        }else{
          c(i) = 1-c(i-1)
          ans=2
        }

      })

      val numColors = if(t(0) == t(n-1)){
        ans
      }else{
        if(c(0) == c(n-1)){
          if(moreThan1){
            (indexToChange until n).foreach{ i =>
              c(i) = 1 - c(i)
            }
            ans
          }else{
            c(0) = 2
            3
          }

        }else{
          ans
        }
      }

      //println(numColors)
      //println(c.slice(0,n).map(_ + 1).mkString(" "))
      out.println(numColors)
      out.println(c.slice(0,n).map(_ + 1).mkString(" "))

    }
  }
  class InputReader(stream: InputStream){
    val reader: BufferedReader = new BufferedReader(new InputStreamReader(stream),32768)
    var tokenizer: StringTokenizer = _
    def next(): String = {
      while(tokenizer == null || !tokenizer.hasMoreTokens){
        try{
          tokenizer = new StringTokenizer(reader.readLine())
        }catch {
          case e: IOException => throw new RuntimeException(e)
        }
      }
      tokenizer.nextToken()
    }
    def nextInt(): Int =  next().toInt
    def nextLong(): Long =  next().toLong
  }
}

object D {
  import InOut._
  def main(args: Array[String]): Unit = {}

  for (_ <- 1 to nextInt) {
    val n = nextInt
    val ts = nextInts(n)

    val res = Array.ofDim[Int](n)
    var prev = ts(0)
    var prevC = 1
    var lastSpan = -1
    for (i <- 1 until n) {
      if (ts(i) == prev) {
        lastSpan = i
        res(i) = prevC
      } else {
        prevC = 3 - prevC
        res(i) = prevC
      }
      prev = ts(i)
    }

    if (lastSpan == -1 && ts(0) != prev && n % 2 == 1) {
      res(0) = 3
    } else {
      if (ts(0) == prev) {
        res(0) = if (ts(0) == ts(1)) res(1) else 3 - res(1)
      } else {
        val for0 = 3 - prevC
        if (ts(0) == ts(1) || res(1) != for0) {
          res(0) = for0
        } else {
          for (i <- 1 until lastSpan) {
            res(i) = 3 - res(i)
          }
          res(0) = for0
        }
      }
    }

    out.println(res.max)
    out.println(res.mkString(" "))
  }

  out.flush()

  final object InOut {
    val in = new java.io.BufferedReader(new java.io.InputStreamReader(System.in))
    val out = new java.io.PrintWriter(System.out, false)
    private[this] var tokenizer: java.util.StringTokenizer = _
    def nextToken: String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new java.util.StringTokenizer(in.readLine)
      tokenizer.nextToken
    }
    def nextInt = Integer.parseInt(nextToken)
    def nextLong = java.lang.Long.parseLong(nextToken)
    def nextBig = BigInt(nextToken)
    def nextInts(n: Int) = Array.fill(n) { nextInt }
    def nextLongs(n: Int) = Array.fill(n) { nextLong }
    def nextBigs(n: Int) = Array.fill(n) { nextBig }
    def nextLine = in.readLine
  }
}

object D {
  import InOut._
  def main(args: Array[String]): Unit = {}

  for (_ <- 1 to nextInt) {
    val n = nextInt
    val ts = nextInts(n)

    val res = Array.ofDim[Int](n)
    var prev = ts(0)
    var prevC = 1
    var lastSpan = -1
    for (i <- 1 until n) {
      if (ts(i) == prev) {
        lastSpan = i
        res(i) = prevC
      } else {
        prevC = 3 - prevC
        res(i) = prevC
      }
      prev = ts(i)
    }

    if (lastSpan == -1 && ts(0) != prev && n % 2 == 1) {
      res(0) = 3
    } else {
      val for0 = if (ts(0) == prev) prevC else 3 - prevC
      if (ts(0) == ts(1) || res(1) != for0) {
        res(0) = for0
      } else {
        for (i <- 1 until lastSpan) {
          res(i) = 3 - res(i)
        }
        res(0) = for0
      }
    }

    out.println(res.max)
    out.println(res.mkString(" "))
  }

  out.flush()

  final object InOut {
    val in = new java.io.BufferedReader(new java.io.InputStreamReader(System.in))
    val out = new java.io.PrintWriter(System.out, false)
    private[this] var tokenizer: java.util.StringTokenizer = _
    def nextToken: String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new java.util.StringTokenizer(in.readLine)
      tokenizer.nextToken
    }
    def nextInt = Integer.parseInt(nextToken)
    def nextLong = java.lang.Long.parseLong(nextToken)
    def nextBig = BigInt(nextToken)
    def nextInts(n: Int) = Array.fill(n) { nextInt }
    def nextLongs(n: Int) = Array.fill(n) { nextLong }
    def nextBigs(n: Int) = Array.fill(n) { nextBig }
    def nextLine = in.readLine
  }
}

object Main {

  import java.io._
  import java.util.StringTokenizer

  import scala.collection.mutable
  import scala.collection.mutable.ListBuffer
  def main(args: Array[String]): Unit = {
    solve(System.in,System.out)
  }
  def solve(inputStream: InputStream, out: PrintStream): Unit = {
    val in = new InputReader(inputStream)
    val q = in.nextInt()
    val c = Array.fill(200001)(0)
    val t = Array.fill(200001)(0)
    (1 to q).foreach{ _ =>
      val n = in.nextInt()
      (0 until n).foreach(i => {
        t(i) = in.nextInt()

      })
      c(0) = 0
      var ans = 1
      (1 until n).foreach(i => {

        if(t(i-1) == t(i)){
          c(i) = c(i-1)
        }else{
          c(i) = 1-c(i-1)
          ans=2
        }

      })

      val numColors = if(t(0) == t(n-1)){
        ans
      }else{
        if(c(0) == c(n-1)){
          if((t(1) == t(0) && t(2) == t(1)) || (t(n-1) == t(n-2) && t(n-2) == t(n-3))){
            ans
          }else{
            c(0) = 2
            3
          }

        }else{
          ans
        }
      }

      out.println(numColors)
      out.println(c.slice(0,n).map(_ + 1).mkString(" "))

    }
  }
  class InputReader(stream: InputStream){
    val reader: BufferedReader = new BufferedReader(new InputStreamReader(stream),32768)
    var tokenizer: StringTokenizer = _
    def next(): String = {
      while(tokenizer == null || !tokenizer.hasMoreTokens){
        try{
          tokenizer = new StringTokenizer(reader.readLine())
        }catch {
          case e: IOException => throw new RuntimeException(e)
        }
      }
      tokenizer.nextToken()
    }
    def nextInt(): Int =  next().toInt
    def nextLong(): Long =  next().toLong
  }
}

object Main {

  import java.io._
  import java.util.StringTokenizer

  import scala.collection.mutable
  import scala.collection.mutable.ListBuffer
  def main(args: Array[String]): Unit = {
    solve(System.in,System.out)
  }
  def solve(inputStream: InputStream, out: PrintStream): Unit = {
    val in = new InputReader(inputStream)
    val q = in.nextInt()
    val c = Array.fill(200001)(0)
    val t = Array.fill(200001)(0)
    (1 to q).foreach{ _ =>
      val n = in.nextInt()
      (0 until n).foreach(i => {
        t(i) = in.nextInt()

      })
      c(0) = 0
      var ans = 1
      (1 until n).foreach(i => {

        if(t(i-1) == t(i)){
          c(i) = c(i-1)
        }else{
          c(i) = 1-c(i-1)
          ans=2
        }

      })

      val numColors = if(t(0) == t(n-1)){
        ans
      }else{
        if(c(0) == c(n-1)){

          c(0) = 2
          3
        }else{
          ans
        }
      }

      out.println(numColors)
      out.println(c.slice(0,n).map(_ + 1).mkString(" "))

    }
  }
  class InputReader(stream: InputStream){
    val reader: BufferedReader = new BufferedReader(new InputStreamReader(stream),32768)
    var tokenizer: StringTokenizer = _
    def next(): String = {
      while(tokenizer == null || !tokenizer.hasMoreTokens){
        try{
          tokenizer = new StringTokenizer(reader.readLine())
        }catch {
          case e: IOException => throw new RuntimeException(e)
        }
      }
      tokenizer.nextToken()
    }
    def nextInt(): Int =  next().toInt
    def nextLong(): Long =  next().toLong
  }
}


import scala.io.StdIn

object GrumpyCat {
  def main(args:Array[String]): Unit={
    var res = "normal"
    print("9\n")
    val sequen = List("go die in a hole","are you serious?","terrible","worse","no way","don't even")
    val nk = StdIn.readLine()
    if (sequen.contains(nk)){
      res = "grumpy"
    }

    print(res)

  }
}

object Solution {
  def main(args: Array[String]) {
    val normal = Set("cool", "great", "not bad", "don't think so", "don't touch me")
    val grumpy = Set("terrible", "worse", "don't even", "go die in a hole", "are you serious")

    for (i <- 0 to 9) {
      println(i)
      val reaction = readLine
      if (normal.contains(reaction)) {
        println("normal")
      } else if (grumpy.contains(reaction)) {
        println("grumpy")
      }
    }
  }
}

object Solution {
  def main(args: Array[String]) {
    val reactions = (0 to 9).map(i => { println(i); Console.flush(); readLine }).filter(_ == "cool")
    println(if (reactions.isEmpty) "grumpy" else "normal")
  }
}

object Solution {
  def main(args: Array[String]) {
    val normal = Set("cool", "great!", "not bad", "don't think so", "don't touch me!")
    val grumpy = Set("terrible", "worse", "don't even", "go die in a hole", "are you serious?")

    for (i <- 0 to 9) {
      println(i)
      val reaction = readLine
      if (normal.contains(reaction)) {
        println("normal")
      } else if (grumpy.contains(reaction)) {
        println("grumpy")
      }
    }
  }
}

object Solution {
  def main(args: Array[String]) {
    val normal = Set("cool", "great", "not bad", "don't think so", "don't even")
    val grumpy = Set("terrible", "worse", "don't even", "go die in a hole", "are you serious")

    for (i <- 0 to 9) {
      println(i)
      val reaction = readLine
      if (normal.contains(reaction)) {
        println("normal")
      } else if (grumpy.contains(reaction)) {
        println("grumpy")
      }
    }
  }
}

object Solution {
  def main(args: Array[String]) {
    (0 to 9).foreach(println)
    val reactions = (0 to 9).map(_ => readLine).filter(_ == "cool")
    println(if (reactions.isEmpty) "grumpy" else "normal")
  }
}

object Solution {
  def main(args: Array[String]) {
    val flag = false
    (0 to 9).foreach{ x => println(x); Console.flush() }
    val reactions = (0 to 9).map(_ => readLine).filter(_ == "cool")
    println(if (reactions.isEmpty) "grumpy" else "normal")
  }
}

object Solution {
  def main(args: Array[String]) {
    val normal = Set("cool", "great!", "not bad", "don't think so")
    val grumpy = Set("terrible", "worse", "don't even", "go die in a hole")

    for (i <- 0 to 9) {
      println(i)
      val reaction = readLine
      if (normal.contains(reaction)) {
        println("normal")
      } else if (grumpy.contains(reaction)) {
        println("grumpy")
      }
    }
  }
}

import scala.io.StdIn

object GrumpyCat {
  def main(args:Array[String]): Unit={
    print("9\n")
    val nk = StdIn.readLine()
    val read = nk.split(" ").map(_.toInt)
    var res = "normal"
    for(iterator <-0 to  9){
      if(read(iterator) > 9){
        res = "grumpy"
      }
    }

    print(res)
  }
}

import scala.io.StdIn

object GrumpyCat {
  def main(args:Array[String]): Unit={
    print("9")
    print("\n\n\n")
    val nk = StdIn.readLine()
    val read = nk.split(" ").map(_.toInt)
    var res = "normal"
    for(iterator <-0 to  9){

      if(read(iterator) > 9){
        res = "grumpy"
      }
    }

    print(res)

  }
}

import scala.io.StdIn

object GrumpyCat {
  def main(args:Array[String]): Unit={
    println("9\n")

    val nk = StdIn.readLine()
    val read = nk.split(" ").map(_.toInt)
    var res = "normal"
    for(iterator <-0 to  9){

      if(read(iterator) > 9){
        res = "grumpy"
      }
    }

    print(res)
  }
}

object GrumpyCat {
   def main(args:Array[String]): Unit={
     print("normal")
   }

}

import scala.io.StdIn

object GrumpyCat {
  def main(args:Array[String]): Unit={
    print("9")
    print("0\n")
    val nk = StdIn.readLine()
    val read = nk.split(" ").map(_.toInt)
    var res = "normal"
    for(iterator <-0 to  9){

      if(read(iterator) > 9){
        res = "grumpy"
      }
    }

    print(res)

  }
}

import scala.io.StdIn

object GrumpyCat {
   def main(args:Array[String]): Unit={
     print("9\n")
     val nk = StdIn.readLine().split(" ").map(_.toInt)
     var res = "normal"
     for(iterator <-0 to  9){

       if(nk(iterator) > 9){
         res = "grumpy"
       }
     }

     print(res)

  }
}

import scala.io.StdIn

object GrumpyCat {
   def main(args:Array[String]): Unit={
     println("9")
     val nk = StdIn.readLine().split(" ").map(_.toInt)
     var res = "normal"
     for(iterator <-0 to  9){

       if(nk(iterator) > 9){
         res = "grumpy"
       }
     }

     print(res)

  }
}