//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;
import std.bigint;

class InputReader {
  private:
  ubyte[] p;
  ubyte[] buffer;
  size_t cur;
  public:
  this () {
    buffer = uninitializedArray!(ubyte[])(16<<20);
    p = stdin.rawRead (buffer);
  }
  final ubyte skipByte (ubyte lo) {
    while (true) {
      auto a = p[cur .. $];
      auto r = a.find! (c => c >= lo);
      if (!r.empty) {
        cur += a.length - r.length;
        return p[cur++];
      }
      p = stdin.rawRead (buffer);
      cur = 0;
      if (p.empty) return 0;
    }
  }
  final ubyte nextByte () {
    if (cur < p.length) {
       return p[cur++];
    }
    p = stdin.rawRead (buffer);
    if (p.empty) return 0;
    cur = 1;
    return p[0];
  }

  template next(T) if (isSigned!T) {
    final T next ()  {
      T res;
      ubyte b = skipByte (45);
      if (b == 45) {
        while (true) {
          b = nextByte ();
          if (b < 48 || b >= 58) {
            return res;
          }
          res = res * 10 - (b - 48);
        }
      } else {
        res = b - 48;
        while (true) {
          b = nextByte ();
          if (b < 48 || b >= 58) {
            return res;
          }
          res = res * 10 + (b - 48);
        }
      }
    }
  }
  template next(T) if (isUnsigned!T) {
    final T next () {
      T res = skipByte (48) - 48;
      while (true) {
        ubyte b = nextByte ();
        if (b < 48 || b >= 58) {
          break;
        }
        res = res * 10 + (b - 48);
      }
      return res;
    }
  }
  final T[] nextA(T) (int n) {
    auto a = uninitializedArray!(T[]) (n);
    foreach (i; 0 .. n) {
      a[i] = next!T;
    }
    return a;
  }
}

void main() {
  auto r = new InputReader;
  immutable n = r.next!uint;
  immutable k = r.next!uint;
  immutable a = r.nextA!int(n).idup;
  auto s = new long[n];
  long cur;
  foreach_reverse (i; 0 .. n) {
    cur += a[i];
    s[i] = cur;
  }
  auto res = BigInt(s[0]);
  auto t = new long[n-1];
  s[1 .. n].copy (t);
  debug stderr.writeln (s);
  t.sort ();
  t.reverse ();
  foreach (i; 0 .. k - 1) {
    res += BigInt (t[i]);
  }
  writeln (res);
}

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

void main ()
{
	int n, k;
	while (readf !(" %s %s") (n, k) > 0)
	{
		readln;
		auto a = readln.splitter.map !(to !(int)).array;

		long [] s = [0];
		foreach_reverse (c; a)
		{
			s ~= s.back + c;
		}
		reverse (s);
		debug {writeln (s);}

		long res = s[0];
		sort (s[1..$ - 1]);
		reverse (s[1..$ - 1]);
		debug {writeln (s[1..$ - 1]);}
		foreach (i; 1..k)
		{
			res += s[i];
		}

		writeln (res);
	}
}

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; }
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //

void solve(){

	int n = read.to!int;
	int k = read.to!int;

	long[] as;
	foreach(i; 0 .. n) as ~= read.to!long;
	log("as:", as);

	S[] bs;
	long sum = 0;
	foreach_reverse(i; 1 .. n){
		sum += as[i];
		bs ~= S(i, sum);
	}
	log("bs:", bs);

	bs.sort!"a.sum>b.sum"();
	log("bs:", bs);

	bool[] ys = new bool[](n);
	foreach(i; 0 .. k - 1){
		log("i:", i, " b:", bs[i]);
		ys[bs[i].id] = 1;
	}
	log("ys:", ys);

	long ans;
	long f = 1;
	foreach(i, a; as){
		if(ys[i]) f += 1;
		ans += f * a;
	}

	ans.writeln;
}

struct S{
	int id;
	long sum;
}


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 long INF = long.max >> 1;

void main ()
{
	int n, m;
	long b;
	while (readf (" %s %s %s", &n, &m, &b) > 0)
	{
		alias Player = Tuple !(int, "x", long, "y", int, "z");
		auto p = new Player [n];
		foreach (ref q; p)
		{
			int k;
			readf (" %s %s %s", &q.x, &q.y, &k);
			q.z = 0;
			foreach (j; 0..k)
			{
				int v;
				readf (" %s", &v);
				v--;
				q.z |= 1 << v;
			}
		}
		sort !("a.y < b.y") (p);
		long res = INF;
		int m2 = 1 << m;
		auto f = new long [m2];
		f[] = INF;
		f[0] = 0L;
		foreach (ref q; p)
		{
			foreach (u; 0..m2)
			{
				int v = u | q.z;
				f[v] = min (f[v], f[u] + q.x);
			}
			debug {writeln (f, ' ', q.y);}
			if (f[m2 - 1] != INF)
			{
				res = min (res, f[m2 - 1] + b * q.y);
			}
		}
		if (res == INF)
		{
			res = -1;
		}
		writeln (res);
	}
}

import std.algorithm, std.stdio, std.typecons;
immutable long INF = long.max >> 1;
void main () {
	int n, m, b, k, v;
	readf (" %s %s %s", &n, &m, &b);
	alias Player = Tuple !(int, "x", long, "y", int, "z");
	auto p = new Player [n];
	foreach (ref q; p) {
		readf (" %s %s %s", &q.x, &q.y, &k);
		foreach (j; 0..k) {
			readf (" %s", &v);
			q.z |= 1 << (v - 1);
		}
	}
	p.sort !"a.y < b.y";
	long res = INF;
	auto f = new long [1 << m];
	f[1..$] = INF;
	foreach (q; p) {
		foreach (u; 0..1 << m)
			f[u | q.z] = min (f[u | q.z], f[u] + q.x);
		res = min (res, f[$ - 1] + b * q.y);
	}
	writeln (res < INF ? res : -1);
}

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 long INF = long.max >> 1;

void main ()
{
	int n, m;
	long b;
	while (readf (" %s %s %s", &n, &m, &b) > 0)
	{
		alias Player = Tuple !(int, "x", long, "y", int, "z");
		auto p = new Player [n];
		foreach (ref q; p)
		{
			int k;
			readf (" %s %s %s", &q.x, &q.y, &k);
			q.z = 0;
			foreach (j; 0..k)
			{
				int v;
				readf (" %s", &v);
				v--;
				q.z |= 1 << v;
			}
		}
		sort !("a.z < b.z") (p);
		long res = INF;
		int m2 = 1 << m;
		auto f = new long [m2];
		f[] = INF;
		f[0] = 0L;
		foreach (ref q; p)
		{
			foreach (u; 0..m2)
			{
				int v = u | q.z;
				f[v] = min (f[v], f[u] + q.x);
			}
			if (f[m2 - 1] != INF)
			{
				res = min (res, f[m2 - 1] + b * q.y);
			}
		}
		if (res == INF)
		{
			res = -1;
		}
		writeln (res);
	}
}


import std.stdio;
import std.math;
import std.algorithm;

const int oo = 10000000;

void main()
{
    int n;
    scanf("%d", &n);

    int sum = 0, cnt = 0, minx = oo;
    bool has = false;

    for (int i = 0; i < 2 * n - 1; ++i)
    {
	int x;
	scanf("%d", &x);
	sum += abs(x);
	cnt += x < 0;
	minx = min(minx, abs(x));
	if (x == 0)
	    has = true;
    }

    if (n % 2 == 1 || has)
	write(sum);
    else
    {
	if (cnt % 2 == 0)
	    write(sum);
	else
	    write(sum - 2 * minx);
    }
}

import std.algorithm;
import std.container;
import std.exception;
import std.functional;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		int m = n * 2 - 1;
		auto a = new int [m];
		int s = 0;
		foreach (i; 0..m)
		{
			readf (" %s", &a[i]);
			s += (a[i] <= 0);
			a[i] = abs (a[i]);
		}
		sort (a);
		int res = reduce !("a + b") (a);
		if ((n % 2 == 0) && (s % 2 != 0))
		{
			res -= 2 * a[0];
		}
		writeln (res);
	}
}

import std.stdio;
import std.ascii;
import std.range;
import std.array;
import std.functional;
import std.algorithm;
import std.conv;
import std.container;
import std.math;
import std.numeric;
import std.string;
import std.c.string;
import std.random;
import std.regex;
import std.typecons;

int sum(int[] xs) {
    int ret = 0;
    foreach (x; xs) ret += x;
    return ret;
}

void main() {
    int N; scanf("%d\n", &N);
    auto xs = readln.chomp.split(" ").map!(to!int).array;
    xs.sort;
    auto minus = cast(int)xs.count!"a < 0";

    if (N % 2 == 1 || minus % 2 == 0) {
        int ans = 0;
        foreach (x; xs) {
            ans += x.abs;
        }
        writeln(ans);
    } else {
        int ans = 0;
        auto as = xs.filter!"a < 0".array,
             bs = xs.filter!"a >= 0".array;
        ans -= as[0 .. minus - 1].sum;

        if (bs.empty) {
            ans += as.back;
        } else {
            int a = as.back.abs;
            int b = bs.front;
            ans += bs[1 .. $].sum;
            ans += max(a, b) - min(a, b);
        }
        writeln(ans);
    }

}

import std.algorithm;
import std.container;
import std.exception;
import std.functional;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		int m = n * 2 - 1;
		auto a = new int [m];
		int s = 0;
		foreach (i; 0..m)
		{
			readf (" %s", &a[i]);
			s += (a[i] <= 0);
			a[i] = abs (a[i]);
		}
		int res = reduce !("a + b") (a);
		if ((n % 2 == 0) && (s % 2 != 0))
		{
			res -= 2 * a[0];
		}
		writeln (res);
	}
}

import std.stdio;
import std.ascii;
import std.range;
import std.array;
import std.functional;
import std.algorithm;
import std.conv;
import std.container;
import std.math;
import std.numeric;
import std.string;
import std.c.string;
import std.random;
import std.regex;
import std.typecons;

int sum(int[] xs) {
    int ret = 0;
    foreach (x; xs) ret += x;
    return ret;
}

void main() {
    int N; scanf("%d\n", &N);
    auto xs = readln.chomp.split(" ").map!(to!int).array;
    xs.sort;
    auto minus = cast(int)xs.count!"a < 0";

    if (N % 2 == 1 || minus % 2 == 0) {
        int ans = 0;
        foreach (x; xs) {
            ans += x.abs;
        }
        writeln(ans);
    } else {
        int ans = 0;
        auto as = xs.filter!"a < 0".array,
             bs = xs.filter!"a >= 0".array;
        ans -= as[0 .. minus - 1].sum;

        if (bs.empty) {
            ans += as.back;
        } else {
            int a = as.back.abs;
            int b = bs.front;
            ans += max(a, b) - min(a, b);
        }
        writeln(ans);
    }

}


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, std.bitmanip;

immutable long MOD = 10^^9 + 7;

