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(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }

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

		auto cnt_a = new long[](n);
		auto cnt_b = new long[](m);
		foreach (i; 0..k)
		{
			ans[ti] += i - cnt_a[a[i]] - cnt_b[b[i]];
			++cnt_a[a[i]];
			++cnt_b[b[i]];
		}
	}

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

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

void get(Args...)(ref Args args)
{
    import std.traits, std.meta, std.typecons;

    static if (Args.length == 1) {
        alias Arg = Args[0];

        static if (isArray!Arg) {
          static if (isSomeChar!(ElementType!Arg)) {
            args[0] = readln.chomp.to!Arg;
          } else {
            args[0] = readln.split.to!Arg;
          }
        } else static if (isTuple!Arg) {
            auto input = readln.split;
            static foreach (i; 0..Fields!Arg.length) {
                args[0][i] = input[i].to!(Fields!Arg[i]);
            }
        } else {
            args[0] = readln.chomp.to!Arg;
        }
    } else {
        auto input = readln.split;
        assert(input.length == Args.length);

        static foreach (i; 0..Args.length) {
            args[i] = input[i].to!(Args[i]);
        }
    }
}

void get_lines(Args...)(size_t N, ref Args args)
{
    import std.traits, std.range;

    static foreach (i; 0..Args.length) {
        static assert(isArray!(Args[i]));
        args[i].length = N;
    }

    foreach (i; 0..N) {
        static if (Args.length == 1) {
            get(args[0][i]);
        } else {
            auto input = readln.split;
            static foreach (j; 0..Args.length) {
                args[j][i] = input[j].to!(ElementType!(Args[j]));
            }
        }
    }
}

void main()
{
    int T; get(T);
    while (T--) {
        int A, B, K; get(A, B, K);
        int[] AA; get(AA);
        int[] BB; get(BB);
        long res;
        auto ac = new int[](A + 1);
        auto bc = new int[](B + 1);
        int[Tuple!(int, int)] ps;
        foreach_reverse (i; 0..K) {
            int a = AA[i], b = BB[i], p;
            auto t = tuple(a, b);
            if (t in ps) p = ps[t];
            res += (K - i - 1) - ac[a] - bc[b] + p;
            ac[a] += 1;
            bc[b] += 1;
            ps[t] += 1;
        }
        writeln(res);
    }
}

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)
	{
		int p, q, k;
		readf !(" %s %s %s") (p, q, k);
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		auto b = readln.splitter.map !(to !(int)).array;
		a[] -= 1;
		b[] -= 1;
		auto da = new int [p];
		auto db = new int [q];
		foreach (ref c; a)
		{
			da[c] += 1;
		}
		foreach (ref c; b)
		{
			db[c] += 1;
		}
		long res = 0;
		foreach (i; 0..p)
		{
			res += da[i] * (da[i] - 1L);
		}
		foreach (j; 0..q)
		{
			res += db[j] * (db[j] - 1L);
		}
		writeln ((k * (k - 1L) - res) / 2);
	}
}


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

void main() {
    auto S = readln.chomp;
    auto T = readln.chomp;
    auto N = S.length.to!int;
    auto M = T.length.to!int;
    auto A = readln.split.map!(to!int).array;
    auto B = new int[](N);
    foreach (i; 0..N) {
        B[A[i]-1] = i;
    }

    int hi = N;
    int lo = M - 1;
    while (hi - lo > 1) {
        int mid = (hi + lo) / 2;
        int cnt = 0;
        int del = N - mid;
        foreach (i; 0..N) {
            if (B[i] < del)
                continue;
            if (S[i] == T[cnt])
                cnt += 1;
            if (cnt >= M)
                break;
        }
        if (cnt >= M)
            hi = mid;
        else
            lo = mid;
    }

    (N-hi).writeln;
}

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

void main ()
{
	while (true)
	{
		auto s = readln.strip;
		if (s is null)
		{
			break;
		}
		auto t = readln.strip;
		auto p = readln.splitter.map !(to !(int)).array;
		int n = cast (int) s.length;
		p[] -= 1;

		bool ok (int k)
		{
			auto cur = s.dup;
			foreach (i; 0..k)
			{
				cur[p[i]] = '-';
			}
			cur = cur.filter !(c => c != '-').text.dup;
			int pos = 0;
			foreach (c; cur)
			{
				if (c == t[pos])
				{
					pos += 1;
					if (pos >= t.length)
					{
						return true;
					}
				}
			}
			return false;
		}

		int lo = 0;
		int hi = n;
		while (lo < hi)
		{
			int me = (lo + hi + 1) >> 1;
			if (ok (me))
			{
				lo = me;
			}
			else
			{
				hi = me - 1;
			}
		}
		writeln (lo);
	}
}


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

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

  for (auto i = n - 1, r = k; i >= 0; i--) {
    if ((r -= a[i]) < 0) {
      if ((--m) == 0) {
        writeln(n - i - 1);
        return;
      }
      r = k - a[i];
    }
  }

  writeln(n);
}

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

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

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

bool ok(int[] a, int m, int k)
{
    auto n = cast(int)a.length;
    auto i = 0, j = 0;
    auto c = k;
    while (i < n && j < m)
    {
        if (a[i] > c)
        {
            ++ j;
            c = k;
        }
        else
        {
            c -= a[i];
            ++ i;
        }
    }
    return i == n ? true : false;
}

void solve(int[] a, int n, int m, int k)
{
    auto ans = 0;
    auto left = 0, right = n - 1;
    while (left <= right)
    {
        auto mid = (left + right) >> 1;
        if (!ok(a[mid .. n], m, k))
        {
            left = mid + 1;
        }
        else
        {
            ans = n - mid;
            right = mid - 1;
        }
    }
    writeln(ans);
}

int main(string[] args)
{
    int n, m, k;
    while (readf("%d %d %d\n", &n, &m, &k) == 3)
    {
        auto a = new int[n];
        foreach (i; 0 .. n)
        {
            readf(" %d", &a[i]);
        }
        readln;
        solve(a, n, m, k);
    }
    return 0;
}


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

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

		auto d = new int [n];

		void recur (int v, int h)
		{
			d[h] ^= 1;
			foreach (u; a[v])
			{
				recur (u, h + 1);
			}
		}

		recur (0, 0);
		writeln (sum (d));
	}
}

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

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

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

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

