module sigod.codeforces.p296C;

import std.stdio;

void main()
{
	int n, m, k;
	stdin.readf(" %s %s %s", &n, &m, &k);

	long[] a = new long[n + 2];
	foreach (i; 1 .. n + 1) {
		stdin.readf(" %s", &a[i]);
	}

	int[] l = new int[m + 1];
	int[] r = new int[m + 1];
	long[] d = new long[m + 2];

	foreach (i; 1 .. m + 1) {
		stdin.readf(" %s %s %s", &l[i], &r[i], &d[i]);
	}

	// process requests

	int[] multipl = new int[m + 2];

	foreach (i; 0 .. k) {
		int x, y;
		stdin.readf(" %s %s", &x, &y);

		++multipl[x];
		--multipl[y + 1];
	}

	foreach (i; 1 .. m + 1) {
		multipl[i] += multipl[i - 1];

		d[i] *= multipl[i];
	}

	// process operations

	long[] sum = new long[n + 2];

	foreach (i; 1 .. m + 1) {
		sum[l[i]] += d[i];
		sum[r[i] + 1] -= d[i];
	}

	foreach (i; 1 .. n + 1) {
		sum[i] += sum[i - 1];

		a[i] += sum[i];
	}

	// output

	foreach (i; 1 .. n + 1) {
		stdout.write(a[i], " ");
	}
}

//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,m,k;
    readf!"%d %d %d"(n,m,k);
    readln;
    long[] a=readln.splitter.map!(to!long).array;
    //if(n==40) writeln(a);
    Tuple!(int,int,ulong)[] q;
    int l,r;
    ulong v;
    foreach(i;0..m){
        readf!"%d %d %d"(l,r,v);
        readln;
        q~=tuple(l,r,v);
    }
    long[] dk=new long[m];
    foreach(i;0..k){
        readf!"%d %d"(l,r);
        readln;
        dk[l-1]++;
        if(r<m) dk[r]--;
    }
    foreach(i;1..m)
        dk[i]+=dk[i-1];
    long[] d=new long[n];
    foreach(i;0..m){
        v=(cast(ulong)dk[i])*q[i][2];//:q[i][2];
        d[q[i][0]-1]+=v;
        if(q[i][1]<n) d[q[i][1]]-=v;
    }
    a[0]+=d[0];
    foreach(i;1..n){
        d[i]+=d[i-1];
        a[i]+=d[i];
    }
    writefln!"%(%d %)"(a);
}

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

void main ()
{
	int n, m, k;
	while (readf (" %s %s %s", &n, &m, &k) > 0)
	{
		auto a = new long [n];
		foreach (i; 0..n)
		{
			readf (" %s", &a[i]);
		}
		auto l = new int [m];
		auto r = new int [m];
		auto d = new long [m];
		foreach (j; 0..m)
		{
			readf (" %s %s %s", &l[j], &r[j], &d[j]);
			l[j]--;
			r[j]--;
		}
		auto x = new int [k];
		auto y = new int [k];
		foreach (p; 0..k)
		{
			readf (" %s %s", &x[p], &y[p]);
			x[p]--;
			y[p]--;
		}
		auto v = new long [m + 1];
		foreach (p; 0..k)
		{
			v[x[p]]++;
			v[y[p] + 1]--;
		}
		foreach (j; 0..m)
		{
			v[j + 1] += v[j];
		}
		auto t = new long [n + 1];
		foreach (j; 0..m)
		{
			t[l[j]] += v[j] * d[j];
			t[r[j] + 1] -= v[j] * d[j];
		}
		foreach (i; 0..n)
		{
			t[i + 1] += t[i];
		}
		foreach (i; 0..n)
		{
			a[i] += t[i];
		}
		writefln ("%(%s %)", a);
	}
}

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

immutable int mod = 10^^9 + 7;

void main() {
    int n, m, k;
    readVars(n, m, k);

    auto a = readln.split.to!(long[]);

    auto l = new int[](m);
    auto r = new int[](m);
    auto d = new int[](m);

    foreach (i ; 0 .. m) {
        readVars(l[i], r[i], d[i]);
        l[i]--;
    }

    auto ims = new int[](m + 1);
    int xi, yi;

    foreach (i ; 0 .. k) {
        readVars(xi, yi);
        ims[xi - 1]++;
        ims[yi]--;
    }

    iota(m).each!(i => ims[i + 1] += ims[i]);

    auto dif = new long[](n + 1);

    foreach (i ; 0 .. m) {
        dif[l[i]] += d[i].to!long * ims[i];
        dif[r[i]] -= d[i].to!long * ims[i];
    }

    iota(n).each!(i => dif[i + 1] += dif[i]);

    debug {
        stderr.writeln("ims:", ims);
        stderr.writeln("dif:", dif);
    }

    a[] += dif[0 .. $ - 1];

    writefln("%(%s %)", a);
}

int dfs(int n, int[][] child, int u) {
    if (child[u].empty) {
        return 0;
    }

    int res;

    foreach(v ; child[u]) {
        res = max(res, dfs(n, child, v));
    }

    return res + 1;
}

void readVars(T...)(auto ref T args)
{
    auto line = readln.split;
    foreach(ref arg ; args){
        arg = line.front.to!(typeof(arg));
        line.popFront;
    }
    if(!line.empty){
        writeln(line);
        throw new Exception("args num < input num");
    }
}

import core.bitop;
import std.algorithm;
import std.array;
import std.bitmanip;
import std.container;
import std.conv;
import std.datetime;
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, k;
    readf("%s %s %s", &n, &m, &k);
    readln;

    auto arr = readln.chomp.split.map!(to!long).array;

    struct op { int le, r, d; }
    op[] ops;
    foreach (_; 0 .. m) {
        int le, r, d;
        readf("%s %s %s", &le, &r, &d);
        readln;
        --le, --r;

        ops ~= op(le, r, d);
    }

    debug { ops.writeln; }

    auto opscnt = new int[] (m+1);
    foreach (_; 0 .. k) {
        int x, y;
        readf("%s %s", &x, &y);
        readln;
        --x, --y;

        opscnt[x] += 1;
        opscnt[y+1] -= 1;
    }

    auto addcnt = new long[] (n+1);
    foreach (i; 0 .. m) {
        if (i > 0) opscnt[i] += opscnt[i-1];
        auto val = cast(long)ops[i].d * opscnt[i];
        addcnt[ops[i].le] += val;
        addcnt[ops[i].r+1] -= val;
    }

    foreach (i; 0 .. n) {
        if (i > 0) addcnt[i] += addcnt[i-1];
        arr[i] += addcnt[i];
    }

    arr.writefln!("%(%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,m,k;
    readf!"%d %d %d"(n,m,k);
    readln;
    ulong[] a=readln.splitter.map!(to!ulong).array;
    Tuple!(int,int,long)[] q;
    int l,r;
    long v;
    foreach(i;0..m){
        readf!"%d %d %d"(l,r,v);
        readln;
        q~=tuple(l,r,v);
    }
    long[] dk=new long[m];
    while(k--){
        readf!"%d %d"(l,r);
        readln;
        dk[l-1]++;
        if(r<m) dk[r]--;
    }
    foreach(i;1..m)
        dk[i]+=dk[i-1];
    long[] d=new long[n];
    foreach(i;0..m){
        ulong val=dk[i]?dk[i]*q[i][2]:q[i][2];
        d[q[i][0]-1]+=val;
        if(q[i][1]<n) d[q[i][1]]-=val;
    }
    a[0]+=d[0];
    foreach(i;1..n){
        d[i]+=d[i-1];
        a[i]+=d[i];
    }
    writefln!"%(%d %)"(a);
}

//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,m,k;
    readf!"%d %d %d"(n,m,k);
    readln;
    ulong[] a=readln.splitter.map!(to!ulong).array;
    Tuple!(int,int,ulong)[] q;
    int l,r;
    ulong v;
    foreach(i;0..m){
        readf!"%d %d %d"(l,r,v);
        readln;
        q~=tuple(l,r,v);
    }
    long[] dk=new long[m];
    while(k--){
        readf!"%d %d"(l,r);
        readln;
        dk[l-1]++;
        if(r<m) dk[r]--;
    }
    foreach(i;1..m)
        dk[i]+=dk[i-1];
    long[] d=new long[n];
    foreach(i;0..m){
        v=dk[i]?(cast(ulong)dk[i])*q[i][2]:q[i][2];
        d[q[i][0]-1]+=v;
        if(q[i][1]<n) d[q[i][1]]-=v;
    }
    a[0]+=d[0];
    foreach(i;1..n){
        d[i]+=d[i-1];
        a[i]+=d[i];
    }
    writefln!"%(%d %)"(a);
}

//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,m,k;
    readf!"%d %d %d"(n,m,k);
    readln;
    ulong[] a=readln.splitter.map!(to!ulong).array;
    Tuple!(int,int,int)[] q;
    int l,r,v;
    foreach(i;0..m){
        readf!"%d %d %d"(l,r,v);
        readln;
        q~=tuple(l,r,v);
    }
    long[] dk=new long[m];
    while(k--){
        readf!"%d %d"(l,r);
        readln;
        dk[l-1]++;
        if(r<m) dk[r]--;
    }
    foreach(i;1..m)
        dk[i]+=dk[i-1];
    long[] d=new long[n];
    foreach(i;0..m){
        long val=dk[i]?dk[i]*q[i][2]:q[i][2];
        d[q[i][0]-1]+=val;
        if(q[i][1]<n) d[q[i][1]]-=val;
    }
    a[0]+=d[0];
    foreach(i;1..n){
        d[i]+=d[i-1];
        a[i]+=d[i];
    }
    writefln!"%(%d %)"(a);
}

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

