import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

void main ()
{
	readln;
	int n, k;
	while (readf !(" %s %s") (n, k) > 0)
	{
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		bool [int] vis;
		foreach (ref c; a)
		{
			vis[c] = true;
		}
		if (vis.length > k)
		{
			writeln (-1);
		}
		else
		{
			writeln (n * k);
			writefln !("%(%s %)")
			    (vis.byKey.cycle.take (k).repeat (n).joiner);
		}
	}
}

import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen)  return _RDA!T(_f, fix); else return _RDA!T(fix); }

size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }

long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }

void main()
{
	auto t = RD!int;
	auto ans = new int[][](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto k = RD!int;
		auto a = RDA!int;

		bool[int] set;
		foreach (i; 0..n)
		{
			set[a[i]] = true;
		}
		if (set.keys.length > k) continue;

		auto len = (10^^4) / k;
		len *= k;
		ans[ti].length = len;
		auto b = set.keys.dup;
		while (b.length < k)
			b ~= 1;
		foreach (i; 0..len)
		{
			auto pos = i % k;
			ans[ti][i] = b[pos];
		}
	}

	foreach (e; ans)
	{
		if (e.empty)
			writeln(-1);
		else
		{
			writeln(e.length);
			e.map!(to!string).join(" ").writeln;
		}
	}
	stdout.flush;
	debug readln;
}

import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;

void main()
{
    auto T = readln.chomp.to!int;
    foreach (_t; 0..T) {
        auto nk = readln.split.to!(int[]);
        auto N = nk[0];
        auto K = nk[1];
        auto as = readln.split.to!(int[]);
        auto ns = new bool[](100);
        foreach (a; as) ns[a-1] = true;
        int c;
        foreach (n; ns) if (n) ++c;
        if (c > K) {
            writeln("-1");
            continue;
        }
        int[] ii;
        foreach (i, n; ns) if (n) ii ~= i.to!int+1;
        while (ii.length < K) ii ~= ii[0];
        int[] ms;
        size_t i;
        foreach (a; as) {
            while (a != ii[i]) {
                ms ~= ii[i++];
                i %= ii.length;
            }
            ms ~= a;
            ++i;
            i %= ii.length;
        }
        writeln(ms.length);
        writeln(ms.to!(string[]).join(" "));
    }
}

import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

void main ()
{
	readln;
	int n, k;
	while (readf !(" %s %s") (n, k) > 0)
	{
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		bool [int] vis;
		foreach (ref c; a)
		{
			vis[c] = true;
		}
		if (vis.length > k)
		{
			writeln (-1);
		}
		else
		{
			writeln (n * vis.length);
			writefln !("%(%s %)") (vis.byKey.repeat (n).joiner);
		}
	}
}

import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen)  return _RDA!T(_f, fix); else return _RDA!T(fix); }

size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }

long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }

void main()
{
	auto t = RD!int;
	auto ans = new int[][](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto k = RD!int;
		auto a = RDA!int;

		bool[int] set;
		foreach (i; 0..n)
		{
			set[a[i]] = true;
		}
		if (set.keys.length > k) continue;

		auto len = (10^^4) / k;
		ans[ti].length = len;
		auto b = set.keys.dup;
		while (b.length < k)
			b ~= 1;
		foreach (i; 0..len)
		{
			auto pos = i % k;
			ans[ti][i] = b[pos];
		}
	}

	foreach (e; ans)
	{
		if (e.empty)
			writeln(-1);
		else
		{
			writeln(e.length);
			e.map!(to!string).join(" ").writeln;
		}
	}
	stdout.flush;
	debug readln;
}

import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen)  return _RDA!T(_f, fix); else return _RDA!T(fix); }

size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }

long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }

void main()
{
	auto t = RD!int;
	auto ans = new int[][](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto k = RD!int;
		auto a = RDA!int;

		bool[int] set;
		foreach (i; 0..n)
		{
			set[a[i]] = true;
		}
		if (set.keys.length > k) continue;

		auto len = (10^^4) / k;
		ans[ti].length = len;
		foreach (i; 0..len)
		{
			auto pos = i % k;
			ans[ti][i] = a[pos];
		}
	}

	foreach (e; ans)
	{
		if (e.empty)
			writeln(-1);
		else
		{
			writeln(e.length);
			e.map!(to!string).join(" ").writeln;
		}
	}
	stdout.flush;
	debug readln;
}

import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen)  return _RDA!T(_f, fix); else return _RDA!T(fix); }

size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }

long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }

void main()
{
	auto t = RD!int;
	auto ans = new int[][](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto k = RD!int;
		auto a = RDA!int;

		bool[int] set;
		foreach (i; 0..n)
		{
			set[a[i]] = true;
		}
		if (set.keys.length > k) continue;

		auto len = (10^^4) / k;
		ans[ti].length = len;
		auto keys = set.keys;
		foreach (i; 0..len)
		{
			auto pos = i % keys.length;
			ans[ti][i] = keys[pos];
		}
	}

	foreach (e; ans)
	{
		if (e.empty)
			writeln(-1);
		else
		{
			writeln(e.length);
			e.map!(to!string).join(" ").writeln;
		}
	}
	stdout.flush;
	debug readln;
}

import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen)  return _RDA!T(_f, fix); else return _RDA!T(fix); }

size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }

long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }

void main()
{
	auto t = RD!int;
	auto ans = new int[][](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto k = RD!int;
		auto a = RDA!int;

		ans[ti] = a.dup;
		int i = k;
		while (i < ans[ti].length)
		{
			if (ans[ti][i] == ans[ti][i-k])
			{
				++i;
				continue;
			}

			bool ok;
			foreach (j; i-k..i)
			{
				if (ans[ti][j] == ans[ti][i])
				{
					ok = true;
					break;
				}
			}
			if (ok)
			{
				ans[ti] = ans[ti][0..i] ~ ans[ti][i-k] ~ ans[ti][i..$];
			}
			else
			{
				ans[ti].length = 0;
				break;
			}
			++i;
			debug writeln(ans[ti]);
		}
	}

	foreach (e; ans)
	{
		if (e.empty)
			writeln(-1);
		else
		{
			writeln(e.length);
			e.map!(to!string).join(" ").writeln;
		}
	}
	stdout.flush;
	debug readln;
}


import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }

void main() {
  try {
    for (; ; ) {
      const N = readInt();
      const K = readInt();
      const A = readToken();

      auto b = new char[N];
      foreach (i; 0 .. N) {
        b[i] = A[i % K];
      }
      if (A > b) {
        b[K - 1] += 1;
        foreach_reverse (i; 0 .. K) {
          if (b[i] > '9') {
            b[i] -= 10;
            b[i - 1] += 1;
          }
        }
        foreach (i; K .. N) {
          b[i] = b[i % K];
        }
      }
      writeln(N);
      writeln(b);
    }
  } catch (EOFException e) {
  }
}

import core.bitop, std.bitmanip;
import core.checkedint;
import std.algorithm, std.functional, std.meta;
import std.array, std.container;
import std.bigint;
import std.conv;
import std.math, std.numeric;
import std.range, std.range.interfaces;
import std.stdio, std.string;
import std.ascii, std.typecons;

void main()
{
    int n, k;
    readf("%s %s", &n, &k);
    readln;

    auto s = readln.chomp;

    dchar[] ans;
    foreach (i; 0 .. n) { ans ~= s[i % k]; }

    debug { ans.writeln; }

    foreach (i; k .. n) {
        if (ans[i] > s[i]) { break; }
        if (ans[i] < s[i]) {
            int start = k-1;
            while (s[start] == '9') { --start; }
            foreach (j; n.iota.drop(start).stride(k)) { ans[j] = s[start] + 1; }
            foreach (nxt; start + 1 .. k) {
                foreach (j; n.iota.drop(nxt).stride(k)) { ans[j] = '0'; }
            }
            break;
        }
    }

    ans.length.writeln;
    ans.writeln;
}

import core.bitop, std.bitmanip;
import core.checkedint;
import std.algorithm, std.functional, std.meta;
import std.array, std.container;
import std.bigint;
import std.conv;
import std.math, std.numeric;
import std.range, std.range.interfaces;
import std.stdio, std.string;
import std.ascii, std.typecons;

void main()
{
    int n, k;
    readf("%s %s", &n, &k);
    readln;

    auto s = readln.chomp;

    dchar[] ans;
    foreach (i; 0 .. n) { ans ~= s[i % k]; }

    debug { ans.writeln; }

    foreach (i; k .. n) {
        if (ans[i] > s[i]) { break; }
        if (ans[i] < s[i]) {
            int start = k-1;
            while (s[start] == '9') { --start; }
            foreach (j; n.iota.drop(start).stride(k)) { ans[j] = s[i]; }
            break;
        }
    }

    ans.length.writeln;
    ans.writeln;
}

import core.bitop, std.bitmanip;
import core.checkedint;
import std.algorithm, std.functional, std.meta;
import std.array, std.container;
import std.bigint;
import std.conv;
import std.math, std.numeric;
import std.range, std.range.interfaces;
import std.stdio, std.string;
import std.ascii, std.typecons;

void main()
{
    int n, k;
    readf("%s %s", &n, &k);
    readln;

    auto s = readln.chomp;

    dchar[] ans;
    foreach (i; 0 .. n) { ans ~= s[i % k]; }

    debug { ans.writeln; }

    foreach (i; k .. n) {
        if (ans[i] > s[i]) { break; }
        if (ans[i] < s[i]) {
            int start = k-1;
            while (s[start] == '9') { --start; }
            foreach (j; n.iota.drop(start).stride(k)) { ans[j] = s[start] + 1; }
            break;
        }
    }

    ans.length.writeln;
    ans.writeln;
}

import core.bitop, std.bitmanip;
import core.checkedint;
import std.algorithm, std.functional, std.meta;
import std.array, std.container;
import std.bigint;
import std.conv;
import std.math, std.numeric;
import std.range, std.range.interfaces;
import std.stdio, std.string;
import std.ascii, std.typecons;

void main()
{
    int n, k;
    readf("%s %s", &n, &k);
    readln;

    auto s = readln.chomp;

    dchar[] ans;
    foreach (i; 0 .. n) { ans ~= s[i % k]; }

    debug { ans.writeln; }

    foreach (i; k .. n) {
        if (ans[i] > s[i]) { break; }
        if (ans[i] < s[i]) {
            int start = k-1;
            while (s[start] == '9') { --start; }
            foreach (j; n.iota.drop(start).stride(k)) { ans[j] = s[i]; }
            break;
        }
    }

    ans.writeln;
}

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }

void main() {
  try {
    for (; ; ) {
      const N = readInt();
      const K = readInt();
      const A = readToken();

      auto b = new char[N];
      foreach (i; 0 .. N) {
        b[i] = A[i % K];
      }
      if (A > b) {
        b[K - 1] += 1;
        foreach_reverse (i; 0 .. K) {
          if (b[i] > '9') {
            b[i] -= 10;
            b[i - 1] += 1;
          }
        }
        foreach (i; K .. N) {
          b[i] = b[i % K];
        }
      }
      writeln(b);
    }
  } catch (EOFException e) {
  }
}


import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop;