void main() {
    auto N = readln.chomp.to!int;
    auto ans = new int[](N);

    int tmp = 1<<29;
    int x = 1;
    for (int i = 1; i <= N; ++i) {
        if (i + N/i + (N%i>0) < tmp) {
            tmp = i + N/i + (N%i>0);
            x = i;
        }
    }

    for (int i = 1, p = 0; p < N; i += x) {
        for (int j = min(N, i + x - 1); j >= i && p < N; --j, ++p) {
            ans[p] = j;
        }
    }

    ans.map!(to!string).join(" ").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;
    readf("%s", &n);
    readln;

    auto sq = sqrt(cast(double) n).to!int;

    debug { sq.writeln; }

    auto chks = (n+1).iota.dropOne.chunks(sq).map!(c => c.array).array;

    debug { chks.writeln; }

    auto ans = chks.map!(c => retro(c)).join();

    ans.writefln!("%(%s %)");
}

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

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		auto t = cast (int) (sqrt (n - 1.0) + 1);
		auto s = t;
		if (t * (s - 1) >= n)
		{
			s -= 1;
		}
		int [] ans;
		foreach_reverse (i; 0..s)
		{
			foreach (j; 0..t)
			{
				int c = i * t + j + 1;
				if (1 <= c && c <= n)
				{
					ans ~= c;
				}
			}
		}
		debug {writeln (s, " x ", t);}
		writefln ("%(%s %)", ans);
	}
}

void main(){
  import std.stdio, std.string, std.conv, std.algorithm;

  int n; rd(n);
  int k=1;
  while((k+1)*(k+1)<=n) k++;
  int[] perm;
  for(int i=1; i<=n; ){
    int[] a;
    for(int j=0; j<k && i<=n; j++){
      a~=(i++);
    }
    perm=a~perm;
  }
  writefln("%(%s %)", perm);
}

/*
  19
  [17 18 19] [13 14 15 16] [9 10 11 12] [5 6 7 8] [1 2 3 4]
*/

void rd(T...)(ref T x){
  import std.stdio, std.string, std.conv;
  auto l=readln.split;
  assert(l.length==x.length);
  foreach(i, ref e; x) e=l[i].to!(typeof(e));
}

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)); }

// floor(sqrt(a))
long floorSqrt(long a) {
  import core.bitop : bsr;
  import std.algorithm : min;
  long b = a, x = 0, y = 0;
  for (int e = bsr(a) & ~1; e >= 0; e -= 2) {
    x <<= 1;
    y <<= 1;
    if (b >= (y | 1) << e) {
      b -= (y | 1) << e;
      x |= 1;
      y += 2;
    }
  }
  return x;
}

void main() {
  try {
    for (; ; ) {
      const N = readInt();
      const sqrtN = cast(int)(floorSqrt(N - 1)) + 1;
      debug {
        writeln(N, ": ", sqrtN);
      }

      int[] ans;
      foreach_reverse (x; 0 .. sqrtN) {
        foreach (y; 0 .. sqrtN) {
          const a = x * sqrtN + y;
          if (0 <= a && a < N) {
            ans ~= a;
          }
        }
      }
      assert(ans.length == N);

      foreach (i; 0 .. N) {
        if (i > 0) write(" ");
        write(ans[i] + 1);
      }
      writeln();
    }
  } 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, std.bitmanip;

immutable long MOD = 10^^9 + 7;

void main() {
    auto N = readln.chomp.to!int;
    auto ans = new int[](N);

    int tmp = N+1;
    int x = 1;
    for (int i = 2; i <= N; ++i) {
        if (i + N/i + (N%i>0) < tmp) {
            tmp = i + N/i + (N%i>0);
            x = i;
        }
    }

    for (int i = 1, p = 0; p < N; i += x) {
        for (int j = i + x - 1; j >= i && p < N; --j, ++p) {
            ans[p] = j;
        }
    }

    ans.map!(to!string).join(" ").writeln;
}

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, std.bitmanip;

immutable long MOD = 10^^9 + 7;

void main() {
    auto N = readln.chomp.to!int;
    auto ans = new int[](N);

    int tmp = N;
    int x = 1;
    for (int i = 2; i <= N; ++i) {
        if (i + N/i + (N%i>0) < tmp) {
            tmp = i + N/i + (N%i>0);
            x = i;
        }
    }

    for (int i = 1, p = 0; i < N && p < N; i += x) {
        for (int j = i + x - 1; j >= i && p < N; --j, ++p) {
            ans[p] = j;
        }
    }

    ans.map!(to!string).join(" ").writeln;
}

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, std.bitmanip;

immutable long MOD = 10^^9 + 7;

void main() {
    auto N = readln.chomp.to!int;
    auto ans = new int[](N);

    int tmp = N;
    int x = 1;
    for (int i = 2; i <= N; ++i) {
        if (i + N/i + (N%i>0) < tmp) {
            tmp = i + N/i + (N%i>0);
            x = i;
        }
    }

    for (int i = 1, p = 0; p < N; i += x) {
        for (int j = i + x - 1; j >= i && p < N; --j, ++p) {
            ans[p] = j;
        }
    }

    ans.map!(to!string).join(" ").writeln;
}


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) { y.modpow(mod - 2); x.modm(y); }

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

		ans[ti] = n / 2;
	}

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

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		writeln (readln.strip.to !(int) / 2);
	}
}

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 N = readln.chomp.to!int;
        writeln(N/2);
    }
}


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, std.datetime;

void main() {
    auto N = readln.chomp.to!int;
    auto A = readln.split.map!(to!long).array;
    auto M = A.reduce!min;

    int last = - (1 << 29);
    int ans = 1 << 29;
    foreach (i; 0..N) {
        if (A[i] == M) {
            ans = min(ans, i - last);
            last = i;
        }
    }

    ans.writeln;
}

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

void main() {
    int n;
    scan(n);
    auto a = readln.split.to!(int[]);
    auto amin = a.reduce!min;
    auto b = iota(n).filter!(i => a[i] == amin).array;

    int ans = 2*10^^5;

    foreach (i ; 0 .. b.length - 1) {
        ans = min(ans, b[i+1] - b[i]);
    }

    writeln(ans);
}

void scan(T...)(ref T args) {
    string[] line = readln.split;
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}

void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}

void main(){
  import std.stdio, std.string, std.conv, std.algorithm;
  import std.exception;

  int n; rd(n);
  auto a=readln.split.to!(int[]);

  auto mn=reduce!(min)(a);
  int[] ids;
  foreach(int i; 0..n){
    if(a[i]==mn) ids~=i;
  }
  int dist=n;
  foreach(i; 0..(ids.length-1)){
    dist=min(dist, ids[i+1]-ids[i]);
  }

  writeln(dist);
}

void rd(T...)(ref T x){
  import std.stdio, std.string, std.conv;
  auto l=readln.split;
  assert(l.length==x.length);
  foreach(i, ref e; x){
    e=l[i].to!(typeof(e));
  }
}
void wr(T...)(T x){
  import std.stdio;
  foreach(e; x) write(e, " ");
  writeln();
}


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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto a = readln.splitter.map !(to !(int)).array;
		auto b = readln.splitter.map !(to !(int)).array;

		foreach (i; 0..n)
		{
			if (a[i] > b[i])
			{
				swap (a[i], b[i]);
			}
		}

		auto c = new int [n];
		c[] = b[] - a[];
		auto total = sum (c);

		auto f = new bool [total + 1];
		f[0] = true;
		foreach (i; 0..n)
		{
			foreach_reverse (j; c[i]..total + 1)
			{
				f[j] |= f[j - c[i]];
			}
		}

		auto half = total / 2;
		while (!f[half])
		{
			half -= 1;
		}

		int res = 0;
		res += (sum (a) + half) ^^ 2;
		res += (sum (a) + total - half) ^^ 2;
		foreach (i; 0..n)
		{
			res += (n - 2) * a[i] ^^ 2;
			res += (n - 2) * b[i] ^^ 2;
		}
		writeln (res);
	}
}

// cheese-cracker [2022-02-12]

int[][] dp;
int[][] par;
int[] a, b;

int go(int i, int s){
    if(s < 0){
        return 0;
    }
    if(i < 0){
        return (s == 0);
    }
    if(dp[i][s] != -1){
        return dp[i][s];
    }
    int res = 0;
    if(go(i-1, s - a[i])){
        par[i][s] = 1;
        res = 1;
    }else if(go(i-1, s - b[i])){
        par[i][s] = 2;
        res = 1;
    }
    dp[i][s] = res;

    return res;
}

int[] res;

void btrack(int i, int s){
    if(i < 0){
        return;
    }
    res ~= par[i][s];

    if(res.back == 2){
        btrack(i-1, s - b[i]);
    }else{
        btrack(i-1, s - a[i]);
    }
}

long valu(int[] arr){
    long summ = 0;
    for(int i = 0; i < arr.length; ++i){
        for(int j = i+1; j < arr.length; ++j){
            summ += (arr[i] + arr[j])* (arr[i] + arr[j]);
        }
    }
    return summ;
}

void solve(){
    int n = scan!int;

    dp = new int[][](n, 10005);
    for(int i = 0; i < n; ++i){
        dp[i][] = -1;
    }
    par = new int[][](n, 10005);
    a = scanArray!int;
    b = scanArray!int;
    int summ = a.sum + b.sum;
    for(int s = summ/2; s >= 0; --s){
        if(go(n-1, s)){
            btrack(n-1, s);
            res.reverse;
            break;
        }
    }

    int[] t1, t2;
    for(int i = 0; i < n; ++i){
        if(res[i] == 1){
            t1 ~= a[i];
            t2 ~= b[i];
        }else if(res[i] == 2){
            t1 ~= b[i];
            t2 ~= a[i];
        }
    }
    /* show(t1, t2); */
    writeln(valu(t1) + valu(t2));
}

void main(){
    long tests = scan;        // Toggle!
    while(tests--) solve;
}

/*_________________________*That's All Folks!*__________________________*/
import std.stdio, std.range, std.conv, std.typecons, std.algorithm, std.container, std.math, std.numeric;
string[] tk; alias tup = Tuple!(long, int, int, int , int);
T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;}
T[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; }
void show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, "| ");} stderr.writeln; } }

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)); }
double euDist(T)(T[] a, T[] b) { auto c = a.dup; c[] -= b[]; c[] *= c[]; return sqrt(cast(double)c.sum); }
double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }
double norm(double[] vec) { return sqrt(reduce!((a,b)=>a+b*b)(0.0, vec)); }
double dotProd(double[] a, double[] b) { auto r = a.dup; r[] *= b[]; return r.sum; }

