You are given a matrix of size $$$n \times n$$$ filled with lowercase English letters. You can change no more than $$$k$$$ letters in this matrix.Consider all paths from the upper left corner to the lower right corner that move from a cell to its neighboring cell to the right or down. Each path is associated with the string that is formed by all the letters in the cells the path visits. Thus, the length of each string is $$$2n - 1$$$.Find the lexicographically smallest string that can be associated with a path after changing letters in at most $$$k$$$ cells of the matrix.A string $$$a$$$ is lexicographically smaller than a string $$$b$$$, if the first different letter in $$$a$$$ and $$$b$$$ is smaller in $$$a$$$.

The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2000$$$, $$$0 \le k \le n^2$$$) — the size of the matrix and the number of letters you can change. Each of the next $$$n$$$ lines contains a string of $$$n$$$ lowercase English letters denoting one row of the matrix.

Output the lexicographically smallest string that can be associated with some valid path after changing no more than $$$k$$$ letters in the matrix.

7 6
ypnxnnp
pnxonpm
nxanpou
xnnpmud
nhtdudu
npmuduh
pmutsnz

NoteIn the first sample test case it is possible to change letters 'b' in cells $$$(2, 1)$$$ and $$$(3, 1)$$$ to 'a', then the minimum path contains cells $$$(1, 1), (2, 1), (3, 1), (4, 1), (4, 2), (4, 3), (4, 4)$$$. The first coordinate corresponds to the row and the second coordinate corresponds to the column.

var fl = readline().split(' ').map(v=>+v);
var side = fl[0];
var change = fl[1];

var matrix = [];
for(var i = 0; i < side; i++) {
    matrix.push(readline());
}

var symbA = 'a';
var dirs = [{x: 1, y: 0}, {x: 0, y: 1}];

var paths = [{x: 0, y: 0, change: change}];
var line = matrix[0][0];

if(change && line !== symbA) {
    paths[0].change--;
    line = symbA;
}

for(var step = 2; step < side * 2; step++) {
    var min = {letter: '|', paths: []};
    var previous = null;

    for(var i = 0; i < paths.length; i++) {
        var path = paths[i];
        for(var j = 0; j < 2; j++) {
            var dir = dirs[j];
            var x = path.x + dir.x, y = path.y + dir.y;
            if(x === side || y === side) {
                previous = null;
                continue;
            }

            var letter = matrix[y][x];
            var change = path.change;

            if(change && letter !== symbA) {
                change--;
                letter = symbA;
            }

            var newPath = {x: x, y: y, change: change};
            if (letter < min.letter) {
                min.letter = letter;
                min.paths = [];
                previous = null;
            } else if (letter > min.letter) {
                previous = null;
                continue;
            }

            var code = x - y;
            if(j === 0 && previous) {
                if(previous.code === code) {
                    if(previous.change >= change) {
                        newPath = null;
                    } else {
                        min.paths.pop();
                    }
                }
            } else if (j === 1) {
                previous = {code: code, change: change};
            }
            if(newPath) {
                min.paths.push(newPath)
            }
        }
    }

    paths = min.paths;
    line += min.letter;
}

print(line);


Phoenix loves beautiful arrays. An array is beautiful if all its subarrays of length $$$k$$$ have the same sum. A subarray of an array is any sequence of consecutive elements.Phoenix currently has an array $$$a$$$ of length $$$n$$$. He wants to insert some number of integers, possibly zero, into his array such that it becomes beautiful. The inserted integers must be between $$$1$$$ and $$$n$$$ inclusive. Integers may be inserted anywhere (even before the first or after the last element), and he is not trying to minimize the number of inserted integers.

The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \le t \le 50$$$) — the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 100$$$). The second line of each test case contains $$$n$$$ space-separated integers ($$$1 \le a_i \le n$$$) — the array that Phoenix currently has. This array may or may not be already beautiful.

For each test case, if it is impossible to create a beautiful array, print -1. Otherwise, print two lines. The first line should contain the length of the beautiful array $$$m$$$ ($$$n \le m \le 10^4$$$). You don't need to minimize $$$m$$$. The second line should contain $$$m$$$ space-separated integers ($$$1 \le b_i \le n$$$) — a beautiful array that Phoenix can obtain after inserting some, possibly zero, integers into his array $$$a$$$. You may print integers that weren't originally in array $$$a$$$. If there are multiple solutions, print any. It's guaranteed that if we can make array $$$a$$$ beautiful, we can always make it with resulting length no more than $$$10^4$$$.

