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

object C {

  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 m = nextInt
    val t = nextInt
    val r = nextInt
    if (r - 1 > t) {
      out.println(-1)
      return 1
    }
    val w = new Array[Int](m)
    for (i <- 0 until m) {
      w(i) = nextInt + t + 1
    }
    var ans = 0
    val candles = new Array[Int](1000)
    for (i <- 0 until m) {
      var sum = 0
      for (j <- w(i) - 1 to w(i) - t  by -1) {
        sum += candles(j)
      }
      if (sum != r) {
        for (j <- w(i) - 1 to w(i) - t by -1) {
          if (sum < r && candles(j) == 0) {
            candles(j) = 1
            sum += 1
          }
        }
        if (sum < r) {
          out.println(-1)
          return 1
        }
      }
    }
    candles.foreach(ans += _)
    out.println(ans)
    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
  }
}

import scala.io.Source

object Solution extends App {
  val in = Source.stdin.getLines()
  val Array(m, t, r) = in.next().split(' ').map(_.toInt)
  if (r > t)
    println(-1)
  else {
    val data = in.next().split(' ').map(_.toInt)
    val (count, _, _) = data.tail.foldLeft(r, data.head, (1 to r).map(i => data.head - i).toList) {
      case (acc, el) if acc._1 == -1 => acc
      case ((sum, previous, fired), el) =>
        val firedBefore = el - t
//        println("el = " + el)
//        println("sum = " + sum)
//        println("previous = " + previous)
//        println("fired = " + fired)
        val left = fired.filter(_ >= firedBefore)
        val shouldFire = r - left.size
//        println("shouldFire = " + shouldFire)
        if (shouldFire <= 0)
          (sum, el, left)
        else if (shouldFire > (el - previous))
          (-1, el, left)
        else
          (sum + shouldFire, el, (1 to shouldFire).map(i => el - i).toList ::: left)
    }
    println(count)
  }
}

//5 3 2
//1 2 5 10 20

/**
 * Created by helfper on 27/01/2015.
 */
object AnyaAndGhosts {
  def main(args: Array[String]) {
    val Array(m, t, r) = io.StdIn.readLine.split(" ").map(_.toInt)
    val ghosts = io.StdIn.readLine.split(" ").map(_.toInt)

    println(solve(ghosts, t, r))
  }

  def solve(ghosts: Array[Int], t: Int, r: Int): Int = {
    val candles = Array.fill(r)(0)

    def iterate(g: Int): Int = {
      var n = 0
      0.until(r).foreach(i =>
        if (candles(i) < g) {
          candles(i) = g+t-n-1
          n += 1;
        }
      )
      if (t < n) -1
      else n
    }

    ghosts.foldLeft(0) { (r, g) =>
      val it = iterate(g)
      if (it == -1) it
      else r + it
    }
  }
}

import scala.io.Source

object Solution extends App {
  val in = Source.stdin.getLines()
  val Array(m, t, r) = in.next().split(' ').map(_.toInt)
  if (r > t)
    println(-1)
  else {
    val data = in.next().split(' ').map(_.toInt)
    val (count, _, _) = data.tail.foldLeft(r, data.head, (1 to r).map(i => data.head - i).toList) {
      case (acc, el) if acc._1 == -1 => acc
      case ((sum, previous, fired), el) =>
        val firedBefore = el - t
//        println("el = " + el)
//        println("sum = " + sum)
//        println("previous = " + previous)
//        println("fired = " + fired)
        val left = fired.filter(_ > firedBefore)
        val shouldFire = r - left.size
        if (shouldFire <= 0)
          (sum, el, left)
        else if (shouldFire > (el - previous))
          (-1, el, left)
        else
          (sum + shouldFire, el, (1 to shouldFire).map(i => el - i).toList ::: left)
    }
    println(count)
  }
}

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

object C {

  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 m = nextInt
    val t = nextInt
    val r = nextInt
    if (r - 1 > t) {
      out.println(-1)
      return 1
    }
    val w = new Array[Int](m)
    for (i <- 0 until m) {
      w(i) = nextInt
    }
    var ans = 0
    val fullWindow = t - r + 1
    var currWindow = fullWindow
    var currR = r
    ans += r
    for (i <- 1 until m) {
      val diff = w(i) - w(i - 1)
      if (diff <= currWindow) {
          currWindow -= diff
      } else {
        if (diff - currWindow >= r) {
          ans += r
          currWindow = fullWindow
        } else {
          ans += diff - currWindow
          currR = diff - currWindow
          currWindow = fullWindow
        }
      }
    }
    out.println(ans)
    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
  }
}

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

object C {

  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 m = nextInt
    val t = nextInt
    val r = nextInt
    if (r - 1 > t) {
      out.println(-1)
      return 1
    }
    val w = new Array[Int](m)
    for (i <- 0 until m) {
      w(i) = nextInt
    }
    var ans = 0
    val window = t - r + 1
    ans += r
    for (i <- 1 until m) {
      val diff = w(i) - w(i - 1)
      if (diff <= window) {

      } else {
        if (diff - window > r) {
          ans += r
        } else {
          ans += diff - window
        }
      }
    }
    out.println(ans)
    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
  }
}

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

object C {

  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 m = nextInt
    val t = nextInt
    val r = nextInt
    if (r - 1 > t) {
      out.println(-1)
      return 1
    }
    val w = new Array[Int](m)
    for (i <- 0 until m) {
      w(i) = nextInt + t + 1
    }
    var ans = 0
    val candles = new Array[Int](500)
    for (i <- 0 until m) {
      var sum = 0
      for (j <- w(i) to w(i) - t by -1) {
        sum += candles(j)
      }
      if (sum != r) {
        for (j <- w(i) to w(i) - t by -1) {
          if (sum < r && candles(j) == 0) {
            candles(j) = 1
            sum += 1
          }
        }
        if (sum < r) {
          out.println(-1)
          return 1
        }
      }
    }
    candles.foreach(ans += _)
    out.println(ans)
    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
  }
}

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

object C {

  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 m = nextInt
    val t = nextInt
    val r = nextInt
    if (r - 1 > t) {
      out.println(-1)
      return 1
    }
    val w = new Array[Int](m)
    for (i <- 0 until m) {
      w(i) = nextInt + t + 1
    }
    var ans = 0
    val candles = new Array[Int](500)
    for (i <- 0 until m) {
      var sum = 0
      for (j <- w(i) - 1 to w(i) - t by -1) {
        sum += candles(j)
      }
      if (sum != r) {
        for (j <- w(i) to w(i) - t by -1) {
          if (sum < r && candles(j) == 0) {
            candles(j) = 1
            sum += 1
          }
        }
        if (sum < r) {
          out.println(-1)
          return 1
        }
      }
    }
    candles.foreach(ans += _)
    out.println(ans)
    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
  }
}


//package edu29

import scala.collection.immutable.IntMap
import scala.io.StdIn

object E extends App {
  def readInts = StdIn.readLine().split(" ").map(_.toInt)
  val Array(n) = readInts

  val tvs = Array.fill(n)(readInts)

  val tree = {
    var pointMap = IntMap[Int]().withDefaultValue(0)
    for (tv <- tvs) {
      pointMap += tv(0) -> (pointMap(tv(0)) + 1)
      pointMap += (tv(1) + 1) -> (pointMap(tv(1) + 1) - 1)
    }
    val points = Array.ofDim[Int](pointMap.size)
    val counts = Array.ofDim[Int](pointMap.size)
    var i = 0
    for ((p, c) <- pointMap) {
      points(i) = p
      counts(i) = c
      i += 1
    }

    def sumCount(i: Int): Unit =
      if (i < points.length) {
        counts(i) += counts(i - 1)
        sumCount(i + 1)
      }
    sumCount(1)
    new QueueTree(points, counts)
  }

  def go(i: Int): Int =
    if (i == tvs.length) -1
    else if (tree.queue(tvs(i)(0), tvs(i)(1)) > 1) i + 1
    else go(i + 1)

  val result = go(0)
  println(result)
}

class QueueTree(keys: Array[Int], values: Array[Int]) {
  private[this] val total = Array.ofDim[Int](values.length * 3)

  @inline private[this] def median(x: Int, y: Int) = (x + y) / 2

  private[this] def initGo(a: Int, b: Int, i: Int): Unit =
    if (a == b) total(i) = values(a)
    else {
      val m = median(a, b)
      initGo(a, m, 2 * i + 1)
      initGo(m + 1, b, 2 * i + 2)
      total(i) = total(2 * i + 1) min total(2 * i + 2)
    }
  initGo(0, values.length - 1, 0)

  def queue(l: Int, r: Int): Int = {
    def go(a: Int, b: Int, j: Int): Int =
      if (keys(a) > r || keys(b) < l) 1000000000
      else if (keys(a) >= l && keys(b) <= r) total(j)
      else {
        val m = median(a, b)
        go(a, m, 2 * j + 1) min go(m + 1, b, 2 * j + 2)
      }
    go(0, values.length - 1, 0)
  }
}

//package edu29

import scala.collection.immutable.IntMap
import scala.io.StdIn

object E extends App {
  def readInts = StdIn.readLine().split(" ").map(_.toInt)
  val Array(n) = readInts

  val tvs = Array.fill(n)(readInts)

  val tree = {
    var pointMap = IntMap[Int]()
    for (tv <- tvs) {
      pointMap += tv(0) -> (pointMap.getOrElse(tv(0), 0) + 1)
      pointMap += (tv(1) + 1) -> (pointMap.getOrElse(tv(1) + 1, 0) - 1)
    }
    val points = Array.ofDim[Int](pointMap.size)
    val counts = Array.ofDim[Int](pointMap.size)
    var i = 0
    for ((p, c) <- pointMap) {
      points(i) = p
      counts(i) = c
      i += 1
    }

    def sumCount(i: Int): Unit =
      if (i < points.length) {
        counts(i) += counts(i - 1)
        sumCount(i + 1)
      }
    sumCount(1)
    new QueueTree(points, counts)
  }

  def go(i: Int): Int =
    if (i == tvs.length) -1
    else if (tree.queue(tvs(i)(0), tvs(i)(1)) > 1) i + 1
    else go(i + 1)

  val result = go(0)
  println(result)
}

class QueueTree(keys: Array[Int], values: Array[Int]) {
  private[this] val total = Array.ofDim[Int](values.length * 3)

  @inline private[this] def median(x: Int, y: Int) = (x + y) / 2

  private[this] def initGo(a: Int, b: Int, i: Int): Unit =
    if (a == b) total(i) = values(a)
    else {
      val m = median(a, b)
      initGo(a, m, 2 * i + 1)
      initGo(m + 1, b, 2 * i + 2)
      total(i) = total(2 * i + 1) min total(2 * i + 2)
    }
  initGo(0, values.length - 1, 0)

  def queue(l: Int, r: Int): Int = {
    def go(a: Int, b: Int, j: Int): Int =
      if (keys(a) > r || keys(b) < l) 1000000000
      else if (keys(a) >= l && keys(b) <= r) total(j)
      else {
        val m = median(a, b)
        go(a, m, 2 * j + 1) min go(m + 1, b, 2 * j + 2)
      }
    go(0, values.length - 1, 0)
  }
}

//package edu29

import java.util