//long mod = 10^^9 + 7;
long mod = 998_244_353;
//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 modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }

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

		auto dp = new int[][](n+1, n*100+1);
		dp[0][0] = 1;
		foreach (i; 0..n)
		{
			foreach (j; 0..i*100+1)
			{
				if (dp[i][j] == 0) continue;
				dp[i+1][j+a[i]] = 1;
				dp[i+1][j+b[i]] = 2;
			}
		}

		auto tot = a.sum + b.sum;
		int p;
		foreach (j; 0..n*100+1)
		{
			if (dp[n][j] == 0) continue;
			if (abs(j*2 - tot) < abs(p*2 - tot))
			{
				p = j;
			}
		}

		long[] aa, bb;
		foreach_reverse (i; 0..n)
		{
			auto num = dp[i+1][p];
			if (num == 1)
			{
				aa ~= a[i];
				bb ~= b[i];
				p -= a[i];
			}
			else
			{
				aa ~= b[i];
				bb ~= a[i];
				p -= b[i];
			}
		}
		foreach (i; 0..n-1)
		{
			foreach (j; i+1..n)
			{
				ans[ti] += (aa[i]+aa[j])^^2 + (bb[i]+bb[j])^^2;
			}
		}
	}

	foreach (e; ans)
		writeln(e);
	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 numCases = readInt;
      foreach (caseId; 0 .. numCases) {
        const N = readInt;
        auto A = new int[N]; foreach (i; 0 .. N) A[i] = readInt;
        auto B = new int[N]; foreach (i; 0 .. N) B[i] = readInt;

        foreach (i; 0 .. N) {
          if (A[i] > B[i]) {
            swap(A[i], B[i]);
          }
        }
        const sumA = A.sum;
        const sumB = B.sum;
        const lim = sumB - sumA;
        auto dp = new bool[lim + 1];
        dp[0] = 1;
        foreach (i; 0 .. N) {
          const d = B[i] - A[i];
          foreach_reverse (x; 0 .. lim - d + 1) {
            if (dp[x]) {
              dp[x + d] = true;
            }
          }
        }

        long ans = long.max;
        foreach (x; 0 .. lim + 1) if (dp[x]) {
          const long sa = sumA + x;
          const long sb = sumB - x;
          chmin(ans, sa^^2 + sb^^2);
        }
        foreach (i; 0 .. N) {
          ans += 1L * (N - 2) * (A[i]^^2 + B[i]^^2);
        }
        writeln(ans);
      }
    }
  } catch (EOFException e) {
  }
}

import std.stdio;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import core.stdc.stdio : scanf;
import std.conv;
import std.array;
import std.string;
import std.range;
import std.algorithm;

T read(T)() { return readln.chomp.to!T; }
T[] readarray(T)() { return readln.chomp.split(" ").map!(to!T).array; }

enum long INF = 1L<<56;

void solve() {
    int N = read!int;
    auto A = readarray!int;
    auto B = readarray!int;

    long sq(int x) {
        return x*x;
    }

    int L = 100*100;
    auto f = new long[][](N + 1, L + 1);
    auto s = new int[N + 1];
    foreach (i; 0 .. N) {
        s[i + 1] = s[i] + A[i] + B[i];
    }
    foreach (ref l; f) l[] = INF;
    f[1][A[0]] = f[1][B[0]] = 0;
    for (int k = 1; k < N; k++) {
        for (int sa = 0; sa <= L; sa++) {
            if (sa + A[k] > L || sa + B[k] > L) continue;
            int sb = s[k] - sa;
            f[k + 1][sa + A[k]] = min(f[k + 1][sa + A[k]], f[k][sa] + sa * A[k] + sb * B[k]);
            f[k + 1][sa + B[k]] = min(f[k + 1][sa + B[k]], f[k][sa] + sa * B[k] + sb * A[k]);
        }
    }
    long base = 0;
    for (int i = 0; i < N; i++) {
        for (int j = i + 1; j < N; j++) {
            base += sq(A[i]) + sq(A[j]) + sq(B[i]) + sq(B[j]);
        }
    }
    long add = INF;
    foreach (x; f[N]) {
        add = min(add, x);
    }
    writeln(base + add * 2);
}

void main() {
    int T = read!int;
    foreach (t; 0 .. T) solve();
}


import std.stdio;
import std.uni;
import std.conv;
import std.container;
import std.functional;
import std.algorithm;
import std.array;
import std.typecons;
import std.range;
import std.numeric;
import std.traits;
import std.variant;
import std.concurrency;

void main(string[] args)
{
  auto n = next!int;
  struct Disc
  {
    int outer;
    int inner;
    int height;
  }
  Disc[] discs = new Disc[](n);
  foreach(ref disc; discs)
    {
      disc.inner = next!int;
      disc.outer = next!int;
      disc.height = next!int;
    }
  discs.multiSort!("a.outer > b.outer",
		   "a.inner > b.inner",
		   "a.height > b.height",
		   SwapStrategy.unstable);
  debug writeln("sorted discs: ", discs);
  auto nextDisc = DList!int();
  auto best = new long[](n);
  foreach(i, disc; discs)
    {
      while(!nextDisc.empty &&
	    discs[nextDisc.front].inner >= disc.outer)
	nextDisc.removeFront;
      if (nextDisc.empty)
	best[i] = disc.height;
      else
	best[i] = best[nextDisc.front] + disc.height;
      nextDisc.insertFront([cast(int)i]);
    }
  debug best.writeln;
  best.fold!max.writeln;
}

static this()
{
  popChar;
}

struct Any(T)
{
  static auto read()
  {
    static if (is(T == char))
      {
	auto res = frontChar;
	popChar;
	return res;
      }
  }
}

struct Unsigned(T)
{
  auto read()
  {
    auto res = T(0);
    while(!frontChar.isDigit) popChar;
    do
      {
	res = res * T(10) + T(frontChar - '0');
	popChar;
      }
    while(frontChar.isDigit);
    return res;
  }
}

struct Matrix(T, alias rows, alias cols)
{
  T[][] _cell;
  alias _cell this;
  // Ring constructor
  this(int n)
  {
    final switch(n)
      {
      case 0:
	_cell = new T[][](rows, cols);
	break;
      case 1:
	debug assert(rows == cols);
	_cell = new T[][](rows, cols);
	foreach(i; 0 .. rows)
	  _cell[i][i] = T(1);
	break;
      }
  }
  // Copy constructor
  this(Matrix!(T, rows, cols) other)
  {
    _cell = new T[][](rows, cols);
    foreach(i; 0 .. rows)
      _cell[i][] = other[i][];
  }
  auto opBinary(string op)(Matrix!(T, rows, cols) other)
       if (op == "+" || op == "-")
	 {
	   auto res = Matrix!(T, rows, cols)(0);
	   foreach(i; 0 .. rows)
	     foreach(j; 0 .. cols)
	       res[i][j] = mixin(q{this[i][j]}, op, q{other[i][j]});
	   return res;
	 }
  auto opBinary(string op, alias otherRows, alias otherCols)(Matrix!(T, otherRows, otherCols) other)
       if (op == "*")
	 {
	   debug assert(cols == otherRows);
	   auto res = Matrix!(T, rows, otherCols)(0);
	   foreach(i; 0 .. rows)
	     foreach(k; 0 .. cols)
	       foreach(j; 0 .. otherCols)
		 res[i][j] += this[i][k] * other[k][j];
	   return res;
	 }
  ref auto opOpAssign(string op)(Matrix!(T, rows, cols) other)
  {
    static if (op == "*")
      {
	debug assert(rows == cols);
	alias n = rows;
	auto res = Matrix!(T, n, n)(0);
	T factor;
	foreach(i; 0 .. n)
	  foreach(k; 0 .. n)
	    foreach(j; 0 .. n)
	      res[i][j] += this[i][k] * other[k][j];
	foreach(i; 0 .. n)
	  this[i][] = res[i][];
      }
    else
      {
	foreach(i; 0 .. rows)
	  foreach(j; 0 .. cols)
	    mixin(q{this[i][j] }, text(op, q{=}), q{ other[i][j];});
      }
    return this;
  }
  mixin powMethod;
}

struct Mod(T, alias mod)
{
  alias This = Mod!(T, mod);
  T _rep;
  this(T t)
  {
    _rep = t % mod;
    if (_rep < 0) _rep += mod;
  }
  static auto plain(T t)
  {
    auto res = Mod!(T, mod)();
    res._rep = t;
    return res;
  }
  static auto fromPositive(T t)
  {
    pragma(inline, true);
    return Mod!(T, mod).plain(t % mod);
  }
  static auto fromNegative(T t)
  {
    pragma(inline, true);
    return Mod!(T, mod).plain(t % mod + mod);
  }
  string toString()
  {
    return to!string(_rep);
  }
  debug invariant
  {
    assert(_rep >= 0 && _rep < mod);
  }
  auto opBinary(string op)(This b)
  {
    static if (op == "+")
      {
	T resrep = _rep + b._rep;
	if (resrep >= mod) resrep -= mod;
	return This.plain(resrep);
      }
    else static if (op == "-")
      {
	T resrep = _rep - b._rep;
	if (resrep < 0) resrep += mod;
	return This.plain(resrep);
      }
    else static if (op == "*")
      {
	return This.fromPositive(_rep * b._rep);
      }
  }
  auto opOpAssign(string op)(This b)
  {
    mixin(q{_rep }, text(op, "="), q{b._rep;});
    static if (op == "+")
      {
	if (_rep >= mod) _rep -= mod;
      }
    else static if (op == "-")
      {
	if (_rep < 0) _rep += mod;
      }
    else static if (op == "*")
      {
	_rep %= mod;
      }
    return this;
  }

  auto opBinary(string op)(long exp)
       if (op == "^^")
	 {
	   return pow(exp);
	 }
  mixin powMethod;
}

mixin template powMethod()
{
  auto pow(this ThisType)(long exp)
    {
      auto res = ThisType(1);
      auto pow = ThisType(this);
      while(exp)
	{
	  if (exp & 1) res *= pow;
	  pow *= pow;
	  exp >>= 1;
	}
      return res;
    }
}
// INPUT
char frontChar;
void popChar()
{
  import core.stdc.stdio;
  frontChar = cast(char) getchar;
  if (feof(stdin)) frontChar = ' ';
}
auto makeArray(T, S...)(S s)
{
  enum brackets = () {
    string res;
    foreach(i; 0 .. s.length)
      res ~= "[]";
    return res;
  } ();
  mixin(q{alias Type = T}, brackets, q{;});
  return new Type(s);
}
template isNumberLike(T)
{
  enum isNumberLike = is(typeof((){T test; test = test + test * test; test *= test;}()));
}
void popWhite()
{
  import std.ascii;
  while (frontChar.isWhite) popChar;
}
void print(T...)(T t)
{
  static foreach(ti; t)
    {
      static if (is(typeof(ti) == string))
	{
	  write(ti, " ");
	}
      else static if (isArray!(typeof(ti)))
	{
	  foreach(e; ti) print(e);
	}
      else static if (isTuple!(typeof(ti)))
	{
	  static foreach(i; ti.length) writesp(ti[i]);
	}
      else
	{
	  write(ti, " ");
	}
    }
}
void println(T...)(T t)
{
  static if (t.length == 0)
    writeln;
  static foreach(ti; t)
    {
      print(ti);
      writeln;
    }
}
auto next(alias T, S...)(S s)
{
  pragma(inline, true);
  static if (s.length > 0)
    {
      auto res = makeArray!(typeof(next!T()))(s);
      S t;
      enum loops = () {
	string res;
	foreach(i; 0 .. s.length)
	  {
	    auto ti = text(q{t[}, i, q{]});
	    res ~= text(q{for(}, ti, q{ = 0;}, ti , q{ < s[}, i, q{];}, ti, q{++)});
	  }
	return res;
      } ();
      enum indexing = () {
	string res = "res";
	foreach(i; 0 .. s.length)
	  res ~= text(q{[t[}, i, q{]]});
	return res;
      } ();
      mixin(loops, indexing, q{ = next!T;});
      return res;
    }
  else
    {
      static if (isNumberLike!T)
	{
	  import std.ascii: isDigit;
	  T res;
	  while(frontChar.isWhite) popChar;
	  T multiplier = T(1);
	  if (frontChar == '-')
	    {
	      multiplier = T(-1);
	      popChar;
	    }
	  else if (frontChar == '+')
	    {
	      popChar;
	    }
	  debug assert(frontChar.isDigit);
	  do
	    {
	      res = res * T(10) + T(frontChar - '0');
	      popChar;
	    }
	  while(frontChar.isDigit);
	  return multiplier * res;
	}
      else static if (is(T == string))
	{
	  import std.ascii: isWhite;
	  string res;
	  while(frontChar.isWhite)
	    popChar;
	  while(!frontChar.isWhite)
	    {
	      res ~= frontChar;
	      popChar;
	    }
	  return res;
	}
      else static if (is(T == char))
	{
	  while(frontChar.isWhite) popChar;
	  auto res = frontChar;
	  popChar;
	  return res;
	}
      else
	{
	  return T.read;
	}
    }
}