NoteIn the first test case, we can make array $$$a$$$ beautiful by inserting the integer $$$1$$$ at index $$$3$$$ (in between the two existing $$$2$$$s). Now, all subarrays of length $$$k=2$$$ have the same sum $$$3$$$. There exists many other possible solutions, for example:   $$$2, 1, 2, 1, 2, 1$$$  $$$1, 2, 1, 2, 1, 2$$$ In the second test case, the array is already beautiful: all subarrays of length $$$k=3$$$ have the same sum $$$5$$$.In the third test case, it can be shown that we cannot insert numbers to make array $$$a$$$ beautiful.In the fourth test case, the array $$$b$$$ shown is beautiful and all subarrays of length $$$k=4$$$ have the same sum $$$10$$$. There exist other solutions also.

const input = [];

function splbsi(line) {
  return line.split(" ").map((x) => parseInt(x));
}

const readLine = require("readline").createInterface({
  input: process.stdin,
  output: process.stdout,
});

readLine.on("line", (line) => {
  input.push(line);
});

readLine.on("close", () => {
  for (let i = 1; i < input.length; i += 2) {
    const [n, k] = splbsi(input[i]);
    const arr = splbsi(input[i + 1]);

    const nums = new Set(arr);

    if (nums.size > k) {
      console.log(-1);
      continue;
    }

    const res = [...nums];
    for (let j = 0; j < k - nums.size; j += 1) {
      res.push(1);
    }

    const answer = [];
    for (let i = 0; i < n; i += 1) {
      answer.push(...res);
    }

    console.log(answer.length);
    console.log(answer.join(" "));
  }
});

const processData = (lines) => {
  const nums = +lines[0]
  for (let i =0; i<nums;i++) {
    const [n, k] = lines[i*2+1].split(' ').map(x => +x)
    const arr = lines[i*2+2].split(' ').map(x => +x)
    const ns = new Set()
    for (const num of arr) {
      ns.add(num)
    }
    const s = Array.from(ns)
    if (s.length > k) {
      console.log(-1)
    } else {
      let diff = k - s.length
      while (diff) {
        s.push(s[0])
        diff--
      }
      console.log(n * s.length)
      console.log(arr.reduce((r) => r.concat(s), []).join(' '))
    }
  }
}

let i = ''
process.stdin.on('data', c => i += c)
process.stdin.on('end', () => {
  const {EOL} = require('os')
  const lines = i.split(EOL) /*your input text, split by lines*/
  processData(lines)
})

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}
/**
    5
    1 2
    2 3
    2 4
    4 5
**/

function main() {
    var range = readline();

    for(var r=0;r<range;r++) {
        var [n, limit] = readline().split(" ").map(x=> parseInt(x));
        var numbers = readline().split(" ").map(x=> parseInt(x));
        var unique = new Set(numbers);

        if(unique.size > limit) {
            console.log(-1);
            continue;
        }

        const output = [];
        for(let i=0;i<n;i++) {
            output.push(...Array.from(unique));

            for(let k=0;k<limit - unique.size;k++) {
                output.push(1);
            }
        }
        console.log(output.length);

        console.log(output.join(" "));

    }
}

const input = [];

function splbsi(line) {
  return line.split(" ").map((x) => parseInt(x));
}

const readLine = require("readline").createInterface({
  input: process.stdin,
  output: process.stdout,
});

readLine.on("line", (line) => {
  input.push(line);
});

readLine.on("close", () => {
  for (let i = 1; i < input.length; i += 2) {
    const [n, k] = splbsi(input[i]);
    const arr = splbsi(input[i + 1]);

    const nums = new Set(arr);

    if (nums.size > k) {
      console.log(-1);
      continue;
    }

    const res = [];
    for (let j = 0; j < n; j += 1) {
      res.push(...[...nums, 1]);
    }

    console.log(n * k);
    console.log(res.join(" "));
  }
});

const input = [];

function splbsi(line) {
  return line.split(" ").map((x) => parseInt(x));
}

const readLine = require("readline").createInterface({
  input: process.stdin,
  output: process.stdout,
});

readLine.on("line", (line) => {
  input.push(line);
});

readLine.on("close", () => {
  for (let i = 1; i < input.length; i += 2) {
    const [n, k] = splbsi(input[i]);
    const arr = splbsi(input[i + 1]);

    const nums = new Set(arr);

    if (nums.size > k) {
      console.log(-1);
      continue;
    }

    const res = [];
    for (let j = 0; j < n; j += 1) {
      res.push(...[...nums, 1]);
    }

    console.log(res.length);
    console.log(res.join(" "));
  }
});

const input = [];