import scala.collection.mutable
import scala.io.StdIn
import scala.reflect.ClassTag

object E extends App {
  def readInts = StdIn.readLine().split(" ").map(_.toInt)
  val Array(n) = readInts

  val tvs = Array.fill(n)(readInts)

  val tree = {
    val pointMap = new util.HashMap[Int, Int]
    for (tv <- tvs) {
      pointMap.put(tv(0), pointMap.getOrDefault(tv(0), 0) + 1)
      pointMap.put(tv(1) + 1, pointMap.getOrDefault(tv(1) + 1, 0) - 1)
    }
    val points = Array.ofDim[Int](pointMap.size())
    val counts = Array.ofDim[Int](pointMap.size())
    val iter = pointMap.keySet().iterator()
    def initPoints(i: Int): Unit =
      if (iter.hasNext) {
        points(i) = iter.next()
        initPoints(i + 1)
      }
    initPoints(0)
    util.Arrays.sort(points)
    def initCounts(i: Int): Unit =
      if (i < points.length) {
        counts(i) = pointMap.get(points(i))
        initCounts(i + 1)
      }
    initCounts(0)
    def sumCount(i: Int): Unit =
      if (i < points.length) {
        counts(i) += counts(i - 1)
        sumCount(i + 1)
      }
    sumCount(1)
    new QueueTree(counts, points)
  }

  def go(i: Int): Int =
    if (i == tvs.length) -1
    else if (tree.queue(tvs(i)(0), tvs(i)(1)) > 1) i + 1
    else go(i + 1)

  val result = go(0)
  println(result)
}

class QueueTree(mins: Array[Int], values: Array[Int]) {
  private[this] val total = Array.ofDim[Int](mins.length * 3)

  @inline private[this] def median(x: Int, y: Int) = (x + y) / 2

  private[this] def initGo(a: Int, b: Int, i: Int): Unit =
    if (a == b) total(i) = mins(a)
    else {
      val m = median(a, b)
      initGo(a, m, 2 * i + 1)
      initGo(m + 1, b, 2 * i + 2)
      total(i) = total(2 * i + 1) min total(2 * i + 2)
    }
  initGo(0, mins.length - 1, 0)

  def queue(l: Int, r: Int): Int = {
    def go(a: Int, b: Int, j: Int): Int =
      if (values(a) > r || values(b) < l) 1000000000
      else if (values(a) >= l && values(b) <= r) total(j)
      else {
        val m = median(a, b)
        go(a, m, 2 * j + 1) min go(m + 1, b, 2 * j + 2)
      }
    go(0, mins.length - 1, 0)
  }
}

//package edu29

import java.util

import scala.collection.mutable
import scala.io.StdIn
import scala.reflect.ClassTag

object E extends App {
  def readInts = StdIn.readLine().split(" ").map(_.toInt)
  val Array(n) = readInts

  val tvs = Array.fill(n)(readInts)

  val tree = {
    val pointMap = new util.TreeMap[Int, Int]
    for (tv <- tvs) {
      pointMap.put(tv(0), pointMap.getOrDefault(tv(0), 0) + 1)
      pointMap.put(tv(1) + 1, pointMap.getOrDefault(tv(1) + 1, 0) - 1)
    }
    val points = Array.ofDim[Int](pointMap.size())
    val counts = Array.ofDim[Int](pointMap.size())
    val iter = pointMap.entrySet().iterator()
    def init(i: Int): Unit =
      if (iter.hasNext) {
        val e = iter.next()
        points(i) = e.getKey
        counts(i) = e.getValue
        init(i + 1)
      }
    init(0)
    def sumCount(i: Int): Unit =
      if (i < points.length) {
        counts(i) += counts(i - 1)
        sumCount(i + 1)
      }
    sumCount(1)
    new QueueTree(counts, points)
  }

  def go(i: Int): Int =
    if (i == tvs.length) -1
    else if (tree.queue(tvs(i)(0), tvs(i)(1)) > 1) i + 1
    else go(i + 1)

  val result = go(0)
  println(result)
}

class QueueTree(mins: Array[Int], values: Array[Int]) {
  private[this] val total = Array.ofDim[Int](mins.length * 3)

  @inline private[this] def median(x: Int, y: Int) = (x + y) / 2

  private[this] def initGo(a: Int, b: Int, i: Int): Unit =
    if (a == b) total(i) = mins(a)
    else {
      val m = median(a, b)
      initGo(a, m, 2 * i + 1)
      initGo(m + 1, b, 2 * i + 2)
      total(i) = total(2 * i + 1) min total(2 * i + 2)
    }
  initGo(0, mins.length - 1, 0)

  def queue(l: Int, r: Int): Int = {
    def go(a: Int, b: Int, j: Int): Int =
      if (values(a) > r || values(b) < l) 1000000000
      else if (values(a) >= l && values(b) <= r) total(j)
      else {
        val m = median(a, b)
        go(a, m, 2 * j + 1) min go(m + 1, b, 2 * j + 2)
      }
    go(0, mins.length - 1, 0)
  }
}

//package edu29

import java.util

import scala.collection.mutable
import scala.io.StdIn
import scala.reflect.ClassTag

object E extends App {
  def readInts = StdIn.readLine().split(" ").map(_.toInt)
  val Array(n) = readInts

  val tvs = Array.fill(n)(readInts)
  for (tv <- tvs) {tv(1) += 1 }

  val pointArr = tvs.flatten.distinct
  util.Arrays.sort(pointArr)
  val points = mutable.HashMap[Int, Int]()
  def fillPoints(i: Int): Unit =
    if (i < pointArr.length) {
      points(pointArr(i)) = i
      fillPoints(i + 1)
    }
  fillPoints(0)
  val counts = Array.ofDim[Int](points.size + 1)

  for (lr <- tvs) {
    counts(points(lr(0))) += 1
    counts(points(lr(1))) -= 1
  }
  var i = 1
  while (i < counts.length) {
    counts(i) += counts(i - 1)
    i += 1
  }

  val tree = new QueueTree(counts)
  def go(i: Int): Int =
    if (i == tvs.length) -1
    else if (tree.queue(points(tvs(i)(0)), points(tvs(i)(1)) - 1) > 1) i + 1
    else go(i + 1)

  val result = go(0)
  println(result)
}

class QueueTree(init: Array[Int]) extends Traversable[Int] {
  private[this] val total = Array.ofDim[Int](init.length * 3)

  @inline private[this] def median(x: Int, y: Int) = (x + y) / 2

  private[this] def initGo(a: Int, b: Int, i: Int): Unit =
    if (a == b) total(i) = init(a)
    else {
      val m = median(a, b)
      initGo(a, m, 2 * i + 1)
      initGo(m + 1, b, 2 * i + 2)
      total(i) = total(2 * i + 1) min total(2 * i + 2)
    }

  initGo(0, init.length - 1, 0)

  def queue(l: Int, r: Int): Int = {
    def go(a: Int, b: Int, j: Int): Int =
      if (a > r || b < l) 1000000000
      else if (a >= l && b <= r) total(j)
      else {
        val m = median(a, b)

        go(a, m, 2 * j + 1) min go(m + 1, b, 2 * j + 2)
      }
    go(0, init.length - 1, 0)
  }

  def foreach[U](f: Int => U): Unit = {
    def go(a: Int, b: Int, i: Int): Unit =
      if (a == b) f(total(i))
      else {
        val m = median(a, b)
        go(a, m, 2 * i + 1)
        go(m + 1, b, 2 * i + 2)
      }
    go(0, init.length - 1, 0)
  }
}

//package edu29

import scala.collection.mutable
import scala.io.StdIn
import scala.reflect.ClassTag

object E extends App {
  def readInts = StdIn.readLine().split(" ").map(_.toInt)
  val Array(n) = readInts

  val tvs = Array.fill(n)(readInts)

  val points = tvs.flatten.distinct.sorted.zipWithIndex.toMap
  val counts = Array.ofDim[Int](points.size + 1)

  for (lr <- tvs) {
    counts(points(lr(0))) += 1
    counts(points(lr(1)) + 1) -= 1
  }
  var i = 1
  while (i < counts.length) {
    counts(i) += counts(i - 1)
    i += 1
  }

  val tree = new QueueTree(counts)
  val result = tvs.iterator.zipWithIndex.collectFirst {
    case (lr, i) if tree.queue(points(lr(0)), points(lr(1))) > 1 => i + 1
  }.getOrElse(-1)

  println(result)
}

class QueueTree(init: Array[Int]) extends Traversable[Int] {
  val total = Array.ofDim[Int](init.length * 3)

  @inline private[this] def median(x: Int, y: Int) = (x + y) / 2

  private[this] def initGo(a: Int, b: Int, i: Int): Unit =
    if (a == b) total(i) = init(a)
    else {
      val m = median(a, b)
      initGo(a, m, 2 * i + 1)
      initGo(m + 1, b, 2 * i + 2)
      total(i) = total(2 * i + 1) min total(2 * i + 2)
    }

  initGo(0, init.length - 1, 0)

  def queue(l: Int, r: Int): Int = {
    def go(a: Int, b: Int, j: Int): Int =
      if (a > r || b < l) 1000000000
      else if (a >= l && b <= r) total(j)
      else {
        val m = median(a, b)

        go(a, m, 2 * j + 1) min go(m + 1, b, 2 * j + 2)
      }
    go(0, init.length - 1, 0)
  }

  def foreach[U](f: Int => U): Unit = {
    def go(a: Int, b: Int, i: Int): Unit =
      if (a == b) f(total(i))
      else {
        val m = median(a, b)
        go(a, m, 2 * i + 1)
        go(m + 1, b, 2 * i + 2)
      }
    go(0, init.length - 1, 0)
  }
}

//package edu29

import scala.collection.mutable
import scala.io.StdIn
import scala.reflect.ClassTag

object E extends App {
  def readInts = StdIn.readLine().split(" ").map(_.toInt)
  val Array(n) = readInts

  val tvs = Array.fill(n)(readInts)

  val points = tvs.flatten.distinct.sorted.zipWithIndex.toMap
  val counts = Array.ofDim[Int](points.size + 1)

  for (lr <- tvs) {
    counts(points(lr(0))) += 1
    counts(points(lr(1)) + 1) -= 1
  }
  var i = 1
  while (i < counts.length) {
    counts(i) += counts(i - 1)
    i += 1
  }

  val tree = new QueueTree(points.size - 1, counts)
  val result = tvs.iterator.zipWithIndex.collectFirst {
    case (lr, i) if tree.queue(points(lr(0)), points(lr(1))) > 1 => i + 1
  }.getOrElse(-1)

  println(result)
}

class QueueTree(n: Int, init: Array[Int]) extends Traversable[Int] {
  protected val totalSize = {
    var ts = 0
    def go(a: Int, b: Int, i: Int): Unit =
      if (a == b) ts = ts max i else {
        val m = median(a, b)
        go(a, m, 2 * i + 1)
        go(m + 1, b, 2 * i + 2)
      }
    go(0, n, 0)
    ts
  }

  val total = Array.ofDim[Int](totalSize + 1)

  @inline private[this] def median(x: Int, y: Int) = (x + y) / 2