void main() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto M = s[1] + 2;
    string[] A;
    bool ok = false;
    foreach (i; 0..N) {
        auto t = readln.chomp;
        if (!ok && t.map!(tt => tt == '0').all) continue;
        ok = true;
        A ~= t;
    }
    N = A.length.to!int;
    A.reverse();

    int[] L = new int[](N);
    int[] R = new int[](N);
    foreach (i; 0..N) {
        L[i] = M-1, R[i] = 0;
        foreach (j; 0..M) {
            if (A[i][j] == '1') {
                L[i] = min(L[i], j);
                R[i] = max(R[i], j);
            }
        }
    }

    if (N == 0) {
        writeln(0);
        return;
    } else if (N == 1) {
        writeln(R[0]);
        return;
    }

    auto dp = new int[][](N, 2);

    dp[0][0] = R[0] * 2;
    dp[0][1] = M - 1;

    foreach (i; 1..N-1) {
        dp[i][0] = min(dp[i-1][0] + R[i] * 2 + 1, dp[i-1][1] + M);
        dp[i][1] = min(dp[i-1][1] + (M - L[i] - 1) * 2 + 1, dp[i-1][0] + M);
    }

    int ans = min(dp[N-2][0] + R[N-1] + 1, dp[N-2][1] + (M - L[N-1] - 1) + 1);
    ans.writeln;
}

import core.bitop;
import std.algorithm;
import std.array;
import std.bitmanip;
import std.container;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
import std.range.interfaces;

void main()
{
    int n, m;
    readf("%s %s", &n, &m);
    readln;

    Tuple!(int, int)[] floors;
    foreach (_; 0 .. n) {
        auto s = readln.chomp;
        if (s.all!(x => x == '0')) floors ~= tuple(m+1, 0);
        else {
            auto le = s.countUntil!(x => x == '1').to!int;
            auto r = m+1 - s.retro.countUntil!(x => x == '1').to!int;
            floors ~= tuple(le, r);
        }
    }

    debug { floors.writeln; }

    auto ansle = 0, ansr = 0;
    foreach (t; floors) {
        auto le = t[0], r = t[1];
        auto nowansle = r + (ansle > 0 ? 1 + min(r + ansle, m+1 - r + ansr) : 0);
        auto nowansr = m+1 - le + (ansle > 0 ? 1 + min(le + ansle, m+1 - le + ansr) : 0);

        ansle = nowansle, ansr = nowansr;
        debug { writeln(ansle, ' ', ansr); }
    }

    ansle.writeln;
}


import std.algorithm;
import std.array;
import std.container;
import std.conv;
import std.exception;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
import core.bitop;

immutable int MOD = 1_000_000_007;
immutable string LETTERS = "ACGT";

void main ()
{
	int n;
	while (readf (" %s ", &n) > 0)
	{
		string s = readln ().strip ();
		int [] a;
		int m;
		foreach (c; LETTERS)
		{
			a ~= s.count (c);
			m = max (m, a[$ - 1]);
		}
		string t;
		foreach (i; 0..LETTERS.length)
		{
			if (a[i] == m)
			{
				t ~= LETTERS[i];
			}
		}

		long res = 1;
		foreach (i; 0..s.length)
		{
			res = (res * t.length) % MOD;
		}
		writeln (res);
	}
}

import core.bitop, std.bitmanip;
import std.algorithm, std.functional;
import std.array, std.container;
import std.conv;
import std.math, std.numeric;
import std.range, std.range.interfaces;
import std.stdio, std.string;
import std.typecons;

immutable int INF = 10L ^^ 9 + 23;
immutable int MD = 10 ^^ 9 + 7;

void main()
{
    readln;

    auto s = readln.chomp;

    int [char] cnt;
    s.each!(c => ++cnt[c]);

    auto mx = cnt.values.maxCount[1];

    debug { mx.writeln; }

    int ans = 1;
    foreach (_; 0 .. s.length) ans = ans.to!long * mx % MD;
    ans.writeln;
}

import core.thread;
import std.conv, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.container, std.math, std.range, std.regex;

//	Input
class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) throw new EOFException; tokens = readln.split; } auto token = tokens[0]; tokens.popFront; return token; }
int readInt() { return to!int(readToken); }
long readLong() { return to!long(readToken); }
real readReal() { return to!real(readToken); }

//	chmin/chmax
void chmin(T)(ref T t, in T f) { if (t > f) t = f; }
void chmax(T)(ref T t, in T f) { if (t < f) t = f; }

//	Pair
struct Pair(S, T) {
	S x; T y;
	int opCmp(    const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }
	int opCmp(ref const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }
	string toString() const { return "(" ~ to!string(x) ~ ", " ~ to!string(y) ~ ")"; }
}
auto pair(S, T)(inout(S) x, inout(T) y) { return Pair!(S, T)(x, y); }

//	Array
int binarySearch(T)(in T[] as, in bool delegate(T) test) { int low = -1, upp = as.length; for (; low + 1 < upp; ) { int mid = (low + upp) >> 1; (test(as[mid]) ? low : upp) = mid; } return upp; }
int lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a <  val); }); }
int upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a <= val); }); }
T[] unique(T)(in T[] as) { T[] bs; foreach (a; as) if (bs.empty || bs[$ - 1] != a) bs ~= a; return bs; }

immutable long MO = 10^^9 + 7;

int N;
string S;

void main(string[] args) {
	try {
	for (; ; ) {
		N = readInt;
		S = readToken;

		int[] cnt = new int[26];
		foreach (s; S) {
			++cnt[s - 'A'];
		}

		const num = cnt.count(cnt.reduce!max);
		long ans = 1;
		foreach (i; 0 .. N) {
			(ans *= num) %= MO;
		}
		writeln(ans);

	}
	} catch (EOFException) {}
}


import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii, std.numeric, std.random;
import std.typecons, std.functional, std.traits,std.concurrency;
import std.algorithm, std.container;
import core.bitop, core.time, core.memory;
import std.datetime;
import std.bitmanip;
import std.regex;

void main()
{
    auto N = scanElem;
    foreach(e;scanElem!(long,long)(N))
    {
        auto a = e[0];
        auto b = e[1];
        auto n = b.to!string.length;
        if(b.to!string.any!"a!='9'")n--;
        writeln(a*n);
    }
}

//辞書順順列はiota(1,N),nextPermituionを使う

enum INF = long.max/3;
enum MOD = 10^^9+7;

T binarySearch(alias F, T)(T ok, T ng, long iter=100)
{
    alias FF = unaryFun!F;
    foreach(_;0..iter)
    {
        auto n = (ok+ng)/2;
        if(FF(n)){
            ok = n;
        }else{
            ng = n;
        }
        static if(isIntegral!T){
            if(abs(ok-ng)==1)return ok;
        }
    }
    return ok;
}

//最小を返す
real ternarySearch(alias F)(real l, real r, long iter=200)
{
    alias FF = unaryFun!F;
    foreach(_;0..iter)
    {
        auto nl = lerp(l, r, 1/real(3));
        auto nr = lerp(l, r, 2/real(3));
        if(FF(nl)<FF(nr)){
            r = nr;
        }else{
            l = nl;
        }
    }
    return FF(l);
}
long ternarySearch(alias F)(long l, long r, long iter=200)
{
    alias FF = unaryFun!F;
    foreach(_;0..iter)
    {
        auto nl = lerp(l, r, 1/real(3)).to!long;
        auto nr = lerp(l, r, 2/real(3)).to!long;
        if(nl>11)debugPrint!(l,r,nl,nr,()=>FF(nl),()=>FF(nr));
        if(FF(nl)<FF(nr)){
            r = nr;
        }else{
            l = nl;
        }
        if(r-l<4)break;
    }
    long res=INF;
    foreach(i;l..r+1)
    {
        debugPrint!(l, r, i, ()=>FF(i));
        res = min(FF(i), res);
    }
    return res;
}

CommonType!(A,B,T) lerp(A,B,T)(A a, B b, T t) pure
{
    alias C = CommonType!(A,B,T);
    return (C(b)-a)*t+a;
}

struct Vector{
    real x, y;

    real magnitude()pure
    {
        return sqrt(sqrMagnitude);
    }
    real sqrMagnitude()pure
    {
        return x*x+y*y;
    }

    Vector opBinary(string op, T)(inout T v)
    if(isNumeric!T)
    {
        return Vector(mixin("x"~op~"v"), mixin("y"~op~"v"));
    }
    Vector opBinary(string op)(inout Vector v)
    {
        return Vector(mixin("x"~op~"v.x"), mixin("y"~op~"v.y"));
    }
    void opOpAssign(string op, T)(inout T v)
    {
        this = mixin("this"~op~"v");
    }

    static Vector lerp(Vector a, Vector b, real t)pure
    {
        return (b-a)*t+a;
    }
    static Vector normalized(Vector a)pure
    {
        auto r = a.magnitude();
        return Vector(a.x / r, a.y / r);
    }
    static real distance(Vector a, Vector b)pure
    {
        return (a-b).magnitude;
    }
    static real dot(Vector a, Vector b)pure
    {
        return a.x*b.x+a.y*b.y;
    }
    static real cross(Vector a, Vector b)pure
    {
        return a.x*b.y-b.x*a.y;
    }
}

//ascii文字のみ
long[] suffixArray(Char)(const(Char)[] s) pure
if(isSomeChar!Char)
// in{assert(s.all!"0<=a&&a<127");}
// do
{
    s ~= '\0';
    long[] p = new long[s.length];
    long[] c = new long[s.length];
    {
        long[127] cnt;
        foreach(i;0..s.length)cnt[s[i]]++;
        foreach(i;1..cnt.length)cnt[i] += cnt[i-1];
        foreach(i;0..s.length)p[--cnt[s[i]]] = i;
        long classes=1;
        foreach(i;1..p.length){
            classes += s[p[i]]!=s[p[i-1]]?1:0;
            c[p[i]] = classes-1;
        }
    }
    long[] pn = new long[s.length];
    long[] cn = new long[s.length];
    for(long n=0;(1L<<n) < s.length;n++)
    {
        p.map!(a=>mod(a-(1L<<n), s.length)).copy(pn);
        long[127] cnt;
        foreach(i;0..pn.length)cnt[c[pn[i]]]++;
        foreach(i;1..cnt.length)cnt[i] += cnt[i-1];
        foreach_reverse(i;0..pn.length)p[--cnt[c[pn[i]]]] = pn[i];
        long classes=1;
        for(long i=1;i<c.length;i++)
        {
            if(
                tuple(c[p[i]], c[(p[i]+(1L<<n))%s.length]) !=
                tuple(c[p[i-1]], c[(p[i-1]+(1L<<n))%s.length])
            ) {
                classes++;
            }
            cn[p[i]] = classes - 1;
        }
        cn.copy(c);
    }
    return p[1..$];
}

struct BiparticeMatching{
    int N;
    int[][] path;
    int[] match;
    this(long n)
    {
        N = n.to!int;
        path.length = N;
        match.length = N;
    }
    void addEdge(long u, long v){
        addEdge(u.to!int, v.to!int);
    }
    void addEdge(int u, int v){
        path[u] ~= v;
        path[v] ~= u;
    }
    long calc(){
        auto used = new bool[N];
        bool dfs(int v){
            used[v] = true;
            foreach(u; path[v]){
                int w = match[u];
                if(w==-1||(!used[w]&&dfs(w))){
                    match[v]=u;
                    match[u]=v;
                    return true;
                }
            }
            return false;
        }
        match[] = -1;
        long res = 0;
        foreach(int v; 0..N){
            if(match[v] == -1){
                used[] = false;
                if(dfs(v)){
                    res++;
                }
            }
        }
        return res;
    }
}