function splbsi(line) {
  return line.split(" ").map((x) => parseInt(x));
}

const readLine = require("readline").createInterface({
  input: process.stdin,
  output: process.stdout,
});

readLine.on("line", (line) => {
  input.push(line);
});

readLine.on("close", () => {
  for (let i = 1; i < input.length; i += 2) {
    const [n, k] = splbsi(input[i]);
    const arr = splbsi(input[i + 1]);

    const nums = new Set(arr);

    if (nums.size > k) {
      console.log(-1);
      continue;
    }

    const res = [];
    while (res.length <= n) {
      res.push(...nums);
    }

    console.log(res.length);
    console.log(res.join(" "));
  }
});

const processData = (lines) => {
  const nums = +lines[0]
  for (let i =0; i<nums;i++) {
    const [n, k] = lines[i*2+1].split(' ').map(x => +x)
    const arr = lines[i*2+2].split(' ').map(x => +x)
    const ns = new Set()
    for (const num of arr) {
      ns.add(num)
    }
    const s = Array.from(ns)
    if (s.length > k) {
      console.log(-1)
    } else {
      console.log(n * s.length)
      console.log(arr.reduce((r) => r.concat(s), []).join(' '))
    }
  }
}

let i = ''
process.stdin.on('data', c => i += c)
process.stdin.on('end', () => {
  const {EOL} = require('os')
  const lines = i.split(EOL) /*your input text, split by lines*/
  processData(lines)
})

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}
/**
    5
    1 2
    2 3
    2 4
    4 5
**/

function main() {
    var range = readline();

    for(var r=0;r<range;r++) {
        var [n, limit] = readline().split(" ").map(x=> parseInt(x));
        var numbers = readline().split(" ").map(x=> parseInt(x));
        var oldNumber = [...numbers];
        numbers.sort((a,b) => a-b);

        if(numbers.join("") === oldNumber.join("")) {
            console.log(-1);
        } else {
            let max = Math.max.apply(null, oldNumber);
            let output = [];

            for(let i=0;i<n;i++) {
                let nu = max;
                while(nu) {
                    output.push(nu);
                    nu--;
                }
            }
            console.log(output.length);
            console.log(output.join(" "));
        }

    }
}

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}
/**
    5
    1 2
    2 3
    2 4
    4 5
**/

function main() {
    var range = readline();

    for(var r=0;r<range;r++) {
        var [n, limit] = readline().split(" ").map(x=> parseInt(x));
        var numbers = readline().split(" ").map(x=> parseInt(x));
        var unique = new Set(numbers);

        if(unique.size === numbers.length) {
            console.log(-1);
            continue;
        }

        const output = [];
        console.log(n*limit);
        for(let i=0;i<= n;i++) {
            output.push(...Array.from(unique));
            if(!unique.has(1)) {
                output.push(1);
            }
        }
        console.log(output.join(" "));

    }
}

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}
/**
    5
    1 2
    2 3
    2 4
    4 5
**/

function main() {
    var range = readline();

    for(var r=0;r<range;r++) {
        var [n, limit] = readline().split(" ").map(x=> parseInt(x));
        var numbers = readline().split(" ").map(x=> parseInt(x));
        var oldNumber = [...numbers];
        numbers.sort((a,b) => a-b);

        if(numbers.join("") === oldNumber.join("")) {
            console.log(-1);
        } else {
            let max = Math.max.apply(null, oldNumber);
            limit = limit * 2;
            let output = [];

            for(let i=0;i<limit;i++) {
                let nu = max;
                while(nu && output.length <= limit) {
                    output.push(nu);
                    nu--;
                }
            }
            console.log(output.length);
            console.log(output.join(" "));
        }

    }
}

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}
/**
    5
    1 2
    2 3
    2 4
    4 5
**/

function main() {
    var range = readline();

    for(var r=0;r<range;r++) {
        var [n, limit] = readline().split(" ").map(x=> parseInt(x));
        var numbers = readline().split(" ").map(x=> parseInt(x));
        var unique = new Set(numbers);

        if(unique.size >= limit) {
            console.log(-1);
            continue;
        }

        const output = [];
        for(let i=0;i<n;i++) {
            output.push(...Array.from(unique));

            for(let k=0;k<limit - unique.size;k++) {
                output.push(1);
            }
        }
        console.log(output.length);

        console.log(output.join(" "));

    }
}

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}
/**
    5
    1 2
    2 3
    2 4
    4 5
**/