  private[this] def initGo(a: Int, b: Int, i: Int): Unit =
    if (a == b) total(i) = init(a)
    else {
      val m = median(a, b)
      initGo(a, m, 2 * i + 1)
      initGo(m + 1, b, 2 * i + 2)
      total(i) = total(2 * i + 1) min total(2 * i + 2)
    }

  initGo(0, n, 0)

  def queue(l: Int, r: Int): Int = {
    def go(a: Int, b: Int, j: Int): Int =
      if (a > r || b < l) 1000000000
      else if (a >= l && b <= r) total(j)
      else {
        val m = median(a, b)

        go(a, m, 2 * j + 1) min go(m + 1, b, 2 * j + 2)
      }
    go(0, n, 0)
  }

  def foreach[U](f: Int => U): Unit = {
    def go(a: Int, b: Int, i: Int): Unit =
      if (a == b) f(total(i))
      else {
        val m = median(a, b)
        go(a, m, 2 * i + 1)
        go(m + 1, b, 2 * i + 2)
      }
    go(0, n, 0)
  }
}


import scala.collection._

object B extends App {

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

  val Array(n, m, k) = readInts(3)
  val roads = Array.fill(n){ mutable.Map.empty[Int, Int] }

  for (i <- 0 until m) {
    val tl = tokenizeLine
    val _u, _v, x = tl.nextToken.toInt
    val u = _u - 1
    val v = _v - 1
    if (x < roads(u).getOrElse(v, Int.MaxValue)) {
      roads(u).put(v, x)
      roads(v).put(u, x)
    }
  }

  val rails = Array.fill(n) { -1 }

  for (i <- 0 until k) {
    val tl = tokenizeLine
    val _s, y = tl.nextToken.toInt
    val s = _s - 1
    if (rails(s) == -1 || y < rails(s)) rails(s) = y
  }

  for (i <- 1 until n) {
    if (rails(i) >= 0 && rails(i) < roads(0).getOrElse(i, Int.MaxValue)) {
      roads(0).put(i, rails(i))
    } else rails(i) = -1
  }

  val dists = Array.fill(n) { Long.MaxValue }
  dists(0) = 0L

  val pq = new java.util.PriorityQueue[(Int, Long)](1, Ordering.by(_._2))
  pq.add((0, 0L))
  val visited = new mutable.BitSet(n)

  while (!pq.isEmpty) {
    val (u, distU) = pq.poll()
    if (!visited(u)) {
      visited += u
      for ((v, d) <- roads(u)) {
        val newDist = distU + d
        if (newDist < dists(v)) {
        	dists(v) = newDist
          pq.add((v, newDist))
        }
        if (u > 0 && newDist <= rails(v)) rails(v) = -1
      }
    }
  }

  println(k - rails.count(_ != -1))
}

import scala.collection._

object B extends App {

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

  val Array(n, m, k) = readInts(3)
  val adj = Array.fill(n){ mutable.Map.empty[Int, Int] }

  for (i <- 0 until m) {
    val tl = tokenizeLine
    val _u, _v, x = tl.nextToken.toInt
    val u = _u - 1
    val v = _v - 1
    if (x < adj(u).getOrElse(v, Int.MaxValue)) {
      adj(u).put(v, x)
      adj(v).put(u, x)
    }
  }

  val rails = Array.fill(n) { -1 }

  for (i <- 0 until k) {
    val tl = tokenizeLine
    val _s, y = tl.nextToken.toInt
    val s = _s - 1
    if (rails(s) == -1 || y < rails(s)) rails(s) = y
  }

  for (i <- 1 until n) {
    if (rails(i) >= 0 && rails(i) < adj(0).getOrElse(i, Int.MaxValue)) {
      adj(0).put(i, rails(i))
    } else rails(i) = -1
  }

  val dists = Array.fill(n) { Long.MaxValue }
  dists(0) = 0L

  case class Edge(v: Int, dist: Long) extends Comparable[Edge] {
    def compareTo(other: Edge) = if (this.dist == other.dist) this.v.compare(other.v)
      else -this.dist.compare(other.dist)
  }

  val pq = mutable.PriorityQueue.empty[Edge]
  pq.enqueue(Edge(0, 0L))
  val visited = new mutable.BitSet(n)

  while (!pq.isEmpty) {
    val edge = pq.dequeue()
    if (!visited(edge.v)) {
      visited += edge.v
      for ((v, d) <- adj(edge.v)) {
        val newDist = edge.dist + d
        if (newDist < dists(v)) {
        	dists(v) = newDist
          pq.enqueue(Edge(v, newDist))
        }
        if (edge.v > 0 && newDist <= rails(v)) rails(v) = -1
      }
    }
  }

  println(k - rails.count(_ != -1))
}

import scala.collection._

object B extends App {

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

  val Array(n, m, k) = readInts(3)
  val adj = Array.fill(n){ mutable.Map.empty[Int, Int] }

  for (i <- 0 until m) {
    val tl = tokenizeLine
    val _u, _v, x = tl.nextToken.toInt
    val u = _u - 1
    val v = _v - 1
    if (x < adj(u).getOrElse(v, Int.MaxValue)) {
      adj(u).put(v, x)
      adj(v).put(u, x)
    }
  }

  val rails = Array.fill(n) { -1 }

  for (i <- 0 until k) {
    val tl = tokenizeLine
    val _s, y = tl.nextToken.toInt
    val s = _s - 1
    if (rails(s) == -1 || y < rails(s)) rails(s) = y
  }

  for (i <- 1 until n) {
    if (rails(i) >= 0 && rails(i) < adj(0).getOrElse(i, Int.MaxValue)) {
      adj(0).put(i, rails(i))
    } else rails(i) = -1
  }

  val dists = Array.fill(n) { Long.MaxValue }
  dists(0) = 0L

  final case class Edge(v: Int, dist: Long) extends Comparable[Edge] {
    @inline def compareTo(other: Edge) = java.lang.Long.compare(other.dist, this.dist)
  }

  val pq = mutable.PriorityQueue.empty[Edge]
  pq.enqueue(Edge(0, 0L))
  val visited = new mutable.BitSet(n)

  while (!pq.isEmpty) {
    val Edge(u, distU) = pq.dequeue()
    if (!visited(u)) {
      visited += u
      for ((v, d) <- adj(u)) {
        val newDist = distU + d
        if (newDist < dists(v)) {
        	dists(v) = newDist
          pq.enqueue(Edge(v, newDist))
        }
        if (u > 0 && newDist <= rails(v)) rails(v) = -1
      }
    }
  }

  println(k - rails.count(_ != -1))
}

import scala.collection._

object B extends App {

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

  val Array(n, m, k) = readInts(3)
  val adj = Array.fill(n){ mutable.Map.empty[Int, Int] }

  for (i <- 0 until m) {
    val tl = tokenizeLine
    val _u, _v, x = tl.nextToken.toInt
    val u = _u - 1
    val v = _v - 1
    if (x < adj(u).getOrElse(v, Int.MaxValue)) {
      adj(u).put(v, x)
      adj(v).put(u, x)
    }
  }

  val rails = Array.fill(n) { -1 }

  for (i <- 0 until k) {
    val tl = tokenizeLine
    val _s, y = tl.nextToken.toInt
    val s = _s - 1
    if (rails(s) == -1 || y < rails(s)) rails(s) = y
  }

  for (i <- 1 until n) {
    if (rails(i) >= 0 && rails(i) < adj(0).getOrElse(i, Int.MaxValue)) {
      adj(0).put(i, rails(i))
    } else rails(i) = -1
  }

  val dists = Array.fill(n) { Long.MaxValue }
  dists(0) = 0L

  final case class Edge(v: Int, dist: Long) extends Comparable[Edge] {
    @inline def compareTo(other: Edge) = java.lang.Long.compare(other.dist, this.dist)
  }

  val pq = mutable.PriorityQueue.empty[Edge]
  pq.enqueue(Edge(0, 0L))
  val visited = new mutable.BitSet(n)

  while (!pq.isEmpty) {
    val edge = pq.dequeue()
    if (!visited(edge.v)) {
      visited += edge.v
      for ((v, d) <- adj(edge.v)) {
        val newDist = edge.dist + d
        if (newDist < dists(v)) {
        	dists(v) = newDist
          pq.enqueue(Edge(v, newDist))
        }
        if (edge.v > 0 && newDist <= rails(v)) rails(v) = -1
      }
    }
  }

  println(k - rails.count(_ != -1))
}

import scala.collection._

object B extends App {

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

  val Array(n, m, k) = readInts(3)
  val adj = Array.fill(n){ mutable.Map.empty[Int, Int] }

  for (i <- 0 until m) {
    val tl = tokenizeLine
    val _u, _v, x = tl.nextToken.toInt
    val u = _u - 1
    val v = _v - 1
    if (x < adj(u).getOrElse(v, Int.MaxValue)) {
      adj(u).put(v, x)
      adj(v).put(u, x)
    }
  }

  val rails = Array.fill(n) { -1 }

  for (i <- 0 until k) {
    val tl = tokenizeLine
    val _s, y = tl.nextToken.toInt
    val s = _s - 1
    if (rails(s) == -1 || y < rails(s)) rails(s) = y
  }

  for (i <- 1 until n) {
    if (rails(i) >= 0 && rails(i) < adj(0).getOrElse(i, Int.MaxValue)) {
      adj(0).put(i, rails(i))
    } else rails(i) = -1
  }

  val dists = Array.fill(n) { Long.MaxValue }
  dists(0) = 0L

  case class Edge(v: Int, dist: Long) extends Comparable[Edge] {
    def compareTo(other: Edge) = -this.dist.compare(other.dist)
  }

  val pq = mutable.PriorityQueue.empty[Edge]
  pq.enqueue(Edge(0, 0L))
  val visited = new mutable.BitSet(n)

  while (!pq.isEmpty) {
    val edge = pq.dequeue()
    if (!visited(edge.v)) {
      visited += edge.v
      for ((v, d) <- adj(edge.v)) {
        val newDist = edge.dist + d
        if (newDist < dists(v)) {
        	dists(v) = newDist
          pq.enqueue(Edge(v, newDist))
        }
        if (edge.v > 0 && newDist <= rails(v)) rails(v) = -1
      }
    }
  }

  println(k - rails.count(_ != -1))
}

import java.util.Scanner

/**
 * Created by kshim on 8/08/2014.
 */
object CF257_1B extends App {
  @inline def tokenizeLine = new java.util.StringTokenizer(readLine)
  def readInts(n: Int) = { val tl = tokenizeLine; Array.fill(n)(tl.nextToken.toInt) }

  case class Road(u:Int, v:Int, w:Long)
  case class Train(u:Int, w:Long)

