import scala.io.Source

object Solution extends App {
  val in = Source.stdin.getLines()
  val Array(n, t1) = in.next().split(' ').map(_.toInt)
  val t = n - t1
  val zipped = in.next().zip(in.next())
  val same = zipped.count(i => i._1 == i._2)
  val removeSame = if (same >= t) same - t else 0
  val nSame = same - removeSame
  val selectFirst = t - nSame
//  println("nSame = " + nSame)
//  println("selectFirst = " + selectFirst)
  if (nSame < 0 || (nSame + 2 * selectFirst ) > n)
    println(-1)
  else {
    val answer = zipped.foldLeft(selectFirst, selectFirst, nSame, List.empty[Char]) {
      case ((first, second, same, list), (ch1, ch2)) if same > 0 && ch1 == ch2 =>
        (first, second, same - 1, ch1 :: list)
      case ((first, second, same, list), (ch1, ch2)) if ch1 == ch2 =>
        val s = if (ch1 != 'a') 'a' else 'b'
        (first, second, same, s :: list)
      case ((first, second, same, list), (ch1, ch2)) if first > 0 =>
        (first - 1, second, same, ch1 :: list)
      case ((first, second, same, list), (ch1, ch2)) if second > 0 =>
        (first, second - 1, same, ch2 :: list)
      case ((first, second, same, list), (ch1, ch2)) =>
        val s = if (ch1 != 'a' && ch2 != 'a') 'a' else if (ch1 != 'b' && ch2 != 'b') 'b' else 'c'
        (first, second - 1, same, s :: list)
    }._4.reverse
    println(answer.mkString)
  }
}

/**
 * @author netman
 */

import java.io._
import scala._
import scala.Array._
import java.util.ArrayList
import scala.collection.mutable._
import java.util.StringTokenizer

object HelloWorld {

  class InputReader(reader: BufferedReader) {

    var tokenizer: StringTokenizer = null;

    def this(stream: InputStream) = {
      this(new BufferedReader(new InputStreamReader(stream), (1 << 20)));
    }

    def this(f: File) = {
      this(new BufferedReader(new FileReader(f), (1 << 20)));
    }

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

    def nextInt(): Int = next().toInt

    def nextDouble(): Double = next().toDouble

    def close() = reader.close();
  }

  class OutputWriter(writer: PrintWriter) {
    def this(stream: OutputStream) {
      this(new PrintWriter(new BufferedWriter(new OutputStreamWriter(stream))));
    }

    def this(f: File) = {
      this(new PrintWriter(new BufferedWriter(new FileWriter(f))));
    }

    def print(x: Any) = writer.print(x);

    def println(x: Any) = writer.println(x);

    def close() = writer.close();
  }

  val in = new InputReader(System.in)
  val out = new OutputWriter(System.out)

  val P = 1000 * 1000 * 1000L + 7

  def main(args: Array[String]): Unit = {
    val (n, t) = (in.nextInt, in.nextInt)

    val (a, b) = (in.next, in.next)

    var (eq, neq) = (0, 0)
    var (neq01, neq10, neq11, eq00, eq11) = (0, 0, 0, 0, 0)

    for (i <- 0 until n) {
      if (a.charAt(i) != b.charAt(i)) {
        neq += 1
      }
      else {
        eq += 1
      }
    }

    val (minimal, maximal) = (neq / 2 + (neq % 2), neq + eq)

    if (t >= minimal && t <= maximal) {
      neq11 = neq
      eq11 = eq

      while (neq11 + eq11 + neq01 > t) {
        if (eq11 > 0) {
          eq11 -= 1
          eq00 += 1
        }
        else if (neq11 >= 2){
          neq11 -= 2
          neq01 += 1
          neq10 += 1
        }
      }

      var res = new Array[Char](n)

      for (i <- 0 until n) {
        if (a.charAt(i) == b.charAt(i)) {
          if (eq00 > 0) {
            res(i) = a.charAt(i)
            eq00 -= 1
          }
          else {
            res(i) = if (a.charAt(i) == 'a') 'b' else 'a'
            eq11 -= 1
          }
        }
        else {
          if (neq01 > 0) {
            res(i) = a.charAt(i)
            neq01 -= 1
          }
          else if (neq10 > 0) {
            res(i) = b.charAt(i)
            neq10 -= 1
          }
          else {
            res(i) = if (a.charAt(i) != 'a' && b.charAt(i) != 'a') 'a' else if (a.charAt(i) != 'b' && b.charAt(i) != 'b') 'b' else 'c'
          }
        }
      }

      for (i <- 0 until n) {
        out.print(res(i))
      }
    }
    else out.println("-1")

    out.close
  }
}

/**
 * @author netman
 */

import java.io._
import scala._
import scala.Array._
import java.util.ArrayList
import scala.collection.mutable._
import java.util.StringTokenizer

object HelloWorld {

  class InputReader(reader: BufferedReader) {

    var tokenizer: StringTokenizer = null;

    def this(stream: InputStream) = {
      this(new BufferedReader(new InputStreamReader(stream), (1 << 20)));
    }

    def this(f: File) = {
      this(new BufferedReader(new FileReader(f), (1 << 20)));
    }

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

    def nextInt(): Int = next().toInt

    def nextDouble(): Double = next().toDouble

    def close() = reader.close();
  }

  class OutputWriter(writer: PrintWriter) {
    def this(stream: OutputStream) {
      this(new PrintWriter(new BufferedWriter(new OutputStreamWriter(stream))));
    }

    def this(f: File) = {
      this(new PrintWriter(new BufferedWriter(new FileWriter(f))));
    }

    def print(x: Any) = writer.print(x);

    def println(x: Any) = writer.println(x);

    def close() = writer.close();
  }

  val in = new InputReader(System.in)
  val out = new OutputWriter(System.out)

  val P = 1000 * 1000 * 1000L + 7

  def main(args: Array[String]): Unit = {
    val (n, t) = (in.nextInt, in.nextInt)

    val (a, b) = (in.next, in.next)

    var (eq, neq) = (0, 0)
    var (neq01, neq10, neq11, eq00, eq11) = (0, 0, 0, 0, 0)

    for (i <- 0 until n) {
      if (a.charAt(i) != b.charAt(i)) {
        neq += 1
      }
      else {
        eq += 1
      }
    }

    val (minimal, maximal) = (neq / 2 + (neq % 2), neq + eq)

    if (t >= minimal && t <= maximal) {
      var rem = t
      if (neq % 2 == 1) {
        neq11 += 1
        neq -= 1
        rem -= 1
      }
      while (neq > 0 && neq / 2 > rem) {
        neq -= 2
        neq10 += 1
        neq01 += 1
        rem -= 1
      }
      while (neq > 0) {
        neq -= 1
        neq11 += 1
        rem -= 1
      }
      while (rem > 0) {
        eq11 += 1
        eq -= 1
        rem -= 1
      }
      while (eq > 0) {
        eq00 += 1
        eq -= 1
      }

      var res = new Array[Char](n)

      for (i <- 0 until n) {
        if (a.charAt(i) == b.charAt(i)) {
          if (eq00 > 0) {
            res(i) = a.charAt(i)
            eq00 -= 1
          }
          else {
            res(i) = if (a.charAt(i) == 'a') 'b' else 'a'
            eq11 -= 1
          }
        }
        else {
          if (neq01 > 0) {
            res(i) = a.charAt(i)
            neq01 -= 1
          }
          else if (neq10 > 0) {
            res(i) = b.charAt(i)
            neq10 -= 1
          }
          else {
            res(i) = if (a.charAt(i) != 'a' && b.charAt(i) != 'a') 'a' else if (a.charAt(i) != 'b' && b.charAt(i) != 'b') 'b' else 'c'
          }
        }
      }

      for (i <- 0 until n) {
        out.print(res(i))
      }
    }
    else out.println("-1")

    out.close
  }
}

import scala.io.Source

object Solution extends App {
  val in = Source.stdin.getLines()
  val Array(n, t1) = in.next().split(' ').map(_.toInt)
  val t = n - t1
  val zipped = in.next().zip(in.next())
  val same = zipped.count(i => i._1 == i._2)
  val removeSame = if (same >= t) same - t else if ((t - same) % 2 == 1) 1 else 0
  val nSame = same - removeSame
  val selectFirst = t - nSame
  if (nSame < 0 || (nSame + 2 * selectFirst) > n)
    println(-1)
  else {
    val answer = zipped.foldLeft(selectFirst, selectFirst, nSame, List.empty[Char]) {
      case ((first, second, same, list), (ch1, ch2)) if same > 0 && ch1 == ch2 =>
        (first, second, same - 1, ch1 :: list)
      case ((first, second, same, list), (ch1, ch2)) if ch1 == ch2 =>
        val s = if (ch1 != 'a') 'a' else 'b'
        (first, second, same, s :: list)
      case ((first, second, same, list), (ch1, ch2)) if first > 0 =>
        (first - 1, second, same, ch1 :: list)
      case ((first, second, same, list), (ch1, ch2)) if second > 0 =>
        (first, second - 1, same, ch2 :: list)
      case ((first, second, same, list), (ch1, ch2)) =>
        val s = if (ch1 != 'a' && ch2 != 'a') 'a' else if (ch1 != 'b' && ch2 != 'b') 'b' else 'c'
        (first, second - 1, same, s :: list)
    }._4.reverse
    println(answer.mkString)
  }
}

object Solution extends App {

  def diff(a: Char, b: Char) = {
    (a, b) match {
      case (a, b) if a != 'a' && b != 'a' => 'a'
      case (a, b) if a != 'b' && b != 'b' => 'b'
      case (a, b) if a != 'c' && b != 'c' => 'c'
    }
  }

  val in = scala.io.Source.stdin.getLines()
  val Array(n, t) = in.next().split(' ').map(_.toInt)
  val str1 = in.next()
  val str2 = in.next()
  val zipped = str1.zip(str2)
  val count = zipped.count(t => t._1 != t._2)
  if (t > n || count > 2 * t)
    println(-1)
  else {
    val k = n - t
    val q = n - count
    val r = if (k <= q) {
      zipped.foldLeft((k, List.empty[Char])) {
        case ((left, l), (el1, el2)) if el1 == el2 && left > 0 =>
          (left - 1, el1 :: l)
        case ((left, l), (el1, el2)) => (left, diff(el1, el2) :: l)
      }._2
    } else {
      zipped.foldLeft((count / 2, count / 2, q - count / 2, List.empty[Char])) {
        case ((first, second, change, l), (el1, el2)) if el1 == el2 && change > 0 =>
          (first, second, change - 1, el1 :: l)
        case ((first, second, change, l), (el1, el2)) if second > 0 =>
          (first, second - 1, change, el2 :: l)
        case ((first, second, change, l), (el1, el2)) if first > 0 =>
          (first - 1, second, change, el1 :: l)
        case ((first, second, change,l), (el1, el2)) =>
          (first, second, change, diff(el1, el2) :: l)
      }._4
    }
    //    count = 1
    //    count / 2 = 0
    //    change = t = 1
    //    println(count / 2)
    //    println(t - count + count / 2)
    //    println(count % 2)

    println(r.reverse.mkString)
  }
}

import scala.io.Source

object Solution extends App {
  val in = Source.stdin.getLines()
  val Array(n, t1) = in.next().split(' ').map(_.toInt)
  val t = n - t1
  val zipped = in.next().zip(in.next())
  val same = zipped.count(i => i._1 == i._2)
  val removeSame = if (same >= t) same - t else if ((t - same) % 2 == 1) 1 else 0
  val nSame = same - removeSame
  val selectFirst = t - nSame
  if (nSame < 0)
    println(-1)
  else {
    val answer = zipped.foldLeft(selectFirst, selectFirst, nSame, List.empty[Char]) {
      case ((first, second, same, list), (ch1, ch2)) if same > 0 && ch1 == ch2 =>
        (first, second, same - 1, ch1 :: list)
      case ((first, second, same, list), (ch1, ch2)) if ch1 == ch2 =>
        val s = if (ch1 != 'a') 'b' else 'b'
        (first, second, same, s :: list)
      case ((first, second, same, list), (ch1, ch2)) if first > 0 =>
        (first - 1, second, same, ch1 :: list)
      case ((first, second, same, list), (ch1, ch2)) if second > 0 =>
        (first, second - 1, same, ch2 :: list)
      case ((first, second, same, list), (ch1, ch2)) =>
        val s = if (ch1 != 'a' && ch2 != 'a') 'a' else if (ch1 != 'b' && ch2 != 'b') 'b' else 'c'
        (first, second - 1, same, s :: list)
    }._4.reverse
    println(answer.mkString)
  }
}

object Solution extends App {

  def diff(a: Char, b: Char) = {
    (a, b) match {
      case (a, b) if a != 'a' && b != 'a' => 'a'
      case (a, b) if a != 'b' && b != 'b' => 'b'
      case (a, b) if a != 'c' && b != 'c' => 'c'
    }
  }

  val in = scala.io.Source.stdin.getLines()
  val Array(n, t) = in.next().split(' ').map(_.toInt)
  val str1 = in.next()
  val str2 = in.next()
  val zipped = str1.zip(str2)
  val count = zipped.count(t => t._1 != t._2)
  if (t > n || count > 2 * t)
    println(-1)
  else {
    val k = n - t
    val q = n - count
    val r = if (k <= q) {
      zipped.foldLeft((k, List.empty[Char])) {
        case ((left, l), (el1, el2)) if el1 == el2 && left > 0 =>
          (left - 1, el1 :: l)
        case ((left, l), (el1, el2)) => (left, diff(el1, el2) :: l)
      }._2
    } else {
      zipped.foldLeft((count / 2, count / 2, 2 * t, List.empty[Char])) {
        case ((first, second, change, l), (el1, el2)) if el1 != el2 && second > 0 =>
          (first, second - 1, change - 1, el2 :: l)
        case ((first, second, change, l), (el1, el2)) if el1 != el2 && first > 0 =>
          (first - 1, second, change - 1, el1 :: l)
        case ((first, second, change, l), (el1, el2)) if el1 == el2 && change > 0 =>
          (first, second, change - 2, el1 :: l)
        case ((first, second, change,l), (el1, el2)) =>
          (first, second, change, diff(el1, el2) :: l)
      }._4
    }
    //    count = 1
    //    count / 2 = 0
    //    change = t = 1
    //    println(count / 2)
    //    println(t - count + count / 2)
    //    println(count % 2)

    println(r.reverse.mkString)
  }
}