function main() {
    var range = readline();

    for(var r=0;r<range;r++) {
        var [n, limit] = readline().split(" ").map(x=> parseInt(x));
        var numbers = readline().split(" ").map(x=> parseInt(x));
        var unique = new Set(numbers);

        if(unique.size === numbers.length) {
            console.log(-1);
            continue;
        }

        const output = [];
        for(let i=0;i<n;i++) {
            output.push(...Array.from(unique));

            for(let k=0;k<limit - unique.size;k++) {
                output.push(1);
            }
        }
        console.log(output.length);

        console.log(output.join(" "));

    }
}

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}
/**
    5
    1 2
    2 3
    2 4
    4 5
**/

function main() {
    var range = readline();

    for(var r=0;r<range;r++) {
        var [n, limit] = readline().split(" ").map(x=> parseInt(x));
        var numbers = readline().split(" ").map(x=> parseInt(x));
        var oldNumber = [...numbers];
        numbers.sort((a,b) => a-b);

        if(numbers.join("") === oldNumber.join("")) {
            console.log(-1);
        } else {
            let max = Math.max.apply(null, oldNumber);
            let output = [];

            for(let i=0;i<n * limit;i++) {
                let nu = max;
                while(nu && output.length <= n*limit - 1) {
                    output.push(nu);
                    nu--;
                }
            }
            console.log(output.length);
            console.log(output.join(" "));
        }

    }
}

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}
/**
    5
    1 2
    2 3
    2 4
    4 5
**/

function main() {
    var range = readline();

    for(var r=0;r<range;r++) {
        var [n, limit] = readline().split(" ").map(x=> parseInt(x));
        var numbers = readline().split(" ").map(x=> parseInt(x));
        var unique = new Set(numbers);

        if(unique.size === numbers.length) {
            console.log(-1);
            continue;
        }

        const output = [];
        console.log(n*limit);
        for(let i=0;i<= n;i++) {
            output.push(Array.from(unique));
            if(!unique.has(1)) {
                output.push(1);
            }
        }
        console.log(output.join(" "));

    }
}


Some people leave the lights at their workplaces on when they leave that is a waste of resources. As a hausmeister of DHBW, Sagheer waits till all students and professors leave the university building, then goes and turns all the lights off.The building consists of n floors with stairs at the left and the right sides. Each floor has m rooms on the same line with a corridor that connects the left and right stairs passing by all the rooms. In other words, the building can be represented as a rectangle with n rows and m + 2 columns, where the first and the last columns represent the stairs, and the m columns in the middle represent rooms.Sagheer is standing at the ground floor at the left stairs. He wants to turn all the lights off in such a way that he will not go upstairs until all lights in the floor he is standing at are off. Of course, Sagheer must visit a room to turn the light there off. It takes one minute for Sagheer to go to the next floor using stairs or to move from the current room/stairs to a neighboring room/stairs on the same floor. It takes no time for him to switch the light off in the room he is currently standing in. Help Sagheer find the minimum total time to turn off all the lights.Note that Sagheer does not have to go back to his starting position, and he does not have to visit rooms where the light is already switched off.

The first line contains two integers n and m (1 ≤ n ≤ 15 and 1 ≤ m ≤ 100) — the number of floors and the number of rooms in each floor, respectively. The next n lines contains the building description. Each line contains a binary string of length m + 2 representing a floor (the left stairs, then m rooms, then the right stairs) where 0 indicates that the light is off and 1 indicates that the light is on. The floors are listed from top to bottom, so that the last line represents the ground floor. The first and last characters of each string represent the left and the right stairs, respectively, so they are always 0.

Print a single integer — the minimum total time needed to turn off all the lights.

NoteIn the first example, Sagheer will go to room 1 in the ground floor, then he will go to room 2 in the second floor using the left or right stairs.In the second example, he will go to the fourth room in the ground floor, use right stairs, go to the fourth room in the second floor, use right stairs again, then go to the second room in the last floor.In the third example, he will walk through the whole corridor alternating between the left and right stairs at each floor.