  def Process(n:Int, m:Int, k:Long, roads:Seq[Road], trains:Seq[Train]):Long = {
    val trainDists = Array.fill(n + 1) {
      Long.MaxValue
    }
    val roadDists = Array.fill(n + 1) {
      collection.mutable.Map[Int, Long]()
    }
    for (t <- trains) {
      trainDists(t.u) = trainDists(t.u) min t.w
      roadDists(1)(t.u) = roadDists(1).getOrElse(t.u, t.w) min t.w
      roadDists(t.u)(1) = roadDists(1)(t.u)
    }
    for (r <- roads) {
      roadDists(r.u)(r.v) = roadDists(r.u).getOrElse(r.v, Long.MaxValue) min r.w
      roadDists(r.v)(r.u) = roadDists(r.v).getOrElse(r.u, Long.MaxValue) min r.w
      if(r.v == 1 && trainDists(r.u) >= r.w) {
        trainDists(r.u) = Long.MaxValue
      }
      if(r.u == 1 && trainDists(r.v) >= r.w) {
        trainDists(r.v) = Long.MaxValue
      }
    }
    //println(trainDists.toSeq.toString)

    val visited = collection.mutable.BitSet(n + 1)
    val pq = new java.util.PriorityQueue[(Int, Long)](1, Ordering.by(_._2))
    pq.add((1, 0))

    val dist = Array.fill(n + 1) {
      Long.MaxValue
    }
    dist(1) = 0

    while (!pq.isEmpty) {
      val (u, uDist) = pq.poll()
      if (!visited(u)) {
        visited.add(u)
        for ((v, w) <- roadDists(u)) {
          val newDist = uDist + w
          if (newDist < dist(v)) {
            dist(v) = newDist
            pq.add((v, newDist))
          }
          if( trainDists(v) != Long.MaxValue && u > 1 && newDist <= trainDists(v)){
            trainDists(v) = Long.MaxValue
          }
        }
      }
    }
    k - trainDists.count(_ != Long.MaxValue)
  }

  val Array(n,m,k) = readInts(3)
  val roads = (1 to m).map(_ => {
    val arr = readInts(3)
    Road(arr(0), arr(1), arr(2))
  })
  val trains = (1 to k).map(_ => {
    val arr = readInts(2)
    Train(arr(0), arr(1))})
  println(Process(n, m, k, roads, trains))
}

import scala.collection._

object B extends App {

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

  val Array(n, m, k) = readInts(3)
  val adj = Array.fill(n){ mutable.Map.empty[Int, Int] }

  for (i <- 0 until m) {
    val tl = tokenizeLine
    val _u, _v, x = tl.nextToken.toInt
    val u = _u - 1
    val v = _v - 1
    if (x < adj(u).getOrElse(v, Int.MaxValue)) {
      adj(u).put(v, x)
      adj(v).put(u, x)
    }
  }

  val rails = Array.fill(n) { -1 }

  for (i <- 0 until k) {
    val tl = tokenizeLine
    val _s, y = tl.nextToken.toInt
    val s = _s - 1
    if (rails(s) == -1 || y < rails(s)) rails(s) = y
  }

  for (i <- 1 until n) {
    if (rails(i) >= 0 && rails(i) < adj(0).getOrElse(i, Int.MaxValue)) {
      adj(0).put(i, rails(i))
    } else rails(i) = -1
  }

  val dists = Array.fill(n) { Long.MaxValue }
  dists(0) = 0L

  final case class Edge(v: Int, dist: Long) extends Comparable[Edge] {
    @inline def compareTo(other: Edge) = other.dist.compare(this.dist)
  }

  val pq = mutable.PriorityQueue.empty[Edge]
  pq.enqueue(Edge(0, 0L))
  val visited = new mutable.BitSet(n)

  while (!pq.isEmpty) {
    val edge = pq.dequeue()
    if (!visited(edge.v)) {
      visited += edge.v
      for ((v, d) <- adj(edge.v)) {
        val newDist = edge.dist + d
        if (newDist < dists(v)) {
        	dists(v) = newDist
          pq.enqueue(Edge(v, newDist))
        }
        if (edge.v > 0 && newDist <= rails(v)) rails(v) = -1
      }
    }
  }

  println(k - rails.count(_ != -1))
}

import scala.collection._

object B extends App {

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

  val Array(n, m, k) = readInts(3)
  val adj = Array.fill(n){ mutable.Map.empty[Int, Int] }

  for (i <- 0 until m) {
    val tl = tokenizeLine
    val _u, _v, x = tl.nextToken.toInt
    val u = _u - 1
    val v = _v - 1
    if (x < adj(u).getOrElse(v, Int.MaxValue)) {
      adj(u).put(v, x)
      adj(v).put(u, x)
    }
  }

  val rails = Array.fill(n) { -1 }

  for (i <- 0 until k) {
    val tl = tokenizeLine
    val _s, y = tl.nextToken.toInt
    val s = _s - 1
    if (rails(s) == -1 || y < rails(s)) rails(s) = y
  }

  for (i <- 1 until n) {
    if (rails(i) >= 0 && rails(i) < adj(0).getOrElse(i, Int.MaxValue)) {
      adj(0).put(i, rails(i))
    } else rails(i) = -1
  }

  val dists = Array.fill(n) { Long.MaxValue }
  dists(0) = 0L

  final case class Edge(v: Int, dist: Long) extends Comparable[Edge] {
    @inline def compareTo(other: Edge) = java.lang.Long.compare(this.dist, other.dist)
  }

  //val pq = mutable.PriorityQueue.empty[Edge]
  val pq = new java.util.PriorityQueue[Edge](1)
  pq.add(Edge(0, 0L))
  val visited = new mutable.BitSet(n)

  while (!pq.isEmpty) {
    val Edge(u, distU) = pq.poll()
    if (!visited(u)) {
      visited += u
      for ((v, d) <- adj(u)) {
        val newDist = distU + d
        if (newDist < dists(v)) {
        	dists(v) = newDist
          pq.add(Edge(v, newDist))
        }
        if (u > 0 && newDist <= rails(v)) rails(v) = -1
      }
    }
  }

  println(k - rails.count(_ != -1))
}

import scala.collection._

object B extends App {

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

  val Array(n, m, k) = readInts(3)
  val adj = Array.fill(n){ mutable.Map.empty[Int, Int] }

  for (i <- 0 until m) {
    val tl = tokenizeLine
    val _u, _v, x = tl.nextToken.toInt
    val u = _u - 1
    val v = _v - 1
    if (x < adj(u).getOrElse(v, Int.MaxValue)) {
      adj(u).put(v, x)
      adj(v).put(u, x)
    }
  }

  val rails = Array.fill(n) { -1 }

  for (i <- 0 until k) {
    val tl = tokenizeLine
    val _s, y = tl.nextToken.toInt
    val s = _s - 1
    if (rails(s) == -1 || y < rails(s)) rails(s) = y
  }

  for (i <- 1 until n) {
    if (rails(i) >= 0 && rails(i) < adj(0).getOrElse(i, Int.MaxValue)) {
      adj(0).put(i, rails(i))
    } else rails(i) = -1
  }

  val dists = Array.fill(n) { Long.MaxValue }
  dists(0) = 0L

  case class Edge(v: Int, dist: Long) extends Comparable[Edge] {
    def compareTo(other: Edge) = if (this.dist == other.dist) this.v.compare(other.v)
      else -this.dist.compare(other.dist)
  }

  val pq = mutable.PriorityQueue.empty[Edge]
  pq.enqueue(Edge(0, 0L))
  val visited = new mutable.BitSet(n)

  while (!pq.isEmpty) {
    val Edge(u, distU) = pq.dequeue()
    if (!visited(u)) {
      visited += u
      for ((v, d) <- adj(u)) {
        val newDist = distU + d
        if (newDist < dists(v)) {
        	dists(v) = newDist
          pq.enqueue(Edge(v, newDist))
        }
        if (u > 0 && newDist <= rails(v)) rails(v) = -1
      }
    }
  }

  println(k - rails.count(_ != -1))
}

import scala.collection._

object B extends App {

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

  val Array(n, m, k) = readInts(3)
  val adj = Array.fill(n){ mutable.Map.empty[Int, Int] }

  for (i <- 0 until m) {
    val tl = tokenizeLine
    val _u, _v, x = tl.nextToken.toInt
    val u = _u - 1
    val v = _v - 1
    if (x < adj(u).getOrElse(v, Int.MaxValue)) {
      adj(u).put(v, x)
      adj(v).put(u, x)
    }
  }

  val rails = Array.fill(n) { -1 }

  for (i <- 0 until k) {
    val tl = tokenizeLine
    val _s, y = tl.nextToken.toInt
    val s = _s - 1
    if (rails(s) == -1 || y < rails(s)) rails(s) = y
  }

  for (i <- 1 until n) {
    if (rails(i) >= 0 && rails(i) < adj(0).getOrElse(i, Int.MaxValue)) {
      adj(0).put(i, rails(i))
    } else rails(i) = -1
  }

  val dists = Array.fill(n) { Long.MaxValue }
  dists(0) = 0L

  case class Edge(v: Int, dist: Long) extends Comparable[Edge] {
    def compareTo(other: Edge) = this.dist.compare(other.dist)
  }

  val pq = new java.util.PriorityQueue[Edge](1)//, Ordering.by(_._2))
  pq.add(Edge(0, 0L))
  val visited = new mutable.BitSet(n)

  while (!pq.isEmpty) {
    val Edge(u, distU) = pq.poll()
    if (!visited(u)) {
      visited += u
      for ((v, d) <- adj(u)) {
        val newDist = distU + d
        if (newDist < dists(v)) {
        	dists(v) = newDist
          pq.add(Edge(v, newDist))
        }
        if (u > 0 && newDist <= rails(v)) rails(v) = -1
      }
    }
  }

  println(k - rails.count(_ != -1))
}

import scala.collection._

object B extends App {

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

  val Array(n, m, k) = readInts(3)
  val roads = Array.fill(n){ mutable.Map.empty[Int, Int] }

  for (i <- 0 until m) {
    val tl = tokenizeLine
    val _u, _v, x = tl.nextToken.toInt
    val u = _u - 1
    val v = _v - 1
    if (x < roads(u).getOrElse(v, Int.MaxValue)) {
      roads(u).put(v, x)
      roads(v).put(u, x)
    }
  }

  val rails = Array.fill(n) { -1 }

  for (i <- 0 until k) {
    val tl = tokenizeLine
    val _s, y = tl.nextToken.toInt
    val s = _s - 1
    if (rails(s) == -1 || y < rails(s)) rails(s) = y
  }

  for (i <- 1 until n) {
    if (rails(i) >= 0 && rails(i) < roads(0).getOrElse(i, Int.MaxValue)) {
      roads(0).put(i, rails(i))
    } else rails(i) = -1
  }

  val dists = Array.fill(n) { Long.MaxValue }
  dists(0) = 0

  //val pq = mutable.PriorityQueue(0)(Ordering.by(dist))
  val pq = new java.util.PriorityQueue[(Int, Long)](1, Ordering.by(_._2))
  pq.add((0, 0))
  val visited = new mutable.BitSet(n)

  while (!pq.isEmpty) {
    val (u, distU) = pq.poll()
    if (!visited(u)) {
      visited += u
      for ((v, d) <- roads(u)) {
        val newDist = distU + d
        if (newDist < dists(v)) {
        	dists(v) = newDist
          pq.add((v, newDist))
        }
        if (u > 0 && newDist <= rails(v)) rails(v) = -1
      }
    }
  }

  println(k - rails.count(_ != -1))
}

import scala.collection._

object B extends App {

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

  val Array(n, m, k) = readInts(3)
  val adj = Array.fill(n){ mutable.Map.empty[Int, Int] }

  for (i <- 0 until m) {
    val tl = tokenizeLine
    val _u, _v, x = tl.nextToken.toInt
    val u = _u - 1
    val v = _v - 1
    if (x < adj(u).getOrElse(v, Int.MaxValue)) {
      adj(u).put(v, x)
      adj(v).put(u, x)
    }
  }