void main() {
  try {
    for (; ; ) {
      const N = readInt();
      auto P = new int[N];
      foreach (u; 1 .. N) {
        P[u] = readInt() - 1;
      }
      P[0] = -1;

      auto dep = new int[N];
      dep[0] = 0;
      foreach (u; 1 .. N) {
        dep[u] = 1 + dep[P[u]];
      }

      auto cnt = new int[N];
      foreach (u; 0 .. N) {
        ++cnt[dep[u]];
      }
      int ans;
      foreach (d; 0 .. N) {
        ans += cnt[d] % 2;
      }
      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 = 998244353;
//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 main()
{
	auto n = RD!int;
	auto a = RD!int;
	auto b = RD!int;
	auto k = RD!int;
	auto h = RDA(-1);

	auto cnt = new int[](n);
	foreach (i; 0..n)
	{
		auto r = h[i] % (a+b);
		cnt[i] = r / a;
	}
	cnt.sort();

	int ans;
	foreach (i; 0..n)
	{
		if (cnt[i] <= k)
		{
			++ans;
			k -= cnt[i];
		}
	}

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

import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.typecons;
import std.algorithm.mutation;
import std.container.array;

string[] tokens;

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

int readInt()
{
    return readToken.to!int;
}

void main()
{
    int n = readInt;
    int a = readInt;
    int b = readInt;
    int k = readInt;
    int[] need = new int[n];
    for (int i = 0; i < n; ++i)
    {
        int hp = readInt;
        int s = a + b;
        int rounds = (hp + s - 1) / s;
        hp -= (rounds - 1) * s;
        need[i] = (hp + a - 1) / a - 1;
    }
    sort(need);
    int result = 0;
    for (int i = 0; i < n; ++i)
    {
        if (need[i] <= k)
        {
            k -= need[i], result++;
        }
    }
    writeln(result);
}

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

DList!string _words;

void read(T...)(ref T t)
{
  void singleRead(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);
  }
  static foreach(i; 0 .. T.length)
    {
      static if (is(typeof({foreach(ref e; t[i]){}})) && !is(T[i] == string))
        {
          foreach(ref element; t[i])
            read(element);
        }
      else
        {
          singleRead(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 = E.init)
{
  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 main()
{
  mixin(input(q{n, a, b, k}));
  auto h = new long[n.ind];
  read(h);
  auto rk = h
    .map!((hi)
      {
       auto r = (hi % (a + b) == 0)? (a + b) : (hi % (a + b));
       // a * x >= r
       // x >= r / a
       return (r + pmod(-r, a)) / a - 1;
      })
    .array;
  sort(rk);
  auto points = long(0);
  foreach(rki; rk)
    {
      if (k < rki)
        break;
      k -= rki;
      points++;
    }
  writeln(points);
}


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

void main()
{
	int n;
	scanf("%d", &n);
	getchar();
	auto heroes = readln.split().to!(BigInt[]);
	int m;
	scanf("%d", &m);
	getchar();
	heroes.sort();
	BigInt[] res = new BigInt[m];
	BigInt total = to!BigInt(heroes.sum());
	foreach(i;0..m)
	{
		auto dragon = readln.split().to!(BigInt[]);
		size_t send = 0;
		if (heroes[n - 1] < dragon[0])
			send = n - 1;
		else
		{
			size_t ok = n - 1;
			size_t ng = -1;
			while (ok - ng > 1)
			{
				size_t center = (ng + ok) / 2;
				if (heroes[center] >= dragon[0])
					ok = center;
				else
					ng = center;
			}
			send = ok;
		}
		res[i] = max(BigInt(0), dragon[0] - heroes[send]) + max(BigInt(0), dragon[1] - total + heroes[send]);
		if (send > 0)
			res[i] = min(res[i], max(BigInt(0), dragon[0] - heroes[send - 1] + max(BigInt(0), dragon[1] - total + heroes[send - 1])));
	}
	res.each!(a => writeln(a));
}

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

void main()
{
	int n;
	scanf("%d", &n);
	getchar();
	auto heroes = readln.split().to!(long[]);
	int m;
	scanf("%d", &m);
	getchar();
	heroes.sort();
	long[] res = new long[m];
	long total = to!long(heroes.sum());
	foreach(i;0..m)
	{
		auto dragon = readln.split().to!(long[]);
		size_t send = 0;
		if (heroes[n - 1] < dragon[0])
			send = n - 1;
		else
		{
			size_t ok = n - 1;
			size_t ng = -1;
			while (ok - ng > 1)
			{
				size_t center = (ng + ok) / 2;
				if (heroes[center] >= dragon[0])
					ok = center;
				else
					ng = center;
			}
			send = ok;
		}
		res[i] = max(long(0), dragon[0] - heroes[send]) + max(long(0), dragon[1] - total + heroes[send]);
		if (send > 0)
			res[i] = min(res[i], max(long(0), dragon[0] - heroes[send - 1] + max(long(0), dragon[1] - total + heroes[send - 1])));
	}
	res.each!(a => writeln(a));
}

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

void main()
{
	int n;
	scanf("%d", &n);
	getchar();
	auto heroes = readln.split().to!(ulong[]);
	int m;
	scanf("%d", &m);
	getchar();
	heroes.sort();
	foreach(_;0..m)
	{
		auto dragon = readln.split().to!(ulong[]);
		auto atack = heroes.find!((a,b) => a >= b)(dragon[0]);
		if (atack.empty)
		{
			if (dragon[1] > heroes.sum() - heroes.maxElement)
				writeln(dragon[0] - heroes.maxElement + max(0, dragon[1] - heroes.sum() + heroes.maxElement));
			else
				writeln(dragon[0] - heroes.maxElement);
		}
		else
		{
			ulong[] weak = heroes.find!((a,b) => a < b)(dragon[0]);
			ulong atacker = atack.minElement();
			if (!weak.empty)
			{
				ulong weaker = weak.maxElement();
				if (dragon[1] > heroes.sum() - weaker)
					if (dragon[1] > heroes.sum() - atacker)
						writeln(min(dragon[0] - weaker + dragon[1] - heroes.sum() + weaker, dragon[1] - heroes.sum() + atacker));
					else
						writeln(min(dragon[0] - weaker + dragon[1] - heroes.sum() + weaker, 0));
				else
					if (dragon[1] > heroes.sum() - atacker)
						writeln(min(dragon[0] - weaker, dragon[1] - heroes.sum() + atacker));
					else
						writeln(min(dragon[0] - weaker, 0));
			}
			else
			{
				if (dragon[1] > heroes.sum() - atacker)
					writeln(max(0, dragon[1] - heroes.sum() + atacker));
				else
					writeln(0);
			}
		}
	}
}

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

void main()
{
	int n;
	scanf("%d", &n);
	getchar();
	auto heroes = readln.split().to!(ulong[]);
	int m;
	scanf("%d", &m);
	getchar();
	heroes.sort();
	foreach(_;0..m)
	{
		auto dragon = readln.split().to!(ulong[]);
		auto atack = heroes.find!((a,b) => a >= b)(dragon[0]);
		if (atack.empty)
		{
			writeln(dragon[0] - heroes.maxElement() + max(0, dragon[1] - heroes.sum() + heroes.maxElement));
		}
		else
		{
			ulong[] weak = heroes.find!((a,b) => a < b)(dragon[0]);
			ulong atacker = atack.minElement();
			if (!weak.empty)
			{
				ulong weaker = weak.maxElement();
				if (dragon[1] > heroes.sum() - weaker)
					if (dragon[1] > heroes.sum() - atacker)
						writeln(min(dragon[0] - weaker + dragon[1] - heroes.sum() + weaker, dragon[1] - heroes.sum() + atacker));
					else
						writeln(min(dragon[0] - weaker + dragon[1] - heroes.sum() + weaker, 0));
				else
					if (dragon[1] > heroes.sum() - atacker)
						writeln(min(dragon[0] - weaker, dragon[1] - heroes.sum() + atacker));
					else
						writeln(min(dragon[0] - weaker, 0));
//				writeln(min(dragon[0] - weaker + max(0, dragon[1] - heroes.sum() + weaker), max(0, dragon[1] - heroes.sum() + atacker)));
			}
			else
			{
				if (dragon[1] > heroes.sum() - atacker)
					writeln(max(0, dragon[1] - heroes.sum() + atacker));
				else
					writeln(0);
			}
		}
	}
}

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

void main()
{
	int n;
	scanf("%d", &n);
	getchar();
	auto heroes = readln.split().to!(long[]);
	int m;
	scanf("%d", &m);
	getchar();
	heroes.sort();
	foreach(_;0..m)
	{
		auto dragon = readln.split().to!(long[]);
		auto atack = heroes.find!((a,b) => a >= b)(dragon[0]);
		if (atack.empty)
		{
			writeln(dragon[0] - heroes.maxElement() + max(0, dragon[1] - heroes.sum() + heroes.maxElement));
		}
		else
		{
			long[] weak = heroes.find!((a,b) => a < b)(dragon[0]);
			long atacker = atack.minElement();
			if (!weak.empty)
			{
				long weaker = weak.maxElement();
				writeln(min(dragon[0] - weaker + max(0, dragon[1] - heroes.sum() + weaker), max(0, dragon[1] - heroes.sum() + atacker)));
			}
			else
			{
					writeln(max(0, dragon[1] - heroes.sum() + atacker));
			}
		}
	}
}

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

void main()
{
	int n;
	scanf("%d", &n);
	getchar();
	auto heroes = readln.split().to!(long[]);
	int m;
	scanf("%d", &m);
	getchar();
	heroes.sort();
	foreach(_;0..m)
	{
		auto dragon = readln.split().to!(long[]);
		auto atack = heroes.find!((a,b) => a >= b)(dragon[0]);
		if (atack.empty)
		{
			writeln(dragon[0] - heroes.maxElement() + max(0, dragon[1] - heroes.sum() + heroes.maxElement));
		}
		else
		{
			long[] weak = heroes.find!((a,b) => a < b)(dragon[0]);
			long atacker = atack.minElement();
			if (!weak.empty)
			{
				long weaker = weak.maxElement();
				if (dragon[0] - weaker > atacker - dragon[0])
					writeln(max(0, dragon[1] - heroes.sum() + atacker));
				else
					writeln(dragon[0] - weaker + max(0, dragon[1] - heroes.sum() + weaker));
			}
			else
			{
					writeln(max(0, dragon[1] - heroes.sum()));
			}
		}
	}
}

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

void main()
{
	int n;
	scanf("%d", &n);
	getchar();
	auto heroes = readln.split().to!(long[]);
	int m;
	scanf("%d", &m);
	getchar();
	heroes.sort();
	foreach(_;0..m)
	{
		auto dragon = readln.split().to!(long[]);
		auto atack = heroes.find!((a,b) => a >= b)(dragon[0]);
		if (atack.empty)
		{
			writeln(dragon[0] - heroes.maxElement() + max(0, dragon[1] - heroes.sum() + heroes.maxElement));
		}
		else
		{
			long[] weak = heroes.find!((a,b) => a < b)(dragon[0]);
			long atacker = atack.minElement();
			if (!weak.empty)
			{
				long weaker = weak.maxElement();
				if (dragon[0] - weaker > atacker - dragon[0])
					writeln(max(0, dragon[1] - heroes.sum() + atacker));
				else
					writeln(dragon[0] - weaker + max(0, dragon[1] - heroes.sum() + weaker));
			}
			else
			{
					writeln(max(0, dragon[1] - heroes.sum() + atacker));
			}
		}
	}
}

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

void main()
{
	int n;
	scanf("%d", &n);
	getchar();
	auto heroes = readln.split().to!(long[]);
	int m;
	scanf("%d", &m);
	getchar();
	heroes.sort();
	foreach(_;0..m)
	{
		auto dragon = readln.split().to!(long[]);
		auto atack = heroes.find!((a,b) => a >= b)(dragon[0]);
		if (atack.empty)
		{
			writeln(dragon[0] - heroes.maxElement() + max(0, dragon[1] - heroes.sum() + heroes.maxElement));
		}
		else
		{
			long[] weak = heroes.find!((a,b) => a < b)(dragon[0]);
			long atacker = atack.minElement();
			if (!weak.empty)
			{
				long weaker = weak.maxElement();
				if (dragon[0] - weaker > atacker - dragon[0])
					writeln(atacker - dragon[0] + max(0, dragon[1] - heroes.sum() + atacker));
				else
					writeln(dragon[0] - weaker + max(0, dragon[1] - heroes.sum() + weaker));
			}
			else
			{
					writeln(atacker - dragon[0] + max(0, dragon[1] - heroes.sum() + atacker));
					debug{
						writeln(max(0, dragon[1] - heroes.sum() + atacker));
					}
			}
		}
	}
}


import std.stdio;
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 n = next!int;
  auto m = next!int;
  struct VarDef
  {
    int varname;
    enum ExpType
      {
	value, and, or, xor
      }
    ExpType type;
    int v1 = -1, v2 = -1;
    string s1, s2;
  }
  auto vdefs = new VarDef[](n);
  int[string] indexof;
  int lastindex = 0;
  foreach(i; 0 .. n)
    {
      auto line = inputFile.readln;
      auto words = line.split;
      indexof[words[0]] = lastindex++;
      if (words.length == 5)
	{
	  vdefs[i].varname = indexof[words[0]];

	  if (words[2][0] == '0' || words[2][0] == '1') vdefs[i].s1 = words[2];
	  else if (words[2][0] == '?') {}
	  else vdefs[i].v1 = indexof[words[2]];

	  final switch(words[3])
	    {
	    case "AND":
	      vdefs[i].type = VarDef.ExpType.and;
	      break;
	    case "XOR":
	      vdefs[i].type = VarDef.ExpType.xor;
	      break;
	    case "OR":
	      vdefs[i].type = VarDef.ExpType.or;
	      break;
	    }
	  if (words[4][0] == '0' || words[4][0] == '1') vdefs[i].s2 = words[4];
	  else if (words[4][0] == '?') {}
	  else vdefs[i].v2 = indexof[words[4]];

	}
      else
	{
	  debug assert(words.length == 3);
	  vdefs[i].varname = indexof[words[0]];
	  vdefs[i].type = VarDef.ExpType.value;
	  if (words[2][0] == '0' || words[2][0] == '1') vdefs[i].s1 = words[2];
	  else if (words[2][0] == '?') {}
	  else vdefs[i].v1 = indexof[words[2]];
	}
    }
  debug writeln(vdefs);
  int evalbit(int bitpos, int bitval)
  {
    int[] varval = new int[](n);
    int expval(int v, string exp)
    {
      if (v != -1) return varval[v];
      if (exp !is null) return cast(int)(exp[bitpos] - '0');
      return bitval;
    }
    void eval(VarDef vdef)
    {
      final switch(vdef.type)
	{
	case VarDef.ExpType.value:
	  varval[vdef.varname] = expval(vdef.v1, vdef.s1);
	  break;
	case VarDef.ExpType.and:
	  varval[vdef.varname] = expval(vdef.v1, vdef.s1) & expval(vdef.v2, vdef.s2);
	  break;
	case VarDef.ExpType.xor:
	  varval[vdef.varname] = expval(vdef.v1, vdef.s1) ^ expval(vdef.v2, vdef.s2);
	  break;
	case VarDef.ExpType.or:
	  varval[vdef.varname] = expval(vdef.v1, vdef.s1) | expval(vdef.v2, vdef.s2);
	  break;
	}
    }
    foreach(vdef; vdefs)
      eval(vdef);
    return cast(int)(varval.sum);
  }
  auto ansmax = new char[](m);
  auto ansmin = new char[](m);
  foreach(bitpos; 0 .. m)
    {
      ansmax[bitpos] = '0';
      ansmin[bitpos] = '0';
      auto r0 = evalbit(cast(int)bitpos, 0);
      auto r1 = evalbit(cast(int)bitpos, 1);
      if (r1 < r0)
	ansmin[bitpos] = '1';
      if (r1 > r0)
	ansmax[bitpos] = '1';
    }
  ansmin.idup.writeln;
  ansmax.idup.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;
    }
}

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

void main ()
{
    int n;
    int m;
    while (readf (" %s %s", &n, &m) > 0)
    {
        readln;
        auto expr = new string [] [n];
        int [string] num;
        auto op1 = new int [n];
        auto op2 = new int [n];

        int operand (int i, string s)
        {
            if (s == "?")
            {
                return -1;
            }
            else if ('0' <= s[0] && s[0] <= '1')
            {
                return -2;
            }
            else
            {
                assert (s in num);
                return num[s];
            }
        }

        foreach (i; 0..n)
        {
            expr[i] = readln.split;
            num[expr[i][0]] = i;
            op1[i] = operand (i, expr[i][2]);
            if (expr[i].length == 3)
            {
                continue;
            }
            op2[i] = operand (i, expr[i][4]);
        }

        int calc (int j, int b)
        {
            auto v = new int [n];

            int value (int i, int k, int cur)
            {
                if (cur == -1)
                {
                    return b;
                }
                if (cur == -2)
                {
                    return expr[i][k][j] - '0';
                }
                assert (cur < i);
                return v[cur];
            }

            foreach (i; 0..n)
            {
                v[i] = value (i, 2, op1[i]);
                if (expr[i].length == 5)
                {
                    if (expr[i][3] == "OR")
                    {
                        v[i] |= value (i, 4, op2[i]);
                    }
                    if (expr[i][3] == "XOR")
                    {
                        v[i] ^= value (i, 4, op2[i]);
                    }
                    if (expr[i][3] == "AND")
                    {
                        v[i] &= value (i, 4, op2[i]);
                    }
                }
            }

            return sum (v);
        }

        string resMin;
        string resMax;
        foreach (j; 0..m)
        {
            int u = calc (j, 0);
            int v = calc (j, 1);
            if (u == v)
            {
                resMin ~= '0';
                resMax ~= '0';
            }
            if (u > v)
            {
                resMin ~= '1';
                resMax ~= '0';
            }
            if (u < v)
            {
                resMin ~= '0';
                resMax ~= '1';
            }
        }
        writeln (resMin);
        writeln (resMax);
    }
}


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

void main ()
{
	int n;
	while (readf !(" %s") (n) > 0)
	{
		auto a = new int [] [n];
		auto d = new int [n];
		foreach (i; 0..n - 1)
		{
			int u, v;
			readf !(" %s %s") (u, v);
			u -= 1;
			v -= 1;
			a[u] ~= v;
			a[v] ~= u;
			d[u] += 1;
			d[v] += 1;
		}

		int root = 0;
		while (d[root] != 1)
		{
			root += 1;
		}

		auto depth = new int [n];

		void recur (int v, int p)
		{
			foreach (u; a[v])
			{
				if (u != p)
				{
					depth[u] = depth[v] + 1;
					recur (u, v);
				}
			}
		}

		depth[root] = 0;
		recur (root, -1);

		int res = n - 1;
		foreach (v; 0..n)
		{
			auto numLeaves = a[v].count !(u => d[u] == 1);
			if (numLeaves > 1)
			{
				res -= numLeaves - 1;
			}
		}
		auto haveOdd = n.iota
		    .filter !(v => d[v] == 1)
		    .map !(v => depth[v])
		    .any !(x => x % 2 != 0);
		writeln (haveOdd ? 3 : 1, " ", res);
	}
}

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

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

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

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

int N;
int[] A, B;

int[][] G;
int[] dep;

void dfs(int u, int p) {
  dep[u] = (p == -1) ? 0 : (dep[p] + 1);
  foreach (i; G[u]) {
    const v = A[i] ^ B[i] ^ u;
    if (v != p) {
      dfs(v, u);
    }
  }
}

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

      G = new int[][N];
      foreach (i; 0 .. N - 1) {
        G[A[i]] ~= i;
        G[B[i]] ~= i;
      }
      int rt = -1;
      foreach (u; 0 .. N) {
        if (G[u].length == 1) {
          rt = u;
          break;
        }
      }
      dep = new int[N];
      dfs(rt, -1);

      int ansMin = 1;
      foreach (u; 0 .. N) {
        if (G[u].length == 1) {
          if (dep[u] % 2 != 0) {
            ansMin = 3;
          }
        }
      }

      int ansMax;
      auto used = new bool[N];
      foreach (i; 0 .. N - 1) {
        if (G[A[i]].length == 1) {
          used[B[i]] = true;
        } else if (G[B[i]].length == 1) {
          used[A[i]] = true;
        } else {
          ++ansMax;
        }
      }
      foreach (u; 0 .. N) {
        if (used[u]) {
          ++ansMax;
        }
      }

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

void main()
{
	auto n = RD!int;
	auto edges = new int[][](n);
	foreach (i; 0..n-1)
	{
		auto a = RD!int-1;
		auto b = RD!int-1;
		edges[a] ~= b;
		edges[b] ~= a;
	}
	debug writeln(edges);

	bool odds;
	{
		int[][] open;
		auto dist = new int[](n);
		dist[] = -1;
		foreach (i; 0..n)
		{
			if (edges[i].length == 1)
			{
				open ~= [i, -1];
				dist[i] = 0;
			}
		}

		(){
		while (!open.empty)
		{
			auto nd = open.front; open.popFront;
			auto from = nd[0];
			auto par = nd[1];
			auto d = dist[from]^1;
			foreach (to; edges[from])
			{
				if (to == par) continue;
				if (dist[to] == -1)
				{
					dist[to] = d;
					open ~= [to, from];
				}
				else if (dist[to] != d)
				{
					odds = true;
					return;
				}
			}
		}}();
	}

	long cnt;
	{
		int[][] open;
		auto dist = new int[](n);
		dist[] = -1;
		foreach (i; 0..n)
		{
			if (edges[i].length == 1)
			{
				open ~= [i, -1];
				dist[i] = 0;
			}
		}
		debug writeln("open:", open);
		foreach (e; open)
		{
			int[][] op = [e];
			while (!op.empty)
			{
				auto nd = op.front; op.popFront;
				auto from = nd[0];
				auto par = nd[1];
				auto d = dist[from]+1;
				if (d == 3) continue;
				foreach (to; edges[from])
				{
					if (to == par) continue;
					if (dist[to] == 0)
					{
						++cnt;
						debug writeln(from, ":", to);
					}
					else if (dist[to] == -1 && d != 2)
					{
						dist[to] = d;
						op ~= [to, from];
					}
				}
			}
		}
	}
	debug writeln("cnt:", cnt);

	long ans1 = odds ? 3 : 1;
	long ans2 = n-1-cnt;
	writeln(ans1, " ", ans2);
	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 !(long)).array;

		long p = 1;
		foreach (ref c; a)
		{
			while (c % 2 == 0)
			{
				c /= 2;
				p *= 2;
			}
		}

		sort (a);
		a.back *= p;
		writeln (a.sum);
	}
}

immutable multi = true;

void solve(int tc)
{
  auto n = readInt!int;
  auto a = ma(n, readInt!int);
  long maxA = long.min;
  long sumA = 0;
  auto powers = 0;
  foreach(ref ai; a)
    {
      while ((ai & 1) == 0)
	{
	  powers++;
	  ai >>= 1;
	}
      sumA += ai;
      maxA = max(maxA, ai);
    }
  long power = (1L << powers);
  long ans = power * (cast(long)maxA) + (sumA - maxA);
  ans.writeln;
}

// main {{{
void main()
{
	auto t = multi? readInt!int : 1;
	foreach(tc; 0 .. t) solve(tc);
}
// }}}
// input {{{
int currChar;
static this()
{
	currChar = ' ';
}

char topChar()
{
	return cast(char) currChar;
}

void popChar()
{
	import core.stdc.stdio;
	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;
}
void writes(T...)(T t)
{
	static foreach(i, ti; t)
	{
		static if (isIterable!(T[i]))
		{
			foreach(e; ti)
			{
				write(e, " ");
			}
		}
	}
}
auto ra(V)()
{
	int n = readInt!int;
	return ma(n, readInt!V);
}
auto rt(V)()
{
	int n = readInt!int;
	int m = readInt!int;
	return ma(n, ma(m, readInt!V));
}
auto rbg()
{
	int n = readInt!int;
	int m = readInt!int;
	auto adj = new int[][](n);
	foreach(i; 0 .. m)
	{
		auto u = readInt!int - 1;
		auto v = readInt!int - 1;
		adj[u] ~= v;
		adj[v] ~= u;
	}
	return adj;
}
auto ma(V)(size_t n, lazy V v)
{
	auto arr = new V[](n);
	foreach(ref ai; arr) ai = v;
	return arr;
}
// }}}
// imports {{{
import std.stdio;
import std.algorithm;
import std.typecons;
import std.math;
import std.numeric;
import std.container;
import std.range;
import std.array;
import std.ascii;
// }}}

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()
{
  foreach (casenum ; 1 .. readln.strip.to!int + 1) {
    long n;
    readf!" %d "(n);
    auto a = readln.splitter.map!(to!long).array;
    long mulcnt = 0;
    long sum = 0;
    foreach (ref x ; a) {
      while (x % 2 == 0) {
        mulcnt++;
        x /= 2;
      }
      sum += x;
    }
    a.sort;
    sum -= a.back;
    while (mulcnt-- > 0) {
      a.back *= 2;
    }
    sum += a.back;
    writeln(sum);
  }
}


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

enum maxN = 200_000;
int[maxN] parent = -1;
int[maxN] setSize = 0;
void makeSet(int i)
{
  assert(parent[i] == -1);
  parent[i] = i;
  setSize[i] = 1;
}
int findParent(int i)
{
  auto pi = parent[i];
  if (pi == -1 || pi == i) return pi;
  return parent[i] = findParent(pi);
}
int join(int i, int j)
{
  assert(findParent(i) != -1);
  assert(findParent(j) != -1);
  i = findParent(i);
  j = findParent(j);
  if (setSize[i] < setSize[j]) swap(i, j);
  parent[j] = i;
  setSize[i] += setSize[j];
  return i;
}

void main(string[] args)
{
  auto n = next!int;
  auto a = next!int(n);
  auto sortedIndices = iota(0, n).array.sort!((i, j) => a[i] > a[j]);
  auto sol = new int[](n + 1);
  int soli = 1;
  int mx = 1;
  foreach(i; sortedIndices)
    {
      makeSet(i);
      if (i + 1 < n && parent[i + 1] != -1)
	mx = max(setSize[join(i, i + 1)], mx);
      if (i - 1 >= 0 && parent[i - 1] != -1)
	mx = max(setSize[join(i, i - 1)], mx);
      for(; soli <= mx; soli++)
	sol[soli] = a[i];
    }
  println(sol[1 .. $]);
}

static this()
{
  popChar;
}

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

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

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

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

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

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

import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main ()
{
    int n;
    while (readf (" %s ", &n) > 0)
    {
        auto a = readln.split.map !(to !(int)).array;
        auto p = n.iota.array;
        auto prev = (n + 2).iota.array;
        auto next = prev.dup;
        --prev[];
        ++next[];
        p.sort !((x, y) => a[x] > a[y]);
        ++p[];
        auto ans = new int [n];
        int v = 0;
        foreach (x; p)
        {
            prev[next[x]] = prev[x];
            next[prev[x]] = next[x];
            while (v < next[x] - prev[x] - 1)
            {
                ans[v] = a[x - 1];
                v++;
            }
        }
        writefln ("%(%s %)", ans);
    }
}

import std.stdio;
import std.algorithm;
import std.string;
import std.range;
import std.array;
import std.conv;
import std.complex;
import std.math;
import std.ascii;
import std.bigint;
import std.container;
import std.typecons;

auto readInt() {
	return readInts()[0];
}
auto readInts() {
	return array(map!(to!int)(readln().strip().split()));
}
auto readLong() {
	return readLongs()[0];
}
auto readLongs() {
	return array(map!(to!long)(readln().strip().split()));
}

void main() {
	auto n = readInt();
	auto a = readLongs();
	auto arev = a.dup;
	arev.reverse;
	a = -1 ~ a;
	arev = -1 ~ arev;
	int[] s = [0];
	auto t = new int[](n);
	auto u = new int[](n);
	foreach(i; 0..n) {
		while(!(a[s[$-1]] < a[i+1])) {
			s = s[0..$-1];
		}
		t[i] = i-s[$-1];
		s ~= i+1;
	}
	s = [0];
	foreach(i; 0..n) {
		while(!(arev[s[$-1]] < arev[i+1])) {
			s = s[0..$-1];
		}
		u[n-i-1] = i-s[$-1];
		s ~= i+1;
	}
	auto si = new long[](n);
	foreach(i; 0..n) {
		auto l = t[i]+u[i]+1;
		si[l-1] = max(si[l-1], a[i+1]);
	}
	foreach_reverse(i; 0..n-1) {
		si[i] = max(si[i+1], si[i]);
	}
	foreach(i; 0..n) {
		writeln(si[i]);
	}
}

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

void main ()
{
	int n;
	while (readf (" %s ", &n) > 0)
	{
		auto a = readln.split.map !(to !(int)).array;
		auto p = n.iota.array;
		auto prev = (n + 2).iota.array;
		prev[] -= 1;
		auto next = (n + 2).iota.array;
		next[] += 1;
		sort !((x, y) => a[x] > a[y], SwapStrategy.stable) (p);
		p[] += 1;
		auto ans = new int [n];
		int v = 0;
		foreach (x; p)
		{
			prev[next[x]] = prev[x];
			next[prev[x]] = next[x];
			int u = next[x] - prev[x] - 1;
			while (v < u)
			{
				ans[v] = a[x - 1];
				v++;
			}
		}

		ans.map !(text).join (" ").writeln;
	}
}

import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main ()
{
	int n;
	while (readf (" %s ", &n) > 0)
	{
		auto a = readln.split.map !(to !(int)).array;
		auto p = n.iota.array;
		auto prev = (n + 2).iota.array;
		auto next = prev.dup;
		++prev[];
		--next[];
		p.sort !((x, y) => a[x] > a[y]);
		++p[];
		auto ans = new int [n];
		int v = 0;
		foreach (x; p)
		{
			prev[next[x]] = prev[x];
			next[prev[x]] = next[x];
			while (v < next[x] - prev[x] - 1)
			{
				ans[v] = a[x - 1];
				v++;
			}
		}
		writefln ("%(%s %)", ans);
	}
}


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

alias S = Tuple!(int, "y", int, "x", int, "c");

void solve() {
    int N, M; readf("%d %d\n", &N, &M);
    auto F = N.iota.map!(_ => readln.chomp.split(" ").map!(to!int).array).array;

    bool C() {
        if ( (N + M - 1) % 2 == 1 ) return false;
        auto f = new int[][][](2, N, M);
        foreach (i; 0 .. N) {
            f[0][i][] = int.min;
            f[1][i][] = int.max;
        }
        f[0][0][0] = F[0][0];
        f[1][0][0] = F[0][0];
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                if (i + 1 < N) {
                    f[0][i+1][j] = max(f[0][i+1][j], f[0][i][j] + F[i+1][j]);
                    f[1][i+1][j] = min(f[0][i+1][j], f[1][i][j] + F[i+1][j]);
                }
                if (j + 1 < M) {
                    f[0][i][j+1] = max(f[0][i][j+1], f[0][i][j] + F[i][j+1]);
                    f[1][i][j+1] = min(f[1][i][j+1], f[1][i][j] + F[i][j+1]);
                }
            }
        }
        return f[1][N-1][M-1] <= 0 && 0 <= f[0][N-1][M-1];
    }

    writeln(C() ? "YES" : "NO");
}

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

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

void solve(){
    int n = scan!int;
    int m = scan!int;
    long[][] arr;
    auto dp = new long[][](n, m);
    for(int i = 0; i < n; ++i){
        arr ~= scanArray;
    }
    long num = n + m - 1;
    if(num % 2){
        writeln("NO");
        return;
    }
    for(int i = 0; i < n; ++i){
        for(int j = 0; j < m; ++j){
            if(i > 0 && j > 0){
                dp[i][j] = max(dp[i-1][j], dp[i][j-1]) + arr[i][j];
            }else if(i > 0){
                dp[i][j] = dp[i-1][j] + arr[i][j];
            }else if(j > 0){
                dp[i][j] = dp[i][j-1] + arr[i][j];
            }else{
                dp[i][j] = arr[i][j];
            }
        }
    }
    /* show(dp); */
    long maxx = dp[n-1][m-1];
    for(int i = 0; i < n; ++i){
        dp[i][] = 0;
    }
    for(int i = 0; i < n; ++i){
        for(int j = 0; j < m; ++j){
            if(i > 0 && j > 0){
                dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + arr[i][j];
            }else if(i > 0){
                dp[i][j] = dp[i-1][j] + arr[i][j];
            }else if(j > 0){
                dp[i][j] = dp[i][j-1] + arr[i][j];
            }else{
                dp[i][j] = arr[i][j];
            }
        }
    }
    /* show(dp); */
    long minn = dp[n-1][m-1];
    show(minn, maxx);
    assert(minn <= maxx);
    if(minn <= 0 && maxx >= 0){
        writeln("YES");
    }else{
        writeln("NO");
    }
}

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.stdio;
import std.math;
import std.container;
import std.typecons;
import core.stdc.stdio : scanf;
import std.conv;
import std.array;
import std.string;
import std.range;
import std.algorithm;

alias S = Tuple!(int, "y", int, "x", int, "c");

void solve() {
    int N, M; readf("%d %d\n", &N, &M);
    auto F = N.iota.map!(_ => readln.chomp.split(" ").map!(to!int).array).array;

    bool C() {
        if ( (N + M - 1) % 2 == 1 ) return false;
        auto f = new int[][][](2, N, M);
        f[0][0][0] = F[0][0];
        f[1][0][0] = F[0][0];
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                if (i + 1 < N) {
                    f[0][i+1][j] = max(f[0][i+1][j], f[0][i][j] + F[i+1][j]);
                    f[1][i+1][j] = min(f[0][i+1][j], f[1][i][j] + F[i+1][j]);
                }
                if (j + 1 < M) {
                    f[0][i][j+1] = max(f[0][i][j+1], f[0][i][j] + F[i][j+1]);
                    f[1][i][j+1] = min(f[1][i][j+1], f[1][i][j] + F[i][j+1]);
                }
            }
        }
        return f[0][N-1][M-1] <= 0 && 0 <= f[1][N-1][M-1];
    }

    writeln(C() ? "YES" : "NO");
}

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

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