object Solution extends App {

  def diff(a: Char, b: Char) = {
    (a, b) match {
      case (a, b) if a != 'a' && b != 'a' => 'a'
      case (a, b) if a != 'b' && b != 'b' => 'b'
      case (a, b) if a != 'c' && b != 'c' => 'c'
    }
  }

  val in = scala.io.Source.stdin.getLines()
  val Array(n, t) = in.next().split(' ').map(_.toInt)
  val str1 = in.next()
  val str2 = in.next()
  val zipped = str1.zip(str2)
  val count = zipped.count(t => t._1 != t._2)
  if (t > n || count > 2 * t)
    println(-1)
  else {
    val k = n - t
    val q = n - count
    val r = if (k <= q) {
      zipped.foldLeft((k, List.empty[Char])) {
        case ((left, l), (el1, el2)) if el1 == el2 && left > 0 =>
          (left - 1, el1 :: l)
        case ((left, l), (el1, el2)) => (left, diff(el1, el2) :: l)
      }._2
    } else {
      zipped.foldLeft((count / 2, count / 2, t - count + count / 2, count % 2, List.empty[Char])) {
        case ((first, second, change, 1, l), (el1, el2)) if el1 != el2 =>
          (first, second, change, 0, diff(el1, el2) :: l)
        case ((first, second, change, d, l), (el1, el2)) if el1 == el2 && change > 0 =>
          (first, second, change - 1, d, diff(el1, el2) :: l)
        case ((first, second, change, d, l), (el1, el2)) if el1 == el2 =>
          (first, second, change, d, el1 :: l)
        case ((first, second, change, d, l), (el1, el2)) if second > first =>
          (first, second - 1, change, d, el2 :: l)
        case ((first, second, change, d, l), (el1, el2)) if first > 0 =>
          (first - 1, second, change, d, el1 :: l)
        case ((first, second, change, d, l), (el1, el2)) =>
          (first, second, change, d, el1 :: l)
      }._5
    }
//    count = 1
//    count / 2 = 0
//    change = t = 1
//    println(count / 2)
//    println(t - count + count / 2)
//    println(count % 2)

    println(r.reverse.mkString)
  }
}

object Solution extends App {

  def diff(a: Char, b: Char) = {
    (a, b) match {
      case (a, b) if a != 'a' && b != 'a' => 'a'
      case (a, b) if a != 'b' && b != 'b' => 'b'
      case (a, b) if a != 'c' && b != 'c' => 'c'
    }
  }

  val in = scala.io.Source.stdin.getLines()
  val Array(n, t) = in.next().split(' ').map(_.toInt)
  val str1 = in.next()
  val str2 = in.next()
  val zipped = str1.zip(str2)
  val count = zipped.count(t => t._1 != t._2)
  if (t > n || count > 2 * t)
    println(-1)
  else {
//    count = 1
//    count / 2 = 0
//    change = t = 1
    val r = zipped.foldLeft((count / 2, count / 2, t - count / 2 - count % 2, List.empty[Char])) {
      case ((first, second, 0, l), (el1, el2)) if el1 == el2 => (first, second, 0, el1 :: l)
      case ((first, second, change, l), (el1, el2)) if second > first => (first, second - 1, change, el2 :: l)
      case ((first, second, change, l), (el1, el2)) if first > 0 => (first - 1, second, change, el1 :: l)
      case ((first, second, change, l), (el1, el2)) if change > 0 => (first, second, change - 1, diff(el1, el2) :: l)
      case ((first, second, change, l), (el1, el2)) if el1 == el2 => (first, second, change - 1, el1 :: l)
      case ((first, second, change, l), (el1, el2)) => (first, second, change - 1, diff(el1, el2) :: l)
    }

    println(r._4.reverse.mkString)
  }
}

object Solution extends App {

  def diff(a: Char, b: Char) = {
    (a, b) match {
      case (a, b) if a != 'a' && b != 'a' => 'a'
      case (a, b) if a != 'b' && b != 'b' => 'b'
      case (a, b) if a != 'c' && b != 'c' => 'c'
    }
  }

  val in = scala.io.Source.stdin.getLines()
  val Array(n, t) = in.next().split(' ').map(_.toInt)
  val str1 = in.next()
  val str2 = in.next()
  val zipped = str1.zip(str2)
  val count = zipped.count(t => t._1 != t._2)
  if (t > n || count > 2 * t)
    println(-1)
  else {
    val k = n - t
    val q = n - count
    val r = if (k <= q) {
      zipped.foldLeft((k, List.empty[Char])) {
        case ((left, l), (el1, el2)) if el1 == el2 && left > 0 =>
          (left - 1, el1 :: l)
        case ((left, l), (el1, el2)) => (left, diff(el1, el2) :: l)
      }._2
    } else {
      zipped.foldLeft((count / 2, count / 2, 2 * t, List.empty[Char])) {
        case ((first, second, change, l), (el1, el2)) if el1 != el2 && second > 0 =>
          (first, second - 1, change - 1, el2 :: l)
        case ((first, second, change, l), (el1, el2)) if el1 != el2 && first > 0 =>
          (first - 1, second, change - 1, el1 :: l)
        case ((first, second, change, l), (el1, el2)) if el1 == el2 && change > 0 =>
          (first, second, change - 2, diff(el1, el2) :: l)
        case ((first, second, change,l), (el1, el2)) =>
          (first, second, change,  el1 :: l)
      }._4
    }
    //    count = 1
    //    count / 2 = 0
    //    change = t = 1
    //    println(count / 2)
    //    println(t - count + count / 2)
    //    println(count % 2)

    println(r.reverse.mkString)
  }
}

object Solution extends App {

  def diff(a: Char, b: Char) = {
    (a, b) match {
      case (a, b) if a != 'a' && b != 'a' => 'a'
      case (a, b) if a != 'b' && b != 'b' => 'b'
      case (a, b) if a != 'c' && b != 'c' => 'c'
    }
  }

  val in = scala.io.Source.stdin.getLines()
  val Array(n, t) = in.next().split(' ').map(_.toInt)
  val str1 = in.next()
  val str2 = in.next()
  val zipped = str1.zip(str2)
  val count = zipped.count(t => t._1 != t._2)
  if (t > n || count > 2 * t)
    println(-1)
  else {
    val k = n - t
    val q = n - count
    val r = if (k <= q) {
      zipped.foldLeft((k, List.empty[Char])) {
        case ((left, l), (el1, el2)) if el1 == el2 && left > 0 =>
          (left - 1, el1 :: l)
        case ((left, l), (el1, el2)) => (left, diff(el1, el2) :: l)
      }._2
    } else {
      zipped.foldLeft((count / 2, count / 2, t - count / 2, List.empty[Char])) {
        case ((first, second, change, l), (el1, el2)) if el1 != el2 && second > 0 =>
          (first, second - 1, change, el2 :: l)
        case ((first, second, change, l), (el1, el2)) if el1 != el2 && first > 0 =>
          (first - 1, second, change, el1 :: l)
        case ((first, second, change, l), (el1, el2)) if el1 == el2 && change > 0 =>
          (first, second, change - 1, el1 :: l)
        case ((first, second, change,l), (el1, el2)) =>
          (first, second, change,  diff(el1, el2) :: l)
      }._4
    }
    //    count = 1
    //    count / 2 = 0
    //    change = t = 1
    //    println(count / 2)
    //    println(t - count + count / 2)
    //    println(count % 2)

    println(r.reverse.mkString)
  }
}

object Solution extends App {

  def diff(a: Char, b: Char) = {
    (a, b) match {
      case (a, b) if a != 'a' && b != 'a' => 'a'
      case (a, b) if a != 'b' && b != 'b' => 'b'
      case (a, b) if a != 'c' && b != 'c' => 'c'
    }
  }

  val in = scala.io.Source.stdin.getLines()
  val Array(n, t) = in.next().split(' ').map(_.toInt)
  val str1 = in.next()
  val str2 = in.next()
  val zipped = str1.zip(str2)
  val count = zipped.count(t => t._1 != t._2)
  if (t > n || count > 2 * t)
    println(-1)
  else {
    val k = n - t
    val q = n - count
    val r = if (k <= q) {
      zipped.foldLeft((k, List.empty[Char])) {
        case ((left, l), (el1, el2)) if el1 == el2 && left > 0 =>
          (left - 1, el1 :: l)
        case ((left, l), (el1, el2)) => (left, diff(el1, el2) :: l)
      }._2
    } else {
      zipped.foldLeft((count / 2, count / 2, q - count / 2, List.empty[Char])) {
        case ((first, second, change, l), (el1, el2)) if el1 == el2 && change > 0 =>
          (first, second, change - 1, el1 :: l)
        case ((first, second, change, l), (el1, el2)) if el1 != el2 && second > 0 =>
          (first, second - 1, change, el2 :: l)
        case ((first, second, change, l), (el1, el2)) if el1 != el2 && first > 0 =>
          (first - 1, second, change, el1 :: l)
        case ((first, second, change,l), (el1, el2)) =>
          (first, second, change, diff(el1, el2) :: l)
      }._4
    }
    //    count = 1
    //    count / 2 = 0
    //    change = t = 1
    //    println(count / 2)
    //    println(t - count + count / 2)
    //    println(count % 2)

    println(r.reverse.mkString)
  }
}

object Solution extends App {

  def diff(a: Char, b: Char) = {
    (a, b) match {
      case (a, b) if a != 'a' && b != 'a' => 'a'
      case (a, b) if a != 'b' && b != 'b' => 'b'
      case (a, b) if a != 'c' && b != 'c' => 'c'
    }
  }

  val in = scala.io.Source.stdin.getLines()
  val Array(n, t) = in.next().split(' ').map(_.toInt)
  val str1 = in.next()
  val str2 = in.next()
  val zipped = str1.zip(str2)
  val count = zipped.count(t => t._1 != t._2)
  if (t > n || count > 2 * t)
    println(-1)
  else {
    val k = n - t
    val q = n - count
    val r = if (k <= q) {
      zipped.foldLeft((k, List.empty[Char])) {
        case ((left, l), (el1, el2)) if el1 == el2 && left > 0 =>
          (left - 1, el1 :: l)
        case ((left, l), (el1, el2)) => (left, diff(el1, el2) :: l)
      }._2
    } else {
      zipped.foldLeft((count / 2, count / 2, q - count / 2, List.empty[Char])) {
        case ((first, second, change, l), (el1, el2)) if el1 == el2 && change > 0 =>
          (first, second, change - 1, el1 :: l)
        case ((first, second, change, l), (el1, el2)) if el1 == el2 =>
          (first, second, change, el1 :: l)
        case ((first, second, change, l), (el1, el2)) if second > 0 =>
          (first, second - 1, change, el2 :: l)
        case ((first, second, change, l), (el1, el2)) if first > 0 =>
          (first - 1, second, change, el1 :: l)
        case ((first, second, change,l), (el1, el2)) =>
          (first, second, change, diff(el1, el2) :: l)
      }._4
    }
    //    count = 1
    //    count / 2 = 0
    //    change = t = 1
    //    println(count / 2)
    //    println(t - count + count / 2)
    //    println(count % 2)

    println(r.reverse.mkString)
  }
}

object Solution extends App {
  val in = scala.io.Source.stdin.getLines()
  val Array(n, t) = in.next().split(' ').map(_.toInt)
  val str1 = in.next()
  val str2 = in.next()
  val zipped = str1.zip(str2)
  val count = zipped.count(t => t._1 != t._2)
  if (t > n || count > 2 * t)
    println(-1)
  else {
    val r = zipped.foldLeft((t, t, 2 * t - count, List.empty[Char])) {
      case ((a, b, ch, l), ('a', 'a')) if ch > 0 => (a, b, ch - 1, 'b' :: l)
      case ((a, b, ch, l), (el1, el2)) if el1 == el2 && ch > 0 => (a, b, ch - 1, 'b' :: l)
      case ((a, b, ch, l), (el1, el2)) if a > b => (a - 1, b, ch, el1 :: l)
      case ((a, b, ch, l), (el1, el2)) => (a, b - 1, ch, el2 :: l)
    }
    println(r._4.reverse.mkString)
  }
}

object Solution extends App {
  val in = scala.io.Source.stdin.getLines()
  val Array(n, t) = in.next().split(' ').map(_.toInt)
  val str1 = in.next()
  val str2 = in.next()
  val zipped = str1.zip(str2)
  val count = zipped.count(t => t._1 != t._2)
  if (t > n || count > 2 * t)
    println(-1)
  else {
    val r = zipped.foldLeft((count / 2, count / 2, t - count / 2, List.empty[Char])) {
      case ((a, b, ch, l), ('a', 'a')) if ch > 0 => (a, b, ch - 1, 'b' :: l)
      case ((a, b, ch, l), (el1, el2)) if el1 == el2 && ch > 0 => (a, b, ch - 1, 'b' :: l)
      case ((a, b, ch, l), (el1, el2)) if el1 == el2 => (a, b, ch, el1 :: l)
      case ((0, 0, ch, l), (el1, el2)) if el1 != 'a' && el2 != 'a' => (0, 0, ch, 'a' :: l)
      case ((0, 0, ch, l), (el1, el2)) if el1 != 'b' && el2 != 'b' => (0, 0, ch, 'b' :: l)
      case ((0, 0, ch, l), (el1, el2)) if el1 != 'c' && el2 != 'c' => (0, 0, ch, 'c' :: l)
      case ((a, b, ch, l), (el1, el2)) if a > b => (a - 1, b, ch, el1 :: l)
      case ((a, b, ch, l), (el1, el2)) => (a, b - 1, ch, el2 :: l)
    }
    println(r._4.reverse.mkString)
  }
}