  val rails = Array.fill(n) { -1 }

  for (i <- 0 until k) {
    val tl = tokenizeLine
    val _s, y = tl.nextToken.toInt
    val s = _s - 1
    if (rails(s) == -1 || y < rails(s)) rails(s) = y
  }

  for (i <- 1 until n) {
    if (rails(i) >= 0 && rails(i) < adj(0).getOrElse(i, Int.MaxValue)) {
      adj(0).put(i, rails(i))
    } else rails(i) = -1
  }

  val dists = Array.fill(n) { Long.MaxValue }
  dists(0) = 0L

  final case class Edge(v: Int, dist: Long) extends Comparable[Edge] {
    @inline def compareTo(other: Edge) = java.lang.Long.compare(other.dist, this.dist)
  }

  //val pq = mutable.PriorityQueue.empty[Edge]
  val pq = new java.util.PriorityQueue[Edge](1)
  pq.add(Edge(0, 0L))
  val visited = new mutable.BitSet(n)

  while (!pq.isEmpty) {
    val Edge(u, distU) = pq.poll()
    if (!visited(u)) {
      visited += u
      for ((v, d) <- adj(u)) {
        val newDist = distU + d
        if (newDist < dists(v)) {
        	dists(v) = newDist
          pq.add(Edge(v, newDist))
        }
        if (u > 0 && newDist <= rails(v)) rails(v) = -1
      }
    }
  }

  println(k - rails.count(_ != -1))
}

import scala.collection._

object B extends App {

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

  val Array(n, m, k) = readInts(3)
  val adj = Array.fill(n){ mutable.Map.empty[Int, Int] }

  for (i <- 0 until m) {
    val tl = tokenizeLine
    val _u, _v, x = tl.nextToken.toInt
    val u = _u - 1
    val v = _v - 1
    if (x < adj(u).getOrElse(v, Int.MaxValue)) {
      adj(u).put(v, x)
      adj(v).put(u, x)
    }
  }

  val rails = Array.fill(n) { -1 }

  for (i <- 0 until k) {
    val tl = tokenizeLine
    val _s, y = tl.nextToken.toInt
    val s = _s - 1
    if (rails(s) == -1 || y < rails(s)) rails(s) = y
  }

  for (i <- 1 until n) {
    if (rails(i) >= 0 && rails(i) < adj(0).getOrElse(i, Int.MaxValue)) {
      adj(0).put(i, rails(i))
    } else rails(i) = -1
  }

  val dists = Array.fill(n) { Long.MaxValue }
  dists(0) = 0L

  case class Edge(v: Int, dist: Long) extends Comparable[Edge] {
    def compareTo(other: Edge) = if (this.dist == other.dist) this.v.compare(other.v)
      else this.dist.compare(other.dist)
  }

  val pq = mutable.PriorityQueue.empty[Edge]
  pq.enqueue(Edge(0, 0L))
  val visited = new mutable.BitSet(n)

  while (!pq.isEmpty) {
    val Edge(u, distU) = pq.dequeue()
    if (!visited(u)) {
      visited += u
      for ((v, d) <- adj(u)) {
        val newDist = distU + d
        if (newDist < dists(v)) {
        	dists(v) = newDist
          pq.enqueue(Edge(v, newDist))
        }
        if (u > 0 && newDist <= rails(v)) rails(v) = -1
      }
    }
  }

  println(k - rails.count(_ != -1))
}

/**
 * I've tried to make this piece of code as readable as possible.
 * Enjoy hacking it!
 */
import scala.collection._

object B extends App {

  @inline def tokenizeLine = new java.util.StringTokenizer(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, k) = readInts(3)
  val roads = mutable.Map.empty[(Int, Int), Int].withDefaultValue(Int.MaxValue)

  for (i <- 0 until m) {
    val tl = tokenizeLine
    val _u, _v, x = tl.nextToken.toInt
    val u = _u - 1
    val v = _v - 1
    if (x < roads(u, v)) {
      roads.put((u, v), x)
      roads.put((v, u), x)
    }
  }

  var removed = 0

  val rails = Array.fill(n) { -1 }
  for (i <- 0 until k) {
    val tl = tokenizeLine
    val _s, y = tl.nextToken.toInt
    val s = _s - 1
    if (rails(s) == -1) rails(s) = y
    else if (y < rails(s)) {
      rails(s) = y
      removed += 1
    } else removed += 1
  }

  val adjB = Array.fill(n) { new mutable.ArrayBuilder.ofInt }
  val adjRoadB = Array.fill(n) { new mutable.ArrayBuilder.ofInt }

  for (((u, v), x) <- roads) {
    adjB(u) += v
    adjRoadB(u) += x
  }

  val adj = adjB.map(_.result)
  val adjRoad = adjRoadB.map(_.result)

  val dist = Array.fill(n) { Long.MaxValue }
  dist(0) = 0

  val heap = Array.ofDim[Long](n)
  var heapSize = 0
  val idPos = Array.ofDim[Int](n)
  val posId = Array.ofDim[Int](n)

  @inline def swap[T](as: Array[T], u: Int, v: Int): Unit = {
    val tmp = as(u)
    as(u) = as(v)
    as(v) = tmp
  }

  @inline def swapHeap(u: Int, v: Int): Unit = {
    swap(heap, u, v)
    swap(posId, u, v)
    swap(idPos, u, v)
  }

  def bubbleDown(pos0: Int): Unit = {
    var pos = pos0
    while (pos < (heapSize >> 1)) {
      var child = 2 * pos + 1
      if (child + 1 < heapSize && heap(child + 1) < heap(child)) {
        child += 1
      }
      if (heap(pos) <= heap(child)) pos = heapSize + 1
      else {
        swapHeap(pos, child)
        pos = child
      }
    }
  }

  def bubbleUp(pos0: Int): Unit = {
    var pos = pos0
    while (pos > 0) {
      val parent = (pos - 1) >> 1
      if (heap(pos) >= heap(parent)) pos = -1
      else {
        swapHeap(pos, parent)
        pos = parent
      }
    }
  }

  def extractMin(): Int = {
    val res = posId(0)
    heapSize -= 1
    val lastNode = heap(heapSize)
    if (heapSize > 0) {
      heap(0) = lastNode
      val id = posId(heapSize)
      idPos(id) = 0
      posId(0) = id
      bubbleDown(0)
    }
    res
  }

  def decKey(id: Int, key: Long): Unit = {
    val pos = idPos(id)
    heap(pos) = key
    bubbleUp(pos)
  }

  def add(id: Int, key: Long): Unit = {
    heap(heapSize) = key
    idPos(id) = heapSize
    posId(heapSize) = id
    bubbleUp(heapSize)
    heapSize += 1
  }

  add(0, 0)

  while (heapSize > 0) {
    val u = extractMin()
    if (dist(u) <= rails(u)) removed += 1
    else if (rails(u) >= 0) dist(u) = rails(u)
    for (i <- adj(u).indices) {
      val v = adj(u)(i)
      val newDist = dist(u) + adjRoad(u)(i)
      if (newDist < dist(v)) {
        if (dist(v) == Long.MaxValue) add(v, newDist)
        else decKey(v, newDist)
        dist(v) = newDist
      }
    }
  }

  println(removed)
}

import scala.collection._

object B extends App {

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

  val Array(n, m, k) = readInts(3)
  val adj = Array.fill(n){ mutable.Map.empty[Int, Int] }

  for (i <- 0 until m) {
    val tl = tokenizeLine
    val _u, _v, x = tl.nextToken.toInt
    val u = _u - 1
    val v = _v - 1
    if (x < adj(u).getOrElse(v, Int.MaxValue)) {
      adj(u).put(v, x)
      adj(v).put(u, x)
    }
  }

  val rails = Array.fill(n) { -1 }

  for (i <- 0 until k) {
    val tl = tokenizeLine
    val _s, y = tl.nextToken.toInt
    val s = _s - 1
    if (rails(s) == -1 || y < rails(s)) rails(s) = y
  }

  for (i <- 1 until n) {
    if (rails(i) >= 0 && rails(i) < adj(0).getOrElse(i, Int.MaxValue)) {
      adj(0).put(i, rails(i))
    } else rails(i) = -1
  }

  val dists = Array.fill(n) { Long.MaxValue }
  dists(0) = 0L

  case class Edge(v: Int, dist: Long) extends Comparable[Edge] {
    def compareTo(other: Edge) = this.dist.compare(other.dist)
  }

  val pq = mutable.PriorityQueue.empty[Edge]
  pq.enqueue(Edge(0, 0L))
  val visited = new mutable.BitSet(n)

  while (!pq.isEmpty) {
    val Edge(u, distU) = pq.dequeue()
    if (!visited(u)) {
      visited += u
      for ((v, d) <- adj(u)) {
        val newDist = distU + d
        if (newDist < dists(v)) {
        	dists(v) = newDist
          pq.enqueue(Edge(v, newDist))
        }
        if (u > 0 && newDist <= rails(v)) rails(v) = -1
      }
    }
  }

  println(k - rails.count(_ != -1))
}

/**
 * I've tried to make this piece of code as readable as possible.
 * Enjoy hacking it!
 */
import scala.collection._

object B extends App {

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

  val Array(n, m, k) = readInts(3)
  val roads = mutable.Map.empty[(Int, Int), Int]

  for (i <- 0 until m) {
    val tl = tokenizeLine
    val _u, _v, x = tl.nextToken.toInt
    val u = _u - 1
    val v = _v - 1
    if (x < roads.getOrElse((u, v), Int.MaxValue)) {
      roads.put((u, v), x)
      roads.put((v, u), x)
    }
  }

  var removed = 0

  val rails = Array.fill(n) { -1 }

  for (i <- 0 until k) {
    val tl = tokenizeLine
    val _s, y = tl.nextToken.toInt
    val s = _s - 1
    if (rails(s) == -1) rails(s) = y
    else if (y < rails(s)) {
      rails(s) = y
      removed += 1
    } else removed += 1
  }

  for (i <- 1 until n) {
    if (rails(i) > 0 && rails(i) < roads.getOrElse((0, i), Int.MaxValue)) {
      roads.put((0, i), rails(i))
      removed -= 1
    }
  }

  val adjB = Array.fill(n) { new mutable.ArrayBuilder.ofInt }
  val adjRoadB = Array.fill(n) { new mutable.ArrayBuilder.ofInt }

  for (((u, v), x) <- roads) {
    adjB(u) += v
    adjRoadB(u) += x
  }

  val adj = adjB.map(_.result)
  val adjRoad = adjRoadB.map(_.result)

  val dist = Array.fill(n) { Long.MaxValue }
  dist(0) = 0

  //val pq = mutable.PriorityQueue(0)(Ordering.by(dist))
  val pq = new java.util.PriorityQueue(1, Ordering.by(dist))
  pq.add(0)
  val visited = new mutable.BitSet(n)

  while (!pq.isEmpty) {
    val u = pq.poll()
    if (!visited(u)) {
      visited += u
      if (dist(u) <= rails(u)) removed += 1
      else if (rails(u) >= 0) dist(u) = rails(u)
      for (i <- adj(u).indices) {
        val v = adj(u)(i)
        val newDist = dist(u) + adjRoad(u)(i)
        if (newDist < dist(v)) {
        	dist(v) = newDist
          pq.add(v)
        }
      }
    }
  }