void solve(){
    int n = scan!int;
    int m = scan!int;
    long[][] arr;
    auto dp = new long[][](n, m);
    for(int i = 0; i < n; ++i){
        auto ray = scanArray;
        arr ~= ray;
    }
    for(int i = 0; i < n; ++i){
        for(int j = 0; j < m; ++j){
            if(i > 0 && j > 0){
                dp[i][j] = max(dp[i-1][j], dp[i][j-1]) + arr[i][j];
            }else if(i > 0){
                dp[i][j] = dp[i-1][j] + arr[i][j];
            }else if(j > 0){
                dp[i][j] = dp[i][j-1] + arr[i][j];
            }else{
                dp[i][j] = arr[i][j];
            }
        }
    }
    show(dp);
    long maxx = dp[n-1][m-1];
    for(int i = 0; i < n; ++i){
        dp[i][] = 0;
    }
    for(int i = 0; i < n; ++i){
        for(int j = 0; j < m; ++j){
            if(i > 0 && j > 0){
                dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + arr[i][j];
            }else if(i > 0){
                dp[i][j] = dp[i-1][j] + arr[i][j];
            }else if(j > 0){
                dp[i][j] = dp[i][j-1] + arr[i][j];
            }else{
                dp[i][j] = arr[i][j];
            }
        }
    }
    show(dp);
    long minn = dp[n-1][m-1];
    show(minn, maxx);
    assert(minn <= maxx);
    if(minn <= 0 && maxx >= 0){
        writeln("YES");
    }else{
        writeln("NO");
    }
}

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

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