immutable int mod = 10^^9 + 7;

void main() {
    int n, m, k;
    readVars(n, m, k);

    auto a = readln.split.to!(long[]);

    auto l = new int[](m);
    auto r = new int[](m);
    auto d = new int[](m);

    foreach (i ; 0 .. m) {
        readVars(l[i], r[i], d[i]);
        l[i]--;
    }

    auto ims = new int[](m + 1);
    int xi, yi;

    foreach (i ; 0 .. k) {
        readVars(xi, yi);
        ims[xi - 1]++;
        ims[yi]--;
    }

    iota(m).each!(i => ims[i + 1] += ims[i]);

    auto dif = new long[](n + 1);

    foreach (i ; 0 .. m) {
        dif[l[i]] += d[i] * ims[i];
        dif[r[i]] -= d[i] * ims[i];
    }

    iota(n).each!(i => dif[i + 1] += dif[i]);

    debug {
        stderr.writeln("ims:", ims);
        stderr.writeln("dif:", dif);
    }

    a[] += dif[0 .. $ - 1];

    writefln("%(%s %)", a);
}

int dfs(int n, int[][] child, int u) {
    if (child[u].empty) {
        return 0;
    }

    int res;

    foreach(v ; child[u]) {
        res = max(res, dfs(n, child, v));
    }

    return res + 1;
}

void readVars(T...)(auto ref T args)
{
    auto line = readln.split;
    foreach(ref arg ; args){
        arg = line.front.to!(typeof(arg));
        line.popFront;
    }
    if(!line.empty){
        writeln(line);
        throw new Exception("args num < input num");
    }
}

import core.bitop;
import std.algorithm;
import std.array;
import std.bitmanip;
import std.container;
import std.conv;
import std.datetime;
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, k;
    readf("%s %s %s", &n, &m, &k);
    readln;

    auto arr = readln.chomp.split.map!(to!long).array;

    struct op { int le, r, d; }
    op[] ops;
    foreach (_; 0 .. m) {
        int le, r, d;
        readf("%s %s %s", &le, &r, &d);
        readln;
        --le, --r;

        ops ~= op(le, r, d);
    }

    debug { ops.writeln; }

    auto opscnt = new int[] (m+1);
    foreach (_; 0 .. k) {
        int x, y;
        readf("%s %s", &x, &y);
        readln;
        --x, --y;

        opscnt[x] += 1;
        opscnt[y+1] -= 1;
    }

    auto addcnt = new long[] (n+1);
    foreach (i; 0 .. m) {
        if (i > 0) opscnt[i] += opscnt[i-1];
        addcnt[ops[i].le] += ops[i].d * opscnt[i];
        addcnt[ops[i].r+1] -= ops[i].d * opscnt[i];
    }

    foreach (i; 0 .. n) {
        if (i > 0) addcnt[i] += addcnt[i-1];
        arr[i] += addcnt[i];
    }

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

module sigod.codeforces.p296C;

import std.stdio;

struct FenwickTree
{
	private {
		int _size;
		long[] _array;
	}

	this(int size)
	{
		this._size = size;
		this._array = new long[size];
	}

	this(long[] array)
	{
		//this._size = array.length;
		//this._array = array.dup;

		this(array.length);

		for (int i = 0; i < this._size; ++i) {
			this.inc(i, array[i]);
		}
	}

	void inc(int i, long delta)
	{
		for (; i < this._size; i = (i | (i + 1))) {
			this._array[i] += delta;
		}
	}

	long sum(int r)
	{
		long result = 0;

		for (; r >= 0; r = (r & (r + 1)) - 1) {
			result += this._array[r];
		}

		return result;
	}

	long sum(int l, int r)
	{
		return sum(r) - sum(l);
	}

	long[] toArray()
	{
		long[] result = new long[this._size];

		foreach (i; 0 .. this._size) {
			result[i] = this.sum(i);
		}

		for (int i = this._size - 1; i > 0; --i) {
			result[i] -= result[i - 1];
		}

		return result;
	}
}

void main()
{
	int array_size = read_value!int();
	int operations_count = read_value!int();
	int requests_count = read_value!int();

	long[] array = read_array!long(array_size);

	int[] left_operations = new int[operations_count];
	int[] right_operations = new int[operations_count];
	int[] delta = new int[operations_count];

	foreach (i; 0 .. operations_count) {
		left_operations[i] = read_value!int() - 1;
		right_operations[i] = read_value!int() - 1;
		delta[i] = read_value!int();
	}

	int[] x = new int[requests_count];
	int[] y = new int[requests_count];

	foreach (i; 0 .. requests_count) {
		x[i] = read_value!int() - 1;
		y[i] = read_value!int() - 1;
	}

	FenwickTree array_tree = FenwickTree(array);
	FenwickTree operations_tree = FenwickTree(operations_count);

	foreach (i; 0 .. requests_count) {
		foreach (j; x[i] .. y[i] + 1) {
			operations_tree.inc(j, 1);
		}
	}

	stdout.writeln("operations_counts: ", operations_tree.toArray());

	stdout.writeln("array: ", array_tree.toArray());

	foreach (index, count; operations_tree.toArray()) {
		if (count == 0) continue;

		foreach (i; left_operations[index] .. right_operations[index] + 1) {
			array_tree.inc(i, delta[index] * count);
		}

		stdout.writeln("left: ", left_operations[index], "; right: ", right_operations[index]);
		stdout.writeln("delta: ", delta[index], "; count: ", count);
		stdout.writeln("array: ", array_tree.toArray());
	}

	foreach (element; array_tree.toArray()) {
		stdout.write(element, " ");
	}
}

private
T read_value(T)()
{
	T value;
	stdin.readf(" %s", &value);

	return value;
}

private
T[] read_array(T)(int size)
{
	T[] array = new T[size];

	foreach (ref element; array) {
		stdin.readf(" %s", &element);
	}

	return array;
}

module sigod.codeforces.p296C;

import std.stdio;

void main()
{
	int n, m, k;
	stdin.readf(" %s %s %s", &n, &m, &k);

	long[] a = new long[n + 2];
	foreach (i; 1 .. n + 1) {
		stdin.readf(" %s", &a[i]);
	}

	int[] l = new int[m + 1];
	int[] r = new int[m + 1];
	int[] d = new int[m + 2];

	foreach (i; 1 .. m + 1) {
		stdin.readf(" %s %s %s", &l[i], &r[i], &d[i]);
	}

	// process requests

	int[] multipl = new int[m + 2];

	foreach (i; 0 .. k) {
		int x, y;
		stdin.readf(" %s %s", &x, &y);

		++multipl[x];
		--multipl[y + 1];
	}

	foreach (i; 1 .. m + 2) {
		multipl[i] += multipl[i - 1];

		d[i] *= multipl[i];
	}

	// process operations

	long[] sum = new long[n + 2];

	foreach (i; 1 .. m + 1) {
		sum[l[i]] += d[i];
		sum[r[i] + 1] -= d[i];
	}

	foreach (i; 1 .. n + 1) {
		sum[i] += sum[i - 1];

		a[i] += sum[i];
	}

	// output

	foreach (i; 1 .. n + 1) {
		stdout.write(a[i], " ");
	}
}

private
T max(T)(T a, T b)
{
	if (a > b) return a;
	else return b;
}


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

  int n, m; rd(n, m);
  long sum=0;
  while(m--){
    long x, d; rd(x, d);
    sum+=x*n;
    if(d>0){// 0+d+d*2+...+d*(n-1)
      sum+=d*n*(n-1)/2;
    }else{
      auto k=n/2;
      sum+=d*k*(k+1)/2*2;
      if(n%2==0) sum-=d*k;
    }
  }

  writefln("%.18f", 1.0*sum/n);
}

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

/*

  0 0
  -1 2
  -1 2
  -2 -3

  0 0 0
  0 2 4
  5 7 9

*/

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;

    auto totx = 0, posd = 0, negd = 0;
    while (m--) {
        int x, d;
        readf("%s %s", &x, &d);
        readln;

        totx += x;
        if (d > 0) posd += d;
        else negd += d;
    }

    debug { writeln(totx, ' ', negd, ' ', posd); }

    auto bigd = (cast(long)n).iota.sum;
    auto smalld = n % 2 == 1 ? 2L * (cast(long)n/2 + 1).iota.sum
        : (cast(long)n/2 + 1).iota.sum + (cast(long)n/2).iota.sum;

    debug { writeln(smalld, ' ', bigd); }

    auto ans = totx + cast(double)(posd * bigd + negd * smalld) / n;

    ans.writefln!("%.18f");
}

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;

    auto totx = 0, posd = 0, negd = 0;
    while (m--) {
        int x, d;
        readf("%s %s", &x, &d);
        readln;

        totx += x;
        if (d > 0) posd += d;
        else negd += d;
    }

    debug { writeln(totx, ' ', negd, ' ', posd); }

    auto bigd = (cast(long)n).iota.sum;
    auto smalld = n % 2 == 1 ? 2L * (cast(long)n/2 + 1).iota.sum : (cast(long)n/2 + 1).iota.sum + (cast(long)n/2).iota.sum;

    debug { writeln(smalld, ' ', bigd); }

    auto ans = totx + cast(double)(posd * bigd + negd * smalld) / n;

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

enum A = 10;
enum DIGITS = ["1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011"];