  println(removed)
}

import scala.collection._

object B extends App {

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

  val Array(n, m, k) = readInts(3)
  val roads = Array.fill(n){ mutable.Map.empty[Int, Int] }

  for (i <- 0 until m) {
    val tl = tokenizeLine
    val _u, _v, x = tl.nextToken.toInt
    val u = _u - 1
    val v = _v - 1
    if (x < roads(u).getOrElse(v, Int.MaxValue)) {
      roads(u).put(v, x)
      roads(v).put(u, x)
    }
  }

  val rails = Array.fill(n) { -1 }

  for (i <- 0 until k) {
    val tl = tokenizeLine
    val _s, y = tl.nextToken.toInt
    val s = _s - 1
    if (rails(s) == -1 || y < rails(s)) rails(s) = y
  }

  for (i <- 1 until n) {
    if (rails(i) > 0 && rails(i) < roads(0).getOrElse(i, Int.MaxValue)) {
      roads(0).put(i, rails(i))
    } else rails(i) = -1
  }

  val dist = Array.fill(n) { Long.MaxValue }
  dist(0) = 0

  //val pq = mutable.PriorityQueue(0)(Ordering.by(dist))
  val pq = new java.util.PriorityQueue(1, Ordering.by(dist))
  pq.add(0)
  val visited = new mutable.BitSet(n)

  while (!pq.isEmpty) {
    val u = pq.poll()
    if (!visited(u)) {
      visited += u
      for ((v, d) <- roads(u)) {
        val newDist = dist(u) + d
        if (newDist < dist(v)) {
        	dist(v) = newDist
          pq.add(v)
          if (u > 0 && newDist <= rails(v)) rails(v) = -1
        }
      }
    }
  }

  println(k - rails.count(_ != -1))
}

import scala.collection._

object B extends App {

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

  val Array(n, m, k) = readInts(3)
  val roads = Array.fill(n){ mutable.Map.empty[Int, Int] }

  for (i <- 0 until m) {
    val tl = tokenizeLine
    val _u, _v, x = tl.nextToken.toInt
    val u = _u - 1
    val v = _v - 1
    if (x < roads(u).getOrElse(v, Int.MaxValue)) {
      roads(u).put(v, x)
      roads(v).put(u, x)
    }
  }

  val rails = Array.fill(n) { -1 }

  for (i <- 0 until k) {
    val tl = tokenizeLine
    val _s, y = tl.nextToken.toInt
    val s = _s - 1
    if (rails(s) == -1 || y < rails(s)) rails(s) = y
  }

  for (i <- 1 until n) {
    if (rails(i) > 0 && rails(i) < roads(0).getOrElse(i, Int.MaxValue)) {
      roads(0).put(i, rails(i))
    } else rails(i) = -1
  }

  val dist = Array.fill(n) { Long.MaxValue }
  val steps = Array.fill(n) { 0 }
  dist(0) = 0

  //val pq = mutable.PriorityQueue(0)(Ordering.by(dist))
  val pq = new java.util.PriorityQueue(1, Ordering.by(dist))
  pq.add(0)
  val visited = new mutable.BitSet(n)

  while (!pq.isEmpty) {
    val u = pq.poll()
    if (!visited(u)) {
      visited += u
      if (dist(u) <= rails(u) && steps(u) > 1) rails(u) = -1
      for ((v, d) <- roads(u)) {
        val newDist = dist(u) + d
        if (newDist < dist(v)) {
          steps(v) = steps(u) + 1
        	dist(v) = newDist
          pq.add(v)
        }
      }
    }
  }

  println(k - rails.count(_ != -1))
}

import scala.collection._

object B extends App {

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

  val Array(n, m, k) = readInts(3)
  val roads = Array.fill(n){ mutable.Map.empty[Int, Int] }

  for (i <- 0 until m) {
    val tl = tokenizeLine
    val _u, _v, x = tl.nextToken.toInt
    val u = _u - 1
    val v = _v - 1
    if (x < roads(u).getOrElse(v, Int.MaxValue)) {
      roads(u).put(v, x)
      roads(v).put(u, x)
    }
  }

  val rails = Array.fill(n) { -1 }

  for (i <- 0 until k) {
    val tl = tokenizeLine
    val _s, y = tl.nextToken.toInt
    val s = _s - 1
    if (rails(s) == -1 || y < rails(s)) rails(s) = y
  }

  for (i <- 1 until n) {
    if (rails(i) >= 0 && rails(i) < roads(0).getOrElse(i, Int.MaxValue)) {
      roads(0).put(i, rails(i))
    } else rails(i) = -1
  }

  val dist = Array.fill(n) { Long.MaxValue }
  dist(0) = 0

  //val pq = mutable.PriorityQueue(0)(Ordering.by(dist))
  val pq = new java.util.PriorityQueue(1, Ordering.by(dist))
  pq.add(0)
  val visited = new mutable.BitSet(n)

  while (!pq.isEmpty) {
    val u = pq.poll()
    if (!visited(u)) {
      visited += u
      for ((v, d) <- roads(u)) {
        val newDist = dist(u) + d
        if (newDist < dist(v)) {
        	dist(v) = newDist
          pq.add(v)
        }
        if (u > 0 && newDist <= rails(v)) rails(v) = -1
      }
    }
  }

  println(k - rails.count(_ != -1))
}

import scala.collection._

object B extends App {

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

  val Array(n, m, k) = readInts(3)
  val roads = Array.fill(n){ mutable.Map.empty[Int, Int] }

  for (i <- 0 until m) {
    val tl = tokenizeLine
    val _u, _v, x = tl.nextToken.toInt
    val u = _u - 1
    val v = _v - 1
    if (x < roads(u).getOrElse(v, Int.MaxValue)) {
      roads(u).put(v, x)
      roads(v).put(u, x)
    }
  }

  val rails = Array.fill(n) { -1 }

  for (i <- 0 until k) {
    val tl = tokenizeLine
    val _s, y = tl.nextToken.toInt
    val s = _s - 1
    if (rails(s) == -1 || y < rails(s)) rails(s) = y
  }

  for (i <- 1 until n) {
    if (rails(i) > 0 && rails(i) < roads(0).getOrElse(i, Int.MaxValue)) {
      roads(0).put(i, rails(i))
    } else rails(i) = -1
  }

  val dist = Array.fill(n) { Long.MaxValue }
  dist(0) = 0

  //val pq = mutable.PriorityQueue(0)(Ordering.by(dist))
  val pq = new java.util.PriorityQueue(1, Ordering.by(dist))
  pq.add(0)
  val visited = new mutable.BitSet(n)

  while (!pq.isEmpty) {
    val u = pq.poll()
    if (!visited(u)) {
      visited += u
      for ((v, d) <- roads(u)) {
        val newDist = dist(u) + d
        if (newDist < dist(v)) {
        	dist(v) = newDist
          pq.add(v)
        }
        if (u > 0 && newDist <= rails(v)) rails(v) = -1
      }
    }
  }

  println(k - rails.count(_ != -1))
}

import scala.collection._

object B extends App {

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

  val Array(n, m, k) = readInts(3)
  val roads = Array.fill(n){ mutable.Map.empty[Int, Int] }

  for (i <- 0 until m) {
    val tl = tokenizeLine
    val _u, _v, x = tl.nextToken.toInt
    val u = _u - 1
    val v = _v - 1
    if (x < roads(u).getOrElse(v, Int.MaxValue)) {
      roads(u).put(v, x)
      roads(v).put(u, x)
    }
  }

  val rails = Array.fill(n) { -1 }

  for (i <- 0 until k) {
    val tl = tokenizeLine
    val _s, y = tl.nextToken.toInt
    val s = _s - 1
    if (rails(s) == -1 || y < rails(s)) rails(s) = y
  }

  for (i <- 1 until n) {
    if (rails(i) >= 0 && rails(i) < roads(0).getOrElse(i, Int.MaxValue)) {
      roads(0).put(i, rails(i))
    } else rails(i) = -1
  }

  val dists = Array.fill(n) { Long.MaxValue }
  dists(0) = 0

  val pq = mutable.PriorityQueue((0, 0L))(Ordering.by(_._2))
  //val pq = new java.util.PriorityQueue[(Int, Long)](1, Ordering.by(_._2))
  //pq.add((0, 0))
  val visited = new mutable.BitSet(n)

  while (!pq.isEmpty) {
    val (u, distU) = pq.dequeue()
    if (!visited(u)) {
      visited += u
      for ((v, d) <- roads(u)) {
        val newDist = distU + d
        if (newDist < dists(v)) {
        	dists(v) = newDist
          pq.enqueue((v, newDist))
        }
        if (u > 0 && newDist <= rails(v)) rails(v) = -1
      }
    }
  }

  println(k - rails.count(_ != -1))
}


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

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

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

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

  def solve(): Unit = {
    REP(ni()) { _ =>
      val N, K = ni()
      val q = N / K
      val m = N % K
      REP(K) { i =>
        val cnt = if (i < m) q + 1 else q
        REP(cnt){_ => out.print((i + 'a').toChar) }
      }
      out.println()
    }
  }

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

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

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

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

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

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

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

  override def apply(io: IO): io.type = {
    val input = io.read[Seq[(Int, Int)]]
    val ans = input map {case (n, k) =>
      val res = for {
        i <- 0 until k
        j <- 1 to (n/k)
      } yield ('a' + i).toChar
      res.padTo(n, res.last).mkString
    }
    io.writeAll(ans, separator = "\n")
  }
}
/****************************[Ignore Template Below]****************************************/
trait CodeForcesApp {
  lazy val isOnlineJudge: Boolean = sys.props.get("ONLINE_JUDGE").exists(_.toBoolean)
  def debug(x: => Any): Unit = if (!isOnlineJudge) println(x)
  def apply(io: IO): io.type
  def main(args: Array[String]): Unit = this(new IO(System.in, System.out)).close()
}
/***********************************[Scala I/O]*********************************************/
import java.io._, java.util.StringTokenizer
import scala.collection.generic.CanBuild

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

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

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

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

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

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

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

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

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

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

object Main {

  def main(args: Array[String]) {

    val std = scala.io.StdIn
    val t = std.readLine().toInt

    for (_ <- 0 until t) {
      val values = std.readLine().split(" ").map(_.toInt)
      val n = values(0)
      val k = values(1)
      val stringBuilder = new StringBuilder("")
      for (i <- 0 until n) {
        stringBuilder.append(('a' + i % k).toChar)
      }
      println(stringBuilder.toString())
    }

  }
}

object CF_527_3_A {
  def solve(xs: Seq[(Int, Int)]): Seq[String] = {
    xs.map{
      case (n, k) =>
        val str = ('a' to ('a' + k - 1).toChar).mkString
        str * (n/k) + str.take(n%k)
    }
  }