object Solution extends App {

  def diff(a: Char, b: Char) = {
    (a, b) match {
      case (a, b) if a != 'a' && b != 'a' => 'a'
      case (a, b) if a != 'b' && b != 'b' => 'b'
      case (a, b) if a != 'c' && b != 'c' => 'c'
    }
  }

  val in = scala.io.Source.stdin.getLines()
  val Array(n, t) = in.next().split(' ').map(_.toInt)
  val str1 = in.next()
  val str2 = in.next()
  val zipped = str1.zip(str2)
  val count = zipped.count(t => t._1 != t._2)
  if (t > n || count > 2 * t)
    println(-1)
  else {
    val k = n - t
    val q = n - count
    val r = if (k <= q) {
      zipped.foldLeft((k, List.empty[Char])) {
        case ((left, l), (el1, el2)) if el1 == el2 && left > 0 =>
          (left - 1, el1 :: l)
        case ((left, l), (el1, el2)) => (left, diff(el1, el2) :: l)
      }._2
    } else {
      zipped.foldLeft((count / 2, count / 2, n - t - count / 2, List.empty[Char])) {
        case ((first, second, change, l), (el1, el2)) if el1 != el2 && first > 0 =>
          (first - 1, second, change, el1 :: l)
        case ((first, second, change, l), (el1, el2)) if el1 != el2 && second > 0 =>
          (first, second - 1, change, el2 :: l)
        case ((first, second, change, l), (el1, el2)) if el1 == el2 && change > 0 =>
          (first, second, change, el1 :: l)
        case ((first, second, change,l), (el1, el2)) =>
          (first, second, change,  diff(el1, el2) :: l)
      }._4
    }
    //    count = 1
    //    count / 2 = 0
    //    change = t = 1
    //    println(count / 2)
    //    println(t - count + count / 2)
    //    println(count % 2)

    println(r.reverse.mkString)
  }
}

object Solution extends App {

  def diff(a: Char, b: Char) = {
    (a, b) match {
      case (a, b) if a != 'a' && b != 'a' => 'a'
      case (a, b) if a != 'b' && b != 'b' => 'b'
      case (a, b) if a != 'c' && b != 'c' => 'c'
    }
  }

  val in = scala.io.Source.stdin.getLines()
  val Array(n, t) = in.next().split(' ').map(_.toInt)
  val str1 = in.next()
  val str2 = in.next()
  val zipped = str1.zip(str2)
  val count = zipped.count(t => t._1 != t._2)
  if (t > n || count > 2 * t)
    println(-1)
  else {
    val k = n - t
    val q = n - count
    val r = if (k <= q) {
      zipped.foldLeft((k, List.empty[Char])) {
        case ((left, l), (el1, el2)) if el1 == el2 && left > 0 =>
          (left - 1, el1 :: l)
        case ((left, l), (el1, el2)) => (left, diff(el1, el2) :: l)
      }._2
    } else {
      zipped.foldLeft((count / 2, count / 2, t - count + count / 2, count % 2, List.empty[Char])) {
        case ((first, second, change, 1, l), (el1, el2)) if el1 != el2 =>
          (first, second, change, 0, diff(el1, el2) :: l)
        case ((first, second, change, d, l), (el1, el2)) if el1 == el2 && change > 0 =>
          (first, second, change - 1, d, diff(el1, el2) :: l)
        case ((first, second, change, d, l), (el1, el2)) if el1 == el2 =>
          (first, second, change, d, el1 :: l)
        case ((first, second, change, d, l), (el1, el2)) if second > 0 =>
          (first, second - 1, change, d, el2 :: l)
        case ((first, second, change, d, l), (el1, el2)) if first > 0 =>
          (first - 1, second, change, d, el1 :: l)
        case ((first, second, change, d, l), (el1, el2)) =>
          (first, second, change, d, el1 :: l)
      }._5
    }
//    count = 1
//    count / 2 = 0
//    change = t = 1
//    println(count / 2)
//    println(t - count + count / 2)
//    println(count % 2)

    println(r.reverse.mkString)
  }
}

object Solution extends App {

  def diff(a: Char, b: Char) = {
    (a, b) match {
      case (a, b) if a != 'a' && b != 'a' => 'a'
      case (a, b) if a != 'b' && b != 'b' => 'b'
      case (a, b) if a != 'c' && b != 'c' => 'c'
    }
  }

  val in = scala.io.Source.stdin.getLines()
  val Array(n, t) = in.next().split(' ').map(_.toInt)
  val str1 = in.next()
  val str2 = in.next()
  val zipped = str1.zip(str2)
  val count = zipped.count(t => t._1 != t._2)
  if (t > n || count > 2 * t)
    println(-1)
  else {
    val k = n - t
    val q = n - count
    val r = if (k <= q) {
      zipped.foldLeft((k, List.empty[Char])) {
        case ((left, l), (el1, el2)) if el1 == el2 && left > 0 =>
          (left - 1, el1 :: l)
        case ((left, l), (el1, el2)) => (left, diff(el1, el2) :: l)
      }._2
    } else {
      zipped.foldLeft((count / 2, count / 2, n - t, List.empty[Char])) {
        case ((first, second, change, l), (el1, el2)) if el1 != el2 && first > 0 =>
          (first - 1, second, change, el1 :: l)
        case ((first, second, change, l), (el1, el2)) if el1 != el2 && second > 0 =>
          (first, second - 1, change, el2 :: l)
        case ((first, second, change, l), (el1, el2)) if el1 == el2 && change > 0 =>
          (first, second, change, el1 :: l)
        case ((first, second, change,l), (el1, el2)) =>
          (first, second, change,  diff(el1, el2) :: l)
      }._4
    }
    //    count = 1
    //    count / 2 = 0
    //    change = t = 1
    //    println(count / 2)
    //    println(t - count + count / 2)
    //    println(count % 2)

    println(r.reverse.mkString)
  }
}

object Solution extends App {

  def diff(a: Char, b: Char) = {
    (a, b) match {
      case (a, b) if a != 'a' && b != 'a' => 'a'
      case (a, b) if a != 'b' && b != 'b' => 'b'
      case (a, b) if a != 'c' && b != 'c' => 'c'
    }
  }

  val in = scala.io.Source.stdin.getLines()
  val Array(n, t) = in.next().split(' ').map(_.toInt)
  val str1 = in.next()
  val str2 = in.next()
  val zipped = str1.zip(str2)
  val count = zipped.count(t => t._1 != t._2)
  if (t > n || count > 2 * t)
    println(-1)
  else {
//    count = 1
//    count / 2 = 0
//    change = t = 1
    val r = zipped.foldLeft((count / 2, count / 2, t - count + count / 2, count % 2, List.empty[Char])) {
      case ((first, second, change, 1, l), (el1, el2)) if el1 != el2 =>
        (first, second, change, 0, diff(el1, el2) :: l)
      case ((first, second, change, d, l), (el1, el2)) if el1 == el2 && change > 0 =>
        (first, second, change - 1, d, diff(el1, el2) :: l)
      case ((first, second, change, d, l), (el1, el2)) if second > first =>
        (first, second - 1, change, d, el2 :: l)
      case ((first, second, change, d, l), (el1, el2)) if first > 0 =>
        (first - 1, second, change, d, el1 :: l)
      case ((first, second, change, d, l), (el1, el2)) if change > 0 =>
        (first, second, change - 1, d, diff(el1, el2) :: l)
      case ((first, second, change, d, l), (el1, el2)) if el1 == el2 =>
        (first, second, change - 1, d, el1 :: l)
      case ((first, second, change, d, l), (el1, el2)) =>
        (first, second, change - 1, d, diff(el1, el2) :: l)
    }

    println(r._5.reverse.mkString)
  }
}

import scala.io.Source

object Solution extends App {
  val in = Source.stdin.getLines()
  val Array(n, t1) = in.next().split(' ').map(_.toInt)
  val t = n - t1
  val zipped = in.next().zip(in.next())
  val same = zipped.count(i => i._1 == i._2)
  val removeSame = if (same >= t) same - t else if ((t - same) % 2 == 1) 1 else 0
  val nSame = same - removeSame
  val selectFirst = t - nSame
  if (nSame < 0)
    println(-1)
  else {
    val answer = zipped.foldLeft(selectFirst, selectFirst, nSame, List.empty[Char]) {
      case ((first, second, same, list), (ch1, ch2)) if same > 0 && ch1 == ch2 =>
        (first, second, same - 1, ch1 :: list)
      case ((first, second, same, list), (ch1, ch2)) if ch1 == ch2 =>
        val s = if (ch1 != 'a') 'a' else 'b'
        (first, second, same, s :: list)
      case ((first, second, same, list), (ch1, ch2)) if first > 0 =>
        (first - 1, second, same, ch1 :: list)
      case ((first, second, same, list), (ch1, ch2)) if second > 0 =>
        (first, second - 1, same, ch2 :: list)
      case ((first, second, same, list), (ch1, ch2)) =>
        val s = if (ch1 != 'a' && ch2 != 'a') 'a' else if (ch1 != 'b' && ch2 != 'b') 'b' else 'c'
        (first, second - 1, same, s :: list)
    }._4.reverse
    println(answer.mkString)
  }
}

object Solution extends App {

  def diff(a: Char, b: Char) = {
    (a, b) match {
      case (a, b) if a != 'a' && b != 'a' => 'a'
      case (a, b) if a != 'b' && b != 'b' => 'b'
      case (a, b) if a != 'c' && b != 'c' => 'c'
    }
  }

  val in = scala.io.Source.stdin.getLines()
  val Array(n, t) = in.next().split(' ').map(_.toInt)
  val str1 = in.next()
  val str2 = in.next()
  val zipped = str1.zip(str2)
  val count = zipped.count(t => t._1 != t._2)
  if (t > n || count > 2 * t)
    println(-1)
  else {
    val k = n - t
    val q = n - count
    val r = if (k <= q) {
      zipped.foldLeft((k, List.empty[Char])) {
        case ((left, l), (el1, el2)) if el1 == el2 && left > 0 =>
          (left - 1, el1 :: l)
        case ((left, l), (el1, el2)) => (left, diff(el1, el2) :: l)
      }._2
    } else {
      zipped.foldLeft((count / 2, count / 2, t - count / 2, List.empty[Char])) {
        case ((first, second, change, l), (el1, el2)) if el1 != el2 && second > 0 =>
          (first, second - 1, change, el2 :: l)
        case ((first, second, change, l), (el1, el2)) if el1 != el2 && first > 0 =>
          (first - 1, second, change, el1 :: l)
        case ((first, second, change, l), (el1, el2)) if el1 == el2 && change > 0 =>
          (first, second, change, el1 :: l)
        case ((first, second, change,l), (el1, el2)) =>
          (first, second, change,  diff(el1, el2) :: l)
      }._4
    }
    //    count = 1
    //    count / 2 = 0
    //    change = t = 1
    //    println(count / 2)
    //    println(t - count + count / 2)
    //    println(count % 2)

    println(r.reverse.mkString)
  }
}

object Solution extends App {

  def diff(a: Char, b: Char) = {
    (a, b) match {
      case (a, b) if a != 'a' && b != 'a' => 'a'
      case (a, b) if a != 'b' && b != 'b' => 'b'
      case (a, b) if a != 'c' && b != 'c' => 'C'
    }
  }

  val in = scala.io.Source.stdin.getLines()
  val Array(n, t) = in.next().split(' ').map(_.toInt)
  val str1 = in.next()
  val str2 = in.next()
  val zipped = str1.zip(str2)
  val count = zipped.count(t => t._1 != t._2)
  if (t > n || count > 2 * t)
    println(-1)
  else {
    val r = zipped.foldLeft((count / 2, count / 2, t - count / 2, List.empty[Char])) {
      case ((first, second, 0, l), (el1, el2)) if el1 == el2 => (first, second, 0, el1 :: l)
      case ((first, second, change, l), (el1, el2)) if second > first => (first, second - 1, change, el2 :: l)
      case ((first, second, change, l), (el1, el2)) if first > 0 => (first - 1, second, change, el1 :: l)
      case ((first, second, change, l), (el1, el2)) if change > 0 => (first, second, change - 1, diff(el1, el2) :: l)
      case ((first, second, change, l), (el1, el2)) => (first, second, change - 1, el1 :: l)

    }

    println(r._4.reverse.mkString)

    // 1) different symbols too many
    // 2) different symbols too few

  }
}
//cbca

object Solution extends App {

  def diff(a: Char, b: Char) = {
    (a, b) match {
      case (a, b) if a != 'a' && b != 'a' => 'a'
      case (a, b) if a != 'b' && b != 'b' => 'b'
      case (a, b) if a != 'c' && b != 'c' => 'c'
    }
  }

  val in = scala.io.Source.stdin.getLines()
  val Array(n, t) = in.next().split(' ').map(_.toInt)
  val str1 = in.next()
  val str2 = in.next()
  val zipped = str1.zip(str2)
  val count = zipped.count(t => t._1 != t._2)
  if (t > n || count > 2 * t)
    println(-1)
  else {
    val k = n - t
    val q = n - count
    val r = if (k <= q) {
      zipped.foldLeft((k, List.empty[Char])) {
        case ((left, l), (el1, el2)) if el1 == el2 && left > 0 =>
          (left - 1, el1 :: l)
        case ((left, l), (el1, el2)) => (left, diff(el1, el2) :: l)
      }._2
    } else {
      zipped.foldLeft((count / 2, count / 2, q - count / 2, List.empty[Char])) {
        case ((first, second, change, l), (el1, el2)) if el1 != el2 && first > 0 =>
          (first - 1, second, change, el1 :: l)
        case ((first, second, change, l), (el1, el2)) if el1 != el2 && second > 0 =>
          (first, second - 1, change, el2 :: l)
        case ((first, second, change, l), (el1, el2)) if el1 == el2 && change > 0 =>
          (first, second, change, el1 :: l)
        case ((first, second, change,l), (el1, el2)) =>
          (first, second, change,  diff(el1, el2) :: l)
      }._4
    }
    //    count = 1
    //    count / 2 = 0
    //    change = t = 1
    //    println(count / 2)
    //    println(t - count + count / 2)
    //    println(count % 2)

    println(r.reverse.mkString)
  }
}