void main() {
  try {
    for (; ; ) {
      const N = readInt();
      const K = readInt();
      auto S = new string[N];
      foreach (i; 0 .. N) {
        S[i] = readToken();
      }

      auto dks = new int[][](N, A);
      foreach (i; 0 .. N) {
        foreach (a; 0 .. A) {
          foreach (pos; 0 .. 7) {
            if (S[i][pos] > DIGITS[a][pos]) {
              dks[i][a] += K + 1;
            } else if (S[i][pos] < DIGITS[a][pos]) {
              dks[i][a] += 1;
            }
          }
        }
      }
      auto dp = new bool[][](N + 1, K + 1);
      dp[N][0] = true;
      foreach_reverse (i; 0 .. N) {
        foreach (a; 0 .. A) {
          const dk = dks[i][a];
          foreach (k; dk .. K + 1) {
            if (dp[i + 1][k - dk]) {
              dp[i][k] = true;
            }
          }
        }
      }

      if (dp[0][K]) {
        string ans;
        int k = K;
        foreach (i; 0 .. N) {
          foreach_reverse (a; 0 .. A) {
            if (k >= dks[i][a] && dp[i + 1][k - dks[i][a]]) {
              ans ~= cast(char)('0' + a);
              k -= dks[i][a];
              goto found;
            }
          }
          assert(false);
         found:
        }
        writeln(ans);
      } else {
        writeln(-1);
      }
    }
  } 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;

    int[] arr;
    foreach (_; 0 .. n) {
        string s = readln.chomp;
        arr ~= to!int(s, 2);
    }

    int[] nums;
    nums ~= to!int("1110111", 2);
    nums ~= to!int("0010010", 2);
    nums ~= to!int("1011101", 2);
    nums ~= to!int("1011011", 2);
    nums ~= to!int("0111010", 2);
    nums ~= to!int("1101011", 2);
    nums ~= to!int("1101111", 2);
    nums ~= to!int("1010010", 2);
    nums ~= to!int("1111111", 2);
    nums ~= to!int("1111011", 2);

    auto best = new Tuple!(int, int)[][] (1 << 7);
    foreach (x; 0 .. (1 << 7)) {
        outer: foreach (val, target; nums.enumerate(0)) {
            int need = 0;
            foreach (bit; 0 .. 8) {
                int xb = x & (1 << bit);
                int tb = target & (1 << bit);
                if (tb < xb) { continue outer; }
                if (tb > xb) { ++need; }
            }
            best[x] ~= tuple(val, need);
        }
    }

    auto dp = new int[][] (n+1, k+1);
    foreach (i; 0 .. n+1) { dp[i][] = -2; }

    int go(int pos, int left) {
        if (pos == n) { return left == 0 ? 0 : -1; }
        if (dp[pos][left] > -2) { return dp[pos][left]; }

        foreach (i, pr; best[arr[pos]]) {
            auto target = pr[0], need = pr[1];
            if (need > left) { continue; }

            auto rest = go(pos+1, left - need);
            if (rest >= 0) { dp[pos][left] = i.to!int; }
        }

        if (dp[pos][left] == -2) { dp[pos][left] = -1; }

        return dp[pos][left];
    }

    auto score = go(0, k);

    if (score < 0) {
        writeln(-1);
        return;
    }

    int[] ans;
    int left = k;
    foreach (i; 0 .. n) {
       auto pr = best[arr[i]][dp[i][left]];
       ans ~= pr[0];
       left -= pr[1];
    }

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

import core.bitop;
import std.algorithm;
import std.stdio;

immutable int digits = 10;
int [digits] digit;

void prepare ()
{
	digit[0] = 0b1110111;
	digit[1] = 0b0010010;
	digit[2] = 0b1011101;
	digit[3] = 0b1011011;
	digit[4] = 0b0111010;
	digit[5] = 0b1101011;
	digit[6] = 0b1101111;
	digit[7] = 0b1010010;
	digit[8] = 0b1111111;
	digit[9] = 0b1111011;
}

void main ()
{
	prepare ();

	int n, k;
	while (readf !(" %s %s") (n, k) > 0)
	{
		auto a = new int [n];
		foreach (ref c; a)
		{
			readf !(" %b") (c);
		}
		auto f = new byte [] [] (n + 1, k + 10);
		foreach (ref g; f)
		{
			g[] = -1;
		}
		f[n][0] = digits;
		foreach_reverse (i; 0..n)
		{
			foreach (d, v; digit)
			{
				if ((a[i] | v) != v)
				{
					continue;
				}
				auto add = popcnt (a[i] ^ v);
				foreach (j; add..k + 1)
				{
					if (f[i + 1][j - add] >= 0)
					{
						f[i][j] = cast (byte)
						    ((add << 4) | d);
					}
				}
	        	}
		}

		if (f[0][k] >= 0)
		{
			foreach (i; 0..n)
			{
				write (f[i][k] & 15);
				k -= f[i][k] >> 4;
			}
			writeln;
		}
		else
		{
			writeln (-1);
		}
	}
}

//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.format;
import std.exception;
import std.bitmanip;

immutable int[][] digits = [
  [1,2,3,5,6,7],
  [3,6],
  [1,3,4,5,7],
  [1,3,4,6,7],
  [2,3,4,6],
  [1,2,4,6,7],
  [1,2,4,5,6,7],
  [1,3,6],
  [1,2,3,4,5,6,7],
  [1,2,3,4,6,7]
];

int cvt (in int[] a) {
  return a.map!(i => 1 << (i - 1)).sum;
}

immutable int[] mask = digits.map!(cvt).array.idup;

final class Solve {
  immutable int n, maxk;
  immutable int[] c;
  BitArray dp;
  int[] pc;
  bool f (int i, int t) {
    int idx = (i * (maxk + 1) + t) * 2;
    if (dp[idx]) return dp[idx+1];
    dp[idx] = true;
    if (i == n) {
      if (t == 0) {
        dp[idx+1] = true;
        return true;
      }
      return false;
    }
    const int ci = c[i];
    foreach_reverse (j; mask) {
      const int w = ci & j;
      if (w != ci) continue;
      const int bits = pc[j ^ ci];
      if (bits > t) continue;
      if (f (i + 1, t - bits)) {
        dp[idx+1] = true;
        return true;
      }
    }
    return false;
  }
  void restore () {
    int i, t = maxk;
    while (i < n) {
      const int ci = c[i];
      foreach_reverse (pos, j; mask) {
        const int w = ci & j;
        if (w != ci) continue;
        const int bits = pc[j ^ ci];
        if (bits > t) continue;
        if (f (i + 1, t - bits)) {
          write(pos);
          t -= bits;
          break;
        }
      }
      ++i;
    }
  }
  this (int n, int maxk, int[] c) {
    this.n = n;
    this.maxk = maxk;
    this.c = c.idup;
    dp.length = 2 * (n + 1) * (maxk + 1);
    pc = new int[128];
    foreach (i; 1 .. 128) {
      pc[i] = pc[i & (i - 1)] + 1;
    }
  }
}

void main() {
  auto s = readln;
  int n, k;
  formattedRead(s, " %d %d", n, k);
  auto c = new int[n];
  foreach (i; 0 .. n) {
    auto t = readln;
    foreach (j; 0 .. 7) if (t[j] == '1') {
      c[i] += 1 << j;
    }
  }
  auto solve = new Solve (n, k, c);
  if (solve.f (0, k)) {
    solve.restore ();
    writeln;
  } else {
    writeln (-1);
  }

}

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

enum A = 10;
enum DIGITS = ["1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011"];

void main() {
  try {
    for (; ; ) {
      const N = readInt();
      const K = readInt();
      auto S = new string[N];
      foreach (i; 0 .. N) {
        S[i] = readToken();
      }

      auto dks = new int[][](N, A);
      foreach (i; 0 .. N) {
        foreach (a; 0 .. A) {
          foreach (pos; 0 .. 7) {
            if (DIGITS[a][pos] != S[i][pos]) {
              ++dks[i][a];
            }
          }
        }
      }
      auto dp = new bool[][](N + 1, K + 1);
      dp[N][0] = true;
      foreach_reverse (i; 0 .. N) {
        foreach (a; 0 .. A) {
          const dk = dks[i][a];
          foreach (k; dk .. K + 1) {
            if (dp[i + 1][k - dk]) {
              dp[i][k] = true;
            }
          }
        }
      }

      if (dp[0][K]) {
        string ans;
        int k = K;
        foreach (i; 0 .. N) {
          foreach_reverse (a; 0 .. A) {
            if (k >= dks[i][a] && dp[i + 1][k - dks[i][a]]) {
              ans ~= cast(char)('0' + a);
              k -= dks[i][a];
              goto found;
            }
          }
          assert(false);
         found:
        }
        writeln(ans);
      } else {
        writeln(-1);
      }
    }
  } catch (EOFException e) {
  }
}


import std.container;
import std.algorithm;
import std.range;
import std.array;
import std.stdio;
import std.utf;
import std.math;
import std.typecons;

alias type = multi;
struct TestCase
{
  mixin prettyPrinting;

  long n;
  @Dim("n") long[] a;

  void solve(long tc = -1)
  {
    a ~= long.max;
    long ops = 0;
    foreach_reverse(i; 0 .. cast(size_t)n)
      {
        if (a[i] > a[i + 1])
          {
            ops += a[i] - a[i + 1];
          }
      }
    writeln(ops);
  }
}

struct Interval
{
  long l, h;

  bool empty()
  {
    return l > h;
  }
}

Interval intersect(Interval a, Interval b)
{
  return Interval(max(a.l, b.l), min(a.h, b.h));
}

struct If
{
  string condition;
}
enum ignore = If("false");
struct Dim
{
  string dimensions;
  int levels;
  this(string dimensions)
  {
    this.dimensions = dimensions;
    levels = cast(int)dimensions.count(",") + 1;
  }
}

template getArrayLevel(W)
{
  static if (isDynamicArray!W && !is(W == string))
    enum getArrayLevel = 1 + getArrayLevel!(removeArray!(W, 1));
  else
    enum getArrayLevel = 0;
}

static foreach(fieldName; 'a' .. cast(char)('z' + 1))
  {
    static if (fieldName != 'o')
      mixin("enum d", fieldName, " = Dim(\"", fieldName, "\");");
  }

mixin template prettyPrinting()
{
  string toString()
  {
    alias structType = typeof(this);
    auto res = appender!(string)();
    res.put(structType.stringof);
    res.put("(");
    static foreach(fieldName; FieldNameTuple!structType)
      {
        res.put(text(" ", fieldName, ": ", mixin(fieldName)));
      }
    res.put(" )");
    res.put("\n");
    return res[];
  }
}

struct Graph(N)
{
  size_t size;
  this(S)(S size)
  {
    adjacencyList.length = cast(size_t) size;
    this.size = cast(size_t) size;
    this.visited = makeSlice!bool(size);
  }
  N[][] adjacencyList;
  alias ne = neighbours;
  N[] neighbours(N n)
  {
    return adjacencyList.at(n);
  }
  alias bcon = biconnect;
  void biconnect(N a, N b)
  {
    adjacencyList.at(a) ~= b;
    adjacencyList.at(b) ~= a;
  }
  alias con = connect;
  void connect(N a, N b)
  {
    adjacencyList.at(a) ~= b;
  }
  void connect(R)(R edges)
  {
    foreach(ref edge; edges)
      {
        connect(edge[0], edge[1]);
      }
  }
  void biconnect(R)(R edges)
  {
    foreach(ref edge; edges)
      {
        biconnect(edge[0], edge[1]);
      }
  }
  alias deg = degree!long;
  auto degree(T)(N a)
  {
    return cast(T) adjacencyList.at(a).length;
  }

  auto visited = new bool[0];
  void dfs(Visitor)(long startNode)
  {
    auto visitor = Visitor.init;
    void internalDfs(long node)
    {
      visited.set(node, true);
      static if (is(typeof({visitor.n = node;})))
        visitor.n = node;
      static if (is(typeof({visitor.pre;})))
        visitor.pre;
      foreach(w; ne(node))
        if (!visited.at(w))
          {
            static if (is(typeof({visitor.w = w;})))
              visitor.w = w;
            static if (is(typeof(visitor.pren)))
              visitor.pren;
            internalDfs(w);
            static if (is(typeof(visitor.postn)))
              visitor.postn;
          }
      static if (is(typeof(visitor.post)))
        visitor.post;
    }
    internalDfs(startNode);
  }
  void dfs(long startNode)
  {
    struct DefVisitor {}
    dfs!DefVisitor(startNode);
  }
}

DList!string _words;

template removeArray(W, int times)
{
  static if (times == 0)
    alias removeArray = W;
  else static if (times == 1)
    alias removeArray = typeof(function(){W w; return w[0];}());
  else
    {
      static assert(isDynamicArray!W);
      alias removeArray = removeArray!(removeArray!(W, 1), times - 1);
    }
}

template baseType(W)
{
  static if (isDynamicArray!W && !is(W == string))
    alias baseType = baseType!(typeof((delegate() {W w; return w[0];})()));
  else
    alias baseType = W;
}

auto makeSlice(E, W, T...)(W size, T otherSizes)
{
  static if (T.length == 0)
    {
      E[] res;
      res.length = cast(size_t) size;
      return res;
    }
  else
    {
      typeof(makeSlice!E(otherSizes))[] res;
      res.length = cast(size_t) size;
      foreach(ref row; res)
        row = makeSlice!E(otherSizes);
      return res;
    }
}

void read(T...)(ref T t)
{
  void basicTypeRead(W)(ref W w)
  {
    import std.stdio: readln;
    while(_words.empty)
      {
        import std.array: split;
        foreach(word; readln.split)
          {
            _words.insertBack(word);
          }
      }
    import std.conv: to;
    string word = _words.front;
    _words.removeFront;
    w = to!W(word);
  }
  void iterableRead(W)(ref W w)
  {
    foreach(ref element; w)
      read(element);
  }
  void tupleRead(W)(ref W w)
  {
    static foreach(j; 0 .. W.length)
      read(w[j]);
  }
  void structRead(W)(ref W readStruct)
  {
    static foreach(fieldName; FieldNameTuple!W)
      {
        with(readStruct)
          {
            alias fieldSymbol = mixin(W.stringof, ".", fieldName);
            alias typeOfField = typeof(mixin(fieldName));
            static if (hasUDA!(fieldSymbol, If))
              enum condition = getUDAs!(fieldSymbol, If)[0].condition;
            else
              enum condition = "true";

            if (mixin(condition))
              {
                static if (hasUDA!(fieldSymbol, Dim))
                  {
                    static assert(isDynamicArray!(typeOfField)
                                  , text(W.stringof, ".", fieldName, " has Dim UDA but isn't a Dynamic Array."));
                    enum uda = getUDAs!(fieldSymbol, Dim)[0];
                    static assert(getArrayLevel!(typeOfField) == uda.levels,
                                  text(W.stringof, ".", fieldName, " has a Dim UDA with different number of dimensions than it's type."));
                    mixin(fieldName) = mixin(q{makeSlice!(baseType!typeOfField)(}, uda.dimensions, q{)});
                  }
                else static if (isDynamicArray!typeOfField && !is(typeOfField == string))
                  {
                    pragma(msg, "Warning: You probably want to add dimension to input ", W.stringof, ".", fieldName);
                  }
                read(mixin(fieldName));
              }
          }
      }
  }
  static foreach(i; 0 .. T.length)
    {
      static if (isBasicType!(T[i]) || is(T[i] == string))
        basicTypeRead(t[i]);
      else static if (is(typeof({foreach(ref e; t[i]){}})))
        iterableRead(t[i]);
      else static if (isTuple!(T[i]))
        tupleRead(t[i]);
      else static if (isAggregateType!(T[i]))
        structRead(t[i]);
    }
}

auto next(T)()
{
  T t;
  read(t);
  return t;
}

size_t ind(T)(T t)
{
  return cast(size_t)t;
}

ref auto at(R, I)(ref R r, I i)
{
  return r[cast(size_t)i];
}

void set(R, I, V)(ref R r, I i, V v)
{
  r[cast(size_t)i] = v;
}

import std.conv;
import std.traits;
import std.meta;
int _loglevel = -1;
void _log(string functionName, argSeq...)()
{
  foreach(i; 0 .. _loglevel * 4)
    write(' ');
  write(functionName);
  static foreach(arg; argSeq)
    {
      write(" ", arg.stringof, " = ", arg);
    }
  writeln;
}
enum logCall =
  q{
    debug
    {
     {
     _loglevel++;
     alias _parameterTuple = ParameterIdentifierTuple!(mixin(split(__FUNCTION__, ".").back));
     static if (_parameterTuple.length > 0)
       mixin(`_log!(__FUNCTION__, `, [_parameterTuple].joiner(", ").array, `);`);
     else
       _log!(__FUNCTION__);
      }
     foreach(i; 0 .. _loglevel * 4) write(' ');
     writeln('{');
     scope(exit)
       {
         foreach(i; 0 .. _loglevel * 4) write(' ');
         writeln('}');
         _loglevel--;
       }
    }
};

void logSym(S...)(int line = __LINE__, string file = __FUNCTION__)
{
  debug
    {
      foreach(i; 0 .. _loglevel * 4) write(' ');
      write(file, "(", line, "):");
      static foreach(s; S)
        write(" ", s.stringof, " = ", s);
      writeln;

    }
}

Array!E makeArray(E, S)(S size, lazy E value)
{
  auto a = Array!E();
  a.length = cast(size_t) size;
  foreach(ref ai; a)
    ai = value();
  return a;
}
Array!E makeArray(E, S)(S size)
{
  auto a = Array!E();
  a.length = cast(size_t) size;
  return a;
}

template mem(alias F)
{
  ReturnType!F[Tuple!(Parameters!F)] table;
  ReturnType!F mem(Parameters!F args)
  {
    return table.require(tuple(args), F(args));
  }
}

string input(string names, string type = "long")
{
  return text(type, " ", names, "; read(", names, ");");
}

auto pmod(T, W)(T t, W w)
{
  return (t%w + w) % w;
}

void rep(S, F)(F f, S times)
{
  static if (is(typeof(f(i))))
    foreach(i; 0 .. times)
      f(i);
  else
    foreach(i; 0 .. times)
      f();
}

void multi()
{
  foreach(tc; 0 .. next!long)
    next!TestCase.solve(tc);
}

void single()
{
  auto tc = next!TestCase;
  tc.solve;
}

void main()
{
  type;
}

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 long[N];
        foreach (i; 0 .. N) {
          A[i] = readLong();
        }

        long ans;
        foreach (i; 0 .. N - 1) {
          ans += max(A[i] - A[i + 1], 0);
        }
        writeln(ans);
      }
    }
  } catch (EOFException e) {
  }
}

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 long[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto a = RDA;

		foreach (i; 0..n-1)
		{
			auto x = a[i] - a[i+1];
			if (x > 0)
			{
				ans[ti] += x;
			}
		}
	}

	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)
	{
		auto n = readln.strip.to !(int);
		auto a = readln.splitter.map !(to !(int)).array;
		long res = 0;
		foreach (i; 1..n)
		{
			res += max (0, a[i - 1] - a[i]);
		}
		writeln (res);
	}
}

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;
		auto a = RDA!int;

		foreach (i; 0..n-1)
		{
			auto x = a[i] - a[i+1];
			if (x > 0)
			{
				ans[ti] += x;
			}
		}
	}

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


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