function readInts() {
    return readline().split(' ').map(function (v) { return parseInt(v); });
}
function min(_a, _b) {
    var l1 = _a[0], r1 = _a[1];
    var l2 = _b[0], r2 = _b[1];
    // console.log('min', [l1, r1], [l2, r2]);
    return [l1 < l2 ? l1 : l2, r1 < r2 ? r1 : r2];
}
function solve(n, m, rooms) {
    var _a, _b, _c;
    var first = true;
    var _d = [0, 0], l = _d[0], r = _d[1];
    var f = 0;
    while (f < n) {
        var room = rooms[f];
        var leftmost = room.indexOf('1');
        if (leftmost < 0) {
            if (!first)
                _a = min([l + 1, r + 1], [r + m + 2, l + m + 2]), l = _a[0], r = _a[1];
        }
        else {
            var rightmost = room.lastIndexOf('1');
            if (first) {
                _b = [rightmost, m - leftmost + 1], l = _b[0], r = _b[1];
                first = false;
            }
            else {
                _c = min([m + 2 + r, m + 2 + l], [rightmost * 2 + 1 + l, (m - leftmost + 1) * 2 + 1 + r]), l = _c[0], r = _c[1];
            }
        }
        // console.log({ f, l, r });
        f++;
    }
    return l;
}
// function show(n: number, m: number, rooms: string[]) {
//     console.log({n, m, rooms}, '-->', solve(n, m, rooms))
// }
// show(2, 2, [
//     '0010',
//     '0100']);
// show(3, 4, [
//     '001000',
//     '000010',
//     '000010']);
// show(4, 3, [
//     '01110',
//     '01110',
//     '01110',
//     '01110'])
var _a = readInts(), n = _a[0], m = _a[1];
var rooms = [];
for (var i = 0; i < n; i++)
    rooms.push(readline());
print(solve(n, m, rooms));

var arr = readline().split(' ');
var n = parseInt(arr[0]);
var m = parseInt(arr[1]);
var v = Array(n);
var count = 0;
var ef = 0;
for (var i = 0; i < n; i ++)
{
    var arr = readline();
    var line = arr.substr(1,m);
    // var line = Array(m).fill(0);
    // for (var j = 0; j < m; j ++)
    // {
    //     line[j] = arr.charAt(j+1)=='1'?1:0;
    // }
    // v.splice(v, 0, line);
    if (line.indexOf('1') != -1)
    {
        v[count++] = line;
    }
    else if (count !== 0)
    {
        ef ++;
    }
}
n = count;
// print(JSON.stringify(v));

if (n === 0)
{
    print(0);
}
var dp = [Array(n+2).fill(0), Array(n+2).fill(0)];
for (var i = 0; i < n; i ++)
{
    var r = v[n-1-i].lastIndexOf('1');
    var l = v[n-1-i].indexOf('1');
    // print(l, r);
    if (i === 0)
    {
        if (n == 1)
        {
            print(r+1 + ef);
        }
        else
        {
            dp[0][i] = (r+1)*2 + 1;
            dp[1][i] = m + 2;
        }
    }
    else if (i == n-1)
    {
        print(Math.min( dp[0][i-1]+(r+1), dp[1][i-1]+(m-l)) + ef);
    }
    else
    {
        dp[0][i] = Math.min( dp[0][i-1]+(r+1)*2, dp[1][i-1]+(m+1)) + 1;
        dp[1][i] = Math.min( dp[0][i-1]+(m+1), dp[1][i-1]+(m-l)*2) + 1;
    }
    // print(JSON.stringify(dp));
}

var arr = readline().split(' ');
var n = parseInt(arr[0]);
var m = parseInt(arr[1]);
var v = Array(n);
for (var i = 0; i < n; i ++)
{
    var arr = readline();
    var line = arr.substr(1,m);
    // var line = Array(m).fill(0);
    // for (var j = 0; j < m; j ++)
    // {
    //     line[j] = arr.charAt(j+1)=='1'?1:0;
    // }
    // v.splice(v, 0, line);
    v[n-i-1] = line;
}
// print(JSON.stringify(v));

var dp = [Array(n+2).fill(0), Array(n+2).fill(0)];
for (var i = 0; i < n; i ++)
{
    var r = v[i].lastIndexOf('1');
    var l = v[i].indexOf('1');
    // print(l, r);
    if (i === 0)
    {
        dp[0][i] = (r+1)*2 + 1;
        dp[1][i] = m + 2;
    }
    else if (i == n-1)
    {
        print(Math.min( dp[0][i-1]+(r+1), dp[1][i-1]+(m-l)));
    }
    else
    {
        dp[0][i] = Math.min( dp[0][i-1]+(r+1)*2, dp[1][i-1]+(m+1)) + 1;
        dp[1][i] = Math.min( dp[0][i-1]+(m+1), dp[1][i-1]+(m-l)*2) + 1;
    }
    // print(JSON.stringify(dp));
}