import std.stdio;
import std.uni;
import std.conv;
import std.container;
import std.functional;
import std.algorithm;
import std.array;
import std.typecons;
import std.range;
import std.numeric;
import std.traits;
import std.variant;
import std.concurrency;

void main(string[] args)
{
  auto n = next!int;
  struct Disc
  {
    int outer;
    int inner;
    int height;
  }
  Disc[] discs = new Disc[](n);
  foreach(ref disc; discs)
    {
      disc.inner = next!int;
      disc.outer = next!int;
      disc.height = next!int;
    }
  discs.multiSort!("a.outer > b.outer",
		   "a.inner > b.inner",
		   "a.height > b.height");
  debug writeln("sorted discs: ", discs);
  auto nextDisc = DList!int();
  auto best = new long[](n);
  foreach(i, disc; discs)
    {
      while(!nextDisc.empty &&
	    discs[nextDisc.front].inner >= disc.outer)
	nextDisc.removeFront;
      if (nextDisc.empty)
	best[i] = disc.height;
      else
	best[i] = best[nextDisc.front] + disc.height;
      nextDisc.insertFront([cast(int)i]);
    }
  debug best.writeln;
  best.fold!max.writeln;
}

static this()
{
  popChar;
}

struct Any(T)
{
  static auto read()
  {
    static if (is(T == char))
      {
	auto res = frontChar;
	popChar;
	return res;
      }
  }
}

struct Unsigned(T)
{
  auto read()
  {
    auto res = T(0);
    while(!frontChar.isDigit) popChar;
    do
      {
	res = res * T(10) + T(frontChar - '0');
	popChar;
      }
    while(frontChar.isDigit);
    return res;
  }
}

struct Matrix(T, alias rows, alias cols)
{
  T[][] _cell;
  alias _cell this;
  // Ring constructor
  this(int n)
  {
    final switch(n)
      {
      case 0:
	_cell = new T[][](rows, cols);
	break;
      case 1:
	debug assert(rows == cols);
	_cell = new T[][](rows, cols);
	foreach(i; 0 .. rows)
	  _cell[i][i] = T(1);
	break;
      }
  }
  // Copy constructor
  this(Matrix!(T, rows, cols) other)
  {
    _cell = new T[][](rows, cols);
    foreach(i; 0 .. rows)
      _cell[i][] = other[i][];
  }
  auto opBinary(string op)(Matrix!(T, rows, cols) other)
       if (op == "+" || op == "-")
	 {
	   auto res = Matrix!(T, rows, cols)(0);
	   foreach(i; 0 .. rows)
	     foreach(j; 0 .. cols)
	       res[i][j] = mixin(q{this[i][j]}, op, q{other[i][j]});
	   return res;
	 }
  auto opBinary(string op, alias otherRows, alias otherCols)(Matrix!(T, otherRows, otherCols) other)
       if (op == "*")
	 {
	   debug assert(cols == otherRows);
	   auto res = Matrix!(T, rows, otherCols)(0);
	   foreach(i; 0 .. rows)
	     foreach(k; 0 .. cols)
	       foreach(j; 0 .. otherCols)
		 res[i][j] += this[i][k] * other[k][j];
	   return res;
	 }
  ref auto opOpAssign(string op)(Matrix!(T, rows, cols) other)
  {
    static if (op == "*")
      {
	debug assert(rows == cols);
	alias n = rows;
	auto res = Matrix!(T, n, n)(0);
	T factor;
	foreach(i; 0 .. n)
	  foreach(k; 0 .. n)
	    foreach(j; 0 .. n)
	      res[i][j] += this[i][k] * other[k][j];
	foreach(i; 0 .. n)
	  this[i][] = res[i][];
      }
    else
      {
	foreach(i; 0 .. rows)
	  foreach(j; 0 .. cols)
	    mixin(q{this[i][j] }, text(op, q{=}), q{ other[i][j];});
      }
    return this;
  }
  mixin powMethod;
}

struct Mod(T, alias mod)
{
  alias This = Mod!(T, mod);
  T _rep;
  this(T t)
  {
    _rep = t % mod;
    if (_rep < 0) _rep += mod;
  }
  static auto plain(T t)
  {
    auto res = Mod!(T, mod)();
    res._rep = t;
    return res;
  }
  static auto fromPositive(T t)
  {
    pragma(inline, true);
    return Mod!(T, mod).plain(t % mod);
  }
  static auto fromNegative(T t)
  {
    pragma(inline, true);
    return Mod!(T, mod).plain(t % mod + mod);
  }
  string toString()
  {
    return to!string(_rep);
  }
  debug invariant
  {
    assert(_rep >= 0 && _rep < mod);
  }
  auto opBinary(string op)(This b)
  {
    static if (op == "+")
      {
	T resrep = _rep + b._rep;
	if (resrep >= mod) resrep -= mod;
	return This.plain(resrep);
      }
    else static if (op == "-")
      {
	T resrep = _rep - b._rep;
	if (resrep < 0) resrep += mod;
	return This.plain(resrep);
      }
    else static if (op == "*")
      {
	return This.fromPositive(_rep * b._rep);
      }
  }
  auto opOpAssign(string op)(This b)
  {
    mixin(q{_rep }, text(op, "="), q{b._rep;});
    static if (op == "+")
      {
	if (_rep >= mod) _rep -= mod;
      }
    else static if (op == "-")
      {
	if (_rep < 0) _rep += mod;
      }
    else static if (op == "*")
      {
	_rep %= mod;
      }
    return this;
  }

  auto opBinary(string op)(long exp)
       if (op == "^^")
	 {
	   return pow(exp);
	 }
  mixin powMethod;
}

mixin template powMethod()
{
  auto pow(this ThisType)(long exp)
    {
      auto res = ThisType(1);
      auto pow = ThisType(this);
      while(exp)
	{
	  if (exp & 1) res *= pow;
	  pow *= pow;
	  exp >>= 1;
	}
      return res;
    }
}
// INPUT
char frontChar;
void popChar()
{
  import core.stdc.stdio;
  frontChar = cast(char) getchar;
  if (feof(stdin)) frontChar = ' ';
}
auto makeArray(T, S...)(S s)
{
  enum brackets = () {
    string res;
    foreach(i; 0 .. s.length)
      res ~= "[]";
    return res;
  } ();
  mixin(q{alias Type = T}, brackets, q{;});
  return new Type(s);
}
template isNumberLike(T)
{
  enum isNumberLike = is(typeof((){T test; test = test + test * test; test *= test;}()));
}
void popWhite()
{
  import std.ascii;
  while (frontChar.isWhite) popChar;
}
void print(T...)(T t)
{
  static foreach(ti; t)
    {
      static if (is(typeof(ti) == string))
	{
	  write(ti, " ");
	}
      else static if (isArray!(typeof(ti)))
	{
	  foreach(e; ti) print(e);
	}
      else static if (isTuple!(typeof(ti)))
	{
	  static foreach(i; ti.length) writesp(ti[i]);
	}
      else
	{
	  write(ti, " ");
	}
    }
}
void println(T...)(T t)
{
  static if (t.length == 0)
    writeln;
  static foreach(ti; t)
    {
      print(ti);
      writeln;
    }
}
auto next(alias T, S...)(S s)
{
  pragma(inline, true);
  static if (s.length > 0)
    {
      auto res = makeArray!(typeof(next!T()))(s);
      S t;
      enum loops = () {
	string res;
	foreach(i; 0 .. s.length)
	  {
	    auto ti = text(q{t[}, i, q{]});
	    res ~= text(q{for(}, ti, q{ = 0;}, ti , q{ < s[}, i, q{];}, ti, q{++)});
	  }
	return res;
      } ();
      enum indexing = () {
	string res = "res";
	foreach(i; 0 .. s.length)
	  res ~= text(q{[t[}, i, q{]]});
	return res;
      } ();
      mixin(loops, indexing, q{ = next!T;});
      return res;
    }
  else
    {
      static if (isNumberLike!T)
	{
	  import std.ascii: isDigit;
	  T res;
	  while(frontChar.isWhite) popChar;
	  T multiplier = T(1);
	  if (frontChar == '-')
	    {
	      multiplier = T(-1);
	      popChar;
	    }
	  else if (frontChar == '+')
	    {
	      popChar;
	    }
	  debug assert(frontChar.isDigit);
	  do
	    {
	      res = res * T(10) + T(frontChar - '0');
	      popChar;
	    }
	  while(frontChar.isDigit);
	  return multiplier * res;
	}
      else static if (is(T == string))
	{
	  import std.ascii: isWhite;
	  string res;
	  while(frontChar.isWhite)
	    popChar;
	  while(!frontChar.isWhite)
	    {
	      res ~= frontChar;
	      popChar;
	    }
	  return res;
	}
      else static if (is(T == char))
	{
	  while(frontChar.isWhite) popChar;
	  auto res = frontChar;
	  popChar;
	  return res;
	}
      else
	{
	  return T.read;
	}
    }
}

import std.stdio;
import std.uni;
import std.conv;
import std.container;
import std.functional;
import std.algorithm;
import std.array;
import std.typecons;
import std.range;
import std.numeric;
import std.traits;
import std.variant;
import std.concurrency;

void main(string[] args)
{
  auto n = next!int;
  struct Disc
  {
    int outer;
    int inner;
    int height;
  }
  Disc[] discs = new Disc[](n);
  foreach(ref disc; discs)
    {
      disc.inner = next!int;
      disc.outer = next!int;
      disc.height = next!int;
    }
  discs.sort!((a, b) {
      if (a.outer > b.outer) return true;
      if (a.outer < b.outer) return false;
      if (a.inner > b.inner) return true;
      if (a.inner < b.inner) return false;
      if (a.height > b.height) return true;
      if (b.height > b.height) return false;
      return false;
    });
  debug writeln("sorted discs: ", discs);
  auto nextDisc = DList!int();
  auto best = new long[](n);
  foreach(i, disc; discs)
    {
      while(!nextDisc.empty &&
	    discs[nextDisc.front].inner >= disc.outer)
	nextDisc.removeFront;
      if (nextDisc.empty)
	best[i] = disc.height;
      else
	best[i] = best[nextDisc.front] + disc.height;
      nextDisc.insertFront([cast(int)i]);
    }
  debug best.writeln;
  best.fold!max.writeln;
}