immutable inf = 10^^9 + 7;

void main() {
    int n, m;
    scan(n, m);
    auto c = readln.split.to!(int[]);

    auto uf = UnionFind(n);

    foreach (i ; 0 .. m) {
        int xi, yi;
        scan(xi, yi);
        xi--, yi--;
        uf.unite(xi, yi);
    }

    auto d = new int[](n);
    d[] = inf;

    foreach (i ; 0 .. n) {
        int r = uf.find_root(i);
        d[r] = min(d[r], c[i]);
    }

    long ans;

    foreach (i ; 0 .. n) {
        if (d[i] < inf) ans += d[i];
    }

    writeln(ans);
}

struct UnionFind {
    private {
        int N;
        int[] p;
        int[] rank;
    }

    this (int n) {
        N = n;
        p = iota(N).array;
        rank = new int[](N);
    }

    int find_root(int x) {
        if (p[x] != x) {
            p[x] = find_root(p[x]);
        }

        return p[x];
    }

    bool same(int x, int y) {
        return find_root(x) == find_root(y);
    }

    void unite(int x, int y) {
        int u = find_root(x), v = find_root(y);

        if (u == v) return;

        if (rank[u] < rank[v]) {
            p[u] = v;
        }
        else {
            p[v] = u;

            if (rank[u] == rank[v]) {
                rank[u]++;
            }
        }
    }
}

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