var arr = readline().split(' ');
var n = parseInt(arr[0]);
var m = parseInt(arr[1]);
var v = Array(n);
var count = 0;
var ef = 0;
for (var i = 0; i < n; i ++)
{
    var arr = readline();
    var line = arr.substr(1,m);
    // var line = Array(m).fill(0);
    // for (var j = 0; j < m; j ++)
    // {
    //     line[j] = arr.charAt(j+1)=='1'?1:0;
    // }
    // v.splice(v, 0, line);
    if (line.indexOf('1') != -1)
    {
        v[count++] = line;
    }
    else if (count !== 0)
    {
        ef ++;
    }
}
n = count;
// print(JSON.stringify(v));

var dp = [Array(n+2).fill(0), Array(n+2).fill(0)];
for (var i = 0; i < n; i ++)
{
    var r = v[n-1-i].lastIndexOf('1');
    var l = v[n-1-i].indexOf('1');
    // print(l, r);
    if (i === 0)
    {
        dp[0][i] = (r+1)*2 + 1;
        dp[1][i] = m + 2;
    }
    else if (i == n-1)
    {
        print(Math.min( dp[0][i-1]+(r+1), dp[1][i-1]+(m-l)) + ef);
    }
    else
    {
        dp[0][i] = Math.min( dp[0][i-1]+(r+1)*2, dp[1][i-1]+(m+1)) + 1;
        dp[1][i] = Math.min( dp[0][i-1]+(m+1), dp[1][i-1]+(m-l)*2) + 1;
    }
    // print(JSON.stringify(dp));
}

var arr = readline().split(' ');
var n = parseInt(arr[0]);
var m = parseInt(arr[1]);
var v = Array(n);
var count = 0;
var ef = 0;
for (var i = 0; i < n; i ++)
{
    var arr = readline();
    var line = arr.substr(1,m);
    // var line = Array(m).fill(0);
    // for (var j = 0; j < m; j ++)
    // {
    //     line[j] = arr.charAt(j+1)=='1'?1:0;
    // }
    // v.splice(v, 0, line);
    if (line.indexOf('1') != -1)
    {
        v[count++] = line;
    }
    else if (count !== 0)
    {
        ef ++;
    }
}
n = count;
// print(JSON.stringify(v));

var dp = [Array(n+2).fill(0), Array(n+2).fill(0)];
for (var i = 0; i < n; i ++)
{
    var r = v[n-1-i].lastIndexOf('1');
    var l = v[n-1-i].indexOf('1');
    // print(l, r);
    if (i === 0)
    {
        if (n == 1)
        {
            print(r+1 + ef);
        }
        else
        {
            dp[0][i] = (r+1)*2 + 1;
            dp[1][i] = m + 2;
        }
    }
    else if (i == n-1)
    {
        print(Math.min( dp[0][i-1]+(r+1), dp[1][i-1]+(m-l)) + ef);
    }
    else
    {
        dp[0][i] = Math.min( dp[0][i-1]+(r+1)*2, dp[1][i-1]+(m+1)) + 1;
        dp[1][i] = Math.min( dp[0][i-1]+(m+1), dp[1][i-1]+(m-l)*2) + 1;
    }
    // print(JSON.stringify(dp));
}


You are given two integers $$$A$$$ and $$$B$$$, calculate the number of pairs $$$(a, b)$$$ such that $$$1 \le a \le A$$$, $$$1 \le b \le B$$$, and the equation $$$a \cdot b + a + b = conc(a, b)$$$ is true; $$$conc(a, b)$$$ is the concatenation of $$$a$$$ and $$$b$$$ (for example, $$$conc(12, 23) = 1223$$$, $$$conc(100, 11) = 10011$$$). $$$a$$$ and $$$b$$$ should not contain leading zeroes.

The first line contains $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case contains two integers $$$A$$$ and $$$B$$$ $$$(1 \le A, B \le 10^9)$$$.

Print one integer — the number of pairs $$$(a, b)$$$ such that $$$1 \le a \le A$$$, $$$1 \le b \le B$$$, and the equation $$$a \cdot b + a + b = conc(a, b)$$$ is true.

NoteThere is only one suitable pair in the first test case: $$$a = 1$$$, $$$b = 9$$$ ($$$1 + 9 + 1 \cdot 9 = 19$$$).

process.stdin.resume()
process.stdin.setEncoding('utf-8')

var arr = ''
process.stdin.on('data', (i) => {
  arr += i
})

process.stdin.on('end', () => {
  arr = arr.split('\n').map(a => a.replace(/(\r\n|\n|\r)/gm, ''))
  const t = readInt(arr)

  for(let i = 0; i < t; i++) {
    const [A, B] = readInts(arr, i)
    // wr(arr[i], A, B)

    let st = B.toString()
    let len = st.length

    if(Math.pow(10, len) - 1 > B) len --

    wr(A * len)

  }
})