void solve(){
    int n = scan!int;
    int m = scan!int;
    long[][] arr;
    auto dp = new long[][](n, m);
    for(int i = 0; i < n; ++i){
        auto ray = scanArray;
        arr ~= ray;
    }
    for(int i = 0; i < n; ++i){
        for(int j = 0; j < m; ++j){
            if(i > 0 && j > 0){
                dp[i][j] = max(dp[i-1][j], dp[i][j-1]) + arr[i][j];
            }else if(i > 0){
                dp[i][j] = dp[i-1][j] + arr[i][j];
            }else if(j > 0){
                dp[i][j] = dp[i][j-1] + arr[i][j];
            }else{
                dp[i][j] = arr[i][j];
            }
        }
    }
    show(dp);
    long maxx = dp[n-1][m-1];
    /* for(int i = 0; i < n; ++i){ */
    /*     dp[i][] = 0; */
    /* } */
    for(int i = 0; i < n; ++i){
        for(int j = 0; j < m; ++j){
            if(i > 0 && j > 0){
                dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + arr[i][j];
            }else if(i > 0){
                dp[i][j] = dp[i-1][j] + arr[i][j];
            }else if(j > 0){
                dp[i][j] = dp[i][j-1] + arr[i][j];
            }else{
                dp[i][j] = arr[i][j];
            }
        }
    }
    show(dp);
    long minn = dp[n-1][m-1];
    show(minn, maxx);
    assert(minn <= maxx);
    if(minn <= 0 && maxx >= 0){
        writeln("YES");
    }else{
        writeln("NO");
    }
}

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

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