struct Queue(T) {
    private {
        int N, head, tail;
        T[] data;
    }

    this(int n) {
        N = n + 1;
        data = new T[](N);
    }

    bool empty() {
        return head == tail;
    }

    bool full() {
        return (tail + 1) % N == head;
    }

    T front() {
        return data[head];
    }

    void push(T x) {
        assert(!full);
        data[tail++] = x;
        tail %= N;
    }

    void pop() {
        assert(!empty);
        head = (head + 1) % N;
    }

    void clear() {
        head = tail = 0;
    }
}

// https://codeforces.com/problemset/problem/893/C
import std.algorithm;
import std.array;
import std.conv;
import std.stdio;
import std.string;
import std.range.primitives;

int[][] graph;
int[] visited;
long[] c;

long dfs(int u) {
    visited[u] = 1;
    long minima = c[u];
    for(int i = 0; i < graph[u].length; i++) {
        int v = graph[u][i];
        if(visited[v] == 0) {
            minima = min(minima,dfs(v));
        }
    }
    return minima;
}

void main() {
    int n, m;
    readf("%s %s\n", &n, &m);
    c = readln.split.map!(to!long).array;
    // TODO how to initialize this?
    visited = new int[n];
    foreach(_; 0..n) {
        int[] vertex;
        graph ~= vertex;
    }
    foreach(_; 0..m) {
        int x,y;
        readf("%s %s\n", &x, &y);
        x -= 1;
        y -= 1;
        graph[x] ~= y;
        graph[y] ~= x;
    }
    long[] cost;
    foreach(vertex; 0..n) {
        if(visited[vertex] == 0) {
            cost ~= dfs(vertex);
        }
    }
    long total;
    foreach(item; cost)
        total += item;
    total.writeln;
}

#!/usr/bin/env dub
/+ dub.sdl:
    name "C"
+/

import std.array;
import std.algorithm;
import std.container;
import std.conv;
import std.math;
import std.stdio;
import std.string;

void main()
{
    int N, M;
    readf(" %s %s ", N, M);

    long[] C = new long[N];
    foreach (i; 0..N) {
        readf(" %s ", C[i]);
    }

    int[][] adj = new int[][N];
    foreach (i; 0..M) {
        int x, y;
        readf(" %s %s ", x, y);
        x--;
        y--;

        adj[x] ~= y;
        adj[y] ~= x;
    }

    debug(2) { writeln(adj);}

    int[] comps = replicate([-1], N);
    int ncomps = 0;

    foreach (v; 0..N) {
        if (comps[v] == -1) {
            auto bfs = DList!int(v);
            comps[v] = ncomps;

            while (!bfs.empty) {
                auto cur = bfs.front();
                bfs.removeFront();

                foreach (nbr; adj[cur]) {
                    if (comps[nbr] == -1) {
                        bfs.insertBack(nbr);
                        comps[nbr] = ncomps;
                    }
                }
            }

            ncomps++;
        }
    }

    long[] mins = replicate([long.max], ncomps);

    foreach (v; 0..N) {
        mins[comps[v]] = min(mins[comps[v]], C[v]);
    }

    long ans = sum(mins);
    writeln(ans);
}

#!/usr/bin/env dub
/+ dub.sdl:
    name "C"
+/

import std.array;
import std.algorithm;
import std.container;
import std.conv;
import std.math;
import std.stdio;
import std.string;

class Graph {
    int N;
    int[][] adj;

    private int[] comps;
    private int ncomps;

    bool flag_connected_components = false;

    this(int N) {
        this.N = N;
        adj = new int[][N];
    }

    void addEdge(int u, int v) {
        adj[u] ~= v;
        adj[v] ~= u;
    }

    void do_connected_components() {
        comps = replicate([-1], N);

        foreach (v; 0..N) {
            if (comps[v] == -1) {
                auto bfs = DList!int(v);
                comps[v] = ncomps;

                while (!bfs.empty) {
                    auto cur = bfs.front();
                    bfs.removeFront();

                    foreach (nbr; adj[cur]) {
                        if (comps[nbr] == -1) {
                            bfs.insertBack(nbr);
                            comps[nbr] = ncomps;
                        }
                    }
                }

                ncomps++;
            }
        }

        debug {
            flag_connected_components = true;
        }
    }

    int component(int v) {
        debug {
            assert(flag_connected_components);
        }
        return comps[v];
    }

}

void main()
{
    int N, M;
    readf(" %s %s ", N, M);

    long[] C = new long[N];
    foreach (i; 0..N) {
        readf(" %s ", C[i]);
    }

    Graph G = new Graph(N);
    foreach (i; 0..M) {
        int x, y;
        readf(" %s %s ", x, y);
        x--;
        y--;

        G.addEdge(x, y);
    }
    G.do_connected_components();

    long[] mins = replicate([long.max], G.ncomps);

    foreach (v; 0..N) {
        mins[G.component(v)] =
            min(mins[G.component(v)], C[v]);
    }

    long ans = sum(mins);
    writeln(ans);
}

// https://codeforces.com/problemset/problem/893/C
import std.algorithm;
import std.array;
import std.conv;
import std.stdio;
import std.string;
import std.range.primitives;

int[][] graph;
int[] visited;
int[] c;

int dfs(int u) {
    visited[u] = 1;
    int minima = c[u];
    for(int i = 0; i < graph[u].length; i++) {
        int v = graph[u][i];
        minima = min(minima, c[v]);
        if(visited[v] == 0) {
            dfs(v);
        }
    }
    return minima;
}

void main() {
    int n, m;
    readf("%s %s\n", &n, &m);
    c = readln.split.map!(to!int).array;
    // TODO how to initialize this?
    visited = new int[n];
    foreach(_; 0..n) {
        int[] vertex;
        graph ~= vertex;
    }
    foreach(_; 0..m) {
        int x,y;
        readf("%s %s\n", &x, &y);
        x -= 1;
        y -= 1;
        graph[x] ~= y;
        graph[y] ~= x;
    }
    int[] cost;
    foreach(vertex; 0..n) {
        if(visited[vertex] == 0) {
            cost ~= dfs(vertex);
        }
    }
    sum(cost).writeln;
}

// https://codeforces.com/problemset/problem/893/C
import std.algorithm;
import std.array;
import std.conv;
import std.stdio;
import std.string;
import std.range.primitives;

int[][] graph;
int[] visited;
long[] c;

long dfs(int u) {
    visited[u] = 1;
    long minima = c[u];
    for(int i = 0; i < graph[u].length; i++) {
        int v = graph[u][i];
        minima = min(minima, c[v]);
        if(visited[v] == 0) {
            dfs(v);
        }
    }
    return minima;
}

void main() {
    int n, m;
    readf("%s %s\n", &n, &m);
    c = readln.split.map!(to!long).array;
    // TODO how to initialize this?
    visited = new int[n];
    foreach(_; 0..n) {
        int[] vertex;
        graph ~= vertex;
    }
    foreach(_; 0..m) {
        int x,y;
        readf("%s %s\n", &x, &y);
        x -= 1;
        y -= 1;
        graph[x] ~= y;
        graph[y] ~= x;
    }
    long[] cost;
    foreach(vertex; 0..n) {
        if(visited[vertex] == 0) {
            cost ~= dfs(vertex);
        }
    }
    long total;
    foreach(item; cost)
        total += item;
    total.writeln;
}


// Cheese-Cracker: cheese-cracker.github.io