object Solution extends App {

  def diff(a: Char, b: Char) = {
    (a, b) match {
      case (a, b) if a != 'a' && b != 'a' => 'a'
      case (a, b) if a != 'b' && b != 'b' => 'b'
      case (a, b) if a != 'c' && b != 'c' => 'c'
    }
  }

  val in = scala.io.Source.stdin.getLines()
  val Array(n, t) = in.next().split(' ').map(_.toInt)
  val str1 = in.next()
  val str2 = in.next()
  val zipped = str1.zip(str2)
  val count = zipped.count(t => t._1 != t._2)
  if (t > n || count > 2 * t)
    println(-1)
  else {
    val k = n - t
    val q = n - count
    val r = if (k <= q) {
      zipped.foldLeft((k, List.empty[Char])) {
        case ((left, l), (el1, el2)) if el1 == el2 && left > 0 =>
          (left - 1, el1 :: l)
        case ((left, l), (el1, el2)) => (left, diff(el1, el2) :: l)
      }._2
    } else {
      zipped.foldLeft((count / 2, count / 2, t - count / 2, List.empty[Char])) {
        case ((first, second, change, l), (el1, el2)) if el1 != el2 && first > 0 =>
          (first - 1, second, change, el1 :: l)
        case ((first, second, change, l), (el1, el2)) if el1 != el2 && second > 0 =>
          (first, second - 1, change, el2 :: l)
        case ((first, second, change, l), (el1, el2)) if el1 == el2 && change > 0 =>
          (first, second, change - 1, el1 :: l)
        case ((first, second, change,l), (el1, el2)) =>
          (first, second, change,  diff(el1, el2) :: l)
      }._4
    }
    //    count = 1
    //    count / 2 = 0
    //    change = t = 1
    //    println(count / 2)
    //    println(t - count + count / 2)
    //    println(count % 2)

    println(r.reverse.mkString)
  }
}

object Solution extends App {
  val in = scala.io.Source.stdin.getLines()
  val Array(n, t) = in.next().split(' ').map(_.toInt)
  val str1 = in.next()
  val str2 = in.next()
  val zipped = str1.zip(str2)
  val count = zipped.count(t => t._1 != t._2)
  if (t > n || count > 2 * t)
    println(-1)
  else {
    val r = zipped.foldLeft((count / 2, count / 2, t - count / 2, List.empty[Char])) {
      case ((a, b, ch, l), ('a', 'a')) if ch > 0 => (a, b, ch - 1, 'b' :: l)
      case ((a, b, ch, l), (el1, el2)) if el1 == el2 && ch > 0 => (a, b, ch - 1, 'b' :: l)
      case ((a, b, ch, l), (el1, el2)) if el1 == el2 => (a, b, ch, el1 :: l)
      case ((a, b, ch, l), (el1, el2)) if a > b => (a - 1, b, ch, el1 :: l)
      case ((a, b, ch, l), (el1, el2)) => (a, b - 1, ch, el2 :: l)
    }
    println(r._4.reverse.mkString)
  }
}

object Solution extends App {

  def diff(a: Char, b: Char) = {
    (a, b) match {
      case (a, b) if a != 'a' && b != 'a' => 'a'
      case (a, b) if a != 'b' && b != 'b' => 'b'
      case (a, b) if a != 'c' && b != 'c' => 'c'
    }
  }

  val in = scala.io.Source.stdin.getLines()
  val Array(n, t) = in.next().split(' ').map(_.toInt)
  val str1 = in.next()
  val str2 = in.next()
  val zipped = str1.zip(str2)
  val count = zipped.count(t => t._1 != t._2)
  if (t > n || count > 2 * t)
    println(-1)
  else {
//    count = 1
//    count / 2 = 0
//    change = t = 1
//    println(count / 2)
//    println(t - count + count / 2)
//    println(count % 2)
    val r = zipped.foldLeft((count / 2, count / 2, t - count + count / 2, count % 2, List.empty[Char])) {
      case ((first, second, change, 1, l), (el1, el2)) if el1 != el2 =>
        (first, second, change, 0, diff(el1, el2) :: l)
      case ((first, second, change, d, l), (el1, el2)) if el1 == el2 && change > 0 =>
        (first, second, change - 1, d, diff(el1, el2) :: l)
      case ((first, second, change, d, l), (el1, el2)) if el1 == el2 =>
        (first, second, change, d, el1 :: l)
      case ((first, second, change, d, l), (el1, el2)) if second > first =>
        (first, second - 1, change, d, el2 :: l)
      case ((first, second, change, d, l), (el1, el2)) if first > 0 =>
        (first - 1, second, change, d, el1 :: l)
      case ((first, second, change, d, l), (el1, el2)) =>
        (first, second, change, d, el1 :: l)
    }

    println(r._5.reverse.mkString)
  }
}


object B extends App {

  def readString = Console.readLine

  val s = readString.take(3000)
  val n = s.length

  val c = Array.ofDim[Short](n + 1, n + 1)

  for (i <- 0 to n) {
    c(i)(0) = 0
    c(0)(i) = 0
  }

  def lcs_print(i: Int, j: Int): StringBuilder = {
    if (i == 0 || j == 0) new StringBuilder()
    else if (s(i - 1) == s(n - j)) lcs_print(i - 1, j - 1) + s(i - 1)
    else if (c(i - 1)(j) >= c(i)(j - 1)) lcs_print(i - 1, j)
    else lcs_print(i, j - 1)
  }

  var maxLen = 0

  for (i <- 1 to n; j <- 1 to n) {
      val cc = if (s(i - 1) == s(n - j)) (c(i - 1)(j - 1) + 1).toShort
      else if (c(i - 1)(j) >= c(i)(j - 1)) c(i - 1)(j)
      else c(i)(j - 1)
      c(i)(j) = cc
      if (cc > maxLen) maxLen = cc
  }

  for (i <- 1 to n; j <- 1 to n; if c(i)(j) == maxLen) {
    var candidate = lcs_print(i, j).toString
    if (candidate.equals(candidate.reverse)) {
      while (candidate.length > 100) {
        val l = candidate.length
        if (l % 2 == 1) candidate = candidate.take(l / 2) + candidate.drop(l / 2 + 1)
        else candidate = candidate.substring(1, l - 1)
      }
      println(candidate)
      exit
    }
  }

}

object B extends App {

  def readString = Console.readLine

  val s = readString.take(2601)
  val n = s.length

  val c = Array.fill(n + 2, n + 1)(0)

  def lps_print(l: Int, r: Int): String = {
    if (l > r) ""
    else if (l == r) s(l - 1).toString
    else if (s(l - 1) == s(r - 1)) s(l - 1) + lps_print(l + 1, r - 1) + s(l - 1)
    else if (c(l + 1)(r) >= c(l)(r - 1)) lps_print(l + 1, r)
    else lps_print(l, r - 1)
  }

  var maxLen = 0
  var best = ""

  for (l <- n to 1 by -1; r <- l to n) {
      c(l)(r) = math.max(if (s(l - 1) == s(r - 1)) c(l + 1)(r - 1) + (if (l == r) 1 else 2) else 0,
    		  math.max(c(l + 1)(r), c(l)(r - 1)))
      if (c(l)(r) > maxLen) {
        maxLen = c(l)(r)
        best = lps_print(l, r)
        if (maxLen >= 100) {
          if (maxLen == 101) best = best.take(50) + best.drop(51)
          println(best)
          exit
        }
      }
  }

  println(best)
}

object B extends App {

  def readString = Console.readLine

  val s = readString.take(2601)
  val n = s.length

  val c = Array.ofDim[Int](n + 1, n + 1)

  for (i <- 0 to n) {
    c(i)(0) = 0
    c(0)(i) = 0
  }

  def lcs_print(i: Int, j: Int): StringBuilder = {
    if (i == 0 || j == 0) new StringBuilder()
    else if (s(i - 1) == s(n - j)) lcs_print(i - 1, j - 1) + s(i - 1)
    else if (c(i - 1)(j) >= c(i)(j - 1)) lcs_print(i - 1, j)
    else lcs_print(i, j - 1)
  }

  var maxLen = 0

  for (i <- 1 to n; j <- 1 to n) {
      c(i)(j) = math.max(if (s(i - 1) == s(n - j)) (c(i - 1)(j - 1) + 1) else 0,
    		  math.max(c(i - 1)(j), c(i)(j - 1)))
      if (c(i)(j) > maxLen) maxLen = c(i)(j)
  }

  for (i <- maxLen to n; j <- maxLen to n; if c(i)(j) == maxLen) {
    var candidate = lcs_print(i, j).toString
    if (candidate.equals(candidate.reverse)) {
      while (candidate.length > 100) {
        val l = candidate.length
        if (l % 2 == 1) candidate = candidate.take(l / 2) + candidate.drop(l / 2 + 1)
        else candidate = candidate.substring(1, l - 1)
      }
      println(candidate)
      exit
    }
  }

}

object B extends App {

  def readString = Console.readLine

  val s = readString.take(3000)
  val n = s.length

  val c = Array.ofDim[Short](n + 1, n + 1)

  for (i <- 0 to n) {
    c(i)(0) = 0
    c(0)(i) = 0
  }

  def lcs_print(i: Int, j: Int): StringBuilder = {
    if (i == 0 || j == 0) new StringBuilder()
    else if (s(i - 1) == s(n - j)) lcs_print(i - 1, j - 1) + s(i - 1)
    else if (c(i - 1)(j) >= c(i)(j - 1)) lcs_print(i - 1, j)
    else lcs_print(i, j - 1)
  }

  var maxLen = 0

  for (i <- 1 to n; j <- 1 to n) {
      val cc = if (s(i - 1) == s(n - j)) (c(i - 1)(j - 1) + 1).toShort
      else if (c(i - 1)(j) >= c(i)(j - 1)) c(i - 1)(j)
      else c(i)(j - 1)
      c(i)(j) = cc
      if (cc > maxLen) maxLen = cc
  }

  for (i <- maxLen to n; j <- maxLen to n; if c(i)(j) == maxLen) {
    var candidate = lcs_print(i, j).toString
    if (candidate.equals(candidate.reverse)) {
      while (candidate.length > 100) {
        val l = candidate.length
        if (l % 2 == 1) candidate = candidate.take(l / 2) + candidate.drop(l / 2 + 1)
        else candidate = candidate.substring(1, l - 1)
      }
      println(candidate)
      exit
    }
  }

}

object B extends App {

  def readString = Console.readLine

  val s = readString.take(3000)
  val n = s.length

  val c = Array.ofDim[Int](n + 1, n + 1)

  for (i <- 0 to n) {
    c(i)(0) = 0
    c(0)(i) = 0
  }

  def lcs_print(i: Int, j: Int): StringBuilder = {
    if (i == 0 || j == 0) new StringBuilder()
    else if (s(i - 1) == s(n - j)) lcs_print(i - 1, j - 1) + s(i - 1)
    else if (c(i - 1)(j) >= c(i)(j - 1)) lcs_print(i - 1, j)
    else lcs_print(i, j - 1)
  }

  var maxLen = 0

  for (i <- 1 to n; j <- 1 to n) {
      val cc = math.max(if (s(i - 1) == s(n - j)) (c(i - 1)(j - 1) + 1) else 0,
    		  math.max(c(i - 1)(j), c(i)(j - 1)))
      c(i)(j) = cc
      if (cc > maxLen) maxLen = cc
  }

  for (i <- maxLen to n; j <- maxLen to n; if c(i)(j) == maxLen) {
    var candidate = lcs_print(i, j).toString
    if (candidate.equals(candidate.reverse)) {
      while (candidate.length > 100) {
        val l = candidate.length
        if (l % 2 == 1) candidate = candidate.take(l / 2) + candidate.drop(l / 2 + 1)
        else candidate = candidate.substring(1, l - 1)
      }
      println(candidate)
      exit
    }
  }

}

object B extends App {

  def readString = Console.readLine

  val s = readString.take(3000)
  val n = s.length

  val c = Array.ofDim[Short](n + 1, n + 1)

  for (i <- 0 to n) {
    c(i)(0) = 0
    c(0)(i) = 0
  }

  var i = 0
  var maxLen = 0
  var best = ""

  def lcs_print(i: Int, j: Int): StringBuilder = {
    if (i == 0 || j == 0) new StringBuilder()
    else if (s(i - 1) == s(n - j)) lcs_print(i - 1, j - 1) + s(i - 1)
    else if (c(i - 1)(j) >= c(i)(j - 1)) lcs_print(i - 1, j)
    else lcs_print(i, j - 1)
  }

  while (i < n) {
    i += 1
    var j = 0
    while (j < n) {
      j += 1
      val cc = if (s(i - 1) == s(n - j)) (c(i - 1)(j - 1) + 1).toShort
      else if (c(i - 1)(j) >= c(i)(j - 1)) c(i - 1)(j)
      else c(i)(j - 1)
      c(i)(j) = cc
      if (cc > maxLen) maxLen = cc
    }
  }

  i = 0
  while (i < n) {
    i += 1
    var j = 0
    while (j < n) {
      j += 1
      if (c(i)(j) == maxLen) {
        val candidate = lcs_print(i, j).toString
        if (candidate.equals(candidate.reverse)) {
          best = candidate
          i = n + 1
          j = n + 1
        }
      }
    }
  }

  while (best.length > 100) {
    val l = best.length
    if (l % 2 == 1) best = best.take(l / 2) + best.drop(l / 2 + 1)
    else best = best.substring(1, l - 1)
  }

  println(best)
}

object B extends App {

  def readString = Console.readLine

  val s = readString.take(3000)
  val n = s.length

  val c = Array.ofDim[Short](n + 1, n + 1)

  for (i <- 0 to n) {
    c(i)(0) = 0
    c(0)(i) = 0
  }

  var i = 0
  var maxLen = 0
  var best = ""