// int max_flow(int s, int t){
//     struct edge{
//         int to, cap,  rev;
//     }
//     edge[][MAX_V] G;
//     bool[MAX_V] used;
//     void add_edge(int from, int to, int cap){
//         G[from] ~= edge(to, cap, G[to].length);
//         G[to] ~= edge(from, 0, G[from].length-1);
//     }
//     int dfs(int v, int t, int f)
//     {
//         if(v==t)return f;
//         used[v] = true;
//         foreach(i;0..G[v].length)
//         {
//             edge e = G[v][i];
//             if(!used[e.to] && e.cap > 0){
//                 int d = dfs(e.to, t, min(f, e.cap));
//                 if(d>0){
//                     e.cap -= d;
//                     G[e.to][e.rev].cap +=d ;
//                     return d;
//                 }
//             }
//         }
//         return 0;
//     }
//     int flow = 0;
//     for(;;){
//         int f = dfs(s,t,INF);
//         if(f==0)return flow;
//         flow += f;
//     }
// }

struct CombTable2(long MOD)
{
    static long[] fac;
    static long[] finv;
    static long[] inv;
    this(long n)
    {
        fac = new long[n];
        finv = new long[n];
        inv = new long[n];

        fac[0] = fac[1] = 1;
        finv[0] = finv[1] = 1;
        inv[1] = 1;
        foreach(i;2..n){
            fac[i] = fac[i - 1] * i % MOD;
            inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
            finv[i] = finv[i - 1] * inv[i] % MOD;
        }
    }

    long comb(long n, long k)
    {
        if (n < k) return 0;
        if (n < 0 || k < 0) return 0;
        return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
    }
}
struct CombTable(long MOD, long n)
{
    static long[n] fac;
    static long[n] finv;
    static long[n] inv;
    static this()
    {
        fac[0] = fac[1] = 1;
        finv[0] = finv[1] = 1;
        inv[1] = 1;
        foreach(i;2..n){
            fac[i] = fac[i - 1] * i % MOD;
            inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
            finv[i] = finv[i - 1] * inv[i] % MOD;
        }
    }

    static long comb(long n, long k)
    {
        if (n < k) return 0;
        if (n < 0 || k < 0) return 0;
        return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
    }
}

void outLine(List...)(List list)
{
    foreach(i, v;list)
    {
        static if(isFloatingPoint!(typeof(v)))
        {
            writef("%.12f", v);
        }else{
            write(v);
        }
        if(i+1!=list.length)write(' ');
    }
    writeln;
}

void end(List...)(List list)
{
    outLine(list);
    end;
}
void end()
{
    import core.stdc.stdlib;
    exit(0);
}

long sequenceSum(long n) pure
{
    return n*(n+1)/2;
}

//HL分解
struct HeavyLightDecomposition
{
    immutable int root = 1;

    int[][] edge;
    int[] vid;
    int[] invid;
    int[] parent;
    int[] depth;
    int[] subCount;
    int[] head;

    this(long n, long root = 1)
    {
        this(n.to!int, root.to!int);
    }
    this(int n, int root = 1)
    {
        this.root = root;
        n++;
        edge.length = n;
        vid.length = n;
        invid.length = n;
        parent.length = n; parent[] = -1;
        depth.length = n;
        head.length = n; head[root] = root;
        subCount.length = n; subCount[] = 1;
    }

    void addEdge(long u, long v)
    {
        addEdge(u.to!int, v.to!int);
    }
    void addEdge(int u, int v)
    {
        edge[u] ~= v;
        edge[v] ~= u;
    }

    void build()
    {
        void dfs(int v){
            foreach(ref u; edge[v])
            {
                if(u==parent[v]) continue;
                parent[u] = v;
                depth[u] = depth[v] + 1;
                dfs(u);
                subCount[v] += subCount[u];
                if(edge[v][0]==parent[v]||subCount[u]>subCount[edge[v][0]])
                    swap(u, edge[v][0]);
            }
        }
        void dfsHead(int v, ref int pos){
            invid[pos] = v;
            vid[v] = pos;
            pos++;
            foreach(ref u; edge[v])
            {
                if(u==parent[v]) continue;

                head[u] = u == edge[v][0] ? head[v] : u;
                dfsHead(u, pos);
            }
        }
        dfs(root);
        int pos;
        dfsHead(root, pos);
    }

    long lca(long u, long v)
    {
        return lca(u.to!int, v.to!int);
    }
    int lca(int u, int v)
    {
        while(true){
            if(vid[u]>vid[v]) swap(u,v);
            if(head[u]==head[v]) return u;
            v = parent[head[v]];
        }
    }

    long distance(long u, long v) { return distance(u.to!int, v.to!int); }
    long distance(int u, int v) { return depth[u] + depth[v] - 2 * depth[lca(u,v)]; }

    //idxのn個上の親を返す
    //バグあるかも
    long nParent(long n, long idx){
        return nParent(n.to!int, idx.to!int);
    }
    int nParent(int n, int idx){
        auto u = 0;
        auto v = idx;
        while(true)
        {
            if(vid[u]>vid[v]) swap(u,v);

            immutable _u = max(vid[head[v]], vid[u]);
            immutable _v = vid[v] + 1;
            if(_v<=_u+n){
                n -= _v-_u;
            }else{
                return invid[_v-n-1];
            }

            if(head[u]==head[v]) return -1;
            v = parent[head[v]];
        }
    }

    void each(long u, long v, void delegate(long u, long v) pred)
    {
        each(u.to!int, v.to!int, pred);
    }
    void each(int u, int v, void delegate(int u, int v) pred)
    {
        while(true)
        {
            if(vid[u]>vid[v]) swap(u,v);
            pred(max(vid[head[v]], vid[u]), vid[v] + 1);
            if(head[u]==head[v]) return;
            v = parent[head[v]];
        }
    }

    void each(alias pred)(long u, long v)
    if(is(typeof(binaryFun!pred(0L,0L))))
    {
        while(true)
        {
            if(vid[u]>vid[v]) swap(u,v);
            binaryFun!pred(max(vid[head[v]], vid[u]), vid[v] + 1);
            if(head[u]==head[v]) return;
            v = parent[head[v]];
        }
    }
}

// struct HLD{

//     long[][] G;
//     long[] vid, head, sub, par, dep, inv, type;

//     void dfs_sz(long v) {
//         foreach(ref u; G[v])
//             if(u==par[v]) swap(u,G[v].back());
//         if(~par[v]) G[v].popBack;

//         foreach(ref u; G[v]){
//             par[u]=v;
//             dep[u]=dep[v]+1;
//             dfs_sz(u);
//             sub[v]+=sub[u];
//             if(sub[u]>sub[G[v][0]]) swap(u,G[v][0]);
//         }
//     }

//     void dfs_hld(long v,long c,ref long pos) {
//         vid[v]=pos++;
//         inv[vid[v]]=v;
//         type[v]=c;
//         foreach(u; G[v]){
//             if(u==par[v]) continue;
//             head[u]=(u==G[v][0]?head[v]:u);
//             dfs_hld(u,c,pos);
//         }
//     }

//     this(long n){
//         G = new long[][n];
//         vid = new long[n]; vid[] = -1;
//         head = new long[n];
//         sub = new long[n]; sub[] = 1;
//         par = new long[n]; par[] = -1;
//         dep = new long[n];
//         inv = new long[n];
//         type = new long[n];
//     }

//     void add_edge(long u,long v) {
//         G[u] ~= v;
//         G[v] ~= u;
//     }

//     void build(long[] rs = [0]) {
//         long c=0,pos=0;
//         foreach(r; rs){
//             dfs_sz(r);
//             head[r]=r;
//             dfs_hld(r,c++,pos);
//         }
//     }

//     long lca(long u,long v){
//         while(1){
//             if(vid[u]>vid[v]) swap(u,v);
//             if(head[u]==head[v]) return u;
//             v=par[head[v]];
//         }
//     }

//     long distance(long u,long v){
//         return dep[u]+dep[v]-2*dep[lca(u,v)];
//     }

//     // for_each(vertex)
//     // [l, r) <- attention!!
//     void for_each(F)(long u, long v, F f) {
//         while(1){
//             if(vid[u]>vid[v]) swap(u,v);
//             f(max(vid[head[v]],vid[u]),vid[v]+1);
//             if(head[u]!=head[v]) v=par[head[v]];
//             else break;
//         }
//     }

//     T for_each(T, Q, F)(long u,long v,T ti,Q q,F f)
//     {
//         T l=ti,r=ti;
//         while(1){
//             if(vid[u]>vid[v]){
//                 swap(u,v);
//                 swap(l,r);
//             }
//             l=f(l,q(max(vid[head[v]],vid[u]),vid[v]+1));
//             if(head[u]!=head[v]) v=par[head[v]];
//                 else break;
//         }
//         return f(l,r);
//     }

//     // for_each(edge)
//     // [l, r) <- attention!!
//     void for_each_edge(F)(long u, long v,F f) {
//         while(1){
//             if(vid[u]>vid[v]) swap(u,v);
//             if(head[u]!=head[v]){
//                 f(vid[head[v]],vid[v]+1);
//                 v=par[head[v]];
//             }else{
//                 if(u!=v) f(vid[u]+1,vid[v]+1);
//                 break;
//             }
//         }
//     }
// }

struct SegTree(T, T UNIT, alias pred){
    int n;
    long size;
    T* arr;
    alias F = binaryFun!pred;

    this(long size)
    {
        this.size = size;
        n=1;
        while(n<size) n<<=1;
        arr = new T[n<<1].ptr;
        arr[0..n<<1] = UNIT;
    }

    void set(long k,T x)
    {
        assert(k>=0&&k<size);

        arr[k+=n]=x;
        while(k>>=1)
            arr[k]=F(arr[(k<<1)|0],arr[(k<<1)|1]);
    }

    T query(long a, long b)
    {
        assert(a>=0&&a<size);
        assert(b>=1&&b<=size);

        T vl=UNIT,vr=UNIT;
        for(long l=a+n,r=b+n;l<r;l>>=1,r>>=1)
        {
            if(l&1) vl=F(vl,arr[l++]);
            if(r&1) vr=F(arr[--r],vr);
        }
        return F(vl,vr);
    }
}

bool isInf(Num)(Num v) pure @nogc
if(isIntegral!Num)
{
    return v>=INF/2;
}

Unqual!M mod(N,M)(N n, M mod) pure @nogc
if(isIntegral!N&&isIntegral!M)
{
    return (n%mod+mod)%mod;
}

long pow(long a, long n) {
    long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a;
        a = a * a;
        n >>= 1;
    }
    return res;
}