void theCode(){
    ll W = scan; ll H = scan;
    ll x1 = scan; ll y1 = scan;
    ll x2 = scan; ll y2 = scan;
    ll w = scan; ll h = scan;
    // Height
    ll wdis = max(W - x2, x1);
    ll hdis = max(H - y2, y1);
    ll wdiff = max(w - wdis, 0);
    ll hdiff = max(h - hdis, 0);
    /* show(wdis, hdis); */
    /* show(wdiff, hdiff); */
    ll nx, ny;
    bool f = 1;
    if(W - x2 > x1){
        nx = x1 - wdiff;
        if(nx < 0){
            f = 0;
        }
    }else{
        nx = x1 + wdiff;
        if(x2 + wdiff > W){
            f = 0;
        }
    }
    bool f2 = 1;
    if(H - y2 > y1){
        ny = y1 - hdiff;
        if(ny < 0){
            if(!f){
                writeln(-1);
                return;
            }else{
                f2 = 0;
            }
        }
    }else{
        ny = y1 + hdiff;
        if(y2 + hdiff > H){
            if(!f){
                writeln(-1);
                return;
            }else{
                f2 = 0;
            }
        }
    }
    show(nx, ny);
    ll dist = -1;
    if(f && f2){
        dist = min(abs(nx - x1), abs(ny - y1));
    }else if(f2){
        dist = abs(ny - y1);
    }else if(f){
        dist = abs(nx - x1);
    }
    if(dist == -1){
        writeln(dist);
        return;
    }
    double dis = dist.to!double;
    writefln("%.10f", dis);
}

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

/*********  That's All Folks!  *********/
import std.stdio, std.random, std.functional, std.container, std.algorithm;
import std.numeric, std.range, std.typecons, std.string, std.math, std.conv;
string[] tk; 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; } }
alias ll = long, tup = Tuple!(long, "x",  long, "y");

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

void main()
{
  int t;
  readf!" %d "(t);
  while (t--) {
    int W, H;
    readf!" %d %d "(W, H);
    int x1, y1, x2, y2;
    readf!" %d %d %d %d "(x1, y1, x2, y2);

    int X1 = max(x1, W - x2);
    int Y1 = max(y1, H - y2);

    int w, h;
    readf!" %d %d "(w, h);

    int movex, movey;
    if (w > X1) {
      movex = w - X1;
      if (w + abs(x2 - x1) > W)
        movex = int.max;
    }
    if (h > Y1) {
      movey = h - Y1;
      if (h + abs(y2 - y1) > H)
        movey = int.max;
    }

    int ans = min(movex, movey);
    if (ans == int.max) {
      writeln(-1);
    } else {
      writefln("%.9f", cast(double)ans);
    }
  }
}

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

void main()
{
	int t = readInt!int; while (t--) solve;
}

void solve()
{
	long w = readInt!long;
	long h = readInt!long;
	long[2] xb, yb;
	long x1 = readInt!long;
	long y1 = readInt!long;
	long x2 = readInt!long;
	long y2 = readInt!long;
	xb = [x1, x2];
	yb = [y1, y2];
	long rw = readInt!long;
	long rh = readInt!long;
	long wb = x2 - x1;
	long hb = y2 - y1;
	long minDist = long.max;
	void tryPoint(long x, long y)
	{
		if (rw <= x || rh <= y) { minDist = 0; return; }
		if (rw > x)
		{
			long hx = rw;
			if (hx + wb <= w)
			{
				minDist = min(minDist, rw - x);
			}
		}
		if (rh > y)
		{
			long hy = rh;
			if (hy + hb <= h)
			{
				minDist = min(minDist, rh - y);
			}
		}
	}
	tryPoint(xb[0], yb[0]);
	tryPoint(xb[0], h - yb[1]);
	tryPoint(w - xb[1], yb[0]);
	tryPoint(w - xb[1], h - yb[1]);
	if (minDist != long.max) minDist.writeln; else (-1).writeln;
}
/* 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);
	}
}

// Cheese-Cracker: cheese-cracker.github.io

void theCode(){
    ll W = scan; ll H = scan;
    ll x1 = scan; ll y1 = scan;
    ll x2 = scan; ll y2 = scan;
    ll w = scan; ll h = scan;
    // Height
    ll wdis = max(W - x2, x1);
    ll hdis = max(H - y2, y1);
    ll wdiff = max(w - wdis, 0);
    ll hdiff = max(h - hdis, 0);
    /* show(wdis, hdis); */
    /* show(wdiff, hdiff); */
    ll nx, ny;
    bool f = 1;
    if(W - x2 > x1){
        nx = x1 - wdiff;
        if(x1 < 0){
            f = 0;
        }
    }else{
        nx = x1 + wdiff;
        if(x2 + wdiff > W){
            f = 0;
        }
    }
    bool f2 = 1;
    if(H - y2 > y1){
        ny = y1 - hdiff;
        if(y1 < 0){
            if(!f){
                writeln(-1);
                return;
            }else{
                f2 = 0;
            }
        }
    }else{
        ny = y1 + hdiff;
        if(y2 + hdiff > H){
            if(!f){
                writeln(-1);
                return;
            }else{
                f2 = 0;
            }
        }
    }
    show(nx, ny);
    ll dist = -1;
    if(f && f2){
        dist = min(abs(nx - x1), abs(ny - y1));
    }else if(f2){
        dist = abs(ny - y1);
    }else if(f){
        dist = abs(nx - x1);
    }
    if(dist == -1){
        writeln(dist);
        return;
    }
    double dis = dist.to!double;
    writefln("%.10f", dis);
}

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

/*********  That's All Folks!  *********/
import std.stdio, std.random, std.functional, std.container, std.algorithm;
import std.numeric, std.range, std.typecons, std.string, std.math, std.conv;
string[] tk; 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; } }
alias ll = long, tup = Tuple!(long, "x",  long, "y");

// Cheese-Cracker: cheese-cracker.github.io

void theCode(){
    ll W = scan; ll H = scan;
    ll x1 = scan; ll y1 = scan;
    ll x2 = scan; ll y2 = scan;
    ll w = scan; ll h = scan;
    // Height
    ll wdis = max(W - x2, x1);
    ll hdis = max(H - y2, y1);
    ll wdiff = max(w - wdis, 0);
    ll hdiff = max(h - hdis, 0);
    /* show(wdis, hdis); */
    /* show(wdiff, hdiff); */
    ll nx, ny;
    bool f = 1;
    if(W - x2 > x1){
        nx = x1 - wdiff;
        if(x1 < 0){
            f = 0;
        }
    }else{
        nx = x1 + wdiff;
        if(x2 + wdiff > W){
            f = 0;
        }
    }
    bool f2 = 1;
    if(H - y2 > y1){
        ny = y1 - hdiff;
        if(y1 < 0){
            if(!f){
                writeln(-1);
                return;
            }else{
                f2 = 0;
            }
        }
    }else{
        ny = y1 + hdiff;
        if(y2 + hdiff > H){
            if(!f){
                writeln(-1);
                return;
            }else{
                f2 = 0;
            }
        }
    }
    show(nx, ny);
    ll dist = -1;
    if(f && f2){
        dist = min(abs(nx - x1), abs(ny - y1));
    }else if(f2){
        dist = abs(ny - y1);
    }else if(f){
        dist = abs(nx - x1);
    }
    double dis = dist.to!double;
    writefln("%.10f", dis);
}

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

/*********  That's All Folks!  *********/
import std.stdio, std.random, std.functional, std.container, std.algorithm;
import std.numeric, std.range, std.typecons, std.string, std.math, std.conv;
string[] tk; 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; } }
alias ll = long, tup = Tuple!(long, "x",  long, "y");

// Cheese-Cracker: cheese-cracker.github.io

void theCode(){
    ll W = scan; ll H = scan;
    ll x1 = scan; ll y1 = scan;
    ll x2 = scan; ll y2 = scan;
    ll w = scan; ll h = scan;
    // Height
    ll wdis = max(W - x2, x1);
    ll hdis = max(H - y2, y1);
    ll wdiff = max(w - wdis, 0);
    ll hdiff = max(h - hdis, 0);
    show(wdis, hdis);
    show(wdiff, hdiff);
    ll nx, ny;
    bool f = 1;
    if(W - x2 > x1){
        nx = x1 - wdiff;
        if(x1 < 0){
            f = 0;
        }
    }else{
        nx = x1 + wdiff;
        if(x2 + wdiff > W){
            f = 0;
        }
    }
    bool f2 = 1;
    if(H - y2 > y1){
        ny = y1 - hdiff;
        if(y1 < 0){
            if(!f){
                writeln(-1);
                return;
            }else{
                f2 = 0;
            }
        }
    }else{
        ny = y1 + hdiff;
        if(y2 + hdiff > H && !f){
            if(!f){
                writeln(-1);
                return;
            }else{
                f2 = 0;
            }
        }
    }
    ll dist = -1;
    if(f && f2){
        dist = min(abs(nx - x1), abs(ny - y1));
    }else if(f2){
        dist = abs(ny - y1);
    }else if(f){
        dist = abs(nx - x1);
    }
    double dis = dist.to!double;
    writefln("%.8f", dis);
}

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

/*********  That's All Folks!  *********/
import std.stdio, std.random, std.functional, std.container, std.algorithm;
import std.numeric, std.range, std.typecons, std.string, std.math, std.conv;
string[] tk; 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; } }
alias ll = long, tup = Tuple!(long, "x",  long, "y");


import std.stdio;
import std.range;
import std.uni;
import std.conv;
import std.container;
import std.functional;
import std.algorithm;
import std.array;
import std.typecons;

void main(string[] args)
{
  inputFile = stdin;
  debug inputFile = File(args[1]);
  auto nt = next!int;
  foreach(t; 0 .. nt)
    {
      auto n = next!int;
      auto x = next!int;
      auto y = next!int;
      auto diff = y - x;
      int bestjump = -1;
      int bestjumpmax = int.max;
      foreach(jump; 1 .. diff + 1)
        {
          if (diff % jump == 0)
            {
              if (diff / jump + 1 > n)
                continue;
              int rem = n - (diff / jump + 1);
              int lessthan = cast(int) iota(1, x).count!(num => num % jump == x % jump);
              rem -= min(rem, lessthan);
              int jumpmax = y + rem * jump;
              if (jumpmax < bestjumpmax)
                {
                  bestjump = jump;
                  bestjumpmax = jumpmax;
                }
            }
        }
      foreach(i; 0 .. n)
        {
          write(bestjumpmax - bestjump * i, " ");
        }
      writeln;
    }
}