static this()
{
  popChar;
}

struct Any(T)
{
  static auto read()
  {
    static if (is(T == char))
      {
	auto res = frontChar;
	popChar;
	return res;
      }
  }
}

struct Unsigned(T)
{
  auto read()
  {
    auto res = T(0);
    while(!frontChar.isDigit) popChar;
    do
      {
	res = res * T(10) + T(frontChar - '0');
	popChar;
      }
    while(frontChar.isDigit);
    return res;
  }
}

struct Matrix(T, alias rows, alias cols)
{
  T[][] _cell;
  alias _cell this;
  // Ring constructor
  this(int n)
  {
    final switch(n)
      {
      case 0:
	_cell = new T[][](rows, cols);
	break;
      case 1:
	debug assert(rows == cols);
	_cell = new T[][](rows, cols);
	foreach(i; 0 .. rows)
	  _cell[i][i] = T(1);
	break;
      }
  }
  // Copy constructor
  this(Matrix!(T, rows, cols) other)
  {
    _cell = new T[][](rows, cols);
    foreach(i; 0 .. rows)
      _cell[i][] = other[i][];
  }
  auto opBinary(string op)(Matrix!(T, rows, cols) other)
       if (op == "+" || op == "-")
	 {
	   auto res = Matrix!(T, rows, cols)(0);
	   foreach(i; 0 .. rows)
	     foreach(j; 0 .. cols)
	       res[i][j] = mixin(q{this[i][j]}, op, q{other[i][j]});
	   return res;
	 }
  auto opBinary(string op, alias otherRows, alias otherCols)(Matrix!(T, otherRows, otherCols) other)
       if (op == "*")
	 {
	   debug assert(cols == otherRows);
	   auto res = Matrix!(T, rows, otherCols)(0);
	   foreach(i; 0 .. rows)
	     foreach(k; 0 .. cols)
	       foreach(j; 0 .. otherCols)
		 res[i][j] += this[i][k] * other[k][j];
	   return res;
	 }
  ref auto opOpAssign(string op)(Matrix!(T, rows, cols) other)
  {
    static if (op == "*")
      {
	debug assert(rows == cols);
	alias n = rows;
	auto res = Matrix!(T, n, n)(0);
	T factor;
	foreach(i; 0 .. n)
	  foreach(k; 0 .. n)
	    foreach(j; 0 .. n)
	      res[i][j] += this[i][k] * other[k][j];
	foreach(i; 0 .. n)
	  this[i][] = res[i][];
      }
    else
      {
	foreach(i; 0 .. rows)
	  foreach(j; 0 .. cols)
	    mixin(q{this[i][j] }, text(op, q{=}), q{ other[i][j];});
      }
    return this;
  }
  mixin powMethod;
}

struct Mod(T, alias mod)
{
  alias This = Mod!(T, mod);
  T _rep;
  this(T t)
  {
    _rep = t % mod;
    if (_rep < 0) _rep += mod;
  }
  static auto plain(T t)
  {
    auto res = Mod!(T, mod)();
    res._rep = t;
    return res;
  }
  static auto fromPositive(T t)
  {
    pragma(inline, true);
    return Mod!(T, mod).plain(t % mod);
  }
  static auto fromNegative(T t)
  {
    pragma(inline, true);
    return Mod!(T, mod).plain(t % mod + mod);
  }
  string toString()
  {
    return to!string(_rep);
  }
  debug invariant
  {
    assert(_rep >= 0 && _rep < mod);
  }
  auto opBinary(string op)(This b)
  {
    static if (op == "+")
      {
	T resrep = _rep + b._rep;
	if (resrep >= mod) resrep -= mod;
	return This.plain(resrep);
      }
    else static if (op == "-")
      {
	T resrep = _rep - b._rep;
	if (resrep < 0) resrep += mod;
	return This.plain(resrep);
      }
    else static if (op == "*")
      {
	return This.fromPositive(_rep * b._rep);
      }
  }
  auto opOpAssign(string op)(This b)
  {
    mixin(q{_rep }, text(op, "="), q{b._rep;});
    static if (op == "+")
      {
	if (_rep >= mod) _rep -= mod;
      }
    else static if (op == "-")
      {
	if (_rep < 0) _rep += mod;
      }
    else static if (op == "*")
      {
	_rep %= mod;
      }
    return this;
  }

  auto opBinary(string op)(long exp)
       if (op == "^^")
	 {
	   return pow(exp);
	 }
  mixin powMethod;
}

mixin template powMethod()
{
  auto pow(this ThisType)(long exp)
    {
      auto res = ThisType(1);
      auto pow = ThisType(this);
      while(exp)
	{
	  if (exp & 1) res *= pow;
	  pow *= pow;
	  exp >>= 1;
	}
      return res;
    }
}
// INPUT
char frontChar;
void popChar()
{
  import core.stdc.stdio;
  frontChar = cast(char) getchar;
  if (feof(stdin)) frontChar = ' ';
}
auto makeArray(T, S...)(S s)
{
  enum brackets = () {
    string res;
    foreach(i; 0 .. s.length)
      res ~= "[]";
    return res;
  } ();
  mixin(q{alias Type = T}, brackets, q{;});
  return new Type(s);
}
template isNumberLike(T)
{
  enum isNumberLike = is(typeof((){T test; test = test + test * test; test *= test;}()));
}
void popWhite()
{
  import std.ascii;
  while (frontChar.isWhite) popChar;
}
void print(T...)(T t)
{
  static foreach(ti; t)
    {
      static if (is(typeof(ti) == string))
	{
	  write(ti, " ");
	}
      else static if (isArray!(typeof(ti)))
	{
	  foreach(e; ti) print(e);
	}
      else static if (isTuple!(typeof(ti)))
	{
	  static foreach(i; ti.length) writesp(ti[i]);
	}
      else
	{
	  write(ti, " ");
	}
    }
}
void println(T...)(T t)
{
  static if (t.length == 0)
    writeln;
  static foreach(ti; t)
    {
      print(ti);
      writeln;
    }
}
auto next(alias T, S...)(S s)
{
  pragma(inline, true);
  static if (s.length > 0)
    {
      auto res = makeArray!(typeof(next!T()))(s);
      S t;
      enum loops = () {
	string res;
	foreach(i; 0 .. s.length)
	  {
	    auto ti = text(q{t[}, i, q{]});
	    res ~= text(q{for(}, ti, q{ = 0;}, ti , q{ < s[}, i, q{];}, ti, q{++)});
	  }
	return res;
      } ();
      enum indexing = () {
	string res = "res";
	foreach(i; 0 .. s.length)
	  res ~= text(q{[t[}, i, q{]]});
	return res;
      } ();
      mixin(loops, indexing, q{ = next!T;});
      return res;
    }
  else
    {
      static if (isNumberLike!T)
	{
	  import std.ascii: isDigit;
	  T res;
	  while(frontChar.isWhite) popChar;
	  T multiplier = T(1);
	  if (frontChar == '-')
	    {
	      multiplier = T(-1);
	      popChar;
	    }
	  else if (frontChar == '+')
	    {
	      popChar;
	    }
	  debug assert(frontChar.isDigit);
	  do
	    {
	      res = res * T(10) + T(frontChar - '0');
	      popChar;
	    }
	  while(frontChar.isDigit);
	  return multiplier * res;
	}
      else static if (is(T == string))
	{
	  import std.ascii: isWhite;
	  string res;
	  while(frontChar.isWhite)
	    popChar;
	  while(!frontChar.isWhite)
	    {
	      res ~= frontChar;
	      popChar;
	    }
	  return res;
	}
      else static if (is(T == char))
	{
	  while(frontChar.isWhite) popChar;
	  auto res = frontChar;
	  popChar;
	  return res;
	}
      else
	{
	  return T.read;
	}
    }
}

import std.stdio;
import std.uni;
import std.conv;
import std.container;
import std.functional;
import std.algorithm;
import std.array;
import std.typecons;
import std.range;
import std.numeric;
import std.traits;
import std.variant;
import std.concurrency;

void main(string[] args)
{
  auto n = next!int;
  struct Disc
  {
    int outer;
    int inner;
    int height;
  }
  Disc[] discs = new Disc[](n);
  foreach(ref disc; discs)
    {
      disc.inner = next!int;
      disc.outer = next!int;
      disc.height = next!int;
    }
  discs.sort!((a, b) {
      if (a.outer > b.outer) return true;
      if (a.outer < b.outer) return false;
      if (a.inner > b.inner) return true;
      if (a.inner < b.inner) return false;
      if (a.height > b.height) return true;
      if (b.height > b.height) return false;
      return false;
    });
  debug writeln("sorted discs: ", discs);
  auto nextDisc = DList!int();
  auto best = new int[](n);
  foreach(i, disc; discs)
    {
      while(!nextDisc.empty &&
	    discs[nextDisc.front].inner >= disc.outer)
	nextDisc.removeFront;
      if (nextDisc.empty)
	best[i] = disc.height;
      else
	best[i] = best[nextDisc.front] + disc.height;
      nextDisc.insertFront([cast(int)i]);
    }
  best.fold!max.writeln;
}

static this()
{
  popChar;
}

struct Any(T)
{
  static auto read()
  {
    static if (is(T == char))
      {
	auto res = frontChar;
	popChar;
	return res;
      }
  }
}

struct Unsigned(T)
{
  auto read()
  {
    auto res = T(0);
    while(!frontChar.isDigit) popChar;
    do
      {
	res = res * T(10) + T(frontChar - '0');
	popChar;
      }
    while(frontChar.isDigit);
    return res;
  }
}

struct Matrix(T, alias rows, alias cols)
{
  T[][] _cell;
  alias _cell this;
  // Ring constructor
  this(int n)
  {
    final switch(n)
      {
      case 0:
	_cell = new T[][](rows, cols);
	break;
      case 1:
	debug assert(rows == cols);
	_cell = new T[][](rows, cols);
	foreach(i; 0 .. rows)
	  _cell[i][i] = T(1);
	break;
      }
  }
  // Copy constructor
  this(Matrix!(T, rows, cols) other)
  {
    _cell = new T[][](rows, cols);
    foreach(i; 0 .. rows)
      _cell[i][] = other[i][];
  }
  auto opBinary(string op)(Matrix!(T, rows, cols) other)
       if (op == "+" || op == "-")
	 {
	   auto res = Matrix!(T, rows, cols)(0);
	   foreach(i; 0 .. rows)
	     foreach(j; 0 .. cols)
	       res[i][j] = mixin(q{this[i][j]}, op, q{other[i][j]});
	   return res;
	 }
  auto opBinary(string op, alias otherRows, alias otherCols)(Matrix!(T, otherRows, otherCols) other)
       if (op == "*")
	 {
	   debug assert(cols == otherRows);
	   auto res = Matrix!(T, rows, otherCols)(0);
	   foreach(i; 0 .. rows)
	     foreach(k; 0 .. cols)
	       foreach(j; 0 .. otherCols)
		 res[i][j] += this[i][k] * other[k][j];
	   return res;
	 }
  ref auto opOpAssign(string op)(Matrix!(T, rows, cols) other)
  {
    static if (op == "*")
      {
	debug assert(rows == cols);
	alias n = rows;
	auto res = Matrix!(T, n, n)(0);
	T factor;
	foreach(i; 0 .. n)
	  foreach(k; 0 .. n)
	    foreach(j; 0 .. n)
	      res[i][j] += this[i][k] * other[k][j];
	foreach(i; 0 .. n)
	  this[i][] = res[i][];
      }
    else
      {
	foreach(i; 0 .. rows)
	  foreach(j; 0 .. cols)
	    mixin(q{this[i][j] }, text(op, q{=}), q{ other[i][j];});
      }
    return this;
  }
  mixin powMethod;
}