void solve(){
    int n = scan!int;
    int m = scan!int;
    long[][] arr;
    auto dp = new long[][](n, m);
    for(int i = 0; i < n; ++i){
        auto ray = scanArray;
        arr ~= ray;
    }
    for(int i = 0; i < n; ++i){
        for(int j = 0; j < m; ++j){
            if(i > 0 && j > 0){
                dp[i][j] = max(dp[i-1][j], dp[i][j-1]) + arr[i][j];
            }else if(i > 0){
                dp[i][j] = dp[i-1][j] + arr[i][j];
            }else if(j > 0){
                dp[i][j] = dp[i][j-1] + arr[i][j];
            }else{
                dp[i][j] = arr[i][j];
            }
        }
    }
    /* show(dp); */
    long maxx = dp[n-1][m-1];
    for(int i = 0; i < n; ++i){
        for(int j = 0; j < m; ++j){
            if(i > 0 && j > 0){
                dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + arr[i][j];
            }else if(i > 0){
                dp[i][j] = dp[i-1][j] + arr[i][j];
            }else if(j > 0){
                dp[i][j] = dp[i][j-1] + arr[i][j];
            }else{
                dp[i][j] = arr[i][j];
            }
        }
    }
    /* show(dp); */
    long minn = dp[n-1][m-1];
    show(maxx, minn);
    if(minn <= 0 && maxx >= 0){
        writeln("YES");
    }else{
        writeln("NO");
    }
}

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.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 s = RD!string;
	long ans = 1;
	int cnt;
	char last = '?';
	auto fib = new long[](10^^5+1);
	fib[0] = 1;
	fib[1] = 1;
	foreach (i; 2..fib.length)
	{
		fib[i] = fib[i-1];
		fib[i].moda(fib[i-2]);
	}
	foreach (c; s)
	{
		if (c == 'w' || c == 'm')
		{
			ans = 0;
			break;
		}

		if (c == last)
			++cnt;
		else
		{
			ans.modm(fib[cnt]);
			last = '?';
			cnt = 0;
			if (c == 'u' || c == 'n')
			{
				last = c;
				++cnt;
			}
		}
	}
	if (cnt != 0)
	{
		ans.modm(fib[cnt]);
	}

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

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