  def lcs_print(i: Int, j: Int): StringBuilder = {
    if (i == 0 || j == 0) new StringBuilder()
    else if (s(i - 1) == s(n - j)) lcs_print(i - 1, j - 1) + s(i - 1)
    else if (c(i - 1)(j) >= c(i)(j - 1)) lcs_print(i - 1, j)
    else lcs_print(i, j - 1)
  }

  while (i < n) {
    i += 1
    var j = 0
    while (j < n) {
      j += 1
      val cc = if (s(i - 1) == s(n - j)) (c(i - 1)(j - 1) + 1).toShort
      else if (c(i - 1)(j) >= c(i)(j - 1)) c(i - 1)(j)
      else c(i)(j - 1)
      c(i)(j) = cc
      //if (cc == 50) println(lcs_print(i, j).toString)
      if (cc > maxLen) {
        maxLen = cc
        //best = lcs_print(i, j).toString
      }
      /*if (maxLen == 100) {
        i = n + 1
        j = n + 1
      }*/
    }
  }

  i = 0
  while (i < n) {
    i += 1
    var j = 0
    while (j < n) {
      j += 1
      if (c(i)(j) == maxLen) {
        val candidate = lcs_print(i, j).toString
        if (candidate.equals(candidate.reverse)) {
          best = candidate
          i = n + 1
          j = n + 1
        }
      }
    }
  }

  while (best.length > 100) {
    val l = best.length
    if (l % 2 == 1) best = best.take(l / 2) + best.drop(l / 2 + 1)
    else best = best.substring(1, l - 1)
  }

  println(best)
}

object B extends App {

  def readString = Console.readLine

  val s = readString.take(3000)
  val n = s.length
  val nn = n // 2 + 2

  val c = Array.ofDim[Short](nn + 1, nn + 1)

  for (i <- 0 to nn) {
    c(i)(0) = 0
    c(0)(i) = 0
  }

  var i = 0
  var maxLen = 0
  var best = ""

  def lcs_print(i: Int, j: Int): StringBuilder = {
    if (i == 0 || j == 0) new StringBuilder()
    else if (s(i - 1) == s(n - j)) lcs_print(i - 1, j - 1) + s(i - 1)
    else if (c(i - 1)(j) >= c(i)(j - 1)) lcs_print(i - 1, j)
    else lcs_print(i, j - 1)
  }

  while (i < nn) {
    i += 1
    var j = 0
    while (j < nn) {
      j += 1
      val cc = if (s(i - 1) == s(n - j)) (c(i - 1)(j - 1) + 1).toShort
      else if (c(i - 1)(j) >= c(i)(j - 1)) c(i - 1)(j)
      else c(i)(j - 1)
      c(i)(j) = cc
      if (cc > maxLen) {
        val candidate = lcs_print(i, j).toString
        if (cc != 100 || candidate.equals(candidate.reverse)) {
          maxLen = cc
          best = candidate
        }
      }
      if (maxLen >= 100) {
        i = nn + 1
        j = nn + 1
      }
    }
  }

  println(best)
}

object B extends App {

  def readString = Console.readLine

  val s = readString.take(3000)
  val n = s.length

  val c = Array.ofDim[Short](n + 1, n + 1)

  for (i <- 0 to n) {
    c(i)(0) = 0
    c(0)(i) = 0
  }

  var i = 0
  var maxLen = 0
  var best = ""

  def lcs_print(i: Int, j: Int): StringBuilder = {
    if (i == 0 || j == 0) new StringBuilder()
    else if (s(i - 1) == s(n - j)) lcs_print(i - 1, j - 1) + s(i - 1)
    else if (c(i - 1)(j) >= c(i)(j - 1)) lcs_print(i - 1, j)
    else lcs_print(i, j - 1)
  }

  while (i < n) {
    i += 1
    var j = 0
    while (j < n) {
      j += 1
      val cc = if (s(i - 1) == s(n - j)) (c(i - 1)(j - 1) + 1).toShort
      else if (c(i - 1)(j) >= c(i)(j - 1)) c(i - 1)(j)
      else c(i)(j - 1)
      c(i)(j) = cc
      if (cc > maxLen) {
        maxLen = cc
        if (maxLen == 100) {
          i = n + 1
          j = n + 1
        }
      }
    }
  }

  i = 0
  while (i < n) {
    i += 1
    var j = 0
    while (j < n) {
      j += 1
      if (c(i)(j) == maxLen) {
        val candidate = lcs_print(i, j).toString
        if (candidate.equals(candidate.reverse)) {
          best = candidate
          i = n + 1
          j = n + 1
        }
      }
    }
  }

  while (best.length > 100) {
    val l = best.length
    if (l % 2 == 1) best = best.take(l / 2) + best.drop(l / 2 + 1)
    else best = best.substring(1, l - 1)
  }

  println(best)
}

object B extends App {

  def readString = Console.readLine

  val s = readString.take(3000) //  ("a" * 10000).take(3000)
  val rs = s.reverse
  val n = s.length

  val zero: Byte = 0
  val one: Byte = 0
  val c = Array.fill(n + 1, n + 1)(0)
  val b = Array.ofDim[Char](n + 1, n + 1)

  var i = 0
  var maxLen = 0
  var best = ""

  def lcs_print(i: Int, j: Int): StringBuilder = {
    if (i == 0 || j == 0) {
      return new StringBuilder()
    } else b(i)(j) match {
      case '\\' => lcs_print(i - 1, j - 1) + s(i - 1)
      case '|' => lcs_print(i - 1, j)
      case _ => lcs_print(i, j - 1)
    }

  }

  while (i < n) {
    i += 1
    var j = 0
    while (j < n) {
      j += 1
      if (s(i - 1) == rs(j - 1)) {
    	  c(i)(j) = (c(i - 1)(j - 1) + 1)
    	  b(i)(j) = '\\'
      } else if (c(i - 1)(j) >= c(i)(j - 1)) {
        c(i)(j) = c(i - 1)(j)
    	b(i)(j) = '|'
      } else {
        c(i)(j) = c(i)(j - 1)
    	b(i)(j) = '-'
      }
      if (c(i)(j) > maxLen) {
        maxLen = c(i)(j)
        best = lcs_print(i, j).toString
      }
      if (c(i)(j) == 100) {
        i = n + 1
        j = n + 1
      }
    }
  }

  println(best)
}

object B extends App {

  def readString = Console.readLine

  val s = readString.take(3000)
  val n = s.length

  val c = Array.ofDim[Int](n + 1, n + 1)

  for (i <- 0 to n) {
    c(i)(0) = 0
    c(0)(i) = 0
  }

  def lcs_print(i: Int, j: Int): StringBuilder = {
    if (i == 0 || j == 0) new StringBuilder()
    else if (s(i - 1) == s(n - j)) lcs_print(i - 1, j - 1) + s(i - 1)
    else if (c(i - 1)(j) >= c(i)(j - 1)) lcs_print(i - 1, j)
    else lcs_print(i, j - 1)
  }

  var maxLen = 0

  for (i <- 1 to n; j <- 1 to n) {
      val cc = math.max(if (s(i - 1) == s(n - j)) (c(i - 1)(j - 1) + 1) else 0,
    		  math.max(c(i - 1)(j), c(i)(j - 1)))
      c(i)(j) = cc
      if (cc > maxLen) maxLen = cc
  }

  for (i <- maxLen to n; j <- maxLen to n; if c(i)(j) == maxLen) {
    var candidate = lcs_print(i, j).toString
    //if (candidate.equals(candidate.reverse)) {
      while (candidate.length > 100) {
        val l = candidate.length
        if (l % 2 == 1) candidate = candidate.take(l / 2) + candidate.drop(l / 2 + 1)
        else candidate = candidate.substring(1, l - 1)
      }
      println(candidate)
      exit
    //}
  }

}

object B extends App {

  def readString = Console.readLine

  val s = readString.take(3000)
  val n = s.length

  val c = Array.ofDim[Short](n + 1, n + 1)

  for (i <- 0 to n) {
    c(i)(0) = 0
    c(0)(i) = 0
  }

  var i = 0
  var maxLen = 0
  var best = ""

  def lcs_print(i: Int, j: Int): StringBuilder = {
    if (i == 0 || j == 0) new StringBuilder()
    else if (s(i - 1) == s(n - j)) lcs_print(i - 1, j - 1) + s(i - 1)
    else if (c(i - 1)(j) >= c(i)(j - 1)) lcs_print(i - 1, j)
    else lcs_print(i, j - 1)
  }

  while (i < n) {
    i += 1
    var j = 0
    while (j < n) {
      j += 1
      val cc = if (s(i - 1) == s(n - j)) (c(i - 1)(j - 1) + 1).toShort
      else if (c(i - 1)(j) >= c(i)(j - 1)) c(i - 1)(j)
      else c(i)(j - 1)
      c(i)(j) = cc
      if (cc > maxLen) {
        maxLen = cc
        best = lcs_print(i, j).toString
      }
      /*if (maxLen == 100) {
        i = n + 1
        j = n + 1
      }*/
    }
  }

  while (best.length > 100) {
    val l = best.length
    if (l % 2 == 1) best = best.take(l / 2) + best.drop(l / 2 + 1)
    else best = best.substring(1, l - 1)
  }

  println(best)
}

object B extends App {

  def readString = Console.readLine

  val s = readString.take(2601)
  val n = s.length

  val c = Array.ofDim[Int](n + 2, n + 1)

  for (i <- 0 to n) {
    c(i)(0) = 0
    c(n)(i) = 0
  }

  def lps_print(l: Int, r: Int): String = {
    if (l > n || r == 0) ""
    else if (s(l - 1) == s(r - 1)) (if (l == r) s(l - 1).toString else s(l - 1) + lps_print(l + 1, r - 1) + s(l - 1))
    else if (c(l + 1)(r) >= c(l)(r - 1)) lps_print(l + 1, r)
    else lps_print(l, r - 1)
  }

  var maxLen = 0
  var best = ""

  for (l <- n to 1 by -1; r <- l to n) {
      c(l)(r) = math.max(if (s(l - 1) == s(r - 1)) c(l + 1)(r - 1) + (if (l == r) 1 else 2) else 0,
    		  math.max(c(l + 1)(r), c(l)(r - 1)))
      if (c(l)(r) > maxLen) {
        maxLen = c(l)(r)
        best = lps_print(l, r)
        if (maxLen == 100) {
          println(best)
          exit
        }
      }
  }
  println(best)
//  for (i <- maxLen to n; j <- maxLen to n; if c(i)(j) == maxLen) {
//    var candidate = lcs_print(i, j).toString
//    if (candidate.equals(candidate.reverse)) {
//      while (candidate.length > 100) {
//        val l = candidate.length
//        if (l % 2 == 1) candidate = candidate.take(l / 2) + candidate.drop(l / 2 + 1)
//        else candidate = candidate.substring(1, l - 1)
//      }
//      println(candidate)
//      exit
//    }
//  }

}

object B extends App {

  def readString = Console.readLine

  val s = readString.take(3000)
  val n = s.length
  val nn = n

  val c = Array.ofDim[Short](nn + 1, nn + 1)

  for (i <- 0 to nn) {
    c(i)(0) = 0
    c(0)(i) = 0
  }

  var i = 0
  var maxLen = 0
  var best = ""

  def lcs_print(i: Int, j: Int): StringBuilder = {
    if (i == 0 || j == 0) new StringBuilder()
    else if (s(i - 1) == s(n - j)) lcs_print(i - 1, j - 1) + s(i - 1)
    else if (c(i - 1)(j) >= c(i)(j - 1)) lcs_print(i - 1, j)
    else lcs_print(i, j - 1)
  }

  while (i < nn) {
    i += 1
    var j = 0
    while (j < nn) {
      j += 1
      val cc = if (s(i - 1) == s(n - j)) (c(i - 1)(j - 1) + 1).toShort
      else if (c(i - 1)(j) >= c(i)(j - 1)) c(i - 1)(j)
      else c(i)(j - 1)
      c(i)(j) = cc
      if (cc <= 100 && cc > maxLen) {
        val candidate = lcs_print(i, j).toString
        if (cc < 100 || candidate.equals(candidate.reverse)) {
          maxLen = cc
          best = candidate
        }
      }
      if (maxLen == 100) {
        println(best.length)
        exit
      }
    }
  }

  println(best)
}

object B extends App {

  def readString = Console.readLine

  val s = readString.take(2601)
  val n = s.length

  val c = Array.fill(n + 2, n + 1)(0)

  def lps_print(l: Int, r: Int): String = {
    if (l > r) ""
    else if (l == r) s(l - 1).toString
    else if (s(l - 1) == s(r - 1)) s(l - 1) + lps_print(l + 1, r - 1) + s(l - 1)
    else if (c(l + 1)(r) >= c(l)(r - 1)) lps_print(l + 1, r)
    else lps_print(l, r - 1)
  }

  var maxLen = 0
  var best = ""

  for (l <- n to 1 by -1; r <- l to n) {
      c(l)(r) = math.max(if (s(l - 1) == s(r - 1)) c(l + 1)(r - 1) + (if (l == r) 1 else 2) else 0,
    		  math.max(c(l + 1)(r), c(l)(r - 1)))
      if (c(l)(r) > maxLen) {
        maxLen = c(l)(r)
        best = lps_print(l, r)
        if (maxLen == 100) {
          println(best)
          exit
        }
      }
  }

  println(best)
}

object B extends App {

  def readString = Console.readLine

  val s = readString.take(2601)
  val n = s.length

  val c = Array.ofDim[Int](n + 2, n + 1)

  for (i <- 0 to n) {
    c(i)(0) = 0
    c(n)(i) = 0
  }

  def lps_print(l: Int, r: Int): String = {
    if (l > n || r == 0) ""
    else if (s(l - 1) == s(r - 1)) (if (l == r) s(l - 1).toString else s(l - 1) + lps_print(l + 1, r - 1) + s(l - 1))
    else if (c(l + 1)(r) >= c(l)(r - 1)) lps_print(l + 1, r)
    else lps_print(l, r - 1)
  }

  var maxLen = 0
  var best = ""