struct Mod(T, alias mod)
{
  alias This = Mod!(T, mod);
  T _rep;
  this(T t)
  {
    _rep = t % mod;
    if (_rep < 0) _rep += mod;
  }
  static auto plain(T t)
  {
    auto res = Mod!(T, mod)();
    res._rep = t;
    return res;
  }
  static auto fromPositive(T t)
  {
    pragma(inline, true);
    return Mod!(T, mod).plain(t % mod);
  }
  static auto fromNegative(T t)
  {
    pragma(inline, true);
    return Mod!(T, mod).plain(t % mod + mod);
  }
  string toString()
  {
    return to!string(_rep);
  }
  debug invariant
  {
    assert(_rep >= 0 && _rep < mod);
  }
  auto opBinary(string op)(This b)
  {
    static if (op == "+")
      {
	T resrep = _rep + b._rep;
	if (resrep >= mod) resrep -= mod;
	return This.plain(resrep);
      }
    else static if (op == "-")
      {
	T resrep = _rep - b._rep;
	if (resrep < 0) resrep += mod;
	return This.plain(resrep);
      }
    else static if (op == "*")
      {
	return This.fromPositive(_rep * b._rep);
      }
  }
  auto opOpAssign(string op)(This b)
  {
    mixin(q{_rep }, text(op, "="), q{b._rep;});
    static if (op == "+")
      {
	if (_rep >= mod) _rep -= mod;
      }
    else static if (op == "-")
      {
	if (_rep < 0) _rep += mod;
      }
    else static if (op == "*")
      {
	_rep %= mod;
      }
    return this;
  }

  auto opBinary(string op)(long exp)
       if (op == "^^")
	 {
	   return pow(exp);
	 }
  mixin powMethod;
}

mixin template powMethod()
{
  auto pow(this ThisType)(long exp)
    {
      auto res = ThisType(1);
      auto pow = ThisType(this);
      while(exp)
	{
	  if (exp & 1) res *= pow;
	  pow *= pow;
	  exp >>= 1;
	}
      return res;
    }
}
// INPUT
char frontChar;
void popChar()
{
  import core.stdc.stdio;
  frontChar = cast(char) getchar;
  if (feof(stdin)) frontChar = ' ';
}
auto makeArray(T, S...)(S s)
{
  enum brackets = () {
    string res;
    foreach(i; 0 .. s.length)
      res ~= "[]";
    return res;
  } ();
  mixin(q{alias Type = T}, brackets, q{;});
  return new Type(s);
}
template isNumberLike(T)
{
  enum isNumberLike = is(typeof((){T test; test = test + test * test; test *= test;}()));
}
void popWhite()
{
  import std.ascii;
  while (frontChar.isWhite) popChar;
}
void print(T...)(T t)
{
  static foreach(ti; t)
    {
      static if (is(typeof(ti) == string))
	{
	  write(ti, " ");
	}
      else static if (isArray!(typeof(ti)))
	{
	  foreach(e; ti) print(e);
	}
      else static if (isTuple!(typeof(ti)))
	{
	  static foreach(i; ti.length) writesp(ti[i]);
	}
      else
	{
	  write(ti, " ");
	}
    }
}
void println(T...)(T t)
{
  static if (t.length == 0)
    writeln;
  static foreach(ti; t)
    {
      print(ti);
      writeln;
    }
}
auto next(alias T, S...)(S s)
{
  pragma(inline, true);
  static if (s.length > 0)
    {
      auto res = makeArray!(typeof(next!T()))(s);
      S t;
      enum loops = () {
	string res;
	foreach(i; 0 .. s.length)
	  {
	    auto ti = text(q{t[}, i, q{]});
	    res ~= text(q{for(}, ti, q{ = 0;}, ti , q{ < s[}, i, q{];}, ti, q{++)});
	  }
	return res;
      } ();
      enum indexing = () {
	string res = "res";
	foreach(i; 0 .. s.length)
	  res ~= text(q{[t[}, i, q{]]});
	return res;
      } ();
      mixin(loops, indexing, q{ = next!T;});
      return res;
    }
  else
    {
      static if (isNumberLike!T)
	{
	  import std.ascii: isDigit;
	  T res;
	  while(frontChar.isWhite) popChar;
	  T multiplier = T(1);
	  if (frontChar == '-')
	    {
	      multiplier = T(-1);
	      popChar;
	    }
	  else if (frontChar == '+')
	    {
	      popChar;
	    }
	  debug assert(frontChar.isDigit);
	  do
	    {
	      res = res * T(10) + T(frontChar - '0');
	      popChar;
	    }
	  while(frontChar.isDigit);
	  return multiplier * res;
	}
      else static if (is(T == string))
	{
	  import std.ascii: isWhite;
	  string res;
	  while(frontChar.isWhite)
	    popChar;
	  while(!frontChar.isWhite)
	    {
	      res ~= frontChar;
	      popChar;
	    }
	  return res;
	}
      else static if (is(T == char))
	{
	  while(frontChar.isWhite) popChar;
	  auto res = frontChar;
	  popChar;
	  return res;
	}
      else
	{
	  return T.read;
	}
    }
}


import std.stdio : writef, readf, readln, writeln, writefln;
import std.array : array, split;
import std.algorithm : sort, each, maxElement, minElement;
import std.conv : to;
import std.range : iota, tee;
import std.algorithm.comparison : equal, max, min;
import std.container.rbtree : RBT = RedBlackTree;
import std.string : strip;
import core.memory : GC;
import std.typecons : tuple;

alias Tree = RBT!int;

void main() {
    GC.disable();

    int n;
    readf(" %s", n);

    if (n == 1) {
        writeln("1\n1 1\n");
        return;
    }

    writeln((n + 2) / 2);

    int c = 0;
    foreach (i; 0 .. n / 2 + 1) {
        c++;
        if (c > n)
            break;
        writefln("%s %s", i + 1, i + 1);
        c++;
        if (c > n)
            break;
        writefln("%s %s", i + 1, i + 2);
    }

}

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

string FMT_F = "%.10f";

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; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
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; }

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 main()
{
	auto n = RD!int;
	auto x = n / 2;
	writeln(x + 1);
	foreach (i; 0..n)
	{
		writeln(i / 2 + 1, " ", (i+1) / 2 + 1);
	}

	//writeln(ans);
	stdout.flush();
	debug readln();
}

void main() {
  import std.stdio, std.string, std.conv, std.algorithm;
  import std.math : abs;

  int n;
  rd(n);

  auto m = n / 2 + 1;
  writeln(m);
  for (int i = 1, r = 1, c = 1; i <= n; i++) {
    writeln(r, " ", c);
    if (i & 1) {
      c++;
    } else {
      r++;
    }
  }

}

void rd(T...)(ref T x) {
  import std.stdio : readln;
  import std.string : split;
  import std.conv : to;

  auto l = readln.split;
  assert(l.length == x.length);
  foreach (i, ref e; x)
    e = l[i].to!(typeof(e));
}

import std.stdio : writef, readf, readln, writeln, writefln;
import std.array : array, split;
import std.algorithm;
import std.conv : to;
import std.range : iota, tee;
import std.algorithm.comparison;
import std.container.rbtree : RBT = RedBlackTree;
import std.string : strip;
import core.memory : GC;
import std.typecons : tuple;

alias Tree = RBT!int;

void main() {
  GC.disable();

  int n;
  readf(" %s", n);

  writeln( n / 2 + 1);
  foreach(i; 1..n+1) {
    if (i % 2==1) {
      auto idx = (i+1)/2;
      writeln(idx, " ", idx);
    } else {
      auto idx = i/2;
      writeln(idx, " ", idx+1);
    }
  }
}


import std.stdio;
import std.conv;
import std.range;
import std.bigint;
import std.string;
import std.algorithm;
import std.container.rbtree;

void main() {
	readln;
	uint[uint] aa;

	foreach(a; readln.strip.splitter(' ').map!(a => a.to!uint).array)
		aa[a]++;

	auto keys = aa.keys;
	sort(keys);

	foreach_reverse(k; keys) {
		auto cnt = &aa[k];

		if(*cnt % 2) {
			(*cnt)--;
			aa[k - 1]++;
		}
	}

	//keys = aa.keys;
	//sort(keys);

	BigInt res;

	uint p;

	foreach_reverse(k; keys) {
		int cnt = aa[k] / 2;

		if(!cnt) continue;

		if(p) {
			res += ulong(p) * k;
			cnt--;
		}

		res += (ulong(k) * k) * (cnt / 2);
		p = cnt % 2 ? k : 0;
 	}

	res.write;
}

import std.stdio, std.string;
import std.algorithm, std.math;

void solve(int[] arr)
{
    sort(arr);
    long ans = 0;
    int[] nums, cnt;
    for (int i = 0, next; i < arr.length; i = next)
    {
        next = i + 1;
        while (next < arr.length && arr[next] == arr[i]) ++ next;
        nums ~= arr[i];
        cnt ~= next - i;
    }
    for (int i = cast(int)nums.length - 1, next; i >= 0; i = next)
    {
        auto d = cnt[i] / 4;
        auto r = cnt[i] % 4;
        ans += cast(long)nums[i] * nums[i] * d;
        if (i > 0)
        {
            if (r == 0)
            {
                next = i - 1;
                continue;
            }
            else if (r == 1)
            {
                if (nums[i] - nums[i - 1] == 1)
                {
                    ++ cnt[i - 1];
                }
                next = i - 1;
                continue;
            }
            else if (r == 3)
            {
                if (nums[i] - nums[i - 1] == 1)
                {
                    ans += cast(long)nums[i] * nums[i - 1];
                    -- cnt[i - 1];
                    next = i - 1;
                    continue;
                }
            }
            int j;
            for (j = i - 1; j >= 0; -- j)
            {
                if (cnt[j] >= 2) break;
                if (j > 0 && nums[j] - nums[j - 1] == 1) break;
            }
            if (j == -1) break;
            if (cnt[j] >= 2)
            {
                ans += cast(long)nums[i] * nums[j];
                cnt[j] -= 2;
                next = j;
            }
            else
            {
                ans += cast(long)nums[i] * nums[j - 1];
                -- cnt[j - 1];
                next = j - 1;
            }
        }
        else
        {
            next = -1;
        }
    }
    writeln(ans);
}

void main(string[] args)
{
    int n;
    while (scanf("%d", &n) == 1)
    {
        auto arr = new int[n];
        foreach (i; 0 .. n) scanf("%d", &arr[i]);
        solve(arr);
    }
}

import std.stdio;
import std.conv;
import std.range;
import std.bigint;
import std.string;
import std.algorithm;
import std.container.rbtree;

struct S {
	uint len;
	uint *p;
}