// INPUT

enum InputStyle
  {
    byChunk, byLine
  };
enum inputStyle = InputStyle.byLine;

File inputFile;
string popWord();
static if (inputStyle == InputStyle.byChunk)
  {
    const chunkSize = 1024 * 1024 * 8;
    const wordSize = 4096;
    char[chunkSize] chunkBuff;
    char[] currChunk;
    void renewChunk()
    {
      if (inputFile.eof)
        currChunk = null;
      else
        currChunk = inputFile.rawRead(chunkBuff);
    }
    char[wordSize] wordBuff;
    char[] word;
    string popWord()
    {
      if (currChunk.length == 0)
        renewChunk;
      assert(currChunk.length > 0);
      while (currChunk[0].isWhite)
        {

          currChunk = currChunk[1 .. $];
          if (currChunk.length == 0)
            renewChunk;
        }
      word = wordBuff[0 .. 0];
      while (!currChunk[0].isWhite)
        {
          word.length++;
          word[$ - 1] = currChunk[0];
          currChunk = currChunk[1 .. $];
          if (currChunk.length == 0)
            {
              renewChunk;
              if (currChunk.length == 0)
                return cast(string) word;
            }
        }
      return cast(string) word;
    }
  }
 else
   {
     DList!string _words;
     string popWord()
     {
       while (_words.empty)
         {
           foreach(w; inputFile.readln.split)
             {
               _words.insertBack(w);
             }
         }
       auto word = _words.front;
       _words.removeFront;
       return word;
     }
   }
T next(T)()
{
  return to!T(popWord);
}
auto next(T, S...)(S s)
{
  static if (S.length == 0)
    {
      return to!T(popWord);
    }
  else
    {
      auto res = new typeof(next!T(s[1 .. $]))[](cast(size_t)s[0]);
      foreach(ref elem; res)
        elem = next!T(s[1 .. $]);
      return res;
    }
}

// Cheese-Cracker: cheese-cracker.github.io
import std.stdio;
import std.algorithm;
import std.string;
import std.conv;
import std.range;

alias ll = long;

void play(){
    ll n, x, y;
    readf(" %s %s %s ", &n, &x, &y);
    ll diff = y - x;
    ll minmax = long.max / 10;
    ll mind = -1, mina = -1;
    foreach(ll d; 1 .. diff+1){
        if(diff % d == 0){
            ll minn = y - (n - 1) * d;
            while(minn <= 0){
                minn += d;
            }
            if(minn > x) continue;
            ll curmax = minn + (n - 1) * d;
            if(curmax < minmax){
                minmax = curmax;
                mind = d;
                mina = minn;
            }
        }
    }
    foreach(ll i; 0 .. n){
        writef("%s ", mina + i * mind);
    }
    writeln;
}

int main(){
    ll t = 1;
    readf(" %s ", &t);        // Toggle!
    while(t--) play();  // Let's play!
    return 0;
}

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 long[][](t);
	foreach (ti; 0..t)
	{
		auto n = RD;
		auto x = RD;
		auto y = RD;

		auto d = y-x;
		foreach_reverse (i; 1..n)
		{
			if (d % i) continue;
			auto cnt = d / i;
			auto begin = x;
			while (begin <= y)
			{
				ans[ti] ~= begin;
				begin += cnt;
			}

			begin = x - cnt;
			while (begin >= 1 && ans[ti].length < n)
			{
				ans[ti] ~= begin;
				begin -= cnt;
			}
			begin = y + cnt;
			while (ans[ti].length < n)
			{
				ans[ti] ~= begin;
				begin += cnt;
			}

			break;
		}
	}

	foreach (e; ans)
		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.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; }
T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } 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 = 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 main()
{
	auto q = RD!int;
	auto ans = new long[](q);
	foreach (i; 0..q)
	{
		auto c = RD;
		auto m = RD;
		auto x = RD;
		auto a = c + m + x;
		ans[i] = min(min(c, m), a/3);
	}

	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 q = rint;
	foreach(_; 0 .. q){
		long c = rlong, m = rlong, x = rlong;
		long total = c + m + x;
		long ans = min(c, m, total / 3);
		ans.writeln;
	}
}


// cheese-cracker [2022-07-08]

void solve(){
    int n = scan!int;
    auto seen = new bool[](n+1);
    int d = 2;
    writeln(d);
    for(int i = 1; i <= n; ++i){
        if(!seen[i]){
            int num = i;
            while(num <= n){
                seen[num] = 1;
                write(num, " ");
                num *= 2;
            }
        }
    }
    writeln;
}

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

/*_________________________*That's All Folks!*__________________________*/
import std.stdio, std.range, std.conv, std.typecons, std.algorithm;
import std.container, std.math, std.numeric, std.functional;
string[] tk; alias tup = Tuple!(long, long);
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;

void main()
{
    int t;
    scanf("%d", &t);
    foreach(_; 0..t)
    {
        int n;
        scanf("%d", &n);
        int[] arr = [1];
        bool[int] set;
        set[1] = true;
        for (int i = 2; i <= n; i++) {
            for (int j = 0; i * 2 ^^ j <= n; j++)
                if ((i * 2 ^^ j in set) == null) {
                    arr ~= i * 2 ^^ j;
                    set[i * 2 ^^ j] = true;
                }
        }
        writeln(2);
        writefln("%(%s %)", arr);
    }
}


import std.stdio, std.string, std.algorithm, std.conv, std.array;

void main(){
	auto nk=readln.strip.split.map!(to!int).array;
	int n=nk[0],k=nk[1];
	auto a=readln.strip.split.map!(to!int).array;
	sort(a);
	int cur=k,r=0;
	foreach(x;a){
		while(cur<(x+1)/2){
			r++;
			cur*=2;
		}
		cur=max(cur,x);
	}
	writeln(r);
}

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, core.stdc.stdio;

void main() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto K = s[1].to!long * 2;
    auto A = readln.split.map!(to!long).array;
    A.sort();

    long ans = 0;
    foreach (i; 0..N) {
        while (K < A[i]) K *= 2, ans += 1;
        K = max(K, A[i] * 2);
    }

    ans.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, core.stdc.stdio;

void main() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto K = s[1].to!long * 2;
    auto A = readln.split.map!(to!long).array;
    A.sort();
    auto X = A[$-1];

    foreach (i; 0..N) {
        if (A[i] <= K) K = max(K, A[i] * 2);
    }

    long ans = 0;
    while (K <= X) K *= 4, ans += 1;

    ans.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, core.stdc.stdio;

void main() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto K = s[1].to!long * 2;
    auto A = readln.split.map!(to!long).array;
    A.sort();
    auto X = A[$-1];

    foreach (i; 0..N) {
        if (A[i] <= K) K = max(K, A[i] * 2);
    }

    long ans = 0;
    while (K < X) K *= 2, ans += 1;

    ans.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, core.stdc.stdio;

void main() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto K = s[1].to!long * 2;
    auto A = readln.split.map!(to!long).array;
    A.sort();
    auto X = A[$-1];

    foreach (i; 0..N) {
        if (A[i] <= K) K = max(K, A[i] * 2);
    }

    long ans = 0;
    while (K < X) K *= 4, ans += 1;

    ans.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, core.stdc.stdio;

void main() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto K = s[1].to!long * 2;
    auto A = readln.split.map!(to!long).array;
    A.sort();
    auto X = A[$-1];

    foreach (i; 0..N) {
        if (A[i] <= K) K = max(K, A[i] * 2);
    }

    long ans = 0;
    while (K <= X) K *= 2, ans += 1;

    ans.writeln;
}

import std.stdio, std.string, std.algorithm, std.conv, std.array;

void main(){
	auto nk=readln.strip.split.map!(to!int).array;
	int n=nk[0],k=nk[1];
	auto a=readln.strip.split.map!(to!int).array;
	sort(a);
	int cur=k,r=0;
	foreach(x;a){
		while(cur<(x+1)/2){
			r++;
			cur=cur*2+1;
		}
		cur=max(cur,x);
	}
	writeln(r);
}


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 s = RD!string;
		int n = cast(int)s.length;
		auto a = new int[](s.length);
		auto cnt = new int[](10);
		foreach (i; 0..s.length)
		{
			a[i] = s[i] - '0';
			++cnt[a[i]];
		}
		int best;
		foreach (i; 0..10)
			best.chmax(cnt[i]);
		ans[ti] = n - best;

		foreach (i; 0..10)
		{
			foreach (j; i+1..10)
			{
				int last = -1;
				int c;
				foreach (e; a)
				{
					if (last != e && (e == i || e == j))
					{
						++c;
						last = e;
					}
				}
				if (c % 2)
					--c;
				ans[ti].chmin(n - c);
			}
		}
	}

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

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