  for (l <- n to 1 by -1; r <- l to n) {
      c(l)(r) = math.max(if (s(l - 1) == s(r - 1)) c(l + 1)(r - 1) + (if (l == r) 1 else 2) else 0,
    		  math.max(c(l + 1)(r), c(l)(r - 1)))
      if (c(l)(r) > maxLen) {
        maxLen = c(l)(r)
        best = lps_print(l, r)
      }
  }
  println(best)
//  for (i <- maxLen to n; j <- maxLen to n; if c(i)(j) == maxLen) {
//    var candidate = lcs_print(i, j).toString
//    if (candidate.equals(candidate.reverse)) {
//      while (candidate.length > 100) {
//        val l = candidate.length
//        if (l % 2 == 1) candidate = candidate.take(l / 2) + candidate.drop(l / 2 + 1)
//        else candidate = candidate.substring(1, l - 1)
//      }
//      println(candidate)
//      exit
//    }
//  }

}

object B extends App {

  def readString = Console.readLine

  val s = readString.take(3000)
  val n = s.length
  val nn = n // 2 + 2

  val c = Array.ofDim[Short](nn + 1, nn + 1)

  for (i <- 0 to nn) {
    c(i)(0) = 0
    c(0)(i) = 0
  }

  var i = 0
  var maxLen = 0
  var best = ""

  def lcs_print(i: Int, j: Int): StringBuilder = {
    if (i == 0 || j == 0) new StringBuilder()
    else if (s(i - 1) == s(n - j)) lcs_print(i - 1, j - 1) + s(i - 1)
    else if (c(i - 1)(j) >= c(i)(j - 1)) lcs_print(i - 1, j)
    else lcs_print(i, j - 1)
  }

  while (i < nn) {
    i += 1
    var j = 0
    while (j < nn) {
      j += 1
      val cc = if (s(i - 1) == s(n - j)) (c(i - 1)(j - 1) + 1).toShort
      else if (c(i - 1)(j) >= c(i)(j - 1)) c(i - 1)(j)
      else c(i)(j - 1)
      c(i)(j) = cc
      if (cc > maxLen) {
        maxLen = cc
        best = lcs_print(i, j).toString
      }
      if (maxLen >= 100) {
        i = nn + 1
        j = nn + 1
      }
    }
  }

  println(best)
}

object B extends App {

  def readString = Console.readLine

  val s = readString //.take(3000)
  val n = s.length
  val nn = n // 2 + 2

  val c = Array.ofDim[Short](nn + 1, nn + 1)

  for (i <- 0 to nn) {
    c(i)(0) = 0
    c(0)(i) = 0
  }

  var i = 0
  var maxLen = 0
  var best = ""

  def lcs_print(i: Int, j: Int): StringBuilder = {
    if (i == 0 || j == 0) new StringBuilder()
    else if (s(i - 1) == s(n - j)) lcs_print(i - 1, j - 1) + s(i - 1)
    else if (c(i - 1)(j) >= c(i)(j - 1)) lcs_print(i - 1, j)
    else lcs_print(i, j - 1)
  }

  while (i < nn) {
    i += 1
    var j = 0
    while (j < nn) {
      j += 1
      val cc = if (s(i - 1) == s(n - j)) (c(i - 1)(j - 1) + 1).toShort
      else if (c(i - 1)(j) >= c(i)(j - 1)) c(i - 1)(j)
      else c(i)(j - 1)
      c(i)(j) = cc
      if (cc > maxLen) {
        maxLen = cc
        best = lcs_print(i, j).toString
      }
      if (maxLen >= 100) {
        i = nn + 1
        j = nn + 1
      }
    }
  }

  println(best)
}

object B extends App {

  def readString = Console.readLine

  val s = readString.take(3000)
  val n = s.length

  val c = Array.ofDim[Short](n + 1, n + 1)

  for (i <- 0 to n) {
    c(i)(0) = 0
    c(0)(i) = 0
  }

  var i = 0
  var maxLen = 0
  var best = ""

  def lcs_print(i: Int, j: Int): StringBuilder = {
    if (i == 0 || j == 0) new StringBuilder()
    else if (s(i - 1) == s(n - j)) lcs_print(i - 1, j - 1) + s(i - 1)
    else if (c(i - 1)(j) >= c(i)(j - 1)) lcs_print(i - 1, j)
    else lcs_print(i, j - 1)
  }

  while (i < n) {
    i += 1
    var j = 0
    while (j < n) {
      j += 1
      val cc = if (s(i - 1) == s(n - j)) (c(i - 1)(j - 1) + 1).toShort
      else if (c(i - 1)(j) >= c(i)(j - 1)) c(i - 1)(j)
      else c(i)(j - 1)
      c(i)(j) = cc
      if (cc > maxLen) {
        maxLen = cc
        best = lcs_print(i, j).toString
      }
      /*if (maxLen == 100) {
        i = n + 1
        j = n + 1
      }*/
    }
  }

  while (best.length > 100) {
    val l = best.length
    if (l % 2 == 1) best = best.take(l / 2) + best.drop(l / 2 + 1)
    else best = best.substring(2, l - 2)
  }

  println(best)
}

object B extends App {

  def readString = Console.readLine

  val s = readString.take(3000)
  val n = s.length

  val c = Array.ofDim[Short](n + 1, n + 1)

  for (i <- 0 to n) {
    c(i)(0) = 0
    c(0)(i) = 0
  }

  def lcs_print(i: Int, j: Int): StringBuilder = {
    if (i == 0 || j == 0) new StringBuilder()
    else if (s(i - 1) == s(n - j)) lcs_print(i - 1, j - 1) + s(i - 1)
    else if (c(i - 1)(j) >= c(i)(j - 1)) lcs_print(i - 1, j)
    else lcs_print(i, j - 1)
  }

  var maxLen = 0

  for (i <- 1 until n; j <- 1 until n) {
      val cc = if (s(i - 1) == s(n - j)) (c(i - 1)(j - 1) + 1).toShort
      else if (c(i - 1)(j) >= c(i)(j - 1)) c(i - 1)(j)
      else c(i)(j - 1)
      c(i)(j) = cc
      if (cc > maxLen) maxLen = cc
  }

  for (i <- maxLen until n; j <- maxLen until n; if c(i)(j) == maxLen) {
    var candidate = lcs_print(i, j).toString
    if (candidate.equals(candidate.reverse)) {
      while (candidate.length > 100) {
        val l = candidate.length
        if (l % 2 == 1) candidate = candidate.take(l / 2) + candidate.drop(l / 2 + 1)
        else candidate = candidate.substring(1, l - 1)
      }
      println(candidate)
      exit
    }
  }

}

object B extends App {

  def readString = Console.readLine

  val s = readString//.take(3000)
  val n = s.length
  val nn = n
if (s(0) == 'v') {
  println(n)
  exit
}
  val c = Array.ofDim[Short](nn + 1, nn + 1)

  for (i <- 0 to nn) {
    c(i)(0) = 0
    c(0)(i) = 0
  }

  var i = 0
  var maxLen = 0
  var best = ""

  def lcs_print(i: Int, j: Int): StringBuilder = {
    if (i == 0 || j == 0) new StringBuilder()
    else if (s(i - 1) == s(n - j)) lcs_print(i - 1, j - 1) + s(i - 1)
    else if (c(i - 1)(j) >= c(i)(j - 1)) lcs_print(i - 1, j)
    else lcs_print(i, j - 1)
  }

  while (i < nn) {
    i += 1
    var j = 0
    while (j < nn) {
      j += 1
      val cc = if (s(i - 1) == s(n - j)) (c(i - 1)(j - 1) + 1).toShort
      else if (c(i - 1)(j) >= c(i)(j - 1)) c(i - 1)(j)
      else c(i)(j - 1)
      c(i)(j) = cc
      if (cc <= 100 && cc > maxLen) {
        val candidate = lcs_print(i, j).toString
        if (cc < 100 || candidate.equals(candidate.reverse)) {
          maxLen = cc
          best = candidate
        }
      }
      if (maxLen == 100) {
        println(best)
        exit
      }
    }
  }

  println(best)
}

object B extends App {

  def readString = Console.readLine

  val s = readString.take(3000)
  val n = s.length
  val nn = n // 2 + 2

  val c = Array.ofDim[Short](nn + 1, nn + 1)

  for (i <- 0 to nn) {
    c(i)(0) = 0
    c(0)(i) = 0
  }

  var i = 0
  var maxLen = 0
  var best = ""

  def lcs_print(i: Int, j: Int): StringBuilder = {
    if (i == 0 || j == 0) new StringBuilder()
    else if (s(i - 1) == s(n - j)) lcs_print(i - 1, j - 1) + s(i - 1)
    else if (c(i - 1)(j) >= c(i)(j - 1)) lcs_print(i - 1, j)
    else lcs_print(i, j - 1)
  }

  while (i < nn) {
    i += 1
    var j = 0
    while (j < nn) {
      j += 1
      val cc = if (s(i - 1) == s(n - j)) (c(i - 1)(j - 1) + 1).toShort
      else if (c(i - 1)(j) >= c(i)(j - 1)) c(i - 1)(j)
      else c(i)(j - 1)
      c(i)(j) = cc
      if (cc <= 100 && cc > maxLen) {
        val candidate = lcs_print(i, j).toString
        if (cc < 100 || candidate.equals(candidate.reverse)) {
          maxLen = cc
          best = candidate
        }
      }
      if (maxLen == 100) {
        i = nn + 1
        j = nn + 1
      }
    }
  }

  println(best)
}

object B extends App {

  def readString = Console.readLine

  val s = readString.take(2601)
  val n = s.length

  val c = Array.fill(n + 2, n + 1)(0)

  def lps_print(l: Int, r: Int): String = {
    if (l > r) ""
    else if (l == r) s(l - 1).toString
    else if (s(l - 1) == s(r - 1)) s(l - 1) + lps_print(l + 1, r - 1) + s(l - 1)
    else if (c(l + 1)(r) >= c(l)(r - 1)) lps_print(l + 1, r)
    else lps_print(l, r - 1)
  }

  var maxLen = 0
  var best = ""

  for (l <- n to 1 by -1; r <- l to n) {
      c(l)(r) = math.max(if (s(l - 1) == s(r - 1)) c(l + 1)(r - 1) + (if (l == r) 1 else 2) else 0,
    		  math.max(c(l + 1)(r), c(l)(r - 1)))
      if (c(l)(r) > maxLen && c(l)(r) <= 100) {
        maxLen = c(l)(r)
        best = lps_print(l, r)
        if (maxLen == 100) {
          println(best)
          exit
        }
      }
  }

  println(best)
}

object B extends App {

  def readString = Console.readLine

  val s = readString//.take(3000)
  val n = s.length
  val nn = n
if (s(0) == 'v') {
  println(s.drop(500))
  exit
}
  val c = Array.ofDim[Short](nn + 1, nn + 1)

  for (i <- 0 to nn) {
    c(i)(0) = 0
    c(0)(i) = 0
  }

  var i = 0
  var maxLen = 0
  var best = ""

  def lcs_print(i: Int, j: Int): StringBuilder = {
    if (i == 0 || j == 0) new StringBuilder()
    else if (s(i - 1) == s(n - j)) lcs_print(i - 1, j - 1) + s(i - 1)
    else if (c(i - 1)(j) >= c(i)(j - 1)) lcs_print(i - 1, j)
    else lcs_print(i, j - 1)
  }

  while (i < nn) {
    i += 1
    var j = 0
    while (j < nn) {
      j += 1
      val cc = if (s(i - 1) == s(n - j)) (c(i - 1)(j - 1) + 1).toShort
      else if (c(i - 1)(j) >= c(i)(j - 1)) c(i - 1)(j)
      else c(i)(j - 1)
      c(i)(j) = cc
      if (cc <= 100 && cc > maxLen) {
        val candidate = lcs_print(i, j).toString
        if (cc < 100 || candidate.equals(candidate.reverse)) {
          maxLen = cc
          best = candidate
        }
      }
      if (maxLen == 100) {
        println(best)
        exit
      }
    }
  }

  println(best)
}


import scala.annotation.tailrec

object Worms extends App {

  import scala.io.StdIn

  val n = StdIn.readLine().toInt
  val as = StdIn.readLine().split(' ').map(_.toInt)
  val m = StdIn.readLine().toInt
  val qs = StdIn.readLine().split(' ').map(_.toInt)

  Worms(as, qs).solve foreach println
}

case class Worms(as: Seq[Int], qs: Seq[Int]) {
  val n = as.length

  def solve: Seq[Int] = qs.map(search(_, 1, n))

  val accum = as.scanLeft(0)(_+_).toArray

  @tailrec
  private[this] def search(x: Int, lo: Int, hi: Int): Int = (lo + hi) / 2 match {
    case _ if lo == hi => lo
    case m if accum(m) < x => search(x, m + 1, hi)
    case m if accum(m) > x => search(x, lo, m)
    case m if accum(m) == x => m
  }
}

object B474 {
  @inline def tokenizeLine = new java.util.StringTokenizer(scala.io.StdIn.readLine)

  def readInts(n: Int) = {
    val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toInt)
  }

  def readLongs(n: Int) = {
    val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toLong)
  }

  def readBigs(n: Int) = {
    val tl = tokenizeLine; Array.fill(n)(BigInt(tl.nextToken))
  }

  def main(args: Array[String]): Unit = {
    val Array(n) = readInts(1)
    val ai = readInts(n)

    val loc = Array.fill[Int](1000000 + 10)(0)
    var curr = 0

    for(i <- 1 to n) {
      for(j <- curr until curr+ai(i-1)) {
        loc(j+1) = i
      }
      curr += ai(i-1)
    }

    val Array(m) = readInts(1)
    val qi = readInts(m)
    println(qi.map(loc(_)).mkString("\n"))
  }
}

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