void main() {
	readln;
	auto arr = readln.strip.splitter(' ').map!(a => a.to!uint).array;

	S[] ss;

	foreach(ref v; arr) {
		if(v > 1) ss ~= S(v - 1, &v);
		ss ~= S(v, &v);
	}

	sort!((a, b) => a.len < b.len)(ss);

	auto r = ss.assumeSorted!((a, b) => a.len < b.len);

	auto equalS(ref in S s, in S *sa[]...) {
		loop: foreach(ref e; r.equalRange(s))
			if(*e.p && e.p != s.p) {
				foreach(a; sa) if(e.p == a.p) continue loop;
				return e;
			}
		return S.init;
	}

	ulong square(ref S a1) {
		S a2 = equalS(a1);

		if(!a2.len) return 0;

		S b1 = equalS(a1, &a2), b2;

		if(b1.len) b2 = equalS(a1, &a2, &b1);

		if(!b2.len)
			foreach_reverse(ref e; r.lowerBound(a1)) {
				if(!*e.p)
					continue;

				b2 = equalS(e, &a1, &a2);

				if(b2.len) {
					b1 = e;
					break;
				}
			}

		//writefln(`%s %s %s %s`, a1, a2, b1, b2);

		if(b2.len) {
			*a1.p = 0;
			*a2.p = 0;
			*b1.p = 0;
			*b2.p = 0;
			return ulong(a1.len) * b1.len;
		}

		return 0;
	}

	BigInt res;

	foreach_reverse(ref s; ss) {
		res += square(s);
	}

	res.write;
}

import std.stdio;
import std.conv;
import std.range;
import std.bigint;
import std.string;
import std.algorithm;
import std.container.rbtree;

struct S {
	uint len;
	uint *p;
}

void main() {
	readln;
	auto arr = readln.strip.splitter(' ').map!(a => a.to!uint).array;

	S[] ss;

	foreach(ref v; arr) {
		if(v > 1) ss ~= S(v - 1, &v);
		ss ~= S(v, &v);
	}

	sort!((a, b) => a.len < b.len)(ss);

	auto r = ss.assumeSorted!((a, b) => a.len < b.len);

	auto equalS(ref in S s, in S *s2 = null) {
		foreach(ref e; r.equalRange(s)) {
			//e.writeln;
			if(*e.p && e.p != s.p && (!s2 || e.p != s2.p))
				return e;
		}
		return S.init;
	}

	ulong square(ref S s) {
		S a = equalS(s);

		if(!a.len) return 0;

		S b, c;

		foreach_reverse(ref e; r.lowerBound(a)) {
			if(e.p != a.p) {
				c = equalS(e, &a);

				if(c.len) {
					b = e;
					break;
				}
			}
		}

		if(b.len) {
			*a.p = 0;
			*b.p = 0;
			*s.p = 0;
			*c.p = 0;
			return ulong(a.len) * b.len;
		}

		return 0;
	}

	BigInt res;// = square(ss[$ - 1]);

	foreach_reverse(ref s; ss) {
		res += square(s);
	}

	res.write;

	static if(0) {
	auto t = arr.redBlackTree!(/*(a, b) => a.len < b.len,*/ true);

	;

	uint canMake(uint len) {
		uint a;

		foreach(i; 0..2) {
			auto r = t.equalRange(len);
			if(r.empty) return 0;
			if(!i) r.popBack;
			if(!r.empty) {
				a = len;
				break;
			}
			len--;
		}

		uint b;

		{
			auto r = t.lowerRange(a);

			while(!r.empty) {
				b = r.back;
				r.popBack;
			}
		}

		/*auto a = t.equalRange(len).length > 1 ? len : (t.equalRange(len - 1).length ? len - 1 : 0);

		if(!a) return 0;

        foreach_reverse(k; t.lowerRange(a)) {}*/
        return 0;
	}

	foreach_reverse(len; arr) {
		;
	}
	}
}

import std.stdio;
import std.conv;
import std.range;
import std.bigint;
import std.string;
import std.algorithm;
import std.container.rbtree;

struct S {
	uint len;
	uint *p;
}

void main() {
	readln;
	auto arr = readln.strip.splitter(' ').map!(a => a.to!uint).array;

	S[] ss;

	foreach(ref v; arr) {
		if(v > 1) ss ~= S(v - 1, &v);
		ss ~= S(v, &v);
	}

	sort!((a, b) => a.len < b.len)(ss);

	auto r = ss.assumeSorted!((a, b) => a.len < b.len);

	auto equalS(ref in S s, in S *sa[]...) {
		loop: foreach(ref e; r.equalRange(s))
			if(*e.p && e.p != s.p) {
				foreach(a; sa) if(e.p == a.p) continue loop;
				return e;
			}
		return S.init;
	}

	ulong square(ref S a1) {
		S a2 = equalS(a1);

		if(!a2.len) return 0;

		S b1 = equalS(a1, &a2), b2;

		if(b1.len) b2 = equalS(a1, &a2, &b1);

		if(!b2.len)
			foreach_reverse(ref e; r.lowerBound(a1)) {
				if(!(*e.p))
					continue;

				b2 = equalS(e);

				if(b2.len) {
					b1 = e;
					break;
				}
			}

		if(b2.len) {
			*a1.p = 0;
			*a2.p = 0;
			*b1.p = 0;
			*b2.p = 0;
			return ulong(a1.len) * b1.len;
		}

		return 0;
	}

	BigInt res;// = square(ss[$ - 1]);

	foreach_reverse(ref s; ss) {
		res += square(s);
	}

	res.write;

	static if(0) {
	auto t = arr.redBlackTree!(/*(a, b) => a.len < b.len,*/ true);

	;

	uint canMake(uint len) {
		uint a;

		foreach(i; 0..2) {
			auto r = t.equalRange(len);
			if(r.empty) return 0;
			if(!i) r.popBack;
			if(!r.empty) {
				a = len;
				break;
			}
			len--;
		}

		uint b;

		{
			auto r = t.lowerRange(a);

			while(!r.empty) {
				b = r.back;
				r.popBack;
			}
		}

		/*auto a = t.equalRange(len).length > 1 ? len : (t.equalRange(len - 1).length ? len - 1 : 0);

		if(!a) return 0;

        foreach_reverse(k; t.lowerRange(a)) {}*/
        return 0;
	}

	foreach_reverse(len; arr) {
		;
	}
	}
}

import std.stdio, std.string;
import std.algorithm, std.math;

void solve(int[] arr)
{
    sort(arr);
    long ans = 0;
    int[] nums, cnt;
    for (int i = 0, next; i < arr.length; i = next)
    {
        next = i + 1;
        while (next < arr.length && arr[next] == arr[i]) ++ next;
        nums ~= arr[i];
        cnt ~= next - i;
    }
    for (int i = cast(int)nums.length - 1, next; i >= 0; i = next)
    {
        auto d = cnt[i] / 4;
        auto r = cnt[i] % 4;
        ans += cast(long)nums[i] * nums[i] * d;
        if (i > 0)
        {
            if (r == 0)
            {
                next = i - 1;
                continue;
            }
            else if (r == 1)
            {
                if (nums[i] - nums[i - 1] == 1)
                {
                    ++ cnt[i - 1];
                }
                next = i - 1;
                continue;
            }
            else if (r == 3)
            {
                if (nums[i] - nums[i - 1] == 1)
                {
                    ans += cast(long)nums[i] * nums[i - 1];
                    -- cnt[i - 1];
                    next = i - 1;
                    continue;
                }
            }
            int j;
            for (j = i - 1; j >= 0; -- j)
            {
                if (cnt[j] >= 2) break;
                if (j > 0 && nums[j] - nums[j - 1] == 1) break;
            }
            if (j == -1) break;
            if (cnt[j] == 2)
            {
                ans += cast(long)nums[i] * nums[j];
                cnt[j] -= 2;
                next = j;
            }
            else
            {
                ans += cast(long)nums[i] * nums[j - 1];
                -- cnt[j - 1];
                next = j - 1;
            }
        }
        else
        {
            next = -1;
        }
    }
    writeln(ans);
}

void main(string[] args)
{
    int n;
    while (scanf("%d", &n) == 1)
    {
        auto arr = new int[n];
        foreach (i; 0 .. n) scanf("%d", &arr[i]);
        solve(arr);
    }
}

import std.stdio, std.string;
import std.algorithm, std.math;

long calc(int a, int b, int c, int d)
{
    if (b - a > 1 || d - c > 1) return 0;
    return cast(long)a * c;
}

void solve(int[] arr)
{
    sort(arr);
    long ans = 0;
    for (int i = 0; i <= arr.length - 4; ++ i)
    {
        auto ret = calc(arr[i], arr[i + 1], arr[i + 2], arr[i + 3]);
        ans = cast(long)fmax(ans, ret);
    }
    writeln(ans);
}

void main(string[] args)
{
    int n;
    while (scanf("%d", &n) == 1)
    {
        auto arr = new int[n];
        foreach (i; 0 .. n) scanf("%d", &arr[i]);
        solve(arr);
    }
}

import std.stdio, std.string;
import std.algorithm, std.math;

void solve(int[] arr)
{
    sort(arr);
    long ans = 0;
    int[] nums, cnt;
    for (int i = 0, next; i < arr.length; i = next)
    {
        next = i + 1;
        while (next < arr.length && arr[next] == arr[i]) ++ next;
        nums ~= arr[i];
        cnt ~= next - i;
    }
    int pos = -1;
    for (int i = cast(int)nums.length - 1; i >= 0; -- i)
    {
        if (cnt[i] >= 2 || i < nums.length - 1 && nums[i + 1] - nums[i] == 1)
        {
            pos = i;
            if (i < nums.length - 1 && nums[i + 1] - nums[i] == 1) cnt[i] += cnt[i + 1];
            break;
        }
    }
    if (pos != -1)
    {
        if (cnt[pos] >= 4)
        {
            ans = cast(long)nums[pos] * nums[pos];
        }
        else
        {
            for (int i = pos - 1; i >= 0; -- i)
            {
                if (cnt[i] >= 2 || nums[i + 1] - nums[i] == 1 && (i < pos - 1 || cnt[pos] == 3))
                {
                    ans = cast(long)nums[pos] * nums[i];
                    break;
                }
            }
        }
    }
    writeln(ans);
}

void main(string[] args)
{
    int n;
    while (scanf("%d", &n) == 1)
    {
        auto arr = new int[n];
        foreach (i; 0 .. n) scanf("%d", &arr[i]);
        solve(arr);
    }
}


import std.stdio;
import std.string;

void main(string[] argv) {
	int n;
	readf("%s\n", &n);
	string buff;
	foreach(int i; 0..n) {
		buff = readln('\n');
		if(buff.length <= 11)
			write(buff);
		else {
			writeln(buff[0], buff.length-3, buff[$-2]);
		}
	}
	return;
}

import std.stdio;
import std.string;
import std.conv;
//------------------
auto change(string s){
  return s[0] ~ (s.length-2).to!string ~ s[$-1];
}

void main(){
  auto n = readln.chomp.to!int;
  foreach(i; 0..n){
    auto word = readln.chomp;
    if(word.length <= 10){ word.writeln; }
    else{ change(word).writeln; }
  }
}