immutable long MOD = 10^^9 + 7;

void main() {
    auto S = readln.chomp;
    auto N = S.length.to!int;

    if (S.canFind('m') || S.canFind('w')) {
        writeln(0);
        return;
    }

    auto dp = new long[][](N+1, 2);
    dp[0][1] = 1;

    foreach (i; 0..N) {
        (dp[i+1][0] += dp[i][0] + dp[i][1]) %= MOD;
        (dp[i+1][1] += dp[i][0]) %= MOD;
    }

    long ans = 1;
    char prev = '*';
    int tmp = 0;

    foreach (i; 0..N) {
        if (S[i] == prev) {
            tmp += 1;
            continue;
        }
        if (prev == 'n' || prev == 'u') {
            ans = ans * (dp[tmp].sum % MOD) % MOD;
        }
        prev = S[i];
        tmp = 1;
    }

    if (S.back == 'n' || S.back == 'u') {
        ans = ans * (dp[tmp].sum % MOD) % MOD;
    }

    ans.writeln;
}

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

struct IntM(int q = 1_000_000_007) {
  alias N = IntM!q;
  private:
  int v;
  invariant () { assert (v >= 0 && v < q); }
  pure nothrow @nogc
  void fromInt (int m) {
    v = m % q;
    if (v < 0) {
      v += q;
    }
  }
  public:
  pure nothrow @nogc
  this (int m) {
    fromInt (m);
  }
  pure nothrow @nogc
  N opAssign (int m) {
    fromInt (m);
    return this;
  }
  pure nothrow @nogc
  N opUnary (string op : "-")() const {
    return N (-v);
  }
  pure nothrow @nogc
  ref N opUnary (string op : "++")() {
    if (++v >= q) {
      v -= q;
    }
    return this;
  }
  pure nothrow @nogc
  ref N opUnary (string op : "--")() {
    if (--v < 0) {
      v += q;
    }
    return this;
  }
  pure nothrow @nogc
  ref N opOpAssign (string op : "+")(in N rhs) {
    v = (v + rhs.v) % q;
    return this;
  }
  pure nothrow @nogc
  ref N opOpAssign (string op : "-")(in N rhs) {
    v = (v - rhs.v + q) % q;
    return this;
  }
  pure nothrow @nogc
  ref N opOpAssign (string op : "*")(in N rhs) {
    v = (v.to!(long) * rhs.v) % q;
    return this;
  }
  pure nothrow @nogc
  ref N opOpAssign (string op : "/")(in N rhs) {
    return this *= rhs.inversion ();
  }
  pure nothrow @nogc
  ref N opOpAssign (string op)(in int rhs) if (op == "+" || op == "-" || op == "*" || op == "/") {
    mixin ("return this " ~ op ~ "= N(rhs);");
  }
  pure nothrow @nogc
  N opBinary (string op)(in N rhs) const if (op == "+" || op == "-" || op == "*" || op == "/") {
    N t = this;
    mixin ("t " ~ op ~ "= rhs;");
    return t;
  }
  pure nothrow @nogc
  N opBinary (string op)(in int rhs) const if (op == "+" || op == "-" || op == "*" || op == "/") {
    mixin ("return this " ~ op ~ " N(rhs);");
  }
  pure nothrow @nogc
  N opBinaryRight (string op)(in int rhs) const if (op == "+" || op == "*") {
    mixin ("return this " ~ op ~ " N(rhs);");
  }
  pure nothrow @nogc
  int opCast(T : int)() const { return v; }
  pure nothrow @nogc
  int opCmp (const N rhs) const {
    if (v < rhs.v) {
      return -1;
    }
    if (v > rhs.v) {
      return 1;
    }
    return 0;
  }
  pure nothrow
  string toString() const { return v.text; }
  //a ^ x = this (mod q)
}
alias N = IntM!1_000_000_007;