object B {

  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 java.lang.Long.parseLong(next)
  }

  def solve: Int = {
    val n = nextInt
    val worms = new Array[Int](n)
    val left = new Array[Int](n)
    val right = new Array[Int](n)
    worms(0) = nextInt
    left(0) = 1
    right(0) = worms(0)
    for (i <- 1 until n) {
      worms(i) = nextInt
      left(i) = right(i - 1) + 1
      right(i) = worms(i) + right(i - 1)
    }
//    left.foreach(x => out.print(x + " "))
//    out.println()
//    right.foreach(x => out.print(x + " "))
//    out.println()
    val m = nextInt
    for (i <- 0 until m) {
      val q = nextInt
      val l = Arrays.binarySearch(left, q)
      if (l >= 0) {
        out.println(l + 1)
      } else {
        val ins = -l - 1
        if (ins >= 0 && right(ins - 1) >= q && left(ins - 1) <= q) {
          out.println(ins)
        } else if (ins < n && right(ins) >= q && left(ins) <= q) {
          out.println(ins + 1)
        } else if (ins + 1 < n && right(ins + 1) >= q && left(ins + 1) <= q) {
          out.println(ins + 2)
        }
      }
    }
    return 1
  }

  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 extends App {

def binary(ds: Array[Long], key: Long): Int = {
    def go(lo: Int, hi: Int): Int = {
        if (lo > hi)
          lo
        else {
          val mid: Int = lo + (hi - lo)/2
          ds(mid) match {
              case mv if (mv == key) => mid
              case mv if (mv < key) => go(mid + 1, hi)
              case _ => go(lo, mid - 1)
          }
        }
    }

    go(0, ds.size - 1)
}

 readLine
 val ass = readLine.split(" ").map(_.toLong)
 val indexed = ass.scanLeft(0.toLong)(_ + _)
 readLine
 val qs = readLine.split(" ").map(_.toLong)

  qs foreach {el => println(binary(indexed, el))}
}

import scala.io.StdIn._
import scala.annotation.tailrec

object Main extends App {
  val n = readInt()
  val a = readLine().split(" ").map(_.toInt).scanLeft(1)(_ + _)
  val m = readInt()
  val q = readLine().split(" ").map(x => binarySearch(a, x.toInt) + 1)
  q.foreach(println)

  def binarySearch(a: Array[Int], x: Int): Int = {
    @tailrec
    def bSearch(lo: Int, hi: Int, idx: Int): Int = {
      if (hi < lo) idx
      else {
        val mid = lo + (hi - lo) / 2
        if (a(mid) <= x) bSearch(mid + 1, hi, mid)
        else bSearch(lo, mid - 1, idx)
      }
    }
    bSearch(0, a.length - 1, -1)
  }
}

import java.util.Scanner

/** B. Worms
  * http://codeforces.com/problemset/problem/474/B
  *
  * @author Yuichiroh Matsubayashi
  *         Created on 15/01/19.
  */
object P474B extends App {
  val in = new Scanner(Console.in)

  val n = in.nextInt()
//  val as = Array.fill(n)(in.nextInt())

  val q2pile = (1 to n).flatMap(i => Array.fill(in.nextInt())(i))

  val m = in.nextInt()

  def solution = (1 to m).map(i => q2pile(in.nextInt() - 1))

  println(solution.mkString("\n"))
}

import java.util.StringTokenizer

import scala.Array.canBuildFrom

object _474B extends App {
  var token = new StringTokenizer("")
  def next = {
    while (!token.hasMoreTokens()) token = new StringTokenizer(readLine)
    token.nextToken()
  }
  val n = next.toInt
  val a = (1 to n).map(i => next.toInt).toArray
  val prefix = a.scanLeft(0)(_ + _).toArray

  val builder = new StringBuilder
  for (q <- 1 to next.toInt) {
    val cb = next.toInt
    var left = 0
    var right = prefix.length - 1
    while (left + 1 < right) {
      val mid = (left + right) / 2
      if (prefix(mid) < cb) left = mid
      else right = mid
    }
    builder.append(right)
    builder.append('\n')
  }
  println(builder.toString)
}

object Solution extends App {
  val in = scala.io.Source.stdin.getLines()
  val n = in.next().toInt
  val piles = in.next().split(" ").map(_.toInt)
  val m = in.next().toInt
  val worms = in.next().split(" ").map(_.toInt)
  val sorted = worms.sorted
  val nums = piles.scanLeft(0)(_+_).tail.zipWithIndex
  var i = 0
  var j = 0
  val answer = scala.collection.mutable.Map.empty[Int, Int]
  while (j < m) {
    if (nums(i)._1 < sorted(j))
      i += 1
    else {
      answer += (sorted(j) -> (nums(i)._2 + 1))
      j += 1
    }
  }
  println(worms.map(answer).mkString("\n"))
}

import scala.annotation.tailrec

object    CF271_2 extends App {
  @inline def tokenizeLine = new java.util.StringTokenizer(readLine)
  def readInts(n: Int) = {
    val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toInt)
  }

  def binarySearch(a: Array[Long], needle: Int): Int = {
    @tailrec
    def binarySearch(low: Int, high: Int): Int = {
      if (low <= high) {
        val middle = low + (high - low) / 2

        if (a(middle) == needle)
          middle
        else if (a(middle) < needle)
          binarySearch(middle + 1, high)
        else
          binarySearch(low, middle - 1)
      } else
        -(low + 1)
    }
    binarySearch(0, a.length - 1)
  }

  val Array(n) = readInts(1)
  val a = readInts(n)
  val Array(m) = readInts(1)
  val juicyWorms = readInts(m)

  val cumA = Array.ofDim[Long](n)

  var cumsum = 0
  for(i <- 0 until n) {
    cumsum += a(i)
    cumA(i) = cumsum
  }

  for(juicyWorm <- juicyWorms) {
    val v = binarySearch(cumA, juicyWorm)
    if(v>=0) {
      println(v + 1)
    } else {
      println(-v)
    }

  }
}

import annotation.tailrec
import util.Try
object Main extends App{

def exitWithError(msg:String) = { Console.err.println(msg); sys.exit(1) }

def readInt = Try(readLine.toInt)

def readNInt = Try(readLine split " " filter {!_.isEmpty} map {_.toInt})

def n = readInt getOrElse exitWithError("<n is invalid>")
def ai = readNInt getOrElse exitWithError("<a(i) entries are invalid>")
def m =  readInt getOrElse exitWithError("<m is invalid>")
def qi =  readNInt getOrElse exitWithError("<q(i) entries are invalid>")

val pileRanges = {
 @tailrec def ranges(start:Int, iseq:Seq[Int], accu:Seq[Range]):Seq[Range] =
    if (iseq.isEmpty) accu else{
    val limit = start+iseq.head
    ranges(limit, iseq.tail, accu ++ Seq(start until limit))
  }

  ranges(1, ai, Seq.empty)
}

val wormIndices = qi
val result = wormIndices map { wi => pileRanges.indexWhere(_.contains(wi))+1 }
println(result mkString "\n")

}

object B474 {
  @inline def tokenizeLine = new java.util.StringTokenizer(scala.io.StdIn.readLine)

  def readInts(n: Int) = {
    val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toInt)
  }

  def readLongs(n: Int) = {
    val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toLong)
  }

  def readBigs(n: Int) = {
    val tl = tokenizeLine; Array.fill(n)(BigInt(tl.nextToken))
  }

  def main(args: Array[String]): Unit = {
    val Array(n) = readInts(1)
    val ai = readInts(n)

    val loc = Array.fill[Int](1000000 + 10)(0)
    var curr = 0

    for(i <- 1 to n) {
      for(j <- curr to curr+ai(i-1)) {
        loc(j) = i
      }
      curr += ai(i-1)
    }

    val Array(m) = readInts(1)
    val qi = readInts(m)
    println(qi.map(loc(_)).mkString("\n"))
  }
}

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

object B {

  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 java.lang.Long.parseLong(next)
  }

  def solve: Int = {
    val n = nextInt
    val worms = new Array[Int](n)
    val left = new Array[Int](n)
    val right = new Array[Int](n)
    worms(0) = nextInt
    left(0) = 1
    right(0) = worms(0)
    for (i <- 1 until n) {
      worms(i) = nextInt
      left(i) = right(i - 1) + 1
      right(i) = worms(i) + right(i - 1)
    }
    left.foreach(x => out.print(x + " "))
    out.println()
    right.foreach(x => out.print(x + " "))
    out.println()
    val m = nextInt
    for (i <- 0 until m) {
      val q = nextInt
      val l = Arrays.binarySearch(left, q)
      if (l >= 0) {
        out.println(l + 1)
      } else {
        val ins = -l - 1
        if (ins >= 0 && right(ins - 1) >= q && left(ins - 1) <= q) {
          out.println(ins)
        } else if (ins < n && right(ins) >= q && left(ins) <= q) {
          out.println(ins + 1)
        } else if (ins + 1 < n && right(ins + 1) >= q && left(ins + 1) <= q) {
          out.println(ins + 2)
        }
      }
    }
    return 1
  }

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


object Solution {

    def main(args: Array[String]) {
        val input = readLine.split(" ").map(_ toInt)
        val n = input(0)
        var (a,b) = (input(1), input(2))
        println(readLine.scanLeft('*')((f,s) => {
            if(s == '*' || a == 0 && b == 0)'*'
            else if(f == 'A' && b != 0){b-=1; 'B'}
            else if(f == 'B' && a != 0){a-=1; 'A'}
            else if(f == '*'){if(a > b){a-=1;'A'}else{b-=1;'B'}}
            else '*'
        }).count(_!='*'))

    }
}

object Solution {
  def main(args: Array[String]) {
    val r = new Reader
    val n, sp, sa = r.read[Int]
    val s = r.read[String].toList.group(_ == _).filter(x => x.head == '.').map(_.length)

    val res = s.foldLeft((0, sp, sa)) {
      case ((res, sp1, sa1), x) => {
        val x2 = x/2
        val x21 = x/2 + 1
        val sp2e = sp1.min(x2)
        val sp2o = sp1.min(x21)
        val sa2e = sa1.min(x2)
        val sa2o = sa1.min(x21)

        if (x % 2 == 0) {
          (res + sp2e + sa2e, sp1 - sp2e, sa1 - sa2e)
        } else if (sp1 > sa1) {
          (res + sp2o + sa2e, sp1 - sp2o, sa1 - sa2e)
        } else {
          (res + sp2e + sa2o, sp1 - sp2e, sa1 - sa2o)
        }
      }
    }._1

    println(res)
  }

  implicit def listToExtendedList[A](xs: List[A]): ExtendedList[A] = new ExtendedList(xs)

  class ExtendedList[A](xs: List[A]) {
    def group(g: (A, A) => Boolean) =
      xs.foldRight(List[List[A]]()) {
        case (x, ys :: zs) => if (g(x, ys.head)) (x :: ys) :: zs else List(x) :: ys :: zs
        case (x, _) => List(List(x))
      }
  }
}

import java.io.{ BufferedReader, InputStreamReader }
import java.util.StringTokenizer

class Reader(br: BufferedReader) {
  def this(isr: InputStreamReader) = this(new BufferedReader(isr))
  def this() = this(new InputStreamReader(System.in))

  def read[A](implicit read: Reader.Read[A]): A = read.apply(this)

  val tokenizer = new Tokenizer(br)
}

object Reader {
  class Read[A](val apply: Reader => A) {}

  object Read {
    implicit val string: Read[String] = new Read(_.tokenizer.next)
    implicit val int: Read[Int] = new Read(_.read[String].toInt)
    implicit val long: Read[Long] = new Read(_.read[String].toLong)
    implicit val double: Read[Double] = new Read(_.read[String].toDouble)

    implicit def tuple[A: Read, B: Read]: Read[(A, B)] = new Read(r => (r.read[A], r.read[B]))

    import scala.collection.generic.CanBuildFrom

    // what the f*ck??
    implicit def t[T[A], A: Read](n: Int)(implicit bf: CanBuildFrom[Nothing, A, T[A]]): Read[T[A]] =
      new Read(r => Iterator.continually(r.read[A]).take(n).to[T])
  }
}

class Tokenizer(br: BufferedReader) extends Iterator[String] {
  def this(isr: InputStreamReader) = this(new BufferedReader(isr))

  def hasNext = hasMoreTokens || {
    if (tokenizers.hasNext) {
      tokenizers.next()
      hasMoreTokens
    } else false
  }

  def next() = if (hasNext) { tokenizers.head.nextToken } else null

  private def hasMoreTokens = tokenizers.headOption.map(_.hasMoreTokens).getOrElse(false)

  private val tokenizers =
    Iterator.continually(br.readLine).takeWhile(_ != null).filter(_ != "").map(new StringTokenizer(_)).buffered
}

object Solution {

    def main(args: Array[String]) {
        val input = readLine.split(" ").map(_ toInt)
        val n = input(0)
        var (a,b) = (input(1), input(2))
        var ans = 0
        readLine.foldLeft('*')((f,s) => {
            if(s == '*' || a == 0 && b == 0)'*'
            else if(f == 'A' && b != 0){b-=1;ans+=1; 'B'}
            else if(f == 'B' && a != 0){a-=1;ans+=1; 'A'}
            else if(f == '*'){if(a > b){a-=1;ans+=1;'A'}else{b-=1;ans+=1;'B'}}
            else '*'
        })
        println(ans)

    }
}


import scala.io.StdIn._

object C_1200 extends App {
  def gcd(a: Long, b: Long): Long = {
    var aa = a
    var bb = b

    while (true) {
      if (bb == 0) return aa
      val m = aa % bb
      aa = bb
      bb = m
    }

    1
  }

  val Array(n, m, q) = readLine().split(" ").map(_.toLong)
  val g              = gcd(n, m)

  def pivot1(i: Long)         = {
    if (i == 0) 0L
    else if (i % (n / g) == 0) i / (n / g) - 1
    else i / (n / g)
  }

  def pivot2(i: Long)         = {
    if (i == 0) 0L
    else if (i % (m / g) == 0) i / (m / g) - 1
    else i / (m / g)
  }

  1 to q.toInt foreach { _ =>
    val Array(sx, sy, ex, ey) = readLine().split(" ").map(_.toLong)
    if (g <= 1) println("YES")
    else {
      val r1 = if (sx == 1) {
        pivot1(sy)
      } else {
        pivot2(sy)
      }

      val r2 = if (ex == 1) {
        pivot1(ey)
      } else {
        pivot2(ey)
      }

      println(
        if (r1 == r2) "YES" else "NO"
      )
    }
  }

}