Unqual!M powmod(A,N,M)(A _a, N _n, M mod)
{
    Unqual!A a = _a;
    Unqual!N n = _n;
    M res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

alias MInt = ModInt!MOD;

struct ModInt(alias Mod)
if(isIntegral!(typeof(Mod)) && isPrime(Mod))
{
    int value;

    this(ModInt!Mod v)
    {
        value = v.value;
    }
    this(T)(T v)
    if(isIntegral!T)
    {
        value = mod(v, Mod);
    }

    ModInt!Mod opBinaryRight(string op, T)(inout T v) const
    if((op=="+"||op=="-"||op=="*")&&isIntegral!T)
    {
        return typeof(this)(mixin("v"~op~"value"));
    }
    ModInt!Mod opBinary(string op, T)(inout T v) const
    if((op=="+"||op=="-"||op=="*")&&(is(T==ModInt!Mod)||isIntegral!T))
    {
        return typeof(this)(mixin("value"~op~"v"));
    }

    ModInt!Mod opBinaryRight(string op, T)(inout T v) const
    if((op=="/")&&isIntegral!T)
    {
        return v * (this^^(Mod-2));
    }
    ModInt!Mod opBinary(string op, T)(inout T v) const
    if((op=="/")&&(is(T==ModInt!Mod)||isIntegral!T))
    {
        return this * (typeof(this)(v)^^(Mod-2));
    }

    ModInt!Mod opBinary(string op, T)(inout T v) const
    if((op=="^^")&&isIntegral!T)
    {
        return typeof(this)(powmod(value, v, Mod));
    }

    void opAssign(T)(inout T v)
    if(isIntegral!T)
    {
        this = typeof(this)(v);
    }

    void opOpAssign(string op, T)(inout T v)
    {
        this = mixin("this"~op~"v");
    }

    bool opEquals(T)(const T v) const
    if(is(T==ModInt!Mod)||isIntegral!T)
    {
        return value == typeof(this)(v).value;
    }

    long opCast() const {
        return value;
    }

    string toString() const {
        return value.to!string;
    }
}
unittest
{
    assert(is(ModInt!MOD));
    assert(is(ModInt!17));
    assert(!is(ModInt!0));
    assert(!is(ModInt!10));
}

unittest
{
    alias MInt = ModInt!13;
    MInt value;
    value = MInt(14) + MInt(18);
    assert(value==6);
    value = 14 - MInt(28);
    assert(value==12);
    value = MInt(17) * -19;
    assert(value==2);

    value = MInt(7) / 4;
    assert(value==5);
    value = 8 / MInt(4);
    assert(value==2);
    value = 9;
    value /= 4;
    assert(value==12);

    assert(MInt(3) ^^ 9 == 1);

    value = 29-MInt(16);
    assert(value==0);
    value = MInt(13)*5;
    assert(value==0);
    value = 0;
    assert(value==0);

    value = 3;
    value += MInt(11);
    assert(value==1);
    value -= 7;
    assert(value==7);
    value *= MInt(4);
    assert(value==2);
    value = 23;
    assert(value == cast(long)value);
}
unittest
{
    ModInt!MOD value;
    value = MOD-1;
    assert(value==MOD-1);
    value = MOD;
    assert(value==0);
}

struct Grid(T){
    private T[] grid;
    size_t width, height;
    private size_t stride;

    private this(T[] g, size_t w, size_t h, size_t s)
    {
        grid = g;
        width = w;
        height = h;
        stride = s;
    }

    this(long w, long h, T init = T.init)
    {
        auto arr = new T[(w*h).to!int];
        arr[] = init;
        this(arr, w.to!size_t, h.to!size_t, w.to!size_t);
    }

    // void fill(T elem){
    //     grid[] = elem;
    // }

    bool isInRange(long x, long y) const nothrow
    {
        return
            x>=0 &&
            x<width &&
            y>=0 &&
            y<height;
    }

    int opApply(scope int delegate(ref T) dg)
    {
        int result = 0;

        foreach(y;0..height)
        foreach(x;0..width)
        {
            result = dg(this[x,y]);
            if (result)
                break;
        }
        return result;
    }
    int opApply(scope int delegate(long i, ref T) dg)
    {
        int result = 0;

        long idx;
        foreach(y;0..height)
        foreach(x;0..width)
        {
            result = dg(idx++, this[x,y]);
            if (result)
                break;
        }
        return result;
    }
    int opApply(scope int delegate(long x, long y, ref T) dg)
    {
        int result = 0;

        foreach(y;0..height)
        foreach(x;0..width)
        {
            result = dg(x, y, this[x,y]);
            if (result)
                break;
        }
        return result;
    }

    ref T opIndex(long x, long y)
    {
        assert(isInRange(x,y));
        return (grid.ptr)[(x+stride*y).to!size_t];
    }

    Grid!T opIndex(long[2] r1, long[2] r2)
    {
        auto startOffset = (r1[0] + r2[0]*stride).to!size_t;
        auto endOffset = (r1[1] + (r2[1] - 1)*stride).to!size_t;

        auto resGrid = grid[startOffset .. endOffset];
        auto resStride = stride;
        auto resWidth = (r1[1] - r1[0]).to!size_t;
        auto resHeight = (r2[1] - r2[0]).to!size_t;
        return Grid!T(resGrid, resWidth, resHeight, resStride);
    }

    Grid!T opIndex(long[2] r1, long j) { return opIndex(r1, [j, j+1]); }
    Grid!T opIndex(long i, long[2] r2) { return opIndex([i, i+1], r2); }

    long[2] opSlice(long dim)(long start, long end)
    if (dim == 0 || dim == 1)
    // in { assert(start >= 0 && end <= this.opDollar!dim); }
    body{
        return [start, end];
    }

    Grid!T transpose(){
        auto grid = Grid!T(height, width);

        foreach(w; 0..width)
        foreach(h; 0..height)
        {
            grid[h, w] = this[w, h];
        }
        return grid;
    }

    long opDollar(long dim : 0)() const { return width; }
    long opDollar(long dim : 1)() const { return height; }

    string toString() pure {
        long count;
        foreach(y; 0..height)
        {
            foreach(x; 0..width)
            {
                long eCount = this[x, y].to!string.length;
                static if(is(typeof(this[x,y].isInf)))
                {
                    if(this[x,y].isInf)
                    {
                        eCount = 1;
                    }
                }
                static if(is(typeof(this[x,y])==bool))
                {
                    eCount = 1;
                }
                count = max(count, eCount);
            }
        }
        count++;

        string res = "\n";
        foreach(y; 0..height)
        {
            foreach(x; 0..width)
            {
                string joinStr = this[x, y].to!string;
                static if(is(typeof(this[x,y].isInf)))
                {
                    if(this[x,y].isInf)
                    {
                        joinStr = "*";
                    }
                }
                static if(is(typeof(this[x,y])==bool))
                {
                    joinStr = this[x,y]?"+":"-";
                }
                foreach(i;0..count-joinStr.length)
                {
                    res ~= ' ';
                }
                res ~= joinStr;
            }
            res ~= '\n';
        }
        return res;
    }
}

struct UnionFind{
    private int[] arr;
    int rootCount;

    @disable this();
    this(long n){
        arr.length = n.to!int;
        arr[] = -1;
        rootCount = n.to!int;
    }

    void merge(long a, long b)
    {
        merge(a.to!int, b.to!int);
    }
    void merge(int a, int b)
    {
        if(same(a,b)) return;
        arr[root(a)] = root(b);
        rootCount--;
    }

    bool same(long a, long b)
    {
        return same(a.to!int, b.to!int);
    }
    bool same(int a, int b)
    {
        return root(a)==root(b);
    }

    private int root(int i)
    {
        if(arr[i] == -1) return i;
        return arr[i] = root(arr[i]);
    }
}

unittest{
    assert(is(typeof(UnionFind(10))));
    assert(!is(typeof(UnionFind())));

    auto uf = UnionFind(10);
    uf.merge(2,3);
    assert(uf.same(2,3));
    uf.merge(3,4);
    uf.merge(1,5);
    uf.merge(5,6);
    uf.merge(6,7);
    assert(!uf.same(4,7));
    uf.merge(1,2);
    assert(uf.same(4,7));
}

void debugPrint(List...)()
{
    void _debugPrintElem(alias elem, float rad)()
    {
        import std.experimental.color;
        import std.experimental.color.lab;
        import std.experimental.color.rgb;
        import std.experimental.color.xyz;

        enum color = LCh!float(80f, 100f, rad);
        enum rgb = color.convertColor!(Lab!float).convertColor!(XYZ!float).convertColor!(RGB8).tristimulus;
        enum r = rgb[0].value;
        enum g = rgb[1].value;
        enum b = rgb[2].value;

        stderr.writef!"\033[38;2;%s;%s;%sm"(r, g, b);
        static if(isSomeFunction!elem)
        {
            stderr.write("elem: ", elem(), " ");
        }else{
            enum name =  __traits(identifier, elem);
            stderr.write(name, ": ");
            stderr.write(elem, " ");
        }
    }
    void _debugPrint(int i, float rad, List...)()
    {
        _debugPrintElem!(List[0], i * rad)();
        static if(List.length>1)
        {
            _debugPrint!(i+1, rad, List[1..$])();
        }
    }

    debug(Local)
    {
        _debugPrint!(0, 360f/List.length, List)();
        stderr.writeln("\033[0m");
    }
}

struct Stack(Elem){
    private Elem[] array;
    private size_t endIdx;

    void insertBack(Elem e)
    {
        if(endIdx==array.length){
            array.length = array.length*2+10;
        }
        (array.ptr)[endIdx++] = e;
    }

    void insertBack(Range)(Range range)
    if(is(typeof(array[0] = range.popFront)))
    {
        if(endIdx+range.length>array.length){
            array.length = (endIdx+range.length)*2+10;
        }
        foreach(ref e;range)
        {
            (array.ptr)[endIdx++] = e;
        }
    }

    Elem[] opSlice(){
        return array[0..endIdx];
    }

    void popBack()
    {
        assert(endIdx!=0);
        endIdx--;
    }

    ref Elem back()
    {
        assert(endIdx!=0);
        return (array.ptr)[endIdx-1];
    }

    size_t count() const
    {
        return endIdx;
    }

    bool empty() const
    {
        return endIdx==0;
    }

    void clear()
    {
        endIdx = 0;
    }
}

Grid!T scanGrid(T=long)(long w, long h, dchar t='.')
{
    auto grid = Grid!T(w,h);
    foreach(y;0..h)
    {
        foreach(x;0..w)
        {
            grid[x, y] = scanElem!T;
        }
    }

    return grid;
}

Grid!bool scanGridBool(long w, long h, dchar t='.')
{
    auto grid = Grid!bool(w,h);
    foreach(y;0..h.to!size_t)
    {
        auto line = scanString;
        foreach(x;0..w.to!size_t)
        {
            grid[x, y] = line[x.to!size_t]==t;
        }
    }

    return grid;
}

T[] scanLineArray(T = long)()
{
    static char[] scanBuf;
    readln(scanBuf);
    return scanBuf.split.to!(T[]);
}

char scanChar()
{
    int c = ' ';
    while (isWhite(c) && c != -1)
    {
        c = getchar;
    }
    return cast(char)c;
}

T[] scanElem(T=long)(long size)
{
    T[] list = new T[size.to!size_t];
    foreach(i;0..size.to!size_t)
    {
        list[i] = scanElem!T;
    }
    return list;
}

T scanElem(T)()
if(is(T==struct))
{
    T res;
    foreach(ref field; res.tupleof){
        field = scanElem!(typeof(field));
    }
    return res;
}

Tuple!List[] scanElem(List...)(long size)
{
    auto list = new Tuple!List[size.to!size_t];
    foreach(i;0..size.to!size_t)
    {
        list[i] = scanElem!List;
    }
    return list;
}
Tuple!List scanElem(List...)()
{
    List res;
    foreach(i, e; List){
        res[i] = scanElem!e;
    }
    return tuple(res);
}

T scanElem(T = long)()
if(isBasicType!T||isSomeString!T)
{
    import core.stdc.stdlib;
    static auto scanBuf = appender!(char[])([]);

    scanBuf.clear;

    int c = ' ';
    while (isWhite(c) && c != -1)
    {
        c = getchar;
    }
    while (!isWhite(c) && c != -1)
    {
        scanBuf ~= cast(char) c;
        c = getchar;
    }
    return scanBuf.data.to!T;
}

string scanString(){
    return scanElem!string;
}

CommonType!(A,B) gcd(A,B)(A a, B b) pure
if(isIntegral!A&&isIntegral!B)
{
    if(b==0)return a;
    return gcd(b, a % b);
}

CommonType!(A,B) lcm(A,B)(A a, B b) pure
if(isIntegral!A&&isIntegral!B)
{
    return a / gcd(a, b) * b;
}

struct Factor
{
    long n;
    long c;
}

//素因数分解
Factor[] factors(long n) pure
{
    Factor[] res;
    for (long i = 2; i ^^ 2 <= n; i++)
    {
        if (n % i != 0)
            continue;

        long c;
        while (n % i == 0)
        {
            n = n / i;
            c++;
        }
        res ~= Factor(i, c);
    }
    if (n != 1)
        res ~= Factor(n, 1);

    return res;
}
//約数をすべて列挙
long[] divisors(long n) pure
{
    long[] list;
    void func(Factor[] fs, long n)
    {
        if(fs.empty){
            list ~= n;
            return;
        }

        foreach(c; 0..fs[0].c+1)
        {
            func(fs[1..$], n * (fs[0].n ^^ c));
        }
    }

    func(factors(n), 1);
    sort(list);
    return list;
}
//nまでの素数のリスト
long[] primes(long n) pure
{
    if(n<2)return [];

    auto table = new long[cast(size_t)n+1];

    long[] res;
    for(size_t i = 2;i<=n;i++)
    {
        if(table[i]==-1) continue;
        for(size_t a = i;a<table.length;a+=i)
        {
            table[a] = -1;
        }
        res ~= i;
    }
    return res;
}
//素数判定
bool isPrime(long n) pure
{
    if (n <= 1)
        return false;
    if (n == 2)
        return true;
    if (n % 2 == 0)
        return false;
    for (long i = 3; i ^^ 2 <= n; i += 2)
        if (n % i == 0)
            return false;
    return true;
}

//桁を数える
long digitCount(long n, long base=10) pure
{
    long res;
    do{
        n/=base;
        res++;
    }while(n!=0);

    return res;
}

//prewritten code: https://github.com/antma/algo
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.traits;

final class InputReader {
  private:
  ubyte[] p, buffer;
  bool eof;
  bool rawRead () {
    if (eof) {
      return false;
    }
    p = stdin.rawRead (buffer);
    if (p.empty) {
      eof = true;
      return false;
    }
    return true;
  }
  ubyte nextByte(bool check) () {
    static if (check) {
      if (p.empty) {
        if (!rawRead ()) {
          return 0;
        }
      }
    }
    auto r = p.front;
    p.popFront ();
    return r;
  }
  public:
  this () {
    buffer = uninitializedArray!(ubyte[])(16<<20);
  }
  bool seekByte (in ubyte lo) {
    while (true) {
      p = p.find! (c => c >= lo);
      if (!p.empty) {
        return false;
      }
      if (!rawRead ()) {
        return true;
      }
    }
  }
  template next(T) if (isSigned!T) {
    T next ()  {
      if (seekByte (45)) {
        return 0;
      }
      T res;
      ubyte b = nextByte!false ();
      if (b == 45) {
        while (true) {
          b = nextByte!true ();
          if (b < 48 || b >= 58) {
            return res;
          }
          res = res * 10 - (b - 48);
        }
      } else {
        res = b - 48;
        while (true) {
          b = nextByte!true ();
          if (b < 48 || b >= 58) {
            return res;
          }
          res = res * 10 + (b - 48);
        }
      }
    }
  }
  template next(T) if (isUnsigned!T) {
    T next () {
      if (seekByte (48)) {
        return 0;
      }
      T res = nextByte!false () - 48;
      while (true) {
        ubyte b = nextByte!true ();
        if (b < 48 || b >= 58) {
          break;
        }
        res = res * 10 + (b - 48);
      }
      return res;
    }
  }
  T[] nextA(T) (in int n) {
    auto a = uninitializedArray!(T[]) (n);
    foreach (i; 0 .. n) {
      a[i] = next!T;
    }
    return a;
  }
}

long test (in int a, in int b) {
  long res;
  long d = 10;
  while (d - 1 <= b) {
    res += a;
    d *= 10;
  }
  return res;
}

//a * b + a + b = conc (a, b)
//a * (b + 1) + b = conc (a, b)

void main() {
  auto r = new InputReader ();
  immutable nt = r.next!uint ();
  auto ans = new long[nt];
  foreach (tid; 0 .. nt) {
    int a = r.next!int;
    int b = r.next!int;
    ans[tid] = test (a, b);
  }
  writefln! ("%(%s\n%)")(ans);
}

import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen)  return _RDA!T(_f, fix); else return _RDA!T(fix); }