void main() {
  auto s = readln.strip;
  debug stderr.writeln (s);
  immutable n = s.length;
  debug stderr.writeln (n);
  auto d = new N[n+1];
  d[n] = N (1);
  bool sol = true;
  foreach_reverse (i; 0 .. n) {
    d[i] = d[i+1];
    if (i + 1 < n && s[i] == s[i+1] && (s[i] == 'u' || s[i] == 'n')) {
      d[i] += d[i + 2];
    }
    if (s[i] == 'm' || s[i] == 'w') {
      sol = false;
    }
  }
  debug stderr.writeln (d);
  if (!sol) {
    d[0] = N (0);
  }
  writeln (d[0]);
}


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

  int n, m; rd(n, m);
  auto x=readln.split.to!(long[]);
  auto y=readln.split.to!(long[]);

  int cnt=0;
  for(int i=0, j=0, sx=0, sy=0; i<n; i++){
    sx+=x[i];
    while(j<m && (sy+=y[j])<sx) j++;
    if(sy==sx) sx=sy=0, j++, cnt++;
    else sy-=y[j];
  }

  writeln(cnt);
}

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

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

void Isle(T)( T küçükDizi, T büyükDizi, ref int kacKere )
{
	int hedef = büyükDizi.front();
	büyükDizi.popFront();
	int toplam = 0;
	while( true )
	{
		toplam += küçükDizi.front();
		if (toplam <= hedef )
			küçükDizi.popFront();

		if ( toplam >= hedef )
		{
			if ( küçükDizi.empty() || büyükDizi.empty() )
			{
				kacKere++;
				return;
			}
			else
			{
				if ( toplam > hedef)
					küçükDizi.front() = toplam - hedef;
				else
					kacKere++;

				if( küçükDizi.front > büyükDizi.front )
					Isle(büyükDizi, küçükDizi, kacKere);
				else
					Isle(küçükDizi, büyükDizi, kacKere);
			}
			return;
		}
	}
}

int main(string[] argv)
{
	auto girişDizisi = stdin.readln.to!dstring.chomp.split.map!( a => to!int(a) );
	auto ilkDizi = stdin.readln.to!dstring.chomp.split.map!( a => to!int(a) ).array;
	auto ikinciDizi = stdin.readln.to!dstring.chomp.split.map!( a => to!int(a) ).array;
	int sonuç = 0;
	if( ilkDizi.front > ikinciDizi.front )
		Isle(ikinciDizi, ilkDizi, sonuç);
	else
		Isle(ilkDizi, ikinciDizi, sonuç);
	writeln(sonuç);
    return 0;
}

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

void main()
{
	readln;

	auto a = readln.splitter.map!(to!uint).array;
	auto b = readln.splitter.map!(to!uint).array;

	uint cnt;

	while(a.length)
	{
		uint u = a[0], v = b[0];

		a.popFront;
		b.popFront;

		while(true)
		{
			if(u == v)
			{
				cnt++;
				break;
			}

			if(u > v)
			{
				v += b[0];
				b.popFront;
			}
			else
			{
				u += a[0];
				a.popFront;
			}
		}
	}

	cnt.writeln;
}

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