function wr(...x) {
  console.log(...x)
}

function sort(a, inc = true) {
  if(inc) a.sort((a, b) => a - b)
  else a.sort((a, b) => b - a)
}

function readInt(a, i) {
  if(i === undefined) return parseInt(a.shift())
  else return parseInt(a[i])
}
function readInts(a, i) {
  if(i === undefined) return arr.shift().split(' ').map(a => parseInt(a))
  else return arr[i].split(' ').map(a => parseInt(a))
}

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}

function main() {
    var t = parseInt(readline());
    var A, B;
    for (var i = 0; i < t; i++)
    {
        var line = readline().split(' ')
        A = parseInt(line[0]);
        B = parseInt(line[1]);
        console.log(A * (('' + (B + 1)).length - 1));
    }

}

'use strict'
var t=Number(readline());

while(t--){
    var s=readline().replace(/\r$/, '').split(' ').map(Number);
    var a=s[0];
    var b=s[1];

    var cnt=0,nine=0;

    while((nine*10+9)<=b){
        nine*=10;
        nine+=9;
        cnt++;
   }
    var ans=Number(a*cnt);
    print(ans);
}

'use strict'
var t=Number(readline());

while(t--){
    var s=readline().replace(/\r$/, '').split(' ').map(Number);
    var a=s[0];
    var b=s[1];

    var cnt=0,nine=0;

    while((nine*10+9)<=b){
        nine*=10;
        nine+=9;
        cnt++;
        print("pp");
   }
    var ans=Number(a*cnt);
    print(ans);
}


Asya loves animals very much. Recently, she purchased $$$n$$$ kittens, enumerated them from $$$1$$$ and $$$n$$$ and then put them into the cage. The cage consists of one row of $$$n$$$ cells, enumerated with integers from $$$1$$$ to $$$n$$$ from left to right. Adjacent cells had a partially transparent partition wall between them, hence there were $$$n - 1$$$ partitions originally. Initially, each cell contained exactly one kitten with some number.Observing the kittens, Asya noticed, that they are very friendly and often a pair of kittens in neighboring cells wants to play together. So Asya started to remove partitions between neighboring cells. In particular, on the day $$$i$$$, Asya:  Noticed, that the kittens $$$x_i$$$ and $$$y_i$$$, located in neighboring cells want to play together.  Removed the partition between these two cells, efficiently creating a single cell, having all kittens from two original cells. Since Asya has never putted partitions back, after $$$n - 1$$$ days the cage contained a single cell, having all kittens.For every day, Asya remembers numbers of kittens $$$x_i$$$ and $$$y_i$$$, who wanted to play together, however she doesn't remember how she placed kittens in the cage in the beginning. Please help her and find any possible initial arrangement of the kittens into $$$n$$$ cells.

The first line contains a single integer $$$n$$$ ($$$2 \le n \le 150\,000$$$) — the number of kittens. Each of the following $$$n - 1$$$ lines contains integers $$$x_i$$$ and $$$y_i$$$ ($$$1 \le x_i, y_i \le n$$$, $$$x_i \ne y_i$$$) — indices of kittens, which got together due to the border removal on the corresponding day. It's guaranteed, that the kittens $$$x_i$$$ and $$$y_i$$$ were in the different cells before this day.

For every cell from $$$1$$$ to $$$n$$$ print a single integer — the index of the kitten from $$$1$$$ to $$$n$$$, who was originally in it. All printed integers must be distinct. It's guaranteed, that there is at least one answer possible. In case there are multiple possible answers, print any of them.

NoteThe answer for the example contains one of several possible initial arrangements of the kittens.The picture below shows how the cells were united for this initial arrangement. Note, that the kittens who wanted to play together on each day were indeed in adjacent cells.

var nums = readline().split(" ").map(function(x) { return parseInt(x); });
var n = nums[0];
var rank = Array(n).fill(0);
var ppp = Array(n).fill(0);
var vectors = Array(n).fill([]);
for (var i=0; i<n; i++) {
	var p = [];
	p.push(i);
	vectors[i] = p;
}
for (var i=0; i<n; i++) ppp[i] = i;

find_set = function(v) {
	if (v !== ppp[v])
		ppp[v] = find_set(ppp[v]);
	return ppp[v];
};

add_vector = function(x, y) {
	for (var j=0; j<vectors[y].length; j++) vectors[x].push(vectors[y][j]);
};

Union = function(x,y) {
	if (rank[x] < rank[y]) {
		ppp[x] = y;
		add_vector(y, x);
	}
	else {
		ppp[y] = x;
		add_vector(x, y);
	}
	rank[x] += rank[y]===rank[x];
};