  // Specify Input and Output formats here:
  def formatIn(i: Input) = Seq.fill(i.int)((i.int, i.int))
  def formatOut(out: Seq[String]): String = out.mkString("\n")

//     ~~~ Boilerplate & utility methods that don't change ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  def main(args: Array[String]) = {
    val out = new java.io.PrintWriter(System.out)
    val resultStr = new Input(System.in).solveStr
    out.println(resultStr)
    out.close()
  }

  import java.util.Scanner
  class Input(sc: Scanner) {
    def this(i: java.io.InputStream) = this(new Scanner(i))
    def this(s: String) = this(new Scanner(s.stripMargin))
    def int = sc.nextInt()
    def long = sc.nextLong()
    def double = sc.nextDouble()
    def nextLine = sc.nextLine()
    // Remember to call nextLine to advance past the newline character (if required) before collecting the line:
    def collect[T](f: String => T) = {sc.nextLine().split(" ").toVector.map(f)}
    def intSeq = collect(_.toInt)
    def doubleSeq = collect(_.toDouble)
    def getLines: Vector[String] = if (!sc.hasNextLine) Vector.empty else sc.nextLine +: getLines
    def solveVal = (solve _).tupled(formatIn(this))
    def solveStr = formatOut(solveVal)
  }

  def ceil(a: Int, b: Int) = (a - 1) / b + 1
  def ceil(a: Long, b: Long) = (a - 1) / b + 1
  val modulo = 1000000007

  import language.implicitConversions
  implicit def stringToInput(s: String): Input = new Input(s)
  implicit class makeTupledWorkOnFunction1[-T,+R](f: T => R) { def tupled: T => R = x => f(x) }
}

object CF_527_3_A {
  def solve(xs: Seq[(Int, Int)]): Seq[String] = {
    xs.map{
      case (n, k) =>
        println(s"$n $k")
        val str = ('a' to ('a' + k - 1).toChar).mkString
        str * (n/k) + str.take(n%k)
    }
  }

  // Specify Input and Output formats here:
  def formatIn(i: Input) = Seq.fill(i.int)((i.int, i.int))
  def formatOut(out: Seq[String]): String = out.mkString("\n")

//     ~~~ Boilerplate & utility methods that don't change ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  def main(args: Array[String]) = {
    val out = new java.io.PrintWriter(System.out)
    val resultStr = new Input(System.in).solveStr
    out.println(resultStr)
    out.close()
  }

  import java.util.Scanner
  class Input(sc: Scanner) {
    def this(i: java.io.InputStream) = this(new Scanner(i))
    def this(s: String) = this(new Scanner(s.stripMargin))
    def int = sc.nextInt()
    def long = sc.nextLong()
    def double = sc.nextDouble()
    def nextLine = sc.nextLine()
    // Remember to call nextLine to advance past the newline character (if required) before collecting the line:
    def collect[T](f: String => T) = {sc.nextLine().split(" ").toVector.map(f)}
    def intSeq = collect(_.toInt)
    def doubleSeq = collect(_.toDouble)
    def getLines: Vector[String] = if (!sc.hasNextLine) Vector.empty else sc.nextLine +: getLines
    def solveVal = (solve _).tupled(formatIn(this))
    def solveStr = formatOut(solveVal)
  }

  def ceil(a: Int, b: Int) = (a - 1) / b + 1
  def ceil(a: Long, b: Long) = (a - 1) / b + 1
  val modulo = 1000000007

  import language.implicitConversions
  implicit def stringToInput(s: String): Input = new Input(s)
  implicit class makeTupledWorkOnFunction1[-T,+R](f: T => R) { def tupled: T => R = x => f(x) }
}


object B extends App {

  @inline def tokenizeLine = new java.util.StringTokenizer(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) }

  val Array(_n, l, _k) = readLongs(3)
  val n = _n.toInt
  val k = if (l % _n == 0) (_k min (l / _n)).toInt else (_k min (l / _n + 1)).toInt
  val remainder = (l % n).toInt
  val MOD = 1000000007L

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

  val _as = readInts(n).zipWithIndex.sorted
  //val _as = Array.fill(n){ Random.nextInt }.zipWithIndex.sorted
  val as = Array.ofDim[(Int, Int)](n)

  as(0) = (1, _as(0)._2)
  for (i <- 1 until n) {
    as(i) = (if (_as(i)._1 == _as(i - 1)._1) as(i - 1)._1 else as(i - 1)._1 + 1, _as(i)._2)
  }

  for (i <- as.indices) dp1(1)(as(i)._1) += 1

  var res = l % MOD

  for (i <- 2 to k) {
    var prev = 0
    var acc = 0
    for (j <- 0 until n) {
      val (a, aInd) = as(j)
      if (a != prev) acc = ((acc + dp1(i - 1)(a)) % MOD).toInt
      val d1 = if (i <= l / n) acc else 0
      dp1(i)(a) = (dp1(i)(a) + d1) % MOD.toInt
      val d2 = if (aInd < remainder) acc else 0
      res = ((res + d1 * (l / n % MOD + MOD - i + 1) % MOD) + d2) % MOD
      prev = a
    }
  }

  println(res % MOD)
}

object B extends App {

  @inline def tokenizeLine = new java.util.StringTokenizer(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) }

  val Array(_n, l, _k) = readLongs(3)
  val n = _n.toInt
  val k = if (l % _n == 0) (_k min (l / _n)).toInt else (_k min (l / _n + 1)).toInt
  val remainder = (l % n).toInt
  val MOD = 1000000007L

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

  val _as = readInts(n).zipWithIndex.sorted
  val as = Array.ofDim[(Int, Int)](n)

  as(0) = (1, _as(0)._2)
  for (i <- 1 until n) {
    as(i) = (if (_as(i)._1 == _as(i - 1)._1) as(i - 1)._1 else as(i - 1)._1 + 1, _as(i)._2)
  }

  for (i <- as.indices) dp1(1)(as(i)._1) += 1

  var res = l % MOD

  for (i <- 2 to k) {
    var prev = 0
    var acc = 0
    for (j <- 0 until n) {
      val (a, aInd) = as(j)
      if (a != prev) acc = ((acc + dp1(i - 1)(a)) % MOD).toInt
      val d1 = if (i <= l / n) acc else 0
      dp1(i)(a) = (dp1(i)(a) + d1) % MOD.toInt
      val d2 = if (aInd < remainder) acc else 0
      res = ((res + d1 * (l / n % MOD + MOD - i + 1) % MOD) + d2) % MOD
      prev = a
    }
  }

  println(res % MOD)
}

import scala.collection._

object B extends App {

  @inline def tokenizeLine = new java.util.StringTokenizer(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, l, _k) = readLongs(3)
  val n = _n.toInt
  val k = if (l % _n == 0) (_k min (l / _n)).toInt else (_k min (l / _n + 1)).toInt
  val MOD = 1000000007L

  val _as = readInts(n).zipWithIndex.sorted
  val as = Array.ofDim[(Int, Int)](n)

  as(0) = (1, _as(0)._2)
  for (i <- 1 until n) {
    as(i) = (if (_as(i)._1 == _as(i - 1)._1) as(i - 1)._1 else as(i - 1)._1 + 1, _as(i)._2)
  }

  val cnts = Array.fill(n + 1) { 0 }
  for (a <- as) cnts(a._1) += 1

  val MODB = BigInt(MOD)
  val fs, fs2, fInvs = Array.ofDim[Long](k + 1)

  var f1 = 1L
  fInvs(0) = 1
  for (i <- 1 until fs.length) { // precompute factorials
    fInvs(i) = BigInt(f1).modInverse(MODB).toLong
    f1 = f1 * (i + 1) % MOD
  }

  val base = l / _n
  var f2 = base
  fs(0) = 1
  for (i <- 1 until fs.length) { // precompute factorials
    fs(i) = f2
    f2 = f2 * (base - i) % MOD
  }

  val base2 = l / _n + 1
  var f3 = base2
  fs2(0) = 1
  for (i <- 1 until fs.length) { // precompute factorials
    fs2(i) = f3
    f3 = f3 * (base2 - i) % MOD
  }

  def pascal(_k: Int): Long = {
    fs(_k) * fInvs(_k) % MOD
  }

  def pascal2(_k: Int): Long = {
    fs2(_k) * fInvs(_k) % MOD
  }

  val dp = Array.fill(k + 1, n + 1) { 0L }

  for (j <- dp(1).indices) dp(1)(j) = 1
  //println(pascal(1), pascal(2))
  var res = l % MOD

  for (i <- 2 to k) {
    var prev = 1
    for (j <- 0 until n) {
      val (a, aInd) = as(j)
      dp(i)(a) = (dp(i - 1)(a) * cnts(a) + (if (a > prev) dp(i)(a - 1) else 0)) % MOD
      val p = if (aInd < l % n) pascal2(i) else pascal(i)
      res = (res + dp(i)(a) * p) % MOD
      prev = a
    }
  }

  println(res % MOD)
}

import scala.collection._

object B extends App {

  @inline def tokenizeLine = new java.util.StringTokenizer(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, l, _k) = readLongs(3)
  val n = _n.toInt
  val k = if (l % _n == 0) (_k min (l / _n)).toInt else (_k min (l / _n + 1)).toInt
  val MOD = 1000000007L

  val _as = readInts(n).zipWithIndex.sorted
  val as = Array.ofDim[(Int, Int)](n)

  as(0) = (1, _as(0)._2)
  for (i <- 1 until n) {
    as(i) = (if (_as(i)._1 == _as(i - 1)._1) as(i - 1)._1 else as(i - 1)._1 + 1, _as(i)._2)
  }

  val cnts = Array.fill(n + 1) { 0 }
  for (a <- as) cnts(a._1) += 1

  val MODB = BigInt(MOD)
  val fs, fs2, fInvs = Array.ofDim[Long](k + 1)

  var f1 = 1L
  fInvs(0) = 1
  for (i <- 1 until fs.length) { // precompute factorials
    fInvs(i) = BigInt(f1).modInverse(MODB).toLong
    f1 = f1 * (i + 1) % MOD
  }

  val base = l / _n
  var f2 = base
  fs(0) = 1
  for (i <- 1 until fs.length) { // precompute factorials
    fs(i) = f2
    f2 = f2 * (base - i) % MOD
  }

  val base2 = l / _n + 1
  var f3 = base2
  fs2(0) = 1
  for (i <- 1 until fs.length) { // precompute factorials
    fs2(i) = f3
    f3 = f3 * (base2 - i) % MOD
  }

  def pascal(_k: Int): Long = {
    fs(_k) * fInvs(_k) % MOD
  }

  def pascal2(_k: Int): Long = {
    fs2(_k) * fInvs(_k) % MOD
  }

  val dp = Array.fill(k + 1, n + 1) { 0L }

  for (j <- dp(1).indices) dp(1)(j) = 1
  //println(pascal(1), pascal(2))
  var res = l

  for (i <- 2 to k) {
    var prev = 1
    for (j <- 0 until n) {
      val (a, aInd) = as(j)
      dp(i)(a) = (dp(i - 1)(a) * cnts(a) + (if (a > prev) dp(i)(a - 1) else 0)) % MOD
      val p = if (aInd < l % n) pascal2(i) else pascal(i)
      res = (res + dp(i)(a) * p) % MOD
      prev = a
    }
  }

  println(res)
}


object Main {
  def main(args: Array[String]): Unit = {
    val n = readInt()
    val a = readLine().split(" ").map(_.toInt)
    if (a.sum % n == 0) println(n)
    else println(n - 1)
  }
}