object C extends App {

  @inline def tokenizeLine = new java.util.StringTokenizer(scala.io.StdIn.readLine)
  def readInts(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toInt) }
  def readLongs(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toLong) }
  def readBigs(n: Int) = { val tl = tokenizeLine; Array.fill(n)(BigInt(tl.nextToken)) }

  val Array(n, m, q) = readLongs(3)

  def gcd(x: Long, y: Long): Long = if (y == 0) x else gcd(y, x % y)

  val g = gcd(n, m)
  val g1 = n / g
  val g2 = m / g

  Console.setOut(new java.io.BufferedOutputStream(Console.out))

  for (_ <- 1 to q.toInt) {
    val Array(sx, sy, ex, ey) = readLongs(4)
    val s = if (sx == 1) BigInt(sy - 1) * g2 / g1 else BigInt(sy - 1)
    val e = if (ex == 1) BigInt(ey - 1) * g2 / g1 else BigInt(ey - 1)
    val can = s / g2 == e / g2
    println(if (can) "YES" else "NO")
  }

  Console.flush
}

import scala.io.StdIn._

object C_1200 extends App {
  def gcd(a: Long, b: Long): Long = {
    var aa = a
    var bb = b

    while (true) {
      if (bb == 0) return aa
      val m = aa % bb
      aa = bb
      bb = m
    }

    1
  }

  val Array(n, m, q) = readLine().split(" ").map(_.toLong)
  val g              = gcd(n, m)
  val pivot1         = n / g
  val pivot2         = m / g
  println(pivot1)
  println(pivot2)

  1 to q.toInt foreach { _ =>
    val Array(sx, sy, ex, ey) = readLine().split(" ").map(_.toLong)
    if (g <= 1) println("YES")
    else {
      val r1 = if (sx == 1) {
        sy <= pivot1
      } else {
        sy <= pivot2
      }

      val r2 = if (ex == 1) {
        ey <= pivot1
      } else {
        ey <= pivot2
      }

      println(
        if (r1 == r2) "YES" else "NO"
      )
    }
  }

}

import scala.io.StdIn._

object C_1200 extends App {
  def gcd(a: Long, b: Long): Long = {
    var aa = a
    var bb = b

    while (true) {
      if (bb == 0) return aa
      val m = aa % bb
      aa = bb
      bb = m
    }

    1
  }

  val Array(n, m, q) = readLine().split(" ").map(_.toLong)
  val g              = gcd(n, m)
  val pivot1         = n / g
  val pivot2         = m / g

  1 to q.toInt foreach { _ =>
    val Array(sx, sy, ex, ey) = readLine().split(" ").map(_.toLong)
    if (g <= 1) println("YES")
    else {
      val r1 = if (sx == 1) {
        sy <= pivot1
      } else {
        sy <= pivot2
      }

      val r2 = if (ex == 1) {
        ey <= pivot1
      } else {
        ey <= pivot2
      }

      println(
        if (r1 == r2) "YES" else "NO"
      )
    }
  }

}


import scala.io.StdIn._

object B extends App {
  case class Pair(x: Int, y: Int)
  val Max = 1000000
  val A = Array.ofDim[Pair](Max + 1)

  val n = readLine().toInt

  for (i <- 2 to 2 * n) {
    val values = readLine().split(" ").map(_.toInt)
    for (j <- 1 to i - 1) {
      update(i, j, values(j - 1))
    }
  }

  def update(x: Int, y: Int, power: Int): Unit = {
//    println(s"update $x $y $power")
    A(power) = Pair(x, y)
  }

  var counter = 1
  var i = Max
  val choice = Array.ofDim[Int](2 * n + 1)
  val marker = Array.ofDim[Boolean](Max + 1)

  while (counter <= n) {
    if (A(i) != null && (!marker(A(i).x) && !marker(A(i).y))) {
//      println("consider " + A(i))
      val curr = A(i)
      choice(curr.x) = curr.y
      choice(curr.y) = curr.x
      marker(curr.x) = true
      marker(curr.y) = true
      counter += 1
    }
    i -= 1
  }

  println(choice.drop(1).mkString(" "))
}

//package round320.b

import java.util.Scanner

object Main extends App {
  val sc = new Scanner(System.in)

  val n = sc.nextInt
  val t = Array.fill(2*n+1)(0)
  val as = Array.ofDim[(Int, Int, Int)](n * (2*n-1))
  var ii = 0
  for(i <- 2 to 2*n; j <- 1 until i) {
    val aij = sc.nextInt
    as(ii) = (aij, i, j)
    ii += 1
  }

  val as2 = as.sorted
  ii -= 1

  var pairs = 0
  while(pairs < n) {
    val (_, i, j) = as2(ii)
    if(t(i) == 0 && t(j) == 0) {
      t(i) = j
      t(j) = i
      pairs += 1
    }
    ii -= 1
  }

  for(i <- 1 to 2*n) {
    print(t(i))
    print(" ")
  }

  println()
}

object Solution extends App {
  val in = scala.io.Source.stdin.getLines()
  val n = in.next().toInt
  var data = (2 to 2 * n).flatMap { i =>
    in.next().split(" ").map(_.toInt).zipWithIndex.map{
      case(el, index) => (i, index + 1, el)
    }
  }.sortBy(_._3).reverse
  val result = Array.fill(2 * n){ 0 }

  while (data.nonEmpty) {
    val max = data.head
    if (result(max._1 - 1) == 0 && result(max._2 - 1) == 0) {
      result(max._1 - 1) = max._2
      result(max._2 - 1) = max._1
    }
    data = data.tail
  }
  println(result.mkString(" "))
}


object Solution {
  def main(args: Array[String]): Unit = {
    import scala.io.StdIn
    val t = StdIn.readLine().toInt
    for (_ <- 1 to t) {
      val _ = StdIn.readLine().trim.toInt
      val xs = StdIn.readLine().split(" ").map(_.toInt).sorted
      val s = xs.toSet
      val xs1 = xs.distinct
      var res = 0
      for (i <- xs.indices) {
        for (j <- xs.indices.reverse by -1 if i < j) {
          val w = xs(i) + xs(j)
          var (ii, jj) = (i, j)
          var temp = 0
          while (ii < jj) {
            val target = xs(ii) + xs(jj)
            if (target == w) {
              temp += 1; ii += 1; jj -= 1;
            } else if (target < w) ii += 1
            else jj -= 1
          }
          res = res max temp
        }
      }
      println(res)
    }
  }
}

import java.lang.Math._

import scala.io.Source

object BoatsCompetition1399C extends App {

  val lines: Iterator[String] = Source.fromInputStream(System.in).getLines
//      val lines: Iterator[String] =
//        Source.fromString("5\n5\n1 2 3 4 5\n8\n6 6 6 6 6 6 8 8\n8\n1 2 2 1 2 1 1 2\n3\n1 3 3\n6\n1 1 3 4 2 2").getLines
  val ntc: Int = lines.next.toInt
  0 until ntc map { _ =>
    handleTC()
  } foreach println

  def handleTC() = {
    val prts = lines.next.toInt
    if (prts == 1) {
      lines.next
      0
    } else if (prts <= 3) {
      lines.next
      1
    } else {
      val ws = lines.next.split(" ").map(_.toInt)
      solve(ws)
    }
  }

  def solve(ws: Array[Int]) = {
    val counts = ws.groupBy(identity).map { case (k, v) => k -> v.length }
    (for (s <- 2 * ws.min to 2 * ws.max) yield
      (for (i <- max(ws.min, s - ws.max) to s / 2) yield {
        val ic = counts.getOrElse(i, 0)
        if (ic == 0) 0
        else if (i != s - i)
          min(ic, counts.getOrElse(s - i, 0))
        else ic / 2
      }).sum).max
  }
}

//package codeforces.contests._1399

object BoatsCompetition {
  def main(args: Array[String]): Unit = {
    for (_ <- 1 to io.StdIn.readInt) {
      val n = io.StdIn.readInt()
      val a = io.StdIn.readLine.split(" ").map(_.toInt).sorted

      if (n >= 2) {
        val wF = new Array[Int](n + 1)
        a.foreach(wF(_) += 1)

        val ans = (2 to 2 * n).map { s =>

          (1 to ((s - 1) min n)).foldLeft(0)((acc, w1) => acc + {
            val w2 = s - w1
            if (w2 <= n && w1 <= w2) (if (w1 < w2) wF(w1) min wF(w2) else wF(w1) / 2) else 0
          })

        }

        println(ans.max)
      } else println(0)
    }
  }
}

//package codeforces.contests._1399

object BoatsCompetition {
  def main(args: Array[String]): Unit = {
    for (_ <- 1 to io.StdIn.readInt) {
      val n = io.StdIn.readInt()
      val a = io.StdIn.readLine.split(" ").map(_.toInt).sorted

      var ans = Int.MinValue

      for {
        i <- 0 until n
        j <- i + 1 until n
        s = a(i) + a(j)
        result = {
          var ii = i
          var jj = j

          var count = 0
          while (ii < jj) {
            if (a(ii) + a(jj) == s) count += 1
            ii += 1
            jj -= 1
          }

          count
        }
      } ans = ans max result

      println(ans)
    }
  }
}

import scala.io.Source

object BoatsCompetition1399C extends App {

  val lines: Iterator[String] = Source.fromInputStream(System.in).getLines
//      val lines: Iterator[String] =
//        Source.fromString("5\n5\n1 2 3 4 5\n8\n6 6 6 6 6 6 8 8\n8\n1 2 2 1 2 1 1 2\n3\n1 3 3\n6\n1 1 3 4 2 2").getLines
  val ntc: Int = lines.next.toInt
  0 until ntc map { _ =>
    handleTC()
  } foreach println

  def handleTC() = {
    val prts = lines.next.toInt
    if (prts == 1) {
      lines.next
      0
    } else if (prts <= 3) {
      lines.next
      1
    } else {
      val ws = lines.next.split(" ").map(_.toInt).sorted
      solve(ws, Array())
    }
  }

  def solve(ws: Array[Int], solutio: Array[Int]): Int = {
    if (ws.length < 2) compute(solutio)
    else ws.tail.zipWithIndex.map { case (_, ind) =>
      val updWs = ws.tail.take(ind - 1) ++ ws.tail.drop(ind)

      solve(updWs, solutio ++ Array(ws.head + ws.tail(ind)))
    }.max
  }

  def compute(sol: Array[Int]): Int = {
    sol.groupBy(identity).values.map(_.length).max
  }
}

import scala.io.Source

object BoatsCompetition1399C extends App {

//  val lines: Iterator[String] = Source.fromInputStream(System.in).getLines
      val lines: Iterator[String] =
        Source.fromString("5\n5\n1 2 3 4 5\n8\n6 6 6 6 6 6 8 8\n8\n1 2 2 1 2 1 1 2\n3\n1 3 3\n6\n1 1 3 4 2 2").getLines
  val ntc: Int = lines.next.toInt
  0 until ntc map { _ =>
    handleTC()
  } foreach println

  def handleTC() = {
    val prts = lines.next.toInt
    if (prts == 1) {
      lines.next
      0
    } else if (prts <= 3) {
      lines.next
      1
    } else {
      val ws = lines.next.split(" ").map(_.toInt).sorted
      solve(ws, Array())
    }
  }

  def solve(ws: Array[Int], solutio: Array[Int]): Int = {
    if (ws.length < 2) compute(solutio)
    else ws.tail.zipWithIndex.map { case (_, ind) =>
      val updWs = ws.tail.take(ind) ++ ws.tail.drop(ind+1)

      solve(updWs, solutio ++ Array(ws.head + ws.tail(ind)))
    }.max
  }

  def compute(sol: Array[Int]): Int = {
    sol.groupBy(identity).values.map(_.length).max
  }
}


import scala.io.Source

object Solution extends App {
  val in = Source.stdin.getLines()
  val Array(n, k, l, r, sAll, sK) = in.next().split(' ').map(_.toInt)
  val answer = Array.ofDim[Int](n)
  val m = sK / k
  val left = sK % k
  (0 until left).foreach(i => answer(i) = m + 1)
  (left to k - 1).foreach(i => answer(i) = m)
  if (k != n) {
    val m1 = (sAll - sK) / (n - k)
    val left1 = (sAll - sK) % (n - k)
    (k until k + left1).foreach(i => answer(i) = m1 + 1)
    (k + left1 to n - 1).foreach(i => answer(i) = m1)
  }
  println(answer.mkString(" "))

}

object HelloWorld   {
    def main(args:Array[String]){
        var Array(n,k,l,r,sall,sk)=readLine().split(" ").map(_.toInt)
        var array=Array.fill(n)(l)
        var pos=0

        var ka:Int=sk/k
        var kb:Int=sk%k

        for(i<-0 to kb-1){
            array(i)=ka+1
        }
        for(i<-kb to k-1){
            array(i)=ka
        }
        if(n!=k)
        {
            var la:Int=(sall-sk)/(n-k)
            var lb:Int=(sall-sk)%(n-k)
            for(i<-k to k+lb-1){
                array(i)=la+1
            }
            for(i<-k+lb to n-1){
                array(i)=la
            }
        }
        println(array.mkString(" "))
    }
}

import scala.io.Source

object Solution extends App {
  val in = Source.stdin.getLines()
  val Array(n, k, l, r, sAll, sK) = in.next().split(' ').map(_.toInt)
  val answer = Array.ofDim[Int](n)
  val m = sK / k
  val left = sK % k
  (0 until left).foreach(i => answer(i) = m + 1)
  (left to k - 1).foreach(i => answer(i) = m)
  if (k <= n - 2) {
    val m = (sAll - sK) / (n - k)
    val left = (sAll - sK) % (n - k)
    (k until k + left).foreach(i => answer(i) = m + 1)
    (k + left to n - 1).foreach(i => answer(i) = m)
  }
  println(answer.mkString(" "))

}