void main()
{
	readln;

	auto a = readln.splitter.map!(to!uint).array;
	auto b = readln.splitter.map!(to!uint).array;

	uint cnt;

	while(a.length)
	{
		uint u = a[0], v = b[0];

		a.popFront;
		b.popFront;

		while(true)
		{
			a.writeln;
			b.writeln;

			if(u == v)
			{
				cnt++;
				break;
			}

			if(u > v)
			{
				v += b[0];
				b.popFront;
			}
			else
			{
				u += a[0];
				a.popFront;
			}
		}
	}

	cnt.writeln;
}


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

void solve(){
    auto n = scan!int;
    auto arr = iota(1L, n+1L).array;
    long a = scan;
    long b = scan;
    if(abs(a - b) > 1 || a + b > n - 2){
        writeln(-1);
        return;
    }
    long swp = max(a, b);
    if(a > b){
        arr.reverse;
    }
    for(int i = 0; i+1 < n; i += 2){
        if(!swp){
            break;
        }
        swap(arr[i], arr[i+1]);
        --swp;
    }
    long ac = 0, bc = 0;
    for(int i = 1; i < n-1; ++i){
        if(arr[i] > arr[i-1] && arr[i] > arr[i+1]){
            ++ac;
        }else if( arr[i] < arr[i-1] && arr[i] < arr[i+1]){
            ++bc;
        }
    }
    if(ac == a && bc == b){
        for(int i = 0; i < n; ++i){
            write(arr[i], " ");
        }
        writeln;
        return;
    }else if (ac == a || bc == b){
        swap(arr[n-1], arr[n-2]);
        ac = 0; bc = 0;
        for(int i = 1; i < n-1; ++i){
            if(arr[i] > arr[i-1] && arr[i] > arr[i+1]){
                ++ac;
            }else if( arr[i] < arr[i-1] && arr[i] < arr[i+1]){
                ++bc;
            }
        }
    }
    if(ac == a && bc == b){
        for(int i = 0; i < n; ++i){
            write(arr[i], " ");
        }
        writeln;
    }else{
        show(arr);
        writeln(-1);
    }
}

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

/**************    *****    That's All Folks!    *****    **************/
import std.stdio, std.range, std.conv, std.typecons, std.algorithm, std.container, std.math;
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;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;

int [] solve (int n, int a, int b)
{
	if (abs (a - b) > 1)
	{
		return [-1];
	}
	if (a + b > n - 2)
	{
		return [-1];
	}
	if (a > b)
	{
		return solve (n, b, a).map !(x => n + 1 - x).array;
	}
	auto res = new int [n];
	int lo = 1;
	int hi = n;
	int pos = 0;
	if (b > a)
	{
		res[pos++] = hi--;
		res[pos++] = lo++;
		b -= 1;
	}
	else
	{
		res[pos++] = lo++;
	}
	while (a > 0 && b > 0)
	{
		res[pos++] = hi--;
		a -= 1;
		res[pos++] = lo++;
		b -= 1;
	}
	while (pos < n)
	{
		res[pos++] = lo++;
	}
	return res;
}

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		int n, a, b;
		readf !(" %s %s %s") (n, a, b);
		writefln !("%(%s %)") (solve (n, a, b));
	}
}

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

void solve(){
    auto n = scan!int;
    auto arr = iota(1L, n+1L).array;
    long a = scan;
    long b = scan;
    if(abs(a - b) > 1){
        writeln(-1);
        return;
    }
    int st = (a > b);
    long swp = max(a, b);
    for(int i = st; i+1 < n; i += 2){
        if(!swp){
            break;
        }
        swap(arr[i], arr[i+1]);
        --swp;
    }
    if(swp > 0){
        writeln(-1);
        return;
    }
    long ac = 0, bc = 0;
    for(int i = 1; i < n-1; ++i){
        if(arr[i] > arr[i-1] && arr[i] > arr[i+1]){
            ++ac;
        }else if( arr[i] < arr[i-1] && arr[i] < arr[i+1]){
            ++bc;
        }
    }
    if(ac == a && bc == b){
        for(int i = 0; i < n; ++i){
            write(arr[i], " ");
        }
        writeln;
        return;
    }else if (ac == a || bc == b){
        swap(arr[n-1], arr[n-2]);
        ac = 0; bc = 0;
        for(int i = 1; i < n-1; ++i){
            if(arr[i] > arr[i-1] && arr[i] > arr[i+1]){
                ++ac;
            }else if( arr[i] < arr[i-1] && arr[i] < arr[i+1]){
                ++bc;
            }
        }
    }
    if(ac == a && bc == b){
        for(int i = 0; i < n; ++i){
            write(arr[i], " ");
        }
        writeln;
    }else{
        writeln(-1);
    }
}

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

/**************    *****    That's All Folks!    *****    **************/
import std.stdio, std.range, std.conv, std.typecons, std.algorithm, std.container, std.math;
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

int[][] divs;
const int sz = 200002;
void preprocess(){
    divs = new int[][sz];
    for(int k = 1; k < sz; ++k){
        for(int m = 2*k; m < sz; m += k){
            divs[m] ~= k;
        }
    }
}

void solve(){
    auto n = scan!int;
    auto arr = scanArray!int;

    auto cnt = new long[](sz);
    auto push = new long[](sz);
    long maxcnt = 0;

    foreach(el; arr){
        ++cnt[el];
    }
    auto rbt = redBlackTree!int(arr);

    auto uniqs = rbt.array.sort.reverse;
    foreach(el; uniqs){
        foreach(divi; divs[el]){
            push[divi] = max(push[el] + cnt[el], push[divi]);
        }
        maxcnt = max(push[el] + cnt[el], maxcnt);
    }
    writeln(n - maxcnt);
}

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

/**************    *****    That's All Folks!    *****    **************/
import std.stdio, std.range, std.conv, std.typecons, std.algorithm, std.container, std.math, std.numeric;
string[] tk; 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, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range;

void get(Args...)(ref Args args)
{
    import std.traits, std.meta, std.typecons;

    static if (Args.length == 1) {
        alias Arg = Args[0];

        static if (isArray!Arg) {
          static if (isSomeChar!(ElementType!Arg)) {
            args[0] = readln.chomp.to!Arg;
          } else {
            args[0] = readln.split.to!Arg;
          }
        } else static if (isTuple!Arg) {
            auto input = readln.split;
            static foreach (i; 0..Fields!Arg.length) {
                args[0][i] = input[i].to!(Fields!Arg[i]);
            }
        } else {
            args[0] = readln.chomp.to!Arg;
        }
    } else {
        auto input = readln.split;
        assert(input.length == Args.length);

        static foreach (i; 0..Args.length) {
            args[i] = input[i].to!(Args[i]);
        }
    }
}

void get_lines(Args...)(size_t N, ref Args args)
{
    import std.traits, std.range;

    static foreach (i; 0..Args.length) {
        static assert(isArray!(Args[i]));
        args[i].length = N;
    }

    foreach (i; 0..N) {
        static if (Args.length == 1) {
            get(args[0][i]);
        } else {
            auto input = readln.split;
            static foreach (j; 0..Args.length) {
                args[j][i] = input[j].to!(ElementType!(Args[j]));
            }
        }
    }
}

void main()
{
    int T; get(T);
    while (T--) {
        int N; get(N);
        auto memo = new int[](10^^5 * 2 + 1);
        foreach (a; readln.split) memo[a.to!int] += 1;

        auto DP = new int[](10^^5 * 2 + 1);
        foreach (i; 1..10^^5 * 2 + 1) {
            DP[i] += memo[i];
            auto j = i * 2;
            while (j <= 10^^5 * 2) {
                DP[j] = max(DP[i], DP[j]);
                j += i;
            }
        }

        writeln(N - DP.maxElement());
    }
}