size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * (y / gcd(x, y)); }

long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }

void main()
{
	auto t = RD!int;
	auto ans = new long[](t);
	foreach (ti; 0..t)
	{
		auto A = RD;
		auto B = RD;
		auto x = B;
		long digit;
		while (x != 0)
		{
			x /= 10;
			++digit;
		}
		debug writeln(B, ":", 10^^(digit)-1);
		if (B == 10^^(digit)-1)
		{
			ans[ti] = A * digit;
		}
		else
		{
			ans[ti] = A * (digit-1);
		}
	}

	foreach (e; ans)
		writeln(e);
	stdout.flush;
	debug readln;
}


import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); }
void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T rtype(T)(){ return read.to!T; } alias rint = rtype!int, rlong = rtype!long, rreal = rtype!real;
T[] rtypes(T)(int n){ return n.iota.map!(i => rtype!T()).array; } alias rint = rtypes!int, rlong = rtypes!long, rreal = rtypes!real;
T[][] rtypess(T)(int n, int m){ return n.iota.map!(i => rtypes!T(m)).array; } alias rint = rtypess!int, rlong = rtypess!long, rreal = rtypess!real;

// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //

void solve(){
	int n = rint;
	long[] xs = rlong(n);

	Node[] nodes;
	foreach(i; 0 .. n) nodes ~= new Node(i, xs[i]);

	foreach(i; 0 .. n - 1){
		int a = rint - 1, b = rint - 1;
		nodes[a].nodes ~= nodes[b];
		nodes[b].nodes ~= nodes[a];
	}

	nodes[0].treefy(null);

	Finite ans = nodes[0].calc;

	ans.writeln;
}
/*
Tree DP.

*/
class Node{
	int id;
	static long k;
	Node[] nodes;
	Node[] kids;
	Node parent;
	bool isVisited;
	long value;
	this(int id, long value){
		this.id = id;
		this.value = value;
	}
	void treefy(Node parent){
		this.parent = parent;
		foreach(nd; nodes) if(!parent || nd.id != parent.id) this.kids ~= nd, nd.treefy(this);
	}
	P[] points;
	Finite calc(){
		Finite res = Finite(0);
		this.points = [];
		P[] parpoints = [];
		if(parent) parpoints = parent.points;
		foreach(p; parpoints){
			long d = gcd(p.value, this.value);
			if(points.length && points[$ - 1].value == d){
				points[$ - 1] = P(d, points[$ - 1].count + p.count);
			}
			else{
				points ~= P(d, p.count);
			}
		}
		points ~= P(this.value, 1);

		foreach(p; this.points){
			res += p.value * p.count;
		}
		log("id:", (id + 1), "value:", value, "points:", points, "res:", res);

		foreach(kid; kids){
			res += kid.calc();
		}

		return res;
	}
	override string toString(){
		return "id:" ~ id.to!string ~ " value:" ~ value.to!string ~ " kids:" ~ kids.map!(x => x.id).to!string;
	}
}
struct P{
	long value, count;
}

// --------

import std.conv;
struct Finite{
	ulong value; static ulong mod = 1_000_000_007;
	private static ulong[] _inv = [0, 1], _frac = [1, 1], _invfrac = [1, 1];
	this(ulong v){ value = v % mod; }
	bool opCast(T: bool)(){ return value != 0; }
	Finite opUnary(string s){ ulong v;
		if(s == "+") v = value;
		else if(s == "-") v = mod - value;
		else if(s == "++") v = value + 1;
		else if(s == "--") v = value + mod - 1;
		else assert(0, "Operator unary " ~ s ~ " not implemented");
		return Finite(v);
	}
	Finite opBinary(string s)(Finite b){
		return opBinary!s(b.value);
	}
	Finite opBinary(string s)(ulong u){ ulong v;
		if(s == "+") v = value + u;
		else if(s == "-") v = value + mod - u;
		else if(s == "*") v = value * u;
		else if(s == "/") v = value * inv(u);
		else assert(0, "Operator " ~ s ~ " not implemented");
		return Finite(v);
	}
	Finite opBinaryRight(string s)(ulong u){ ulong v;
		if(s == "+") v = u + value;
		else if(s == "-") v = u + mod - value;
		else if(s == "*") v = u * value;
		else if(s == "/") v = u * invvalue;
		else assert(0, "Operator " ~ s ~ " not implemented");
		return Finite(v);
	}
	Finite opAssign(ulong v){ value = v; return this; }
	Finite opOpAssign(string s)(Finite b){
		return opOpAssign!s(b.value);
	}
	Finite opOpAssign(string s)(ulong v){
		if(s == "+") value = (value + v) % mod;
		else if(s == "-") value = (value + mod - v) % mod;
		else if(s == "*") value = (value * v) % mod;
		else if(s == "/") value = (value * inv(v)) % mod;
		else assert(0, "Operator " ~ s ~ "= not implemented");
		return this;
	}
	bool opEquals(Finite b){
		return(value == b.value);
	}
	string toString(){ return value.to!string; }
	ulong inv(ulong v){
		assert(v > 0);
		assert(v < mod);
		while(v >= _inv.length){
			_inv ~= _inv[mod % $] * (mod - mod / _inv.length) % mod;
		}
		return _inv[v.to!uint];
	}
	ulong invvalue(){
		return inv(value);
	}
	static Finite opCall(ulong v){
		return Finite(v);
	}

}

// ----------

long gcd(long a, long b){ // OK with 0, non-negative
	if(a < 0) return gcd(-a, b);
	if(b < 0) return gcd(a, -b);
	if(a == 0) return b;
	if(b == 0) return a;
	if(a % b == 0) return b;
	if(a < b) return gcd(b, a);
	else return gcd(b, a % b);
}

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }

struct ModInt(long M) {
  int x;
  this(ModInt a) { x = a.x; }
  this(long a) { x = cast(int)(a % M); if (x < 0) x += M; }
  ref ModInt opAssign(long a) { return (this = ModInt(a)); }
  ref ModInt opOpAssign(string op)(ModInt a) {
    static if (op == "+") { x += a.x; if (x >= M) x -= M; }
    else static if (op == "-") { x -= a.x; if (x < 0) x += M; }
    else static if (op == "*") { x = cast(int)((cast(long)(x) * a.x) % M); }
    else static assert(false);
    return this;
  }
  ref ModInt opOpAssign(string op)(long a) {
    static if (op == "^^") {
      ModInt t2 = this, te = ModInt(1);
      for (long e = a; e; e >>= 1) {
        if (e & 1) te *= t2;
        t2 *= t2;
      }
      x = cast(int)(te.x);
      return this;
    } else return mixin("this " ~ op ~ "= ModInt(a)");
  }
  ModInt opUnary(string op)() const if (op == "-") { return ModInt(-x); }
  ModInt opBinary(string op, T)(T a) const { return mixin("ModInt(this) " ~ op ~ "= a"); }
  ModInt opBinaryRight(string op)(long a) const { return mixin("ModInt(a) " ~ op ~ "= this"); }
  string toString() const { return x.to!string; }
}