import scala.io.Source

object Solution extends App {
  val in = Source.stdin.getLines()
  val n = in.next().toInt
  val sum = in.next().split(' ').map(_.toInt).sum
  if (sum % n == 0)
    println(n)
  else
    println(n - 1)
}

import java.util.Scanner

object Test extends App {
  val sc = new Scanner(System.in);
  val n = sc.nextInt()
  val sum = (0 until n).map(_ => sc.nextInt()).sum
  println(if (sum % n == 0) n else n-1)
}

import java.util.Scanner
import scala.collection.mutable._

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

  val n = sc.nextInt()
  val a = Array.tabulate(n)(_ => sc.nextInt())

  val sum = a.fold(0)(_ + _)

  println(if (sum % n == 0) n else n-1)

}

import java.util.Scanner
import java.io.PrintWriter
import java.util.Arrays.binarySearch
import scala.annotation.tailrec
import scala.collection.mutable
import scala.collection.mutable.ListBuffer
import scala.collection.mutable.ArrayBuffer
import scala.util.Random
import scala.util.control.Breaks._

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

  def solve(): Int = {
    val N = sc.nextInt
    val S = List.fill(N)(sc.nextInt).sum
    if (((S % N) + N) % N == 0) N
    else N - 1
  }
  out.println(solve)
  out.close
}

import java.util.Scanner
import java.io.PrintWriter
import java.util.Arrays.binarySearch
import scala.annotation.tailrec
import scala.collection.mutable
import scala.collection.mutable.ListBuffer
import scala.collection.mutable.ArrayBuffer
import scala.util.Random
import scala.util.control.Breaks._

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

  def solve(): Int = {
    val N = sc.nextInt
    val S = List.fill(N)(sc.nextInt).sum

    if (S % 2 == 0) N
    else N - 1
  }
  out.println(solve)
  out.close
}


import scala.io.StdIn._

object A extends App {
  for (_ <- 0 until readInt) {
    def equalize(d: Array[Int]): (Int, List[(Int, Int)]) = {
      @annotation.tailrec
      def loop(count: Int, res: List[(Int, Int)]): (Int, List[(Int, Int)]) = {
        if(d.forall(_ == 0))
          (count, res)
        else {
           val i = d.indexWhere(_ > 0)
           val j = d.indexWhere(_ < 0)
            d(i) = d(i) - 1
            d(j) = d(j) + 1
            loop(count + 1, (i, j) :: res)
        }
      }
      loop(0, Nil)
    }
    val n = readInt
    val a = readLine.split(" ").map(_.toInt)
    val b = readLine.split(" ").map(_.toInt)
    val diff =  (0 until n).map(i => a(i) - b(i)).toArray
    if (diff.sum == 0) {
      val (count, res) = equalize(diff)
      println(count)
      res.foreach{case (i, j) => println(s"${i+1} ${j+1}")}
    } else
      println(-1)
  }
}

object A extends App {
  import scala.io.StdIn._

  val t = readInt()

  (0 until t).foreach { _ =>
    val n  = readInt()
    val an = readLine().split(" ").map(_.toInt)
    val bn = readLine().split(" ").map(_.toInt)

    val (pos, neg) = (an zip bn).zipWithIndex.foldLeft((List.empty[Int], List.empty[Int])) {
      case ((pos, neg), ((ai, bi), i)) if ai > bi => (List.fill(ai - bi)(i) ::: pos, neg)
      case ((pos, neg), ((ai, bi), i)) if ai < bi => (pos, List.fill(bi - ai)(i) ::: neg)
      case (state, _)                             => state
    }

    if (pos.length == neg.length) {
      println(pos.length)
      (pos zip neg).foreach { case (pi, ni) => println(s"${pi + 1} ${ni + 1}") }
    } else println("-1")

  }
}


/**
 * Created by Mikhail on 04.01.2015.
 */

object _491A {
  def main(args: Array[String]) {
    var a = readInt()
    var b = readInt()
    var s = new StringBuilder()
    var i = 1
    while (i < a + 1) {
      s ++= (i + " ")
      i += 1
    }
    i = a + b + 1
    while (i > a) {
      s ++= (i + " ")
      i -= 1
    }
    println(s)
  }
}

import java.util.Scanner

object Main {

  def main(args: Array[String]) {
    val scanner = new Scanner(System.in)
    val a = scanner.nextInt()
    val b = scanner.nextInt()

    val n = a + b + 1
    val result = ((b + 1) to n) ++ (1 to b).reverse
    println(result.mkString(" "))
  }

}

object UpTheHill491A  extends App {
  import java.io.{InputStream, PrintStream}
  import java.util.Scanner
  solve(System.in,System.out)
  def solve(in: InputStream, out: PrintStream): Unit = {
    val scanner = new Scanner(in)
    val a = scanner.nextInt()
    val b = scanner.nextInt()
    val n = a+b+1
    val up = (n-a) to n
    val down = (n-a-1) to 1 by -1
    val hill = (up ++ down).mkString(" ")
    out.println(hill)
  }
}


import java.io.BufferedReader
import java.io.ByteArrayInputStream
import java.io.InputStreamReader
import scala.collection.mutable.ArrayBuffer

object FBridgeClub {
  def main(args: Array[String]): Unit = {
    val file = new BufferedReader(new InputStreamReader(System.in))
//                        val file = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(
//            "3 2\n1 9 1 5 7 8 1 1".getBytes)))

    var t = file.readLine.toInt
    while (t > 0) {
        t = t - 1
        val Array(n,s) = file.readLine.split(" ").map(_.toLong)
        println(s / (n*n))
    }
  }
}

import java.io._
import java.util.StringTokenizer
import scala.+:
import scala.collection.mutable
import scala.collection.mutable._
import scala.collection.immutable._
import scala.util.Sorting.quickSort
import scala.math._

object Main extends App {
    val writer =  new PrintWriter(System.out, false)
    val reader = new BufferedReader(new InputStreamReader(System.in))
    var st = new StringTokenizer("")
    def next(): String = {while (!st.hasMoreTokens) {st = new StringTokenizer(reader.readLine())};st.nextToken()}
    def readInt(): Int = next().toInt
    def readLong(): Long = next().toLong
    def readString(): String = next()

    case class fenwick(n: Int) {
        val tree = new Array[Long](n+1)
        def query(ind: Int): Long = {
            var idx = ind
            var ans = 0L
            while (idx > 0) {
                ans += tree(idx)
                idx -= (idx & -idx)
            }
            return ans
        }
        def update(ind: Int, value: Long): Unit = {
            var idx = ind
            while (idx <= n) {
                tree(idx) += value
                idx += (idx & -idx)
            }
        }
    }

    case class pair(var a: Int, var b: Int)extends Ordered[pair] {
        override def compare(that: pair): Int = {
            return (a).compareTo(that.a)
        }
    }

    def gcd(a: Int, b: Int): Int = b match {
        case 0 => a
        case n => gcd(b, a % b)
    }
    val rem = 1000000007
    val maxn = 200010
    val mapp = mutable.Map[pair, Int]()

    val t = readInt()

    for (tt <- 1 to t) {
        var n = readLong()
        val s = readLong()
        n = n * n
        val ans = (s / n).toLong
        writer.println(ans)

        def check(kk: Long, fi: Long): Boolean = {
            return false
        }
        def bs(st:Long, en:Long, fi:Long): Long = {
            if (en - st <= 1L) {
                if (check(st, fi))
                    return st
                else
                    return en
            }
            val mid = (st + en) / 2L
            if (check(mid, fi))
                return bs(st, mid, fi)
            else
                return bs(mid, en, fi)
        }
    }
    writer.flush()
}

import java.io.BufferedReader
import java.io.ByteArrayInputStream
import java.io.InputStreamReader
import scala.collection.mutable.ArrayBuffer

object FBridgeClub {
  def main(args: Array[String]): Unit = {
    val file = new BufferedReader(new InputStreamReader(System.in))
//                        val file = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(
//            "3 2\n1 9 1 5 7 8 1 1".getBytes)))

    var t = file.readLine.toInt
    while (t > 0) {
        t = t - 1
        val Array(n,s) = file.readLine.split(" ").map(_.toLong)
        println(s / n*n)
    }
  }
}


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

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

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

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

  def solve(): Unit = {
    REP(ni()) { _ =>
      val n = nl()
      val a, b = ni()
      if (2 * a <= b) {
        out.println(n * a)
      } else {
        if (n % 2 == 0) {
          out.println(n / 2 * b)
        } else {
          out.println(n / 2 * b + a)
        }
      }
    }
  }

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

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

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

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

  def debug(num: Long): Unit = {
    System.err.println(num)
  }

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

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

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

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

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

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

object CF_540_3_A {
// date: 19/02/2019

  type In  = (Int, Seq[String])
  type Out = String

  def solve(in: In): Out = {
    val (q, xs) = in
    val results = xs map {s =>
      val Seq(n, a, b) = (s: Input).collect(_.toLong)
      val c1 = a * n
      val c2 = (n/2) * b + (n%2)*a
      c1 min c2
    }
    results.mkString("\n")
  }

//   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//   Specify Input and Output formats on RHS here:

  def formatIn(i: Input): In       = (i.int, {i.nextLine; i.getLines})
  def formatOut(out: Out): String  = out.toString

//   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//   Boilerplate & utility methods that don't change (so ignore):

  import java.util.Scanner
  import java.io.InputStream
  import language.implicitConversions

  def main(args: Array[String]): Unit = {
    val out = new java.io.PrintWriter(System.out)
    val resultStr = new Input(System.in).solveStr
    out.println(resultStr)
    out.close()
  }

  class Input(val sc: Scanner) {
    // This class is useful for convenience, and for mock stdin in test scripts
    // Remember to call nextLine to advance past the newline character (if required) before taking a whole line
    def this(i: InputStream)       = this(new Scanner(i))
    def this(s: String)            = this(new Scanner(s.stripMargin))
    def int                        = sc.nextInt()
    def long                       = sc.nextLong()
    def double                     = sc.nextDouble()
    def nextLine                   = sc.nextLine()
    def collect[T](f: String => T) = sc.nextLine().split(" ").toVector.map(f)
    def intSeq                     = collect(_.toInt)
    def doubleSeq                  = collect(_.toDouble)
    def getLines: Vector[String]   = if (!sc.hasNextLine) Vector.empty
                                     else sc.nextLine +: getLines
    def getLines(lineCount: Int)   = {nextLine; (1 to lineCount).map(_ => sc.nextLine)}
    def solveVal: Out              = (solve _).tupled(formatIn(this))
    def solveStr: String           = formatOut(solveVal)
  }

  // Ceiling division for Int & Long
  // `a` MUST be > 0. Incorrect otherwise.
  def divideCeil(a: Int, b: Int)   = (a - 1)/b + 1
  def divideCeil(a: Long, b: Long) = (a - 1)/b + 1

  // A frequently used number in these comps
  val modulo = 1000000007

  // For convenience in test scripts, treat a String as an Input
  implicit def stringToInput(s: String): Input = new Input(s)
  // Required to make `solveVal` work when input has just 1 value (i.e. not a Tuple)
  implicit class makeTupledWorkOnFunction1[-T,+R](f: T => R) {
    def tupled: T => R = x => f(x)
  }
}