import std.stdio;
import std.conv;
import std.string;
void main() {
  auto n = to!int(readln().chomp());
  for (auto i = 0; i < n; i++) {
    auto w = strip(readln());
    if (w.length > 10) {
      writeln(to!string(w[0])~to!string(w.length-2)~to!string(w[$-1]));
    } else {
      writeln(w);
    }
  }
}

import std.stdio : writeln, stdin;
import std.conv : to;
import std.range : dropOne;

void main()
{
    foreach(word; stdin.byLine.dropOne)
    {
        writeln(word.length > 10 ? word[0]~to!string(word.length-2)~word[$-1] : word);
    }
}

import std.stdio;

void main() {
    int n;
    readf("%d\n", &n);

    for (int i = 0; i < n; ++i) {
        string s;
        readf("%s\n", &s);

        if (s.length <= 10) {
            writeln(s);
        } else {
            writefln("%c%d%c", s[0], s.length-2, s[s.length-1]);
        }
    }
}

module main;
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,
	std.stdio,std.bitmanip;
import core.stdc.stdio : freopen;
alias RedBlackTree Rbt;
alias redBlackTree rbt;
alias BigInt big;
alias pair!(int,int) pii;
alias pair!(long,long) pll;
alias boyerMooreFinder strfind;
alias make_pair mp;
alias binary_search bins;
alias pair(X,Y)=Tuple!(X,q{fi},Y,q{se});
pair!(X,Y) make_pair(X,Y)(in X x_,in Y y_)
{
	pair!(X,Y) pp;
	pp.fi=x_;
	pp.se=y_;
	return pp;
}
immutable int mod=10^^9+7;
pure nothrow
{
	T lcm (T)(auto ref T a,auto ref T b)
	{
		return a/gcd(a,b)*b;
	}
	@property X sqr(X)(in X a_) {return a_*a_;}
	@property X cub(X)(in X a_) {return a_*a_*a_;}
	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;
	}
	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;
	}
	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;
	}
	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;
	}
}
void putarr(X)(in X a,in char ch=' '){foreach(const ref i;a)write(i,ch);}
void putarr(X)(in X a,in size_t n,in size_t m) if(isRandomAccessRange!X)
{
	foreach(i;0..n)
	{
		foreach(j;0..m)write(a[i][j],' ');
		writeln;
	}
}
bool getarr(X)(X a,in size_t n) if(hasSlicing!X)
{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;}
bool read(T...)(T ptrs) if (ptrs.length > 0)
{return readf(replicate(" %s", ptrs.length)~'\n', ptrs)==ptrs.length;}
nothrow void inf(in char* name) {freopen(name,"r",core.stdc.stdio.stdin);}
nothrow void ouf(in char* name) {freopen(name,"w",core.stdc.stdio.stdout);}
@property auto arread(T)(){return readln.splitter.map!(to!(T)).array;}
//split strip isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin
//cumulativeFold schwartzSort multiSort partialSort heapify join filter countUntil
//---------------------------------------------------------------program beginning-----------------------------------------------------------------

void main()
{
	int n;
	loop:while(read(&n))
	{
		foreach(i;0..n)
		{
			auto s=readln.strip;
			if(s.length<=10)writeln(s);
			else writeln(s[0],s.length-2,s[$-1]);
		}
	}

}

import std.stdio;
import std.conv;

int main(string[] argv)
{
	string nstr = stdin.readln();
	nstr.length -= 1;
	int n = to!int(nstr);
	for (int i = 0; i < n; i++)
	{
		string l = stdin.readln();
		if (l[l.length - 1] == '\n')
			l.length -= 1;
		if (l.length > 10)
		{
			string r = "" ~ l[0];
			r ~= to!string(l.length - 2);
			r ~= l[l.length - 1];
			writeln(r);
		} else {
			writeln(l);
		}
	}
    return 0;
}

// https://codeforces.com/problemset/problem/71/A
// basic string manipulation
import std.stdio;
import std.string;
import std.conv;

void main() {
    auto n = readln.chomp.to!int;
    foreach(i; 0..n) {
        auto s = readln.chomp;
        if(s.length > 10)
            writeln(s[0], s.length-2, s[$-1]);
        else
            writeln(s);
    }
}

// https://codeforces.com/problemset/problem/71/A
import std.stdio;
import std.string;

void main() {
    int n;
    readf!"%d"(n);
    readln;
    while(n--) {
        string s = readln.strip;
        if(s.length > 10)
            writeln(s[0], s.length-2, s[$-1]);
        else
            writeln(s);
    }
}

//ahmat
import core.stdc.stdio:scanf,printf,puts;
import core.bitop;
import std.stdio;
import std.string;
import std.algorithm;
import std.functional;
import std.array;
import std.container;
import std.conv;
import std.container;
import std.typecons;
import std.math;
import std.numeric;
pragma(inline,true);

void main(){
    int n;
    readf!"%d"(n);
    readln;
    while(n--){
        string s=readln.strip;
        if(s.length>10){
            writeln(s[0],s.length-2,s[$-1]);
        }
        else writeln(s);
    }
}

import std.stdio;
void main()
{
    int n;
    readf("%d\n",&n);
    string x;
    for(int i=1;i<=n;i++)
    {
       // writeln("%d\n",i);
        readf("%s\n",&x);
        if(x.length>10){
            writefln("%c%d%c",x[0],x.length-2,x[x.length-1]);
        }
        else writeln(x);
    }
}

import std.stdio;
void main()
{
    int n;
    readf("%d\n",&n);
    string x;
    for(int i=1;i<=n;i++)
    {
       // writeln("%d\n",i);
        readf("%s\n",&x);
        if(x.length>10){
            writefln("%c%d%c\n",x[0],x.length-2,x[x.length-1]);
        }
        else writeln(x);
    }
}

import std.stdio;
import std.conv;
import std.string;
void main() {
  auto n = to!int(readln().chomp());
  writeln("n=",n);
  for (auto i = 0; i < n; i++) {
    auto w = strip(readln());
    if (w.length >= 10) {
      writeln(to!string(w[0])~to!string(w.length-2)~to!string(w[$-1]));
    } else {
      writeln(w);
    }
  }
}

import std.stdio;
import std.conv;
import std.string;
void main() {
  auto n = to!int(readln().chomp());
  for (auto i = 0; i < n; i++) {
    auto w = strip(readln());
    if (w.length >= 10) {
      writeln(to!string(w[0])~to!string(w.length-2)~to!string(w[$-1]));
    } else {
      writeln(w);
    }
  }
}

// https://codeforces.com/problemset/problem/71/A
import std.stdio;
import std.string;

void main() {
    int n;
    readf!"%d"(n);
    readln;
    while(n--) {
        string s = readln.strip;
        if(s.length > 10)
            writeln(s[0], s.length-1, s[$-1]);
        else
            writeln(s);
    }
}

import std.stdio;
void main()
{
    int n;
    readf("%d",&n);
    string x;
    for(int i=1;i<=n;i++)
    {
       // writeln("%d\n",i);
        readf("%s",&x);
        if(x.length>10){
            writefln("%c%d%c\n",x[0],x.length-2,x[x.length-1]);
        }
        else writeln(x),writeln("\n");
    }
}

import std.stdio;
import std.string;

void main(string[] argv) {
    int n;
    readf("%s\n", &n);
    string buff;
    foreach(int i; 0..n) {
        buff = readln('\n');
        if(buff.length < 10)
            write(buff);
        else {
            writeln(buff[0], buff.length-3, buff[$-2]);
        }
    }
    return;
}

import std.stdio;
import std.string;

void main(string[] argv) {
	int n;
	readf("%s\n", &n);
	string buff;
	foreach(int i; 0..n) {
		buff = readln('\n');
		if(buff.length <= 10)
			write(buff);
		else {
			writeln(buff[0], buff.length-3, buff[$-2]);
		}
	}
	return;
}


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 rstring(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T rtype(T)(){ return rstring.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;
T chmin(T)(ref T x, T y){ if(x > y) x = y; return x; } T chmax(T)(ref T x, T y){ if(x < y) x = y; return x; }

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

void solve(){
	int t = rint;
	foreach(_; 0 .. t){
		long n = rint;
		long[] ans;
		long i;
		for(i = 0; i * i <= n; i ++) ans ~= i;
		for(i -= 1; i >= 1; i --) if(ans[$ - 1] != n / i) ans ~= n / i;
		ans.length.writeln;
		ans.map!(to!string).array.join(" ").writeln;
	}
}

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

void main ()
{
	int tests;
	readf !(" %s") (tests);
	foreach (test; 0..tests)
	{
		int n;
		readf !(" %s") (n);
		int [] ans;
		ans ~= 0;
		for (int d = 1; d * d <= n; d++)
		{
			ans ~= d;
			ans ~= n / d;
		}
		sort (ans);
		ans = ans.uniq.array;
		writeln (ans.length);
		writefln ("%(%s %)", ans);
	}
}

import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import 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; }

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 main()
{
	auto t = RD!int;
	auto ans = new long[][](t);
	foreach (i; 0..t)
	{
		auto n = RD;
		long[long] set;
		for (long j = 1; j*j <= n; ++j)
		{
			auto x = n / j;
			auto y = set.get(x, 0);
			set[x] = max(y, j);
			auto z = set.get(j, 0);
			set[j] = max(z, x);
		}
		auto keys = set.keys;
		ans[i].length = keys.length + 1;
		ans[i][0] = 0;
		foreach (j, key; keys)
		{
			ans[i][j+1] = set[key];
		}
		ans[i].sort();
	}

	foreach (i; 0..t)
	{
		writeln(ans[i].length);
		ans[i].map!(to!string).join(" ").writeln;
	}
	stdout.flush;
	debug readln;
}


import std.algorithm, std.container, std.range, std.array, std.ascii, std.bigint, std.conv, std.format, std.math, std.random, std.stdio, std.string, std.typecons;

void main() {
  long t, a, b;
  string s;
  t.readf!" %d";
  while (t--) {
    a.readf!" %d";
    b.readf!" %d";
    s.readf!" %s\n";

    long total, zeros;
    bool can_connect;
    foreach (i, c; s) {
      if (c == '1') {
        if (i == 0) {
          total += a;
        } else if (s[i - 1] == '1') {
          continue;
        } else {
          assert(zeros > 0);
          total += can_connect ? min(a, zeros * b) : a;
        }
        can_connect = true;
        zeros = 0;
      } else {
        zeros++;
      }
    }
    total.writeln;
  }
}

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) { y.modpow(mod - 2); x.modm(y); }

void main()
{
	auto t = RD!int;
	auto ans = new int[](t);
	foreach (ti; 0..t)
	{
		auto a = RD!int;
		auto b = RD!int;
		auto s = RD!string;
		auto n = cast(int)s.length;

		int l = -1, r;
		foreach (i; 0..n)
		{
			if (s[i] == '1')
			{
				l = i;
				break;
			}
		}
		foreach_reverse (i; 0..n)
		{
			if (s[i] == '1')
			{
				r = i+1;
				break;
			}
		}
		if (l == -1) continue;

		ans[ti] = a;
		int cnt;
		foreach (i; l..r)
		{
			debug writeln("ans:", ans[ti]);
			if (s[i] == '0')
				++cnt;
			else
			{
				ans[ti] += min(a, b*cnt);
				cnt = 0;
			}
		}
	}

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