enum MO = 10L^^9 + 7;
alias Mint = ModInt!MO;

enum E = 17;

int N;
long[] X;
int[] A, B;

int[][] G;
int[][] par;

void dfs(int u, int p) {
  par[0][u] = p;
  foreach (v; G[u]) {
    if (v != p) {
      dfs(v, u);
    }
  }
}

void main() {
  try {
    for (; ; ) {
      N = readInt();
      X = new long[N];
      foreach (u; 0 .. N) {
        X[u] = readLong();
      }
      A = new int[N - 1];
      B = new int[N - 1];
      foreach (i; 0 .. N - 1) {
        A[i] = readInt() - 1;
        B[i] = readInt() - 1;
      }

      G = new int[][N];
      foreach (i; 0 .. N - 1) {
        G[A[i]] ~= B[i];
        G[B[i]] ~= A[i];
      }
      par = new int[][](E, N + 1);
      dfs(0, N);
      par[0][N] = N;
      foreach (e; 0 .. E - 1) {
        foreach (u; 0 .. N + 1) {
          par[e + 1][u] = par[e][par[e][u]];
        }
      }

      auto ds = new long[][](E, N);
      foreach (u; 0 .. N) {
        const v = par[0][u];
        ds[0][u] = (v == N) ? 0 : X[v];
      }
      foreach (e; 0 .. E - 1) {
        foreach (u; 0 .. N) {
          const v = par[e][u];
          ds[e + 1][u] = (v == N) ? ds[e][u] : gcd(ds[e][u], ds[e][v]);
        }
      }
      debug {
        foreach (e; 0 .. 4) {
          writeln(par[e]);
          writeln(ds[e]);
        }
      }

      Mint ans;
      foreach (u; 0 .. N) {
        long g = X[u];
        int v = u;
        for (; ; ) {
          long sum;
          foreach_reverse (e; 0 .. E) {
            const w = par[e][v];
            if (w != N) {
              // gcd(g, ds[e][v]) == g
              if ((g == 0) ? (ds[e][v] == 0) : (ds[e][v] % g == 0)) {
                sum |= 1 << e;
                v = w;
              }
            }
          }
          ans += g * Mint(sum + 1);
          v = par[0][v];
          if (v == N) {
            break;
          }
          g = gcd(g, X[v]);
        }
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}

import std.algorithm;
import std.conv;
import std.numeric;
import std.range;
import std.stdio;

immutable int mod = 10 ^^ 9 + 7;
immutable int NA = -1;

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		readln;
		auto x = readln.splitter.map !(to !(long)).array;
		auto g = new int [] [n];
		foreach (i; 0..n - 1)
		{
			int u, v;
			readf (" %s %s", &u, &v);
			u -= 1;
			v -= 1;
			g[u] ~= v;
			g[v] ~= u;
		}

		int res = 0;

		void recur (int v, int p, int [long] s)
		{
			int [long] t;
			t[x[v]] = 1;
			foreach (k, w; s)
			{
				auto r = gcd (k, x[v]);
				t[r] += w;
			}
			foreach (k, w; t)
			{
				res = (res + k * w) % mod;
			}
			foreach (u; g[v])
			{
				if (u != p)
				{
					recur (u, v, t);
				}
			}
		}

		recur (0, NA, null);
		writeln (res);
	}
}

import std.algorithm, std.conv, std.numeric, std.range, std.stdio, std.string;

void main () {
	auto n = readln.strip.to !(int);
	auto x = readln.splitter.map !(to !(long)).array;
	auto g = new int [] [n];
	foreach (i; 0..n - 1) {
		int u, v;
		readf (" %s %s", &u, &v);
		u -= 1;
		v -= 1;
		g[u] ~= v;
		g[v] ~= u;
	}

	int res = 0;

	void recur (int v, int p, int [long] s) {
		int [long] t;
		t[x[v]] = 1;
		foreach (k, w; s) t[gcd (k, x[v])] += w;
		foreach (k, w; t) res = (res + k * w) % (10 ^^ 9 + 7);
		foreach (u; g[v]) if (u != p) recur (u, v, t);
	}

	recur (0, -1, null);
	writeln (res);
}


import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }

bool solve(int[] as) {
  const n = cast(int)(as.length);
  const cnt1 = as.count(1);
  if (cnt1 == 0) {
    return false;
  }
  if (cnt1 == n) {
    return true;
  }
  foreach (i; 0 .. n - 2 + 1) {
    if (as[i] >= 1 && as[i + 1] >= 1) {
      return true;
    }
  }
  foreach (i; 0 .. n - 3 + 1) {
    if (as[i] >= 1 && as[i + 2] >= 1) {
      return true;
    }
  }
  return false;
}

void main() {
  debug {
    enum lim = 8;
    foreach (n; 1 .. lim + 1) {
      auto graph = new int[][3^^n];
      foreach (u; 0 .. 3^^n) {
        auto as = new int[n];
        foreach (i; 0 .. n) {
          as[i] = u / 3^^i % 3;
        }
        foreach (i; 0 .. n) foreach (j; i + 1 .. n + 1) {
          auto bs = as[i .. j].dup;
          bs.sort;
          auto cs = as.dup;
          cs[i .. j] = bs[($ + 1) / 2 - 1];
          int v;
          foreach_reverse (k; 0 .. n) {
            v = v * 3 + cs[k];
          }
          graph[v] ~= u;
        }
      }
      int start;
      foreach_reverse (i; 0 .. n) {
        start = start * 3 + 1;
      }
      DList!int que;
      auto vis = new bool[3^^n];
      vis[start] = true;
      que ~= start;
      for (; !que.empty; ) {
        const u = que.front;
        que.removeFront;
        foreach (v; graph[u]) {
          if (!vis[v]) {
            vis[v] = true;
            que ~= v;
          }
        }
      }
      foreach (u; 0 .. 3^^n) {
        auto as= new int[n];
        foreach (i; 0 .. n) {
          as[i] = u / 3^^i % 3;
        }
        const res = solve(as);
        if (vis[u] != res) {
          foreach (i; 0 .. n) {
            write(as[i]);
          }
          writeln();
          writeln(vis[u], " ", res);
          assert(false);
        }
      }
    }
  }

  try {
    for (; ; ) {
      const numCases = readInt();
      foreach (caseId; 0 .. numCases) {
        const N = readInt();
        const K = readInt();
        auto A = new int[N];
        foreach (i; 0 .. N) {
          A[i] = readInt();
        }

        auto ss = new int[N];
        foreach (i; 0 .. N) {
          ss[i] = (A[i] < K) ? 0 : (A[i] == K) ? 1 : 2;
        }
        const ans = solve(ss);
        writeln(ans ? "yes" : "no");
      }
    }
  } catch (EOFException e) {
  }
}

import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

bool solve (R) (R a, int n, int k)
{
	if (a.length == 1)
	{
		return k == a.front;
	}
	if (!a.canFind (k))
	{
		return false;
	}
	if (a.length == 2)
	{
		return sum (a) >= 2 * k;
	}
	foreach (i; 0..n - 2)
	{
		if ((a[i] >= k) + (a[i + 1] >= k) + (a[i + 2] >= k) >= 2)
		{
			return true;
		}
	}
	return false;
}

void main ()
{
	readln;
	int n, k;
	while (readf !(" %s %s") (n, k) > 0)
	{
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		writeln (solve (a, n, k) ? "yes" : "no");
	}
}

import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;

void main()
{
    auto T = readln.chomp.to!int;
    foreach (_t; 0..T) {
        auto nk = readln.split.to!(int[]);
        auto N = nk[0];
        auto K = nk[1];
        int[] as;
        bool has;
        foreach (a; readln.split.to!(int[])) {
            if (a == K) has = true;
            as ~= a;
        }
        if (!has) {
            writeln("no");
            continue;
        }
        if (N == 1) {
            writeln(has ? "yes" : "no");
            continue;
        }
        foreach (i, a; as[0..$-1]) {
            if (a == K) {
                if (as[i+1] >= K) goto ok;
                if (i+2 < N && as[i+2] >= K) goto ok;
            } else if (a > K) {
                if (as[i+1] >= K) goto ok;
                if (i+2 < N && as[i+2] >= K) goto ok;
            }
        }
        writeln("no");
        continue;
        ok:
        writeln("yes");
    }
}

import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

bool solve (R) (R a, int n, int k)
{
	if (a.length == 1)
	{
		return k == a.front;
	}
	if (!a.canFind (k))
	{
		return false;
	}
	foreach (i; 0..n - 2)
	{
		if ((a[i] >= k) + (a[i + 1] >= k) + (a[i + 2] >= k) >= 2)
		{
			return true;
		}
	}
	return false;
}

void main ()
{
	readln;
	int n, k;
	while (readf !(" %s %s") (n, k) > 0)
	{
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		writeln (solve (a, n, k) ? "yes" : "no");
	}
}

import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

bool solve (R) (R a, int k)
{
	if (a.length < 2)
	{
		return k == a.front;
	}
	int lo = 0;
	int me = 0;
	int hi = 0;
	// k = 3
	// 3 x
	// 3 1 x
	// 3 1 3 v
	// 3 5 v
	foreach (ref c; a.find (k))
	{
		lo += (c < k);
		me += (c == k);
		hi += (c > k);
		int n = lo + me + hi;
		writeln (lo, " ", me, " ", hi);
		if (n > 1 && lo <= (n - 1) / 2 && lo + me > (n - 1) / 2)
		{
			return true;
		}
		if (c == k)
		{
			lo = 0;
			me = 1;
			hi = 0;
		}
	}
	return false;
}

void main ()
{
	readln;
	int n, k;
	while (readf !(" %s %s") (n, k) > 0)
	{
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		writeln (solve (a, k) || solve (a.retro, k) ? "yes" : "no");
	}
}

import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

bool solve (R) (R a, int k)
{
	if (a.length < 2)
	{
		return k == a.front;
	}
	int lo = 0;
	int me = 0;
	int hi = 0;
	foreach (ref c; a.find (k))
	{
		lo += (c < k);
		me += (c == k);
		hi += (c > k);
		int n = lo + me + hi;
		if (n > 1 && lo <= (n - 1) / 2 && lo + me > (n - 1) / 2)
		{
			return true;
		}
		if (c == k)
		{
			lo = 0;
			me = 1;
			hi = 0;
		}
	}
	return false;
}

void main ()
{
	readln;
	int n, k;
	while (readf !(" %s %s") (n, k) > 0)
	{
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		writeln (solve (a, k) || solve (a.retro, k) ? "yes" : "no");
	}
}

import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;

void main()
{
    auto T = readln.chomp.to!int;
    foreach (_t; 0..T) {
        auto nk = readln.split.to!(int[]);
        auto N = nk[0];
        auto K = nk[1];
        int[] as;
        size_t[] kis = [0];
        foreach (i, a; readln.split.to!(int[])) {
            as ~= a;
            if (a == K) kis ~= i;
        }
        if (N == 1) {
            writeln(as[0] == K ? "yes" : "no");
            continue;
        }
        if (kis.length == 1) {
            writeln("no");
            continue;
        }
        foreach (i, s; kis[1..$]) {
            int x;
            foreach_reverse (j; kis[i]+1..s) {
                if (as[j] >= K) {
                    ++x;
                } else {
                    --x;
                }
                if (x >= 0) goto ok;
            }
        }
        kis = kis[1..$] ~ N.to!size_t;
        foreach (i, s; kis[0..$-1]) {
            int x;
            foreach (j; s+1..kis[i+1]) {
                if (as[j] >= K) {
                    ++x;
                } else {
                    --x;
                }
                if (x >= 0) goto ok;
            }
        }
        writeln("no");
        continue;
        ok:
        writeln("yes");
    }
}