for (var i=0; i<n-1; i++) {
	nums = readline().split(" ").map(function(x) { return parseInt(x); });
	var a = nums[0];
	var b = nums[1];
	a--; b--;
	var aa = find_set(a);
	var bb = find_set(b);
	if (aa !== bb)
		Union(aa,bb);
}
var x = find_set(0);
for (var j=0; j<vectors[x].length; j++) vectors[x][j]++;
write(vectors[x].join(" "));


A rare article in the Internet is posted without a possibility to comment it. On a Polycarp's website each article has comments feed.Each comment on Polycarp's website is a non-empty string consisting of uppercase and lowercase letters of English alphabet. Comments have tree-like structure, that means each comment except root comments (comments of the highest level) has exactly one parent comment.When Polycarp wants to save comments to his hard drive he uses the following format. Each comment he writes in the following format:   at first, the text of the comment is written;  after that the number of comments is written, for which this comment is a parent comment (i. e. the number of the replies to this comments);  after that the comments for which this comment is a parent comment are written (the writing of these comments uses the same algorithm).  All elements in this format are separated by single comma. Similarly, the comments of the first level are separated by comma.For example, if the comments look like:  then the first comment is written as "hello,2,ok,0,bye,0", the second is written as "test,0", the third comment is written as "one,1,two,2,a,0,b,0". The whole comments feed is written as: "hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0". For a given comments feed in the format specified above print the comments in a different format:   at first, print a integer d — the maximum depth of nesting comments;  after that print d lines, the i-th of them corresponds to nesting level i;  for the i-th row print comments of nesting level i in the order of their appearance in the Policarp's comments feed, separated by space.

The first line contains non-empty comments feed in the described format. It consists of uppercase and lowercase letters of English alphabet, digits and commas.  It is guaranteed that each comment is a non-empty string consisting of uppercase and lowercase English characters. Each of the number of comments is integer (consisting of at least one digit), and either equals 0 or does not contain leading zeros. The length of the whole string does not exceed 106. It is guaranteed that given structure of comments is valid.

Print comments in a format that is given in the statement. For each level of nesting, comments should be printed in the order they are given in the input.

NoteThe first example is explained in the statements.

// Generated by CoffeeScript 1.10.0

/*
readline = ->
  #'hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0'
  #'a,5,A,0,a,0,A,0,a,0,A,0'
  'A,3,B,2,C,0,D,1,E,0,F,1,G,0,H,1,I,1,J,0,K,1,L,0,M,2,N,0,O,1,P,0'

print = console.log
 */

(function() {
  var depthstack, el, i, j, layers, len, line, name, numbers, text;

  line = readline().split(',');

  text = line.filter(function(x, i) {
    return i % 2 === 0;
  });

  numbers = line.filter(function(x, i) {
    return i % 2 === 1;
  }).map(Number);

  layers = [];

  depthstack = [Infinity];

  for (i = j = 0, len = text.length; j < len; i = ++j) {
    el = text[i];
    if (layers[name = depthstack.length - 1] == null) {
      layers[name] = [];
    }
    layers[depthstack.length - 1].push(text[i]);
    depthstack[depthstack.length - 1] -= 1;
    depthstack.push(numbers[i]);
    while (depthstack[depthstack.length - 1] === 0) {
      depthstack.pop();
    }
  }

  print(layers.length);

  print(layers.map(function(x) {
    return x.join(' ');
  }).join('\n'));

}).call(this);

"use strict";

var atLevel = {};

function addForLevel (text, level) {
    if (!(level in atLevel)) {
        atLevel[level] = [];
    }
    atLevel[level].push(text);
}

function parseComment (tokens, pos, level) {
    var text = tokens[pos++];
    var kids = parseInt(tokens[pos++]);
    addForLevel (text, level);
    var positions = [kids];
    while (true) {
        while (positions.length > 0 && positions[positions.length - 1] === 0)
            positions.pop();
        if (positions.length === 0)
            break;
        text = tokens[pos++];
        kids = parseInt(tokens[pos++]);
        positions[positions.length - 1]--;
        addForLevel (text, positions.length);
        positions.push(kids)
    }
    return pos;
}

function parseAll (s) {
    var tokens = s.split(',');
    var pos = 0;
    while (pos < tokens.length)
        pos = parseComment(tokens, pos, 0);
}

var s = readline();
parseAll(s);
var level = 0;
while (level in atLevel)
    level++;
print(level);
for (var i = 0; i < level; i++)
    print(atLevel[i].join(' '));