immutable char [] digits = "0123456789";

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto s = readln.strip;
		int res = 0;
		foreach (char c; digits)
		{
			res = max (res, s.count (c).to !(int));
		}
		foreach (char c; digits)
		{
			foreach (char d; digits)
			{
				int pos = 0;
				int cur = 0;
				while (true)
				{
					while (pos < s.length && s[pos] != c)
					{
						pos += 1;
					}
					pos += 1;
					while (pos < s.length && s[pos] != d)
					{
						pos += 1;
					}
					if (pos >= s.length)
					{
						break;
					}
					pos += 1;
					cur += 2;
				}
				res = max (res, cur);
			}
		}
		writeln (s.length - res);
	}
}


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.typecons;

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

    while (t--) {
        auto lens = readln.chomp.split.map!(to!int).array;

        int[][] arr;
        foreach (i; 0 .. 3) {
            arr ~= readln.chomp.split.map!(to!int).array;
            arr[i].sort();
        }

        ulong ans = ulong.max;
        foreach (ps; iota(3).permutations) {
            int smallidx = 0, bigidx = 0;
            foreach (e; arr[ps[0]]) {
                while (smallidx + 1 < lens[ps[1]] && arr[ps[1]][smallidx + 1] <= e) { ++smallidx; }
                while (bigidx < lens[ps[2]] && arr[ps[2]][bigidx] < e) { ++bigidx; }

                if (bigidx == lens[ps[2]]) { continue; }

                auto sm = arr[ps[1]][smallidx];
                auto bg = arr[ps[2]][bigidx];
                auto cur = (e.to!ulong - sm)^^2 + (sm.to!ulong - bg)^^2 + (bg.to!ulong - e)^^2;

                ans = min(ans, cur);
            }
        }

        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 numCases = readInt();
      foreach (caseId; 0 .. numCases) {
        auto N = new int[3];
        foreach (h; 0 .. 3) {
          N[h] = readInt();
        }
        auto A = new long[][3];
        foreach (h; 0 .. 3) {
          A[h] = new long[N[h]];
          foreach (i; 0 .. N[h]) {
            A[h][i] = readLong();
          }
          A[h].sort;
        }

        long ans = long.max;

        void check(long x, long y, long z) {
          debug {
            writeln("check ", x, " ", y, " ", z);
          }
          const score = (y - z)^^2 + (z - x)^^2 + (x - y)^^2;
          chmin(ans, score);
        }
        void checkH(int h, long y, long z) {
          const pos = A[h].upperBound((y + z) / 2);
          if (pos < N[h]) {
            check(A[h][pos], y, z);
          }
          if (pos - 1 >= 0) {
            check(A[h][pos - 1], y, z);
          }
        }

        foreach (h; 0 .. 3) {
          const h1 = (h + 1) % 3;
          const h2 = (h + 2) % 3;
          for (int i1 = 0, i2 = 0; i1 < N[h1] && i2 < N[h2]; ) {
            debug {
              writeln(h1, " ", h2, "; ", i1, " ", i2);
            }
            checkH(h, A[h1][i1], A[h2][i2]);
            if (A[h1][i1] < A[h2][i2]) {
              ++i1;
            } else {
              ++i2;
            }
          }
        }
        writeln(ans);
      }
    }
  } catch (EOFException e) {
  }
}

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

void main() {
  auto r = new InputReader ();
  immutable nt = r.next!uint ();
  int[] ra (int n) {
    auto a = r.nextA!int (n);
    sort (a);
    return a;
  }
  foreach (tid; 0 .. nt) {
    long res = long.max;
    auto a = r.nextA!uint (3);
    auto t = new int[][3];
    foreach (k; 0 .. 3) t[k] = ra (a[k]);
    void check (long u, long v, long w) {
      long q = (u - v) * (u - v);
      q += (u - w) * (u - w);
      q += (v - w) * (v - w);
      if (res > q) res = q;
    }
    void f (in int[] x, in int[] y, in int[] z) {
      size_t j, k;
      foreach (i; 0 .. x.length) {
        int xv = x[i];
        while (j < y.length && y[j] < xv) ++j;
        while (k < z.length && z[k] < xv) ++k;
        void go (size_t u, size_t v) {
          if (u < y.length && v < z.length)
            check (xv, y[u], z[v]);
        }
        go (j, k);
        if (k > 0) {
          go (j, k - 1);
        }
        if (j > 0) {
          go (j - 1, k);
          if (k > 0) {
            go (j - 1, k - 1);
          }
        }
      }
    }
    f (t[0], t[1], t[2]);
    f (t[1], t[0], t[2]);
    f (t[2], t[0], t[1]);
    writeln (res);
  }
}

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

T binarySearch(alias pred, T)(T ok, T ng)
{
	while (abs(ok-ng) > 1)
	{
		auto mid = (ok+ng)/2;
		if (unaryFun!pred(mid)) ok = mid;
		else ng = mid;
	}
	return ok;
}

void main()
{
	auto t = RD!int;
	auto ans = new long[](t);
	foreach (ti; 0..t)
	{
		auto nr = RD!int;
		auto ng = RD!int;
		auto nb = RD!int;
		auto r = RDA;
		auto g = RDA;
		auto b = RDA;
		r.sort();
		g.sort();
		b.sort();
		auto arr = new long[][](nr+ng+nb);
		foreach (i; 0..nr)
		{
			arr[i] = [r[i], 0];
		}
		foreach (i; 0..ng)
		{
			arr[nr+i] = [g[i], 1];
		}
		foreach (i; 0..nb)
		{
			arr[nr+ng+i] = [b[i], 2];
		}
		arr.sort!"a[0] < b[0]"();

		ans[ti] = long.max;
		long lr, lg, lb;
		long search(in long[] _arr, long d)
		{
			auto p = binarySearch!((int a) => _arr[a] >= d)(cast(int)_arr.length-1, -1);
			long res = _arr[p];
			if (p != 0)
			{
				if (abs(d - _arr[p-1]) < abs(res - d))
					res = _arr[p-1];
			}
			return res;
		}
		long eval(long rr, long gg, long bb)
		{
			return (rr-gg)^^2 + (gg-bb)^^2 + (bb-rr)^^2;
		}
		foreach (i; 0..arr.length)
		{
			if (arr[i][1] == 0)
			{
				lr = arr[i][0];
				if (lg != 0 && lb != 0)
				{
					long x;
					if (lg <= lb)
					{
						auto d = (lr+lg+1) / 2;
						x = search(b, d);
						ans[ti].chmin(eval(lr, lg, x));
					}
					else
					{
						auto d = (lr+lb+1) / 2;
						x = search(g, d);
						ans[ti].chmin(eval(lr, x, lb));
					}
				}
			}
			else if (arr[i][1] == 1)
			{
				lg = arr[i][0];
				if (lr != 0 && lb != 0)
				{
					long x;
					if (lr <= lb)
					{
						auto d = (lr+lg+1) / 2;
						x = search(b, d);
						ans[ti].chmin(eval(lr, lg, x));
					}
					else
					{
						auto d = (lg+lb+1) / 2;
						x = search(r, d);
						ans[ti].chmin(eval(x, lg, lb));
					}
				}
			}
			else
			{
				lb = arr[i][0];
				if (lr != 0 && lg != 0)
				{
					long x;
					if (lr <= lg)
					{
						auto d = (lr+lb+1) / 2;
						x = search(g, d);
						ans[ti].chmin(eval(lr, x, lb));
						debug writeln(arr[i], " d:", d, " x:", x);
					}
					else
					{
						auto d = (lg+lb+1) / 2;
						x = search(r, d);
						ans[ti].chmin(eval(x, lg, lb));
					}
				}
			}
		}
	}

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

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

immutable int total = 3;

void main ()
{
	int tests;
	readf !(" %s") (tests);
	foreach (test; 0..tests)
	{
		int [total] n;
		foreach (ref c; n)
		{
			readf !(" %s") (c);
		}
		readln;
		int [] [total] a;
		foreach (ref c; a)
		{
			c = readln.splitter.map !(to !(int)).array;
			sort (c);
		}
		long res = long.max;
		foreach (i; 0..total)
		{
			foreach (j; 0..total)
			{
				if (j != i)
				{
					auto k = total - i - j;
					int v = 0;
					int w = 0;
					foreach (u; 0..n[i])
					{
						while (v < n[j] &&
						    a[j][v] <= a[i][u])
						{
							v += 1;
						}
						if (v > 0)
						{
							v -= 1;
						}
						while (w + 1 < n[k] &&
						    a[k][w] < a[j][v])
						{
							w += 1;
						}
						res = min (res,
						    (cast (long)
						    (a[i][u] - a[j][v])) ^^ 2 +
						    (cast (long)
						    (a[j][v] - a[k][w])) ^^ 2 +
						    (cast (long)
						    (a[k][w] - a[i][u])) ^^ 2);
	                        	}
				}
			}
		}
		writeln (res);
	}
}

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

immutable int total = 3;

void main ()
{
	int tests;
	readf !(" %s") (tests);
	foreach (test; 0..tests)
	{
		int [total] n;
		foreach (ref c; n)
		{
			readf !(" %s") (c);
		}
		readln;
		int [] [total] a;
		foreach (ref c; a)
		{
			c = readln.splitter.map !(to !(int)).array;
			sort (c);
		}
		long res = long.max;
		foreach (i; 0..total)
		{
			foreach (j; 0..total) if (j != i)
			{
				auto k = total - i - j;
				int v = 0;
				int w = 0;
				foreach (u; 0..n[i])
				{
					while (v + 1 < n[j] &&
					    a[j][v] < a[i][u])
					{
						v += 1;
					}
					while (w + 1 < n[k] &&
					    a[k][w] < a[j][v])
					{
						w += 1;
					}
					res = min (res,
					    (cast (long)
					    (a[i][u] - a[j][v])) ^^ 2 +
					    (cast (long)
					    (a[j][v] - a[k][w])) ^^ 2 +
					    (cast (long)
					    (a[k][w] - a[i][u])) ^^ 2);
				}
			}
		}
		writeln (res);
	}
}