import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;

void main()
{
    auto T = readln.chomp.to!int;
    foreach (_t; 0..T) {
        auto nk = readln.split.to!(int[]);
        auto N = nk[0];
        auto K = nk[1];
        int[] as;
        size_t[] kis = [0];
        foreach (i, a; readln.split.to!(int[])) {
            as ~= a;
            if (a == K) kis ~= i;
        }
        if (N == 1) {
            writeln(as[0] == K ? "yes" : "no");
            continue;
        }
        if (kis.length == 1) {
            writeln("no");
            continue;
        }
        foreach (i, s; kis[1..$]) {
            int x;
            foreach_reverse (j; kis[i]..s) {
                if (as[j] >= K) {
                    ++x;
                } else {
                    --x;
                }
                if (x >= 0) goto ok;
            }
        }
        kis ~= (N-1).to!size_t;
        kis = kis[1..$];
        foreach (i, s; kis[0..$-1]) {
            int x;
            foreach (j; s+1..kis[i+1]+1) {
                if (as[j] >= K) {
                    ++x;
                } else {
                    --x;
                }
                if (x >= 0) goto ok;
            }
        }
        writeln("no");
        continue;
        ok:
        writeln("yes");
    }
}

import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;

void main()
{
    auto T = readln.chomp.to!int;
    foreach (_t; 0..T) {
        auto nk = readln.split.to!(int[]);
        auto N = nk[0];
        auto K = nk[1];
        int[] as;
        size_t[] kis;
        foreach (i, a; readln.split.to!(int[])) {
            as ~= a;
            if (a == K) kis ~= i;
        }
        if (N == 1) {
            writeln(as[0] == K ? "yes" : "no");
            continue;
        }
        if (kis.empty) {
            writeln("no");
            continue;
        }
        if (kis[0] != 0) {
            int x;
            foreach_reverse (i; 0..kis[0]) {
                if (as[i] >= K) {
                    ++x;
                } else {
                    --x;
                }
                if (x >= 0) goto ok;
            }
        }
        kis ~= (N-1).to!size_t;
        foreach (i, s; kis[0..$-1]) {
            int x;
            foreach (j; s+1..kis[i+1]+1) {
                if (as[j] >= K) {
                    ++x;
                } else {
                    --x;
                }
                if (x >= 0) goto ok;
            }
        }
        writeln("no");
        continue;
        ok:
        writeln("yes");
    }
}


import std.bigint,std.container,std.array,std.algorithm,std.conv,std.functional,std.math,std.numeric,std.string,std.range,std.complex,std.typecons,std.regex,
std.random,std.uni,std.traits,std.variant;
import core.stdc.stdio:freopen;
import std.stdio;
//import core.stdc.stdio;
alias RedBlackTree Rbt;
alias redBlackTree rbt;
alias BigInt big;
alias pair!(int,int) pii;
alias pair!(long,long) pll;
alias BoyerMooreFinder strfind;
immutable int mod=1000000007;
@property pure nothrow T lcm (T)(auto ref T a,auto ref T b)
{
	return a/gcd(a,b)*b;
}
pure nothrow @property X binpow(X,Y)(X base,Y exp)
if(is(typeof(exp&1)))
{
	if(exp<0)return X(0);
	X res=X(1);
	while(exp>0)
	{
		if(exp&1)res*=base;
		base*=base;
		exp>>=1;
	}
	return res;
}
pure nothrow @property auto binpow(X,Y,Z)(X base,Y exp,in Z mm)
if(is(typeof(exp&1)))
{
	if(mm==0) return binpow(base,exp);
	if(exp<0)return X(0);
	auto res=X(1)%mm;
	while(exp>0)
	{
		if(exp&1)res=(res*base)%mm;
		base*=base;
		base%=mm;
		exp>>=1;
	}
	return res;
}
@property void putarr(X)(in X a,in char ch=' '){foreach(const ref i;a)write(i,ch);}
@property void putarr(X)(X a,in int n,in int m)
{
	foreach(i;0..n)
	{
		foreach(j;0..m)write(a[i][j],' ');
		writeln;
	}
}
@property bool getarr(X)(X a,in int n){bool b=1; foreach(ref i;a[0..n])b=input(&i);return b;}
bool input(T...)(T ptrs) if (ptrs.length > 0) {return readf(replicate(" %s", ptrs.length), ptrs)==ptrs.length;}
struct pair(X,Y)
{
	X first;
	Y second;
	alias first fi;
	alias second se;
	this(X a_,Y b_){
		fi=a_;
		se=b_;
	}
	this(this){fi=fi;se=se;}
	this(A,B)(auto ref pair!(A,B) p)
	{
		fi=p.fi;
		se=p.se;
	}
	int opCmp(in pair!(X,Y) s_) const pure nothrow @safe
	{
		int cmp=0-(fi<s_.fi)+(fi>s_.fi);
		if(cmp!=0)return cmp;
		else return (0-(se<s_.se)+(se>s_.se));
	}
};
pair!(X,Y) mp(X,Y)(in X x_,in Y y_) pure nothrow @safe
{
	pair!(X,Y) pp;
	pp.fi=x_;
	pp.se=y_;
	return pp;
}
X sqr(X)(in X a_) pure nothrow @property @safe @nogc {return a_*a_;}
X cub(X)(in X a_) pure nothrow @property @safe @nogc {return a_*a_*a_;}
nothrow @nogc @system void inf(in char* name) {freopen(name,"r",core.stdc.stdio.stdin);}
nothrow @nogc @system void ouf(in char* name) {freopen(name,"w",core.stdc.stdio.stdout);}
auto arread(T)(){return readln.split.map!(to!(T)).array;}
nothrow @property pure size_t lowb(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
{
	size_t l=0,r=a.length;
	while(r-l>1)
	{
		auto m=(l+r)>>1;
		if(!(a[m]<g))r=m;
		else l=m;
	}
	return (!(a[l]<g))?l:r;
}
pure @property nothrow size_t upb(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
{
	size_t l=0,r=a.length;
	while(r-l>1)
	{
		auto m=(l+r)>>1;
		if(g<a[m])r=m;
		else l=m;
	}
	return (g<a[l])?l:r;
}
pure @property nothrow size_t binf(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
{
	size_t l=0,r=a.length;
	while(r-l>1)
	{
		auto m=(l+r)>>1;
		if(!(a[m]<g))r=m;
		else l=m;
	}
	return (g==a[l])?l:a.length;
}
pure @property nothrow bool binary_search(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
{
	return binf(a,g)!=a.length;
}
//split strip isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin cumulativeFold schwartzSort multiSort partialSort heapify join filter
//---------------------------------------------------------------program beginning-----------------------------------------------------------------

void main()
{
	string s;
	while((s=readln.strip)!="")
	{
		auto a=s.split(',').array;
		int pos,n=a.length;
		string[][] ans=new string[][1];
		auto u=new bool[n];
		void dfs(int lev)
		{
			if(pos==n)return;
			u[pos]=1;
			if(lev==ans.length)
			{
				ans.length++;
			}
			ans[lev]~=a[pos];
			int k=a[pos+1].to!int;
			pos+=2;
			foreach(i;0..k)
			{
				dfs(lev+1);
			}
			//pos-=2;
		}
		int pp=pos;
		for(;pp<n;pp+=2)
		{
			if(!u[pp]){pos=pp;dfs(0);}
			//debug writeln(pos);
		}
		writeln(ans.length);
		foreach(i;ans)
		{
			foreach(j;i)write(j,' ');
			writeln;
		}
		debug writeln("takt");
	}
	debug readln;
}

import core.bitop;
import core.checkedint;
import core.simd;
import core.stdc.stdlib;
import core.stdc.string;
import std.algorithm;
import std.array;
import std.ascii;
import std.bigint;
import std.bitmanip;
import std.complex;
import std.container;
import std.conv;
import std.datetime;
import std.format;
import std.functional;
import std.math;
import std.meta;
import std.numeric;
import std.random;
import std.range;
import std.regex;
import std.stdio;
import std.string;
import std.typecons;
import std.variant;

__gshared:

bool read(T...)(T ptrs) if (ptrs.length > 0) {
    return readf(' ' ~ replicate("%s ", ptrs.length), ptrs) == ptrs.length;
}

T[ ] allocate(T)(size_t n) {
    return (cast(T*)malloc(n * T.sizeof))[0 .. n];
}

auto pairwise(R)(R range) if (isForwardRange!R) {
    return lockstep(range, dropOne(range));
}

int pos;
string s;
string[ ][ ] result;
int resDepth;

void parse(int depth) {
    assert(pos < s.length);
    resDepth = max(resDepth, depth);
    if (depth == result.length)
        result ~= cast(string[ ])null;
    int comma = cast(int)(s.length - s[pos .. $].find(',').length);
    result[depth] ~= s[pos .. comma];
    pos = comma + 1;
    comma = cast(int)(s.length - s[pos .. $].find(',').length);
    int count = s[pos .. comma].to!int;
    pos = comma + 1;
    foreach (i; 0 .. count)
        parse(depth + 1);
}

void main() {
    while ((s = readln()).length > 1) {
        s = s[0 .. $ - 1];
        result = null;
        resDepth = 0;
        pos = 0;
        while (pos < s.length)
            parse(0);
        writeln(resDepth + 1);
        writefln("%(%-(%s %)\n%)", result);
    }
}


import std.stdio;
import std.conv;
import std.container;

void main() {
    string s = stdin.readln;
    Array!char a;
    foreach (c; s) {
        if (a.empty) { a.insert(c); }
        else if (a.back == c) { a.removeBack; }
        else { a.insert(c); }
    }
    foreach (c; a) {
        write(c);
    }
}

module cf_81A;

import std.stdio;

void main() {
    string text;

    readf("%s\n", &text);

    string optText = "";

    while (text.length > 0) {
        if (optText.length == 0) {
            optText ~= text[0];
        } else if (optText[$ - 1] == text[0]) {
            optText = optText[0 .. $ - 1];
        } else {
            optText ~= text[0];
        }

        text = text[1 .. $];
    }

    writeln(optText);
}


import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;

void main ()
{
	auto a = readln.splitter.map !(to !(long)).array;
	auto p = 3.iota.array;
	writeln ("First");
	stdout.flush ();
	while (true)
	{
		schwartzSort !(i => a[i]) (p);
		long delta = a[p[2]] - a[p[0]] + a[p[2]] - a[p[1]];
		if (a[p[2]] - a[p[1]] == a[p[1]] - a[p[0]])
		{
			delta = a[p[2]] - a[p[1]];
		}
		writeln (delta);
		stdout.flush ();
		auto k = readln.strip.to !(int);
		if (k == 0)
		{
			break;
		}
		a[k - 1] += delta;
		debug {stderr.writeln (a);}
	}
}

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }

void main() {
  auto A = new long[3];
  foreach (i; 0 .. 3) {
    A[i] = readLong();
  }
  writeln("First");
  stdout.flush;

  enum ini = 10L^^10;
  writeln(ini);
  stdout.flush;
  const i0 = readInt() - 1;
  if (i0 == -1) {
    return;
  }
  A[i0] += ini;
  debug {
    writeln("A = ", A);
  }

  auto a = A.dup;
  a.sort;
  const magic = (a[1] - a[0]) + 2 * (a[2] - a[1]);
  writeln(magic);
  stdout.flush;
  const i1 = readInt() - 1;
  if (i1 == -1) {
    return;
  }
  A[i1] += magic;
  debug {
    writeln("A = ", A);
  }

  auto b = A.dup;
  b.sort;
  assert(b[1] - b[0] == b[2] - b[1]);
  const diff = b[1] - b[0];
  writeln(diff);
  stdout.flush;
  const i2 = readInt() - 1;
  if (i2 == -1) {
    return;
  }
  assert(false);
}


module _template;
import std.stdio;
import std.string;
import std.algorithm;
import std.container;
import std.range;
import std.math;
import std.numeric;
import std.conv;
import std.typecons;
import std.format;

struct IO {
        string read_string() {
                while (tokens.empty) {
                tokens = readln.split;
        }
        auto token = tokens.front;
                tokens.popFront;
                return token;
        }

        int read_int() {
                return read_string.to!int;
        }

        double read_double() {
                return read_string.to!double;
        }

        string[] tokens;
}

int count_greater(ref int[] a1, int n) {
        int count = 0;
        for (int i = 0; i < a1.length; i++) {
                if (a1[i] > n) {
                        count++;
                }
        }
        return count;
}

void main() {
        IO cin;
        int t = 1;
        // int t = cin.read_int;
        while (t--) {
                int n = cin.read_int;
                int[] arr = new int[n];
                foreach (ref i; arr) {
                        i = cin.read_int;
                }
                for (int i = 0; i < n; i++) {
                        write(count_greater(arr, arr[i]) + 1, " ");
                }
                writeln();
        }
}

version (all) {
import std.math,
       std.conv,
       std.stdio,
       std.ascii,
       std.range,
       std.array,
       std.regex,
       std.format,
       std.bigint,
       std.traits,
       std.random,
       std.string,
       std.numeric,
       std.variant,
       std.typecons,
       std.container,
       std.algorithm,
       std.typetuple,
       std.exception,
       core.checkedint;
}

void main() {

	readln.strip;
	auto a = readln.split.map!(to!uint).array;

	foreach (el; a) {
		write(1 + a.filter!(c => c > el).array.length, ' ');
	}

	writeln();
}

import std.stdio;
import std.algorithm;
import std.string;
import std.conv;
import std.array;
import std.range;
import std.ascii;
import std.functional;

alias isEven = unaryFun!("(a & 1) == 0");
alias isOdd =  unaryFun!("(a & 1) != 0");

void readInput()
{
	size_t lineSize;

	auto studentCount = stdin.readln.chomp().to!int();

	auto grades = stdin.readln().split().map!(a => to!int(a)).array;

	int[int] indexHolder;
	auto realGrades = grades.dup;
	grades.sort!"a > b"();
	int lastGrade = 0;
	int lastElem = 0;

	foreach (i, elem; grades)
	{
		if ( lastElem != elem)
		{
			lastElem = elem;
			lastGrade = i + 1;
			indexHolder[elem] = lastGrade;
		}
	}

	writeln(realGrades.map!( a => to!string(indexHolder[a])).joiner(" "));

}

void main(  string[] args )
{
	readInput();
}

import std.stdio;
import std.algorithm;
import std.string;
import std.conv;
import std.array;
import std.range;
import std.ascii;
import std.functional;

alias isEven = unaryFun!("(a & 1) == 0");
alias isOdd =  unaryFun!("(a & 1) != 0");

void readInput()
{
	size_t lineSize;

	auto studentCount = stdin.readln.chomp().to!int();

	auto grades = stdin.readln().split().map!(a => to!int(a)).array;

	int[int] indexHolder;
	auto realGrades = grades.dup;
	grades.sort!"a > b"();
	int lastGrade = 0;
	int lastElem = 0;

	foreach (i, elem; grades)
	{
		if ( lastElem != elem)
		{
			lastElem = elem;
			lastGrade = i + 1;
			indexHolder[elem] = lastGrade;
		}
	}

	writeln(realGrades.map!( a => indexHolder[a]));

}

void main(  string[] args )
{
	readInput();
}

import std.stdio;
import std.algorithm;
import std.string;
import std.conv;
import std.array;
import std.range;
import std.ascii;
import std.functional;

alias isEven = unaryFun!("(a & 1) == 0");
alias isOdd =  unaryFun!("(a & 1) != 0");

void readInput()
{
	size_t lineSize; ;

	auto studentCount = stdin.readln.chomp().to!int();

	auto grades = stdin.readln().split().map!(a => to!int(a)).array;

	int[int] indexHolder;
	auto realGrades = grades.dup;
	grades.sort!"a > b"();
	int lastGrade = 0;
	int lastElem = 0;

	foreach (i, elem; grades)
	{
		if ( lastElem != elem)
		{
			lastElem = elem;
			lastGrade = i + 1;
			indexHolder[elem] = lastGrade;
		}
	}

	writeln(realGrades.map!( a => indexHolder[a]));

}

void main(  string[] args )
{
	readInput();
}


import std.stdio, std.algorithm, std.conv, std.string, std.array, std.range, core.bitop;

int[] phasebase;
int[] fights;
byte[] a;
long[long] cache;

long solve(int phase, int fightnum)
{
  byte outcome = a[phasebase[phase] + fightnum];
  if (phase == 0)
    return outcome == 3 ? 2 : 1;

  long cacheid = cast(long)fightnum * 20 + cast(long)phase;
  long result = cache.get(cacheid, -1);
  if (result != -1)
    return result;
  result = 0;

  if (outcome & 1) {
    result += solve(phase - 1, fightnum * 2 + 0);
  }
  if (outcome & 2) {
    result += solve(phase - 1, fightnum * 2 + 1);
  }
  cache[cacheid] = result;
  return result;
}

void invalidate_cache(int idx)
{
  int phase = 0;
  while (true) {
    if (idx >= phasebase[phase] && idx < phasebase[phase] + fights[phase]) {
      int fightnum = idx - phasebase[phase];

      while (phase < phasebase.length) {
        long cacheid = cast(long)fightnum * 20 + cast(long)phase;
        cache[cacheid] = -1;
        fightnum /= 2;
        phase++;
      }
      return;
    }
    phase++;
  }
}

void main()
{
  int k, t;
  readf!" %d "(k);
  a = readln.strip.split("").map!((x) => (x[0] == '?') ? cast(byte)3 : (x[0] == '0' ? cast(byte)1 : cast(byte)2)).array;
//  writeln(a);
  assert(k == bsf(a.length + 1));
  k = bsf(a.length + 1);
  int pb = 0;
  while (k != 0) {
    phasebase ~= pb;
    k--;
    fights ~= 1 << k;
    pb += (1 << k);
  }
//  writeln(phasebase);
//  writeln(fights);
//  writeln("---");
  readf!" %d "(t);
  while (t--) {
    int i;
    readf!" %d"(i);
    string x = readln.strip;
    a[i - 1] = (x[0] == '?') ? cast(byte)3 : (x[0] == '0' ? cast(byte)1 : cast(byte)2);
//    writeln(a);

    // cache.clear();
    invalidate_cache(i - 1);

    writeln(solve(bsf(a.length + 1) - 1, 0));
  }
}

import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

void main ()
{
	int k;
	while (readf !(" %s") (k) > 0)
	{
		readln;
		auto s = readln.strip.dup ~ '*';
		reverse (s);
		auto m = 1 << k;
		auto f = new int [m * 2];
		f[] = 1;

		void calc (int pos)
		{
			f[pos] = (s[pos] == '0') ? f[pos * 2 + 1] :
			    (s[pos] == '1') ? f[pos * 2 + 0] :
			    f[pos * 2 + 0] + f[pos * 2 + 1];
		}

		foreach_reverse (pos; 1..m)
		{
			calc (pos);
		}

		auto q = readln.strip.to !(int);
		foreach (r; 0..q)
		{
			auto t = readln.split;
			auto pos = t[0].to !(int);
			pos = m - pos;
			s[pos] = t[1][0];
			for ( ; pos > 0; pos >>= 1)
			{
				calc (pos);
			}
			writeln (f[1]);
		}
	}
}

import std.stdio;
import std.ascii;
import std.string;
import std.range;
import std.algorithm;
import core.stdc.stdio;

int main()
{
    int k = readInt!int;
    char[] s = readString.dup.reverse;
    int n = cast(int)s.length;
    int q = readInt!int;

    long[] pos = new long[](n + 1);
    void calculate(int v)
    {
        if (v * 2 > s.length)
        {
            if (s[v - 1] == '0' || s[v - 1] == '1') pos[v] = 1;
            else pos[v] = 2;
            return;
        }
        int left = 2 * v;
        int right = 2 * v + 1;
        calculate(left);
        calculate(right);
        if (s[v - 1] == '1')
        {
            pos[v] = pos[left];
        }
        else if (s[v - 1] == '0')
        {
            pos[v] = pos[right];
        }
        else
        {
            pos[v] = pos[right] + pos[left];
        }
    }
    calculate(1);
    foreach(qi; 0 .. q)
    {
        int p = n - readInt!int + 1;
        string ch = readString;
        s[p - 1] = ch[0];
        while (p >= 1)
        {
            if (p * 2 >= n)
            {
                if (s[p - 1] == '0' || s[p - 1] == '1') pos[p] = 1;
                else pos[p] = 2;
                goto done;
            }
            if (s[p - 1] == '1')
            {
                pos[p] = pos[2 * p];
            }
            else if (s[p - 1] == '0')
            {
                pos[p] = pos[2 * p + 1];
            }
            else
            {
                pos[p] = pos[2 * p] + pos[2 * p + 1];
            }
            done:
            p /= 2;
        }
        pos[1].writeln;
    }
    return 0;
}

/* INPUT ROUTINES */
int currChar;
static this()
{
    currChar = getchar();
}
char topChar()
{
    return cast(char) currChar;
}
void popChar()
{
    currChar = getchar();
}
auto readInt(T)()
{
    T num = 0;
    int sgn = 0;
    while (topChar.isWhite) popChar;
    while (topChar == '-' || topChar == '+') { sgn ^= (topChar == '-'); popChar; }
    while (topChar.isDigit) { num *= 10; num += (topChar - '0'); popChar; }
    if (sgn) return -num; return num;
}
string readString()
{
    string res = "";
    while (topChar.isWhite) popChar;
    while (!topChar.isWhite) { res ~= topChar; popChar; }
    return res;
}
/**MODULAR SYSTEM*/
struct Z(immutable long m)
{
    long rep;
    this(long num)
    {
        rep = num;
    }
    Z!m opBinary(string operator)(Z!m rhs)
    {
        static if (operator == "+")
        {
            long result = rhs.rep + this.rep;
            if (result >= m) result -= m;
            return Z!m(result);
        }
        else static if (operator == "-")
        {
            long result = this.rep - rhs.rep;
            if (result < 0) result += m;
            return Z!m(result);
        }
        else static if (operator == "*")
        {
            long result = this.rep * rhs.rep;
            if (result >= m) result %= m;
            return Z!m(result);
        } else static assert(text("Operator ", operator, " not supported"));
    }
    Z!m opBinary(string operator)(long exponent) if (operator == "^^")
    {
        assert(exponent >= 0);
        Z!m base = this;
        Z!m result = 1;
        while (exponent)
        {
            if (exponent & 1)
                result = result * base;
            base = base * base;
            exponent >>= 1;
        }
        return result;
    }
    invariant
    {
        assert(rep >= 0 && rep < m);
    }
}