immutable multi = true;

void solve(int)
{
	int n = readInt!int;
	assert(n == 1);
	int m = readInt!int;
	assert(m >= 0 && m <= 300);
	auto a = ma(m, readInt!long);
	int[long] cnt;
	foreach(i, ai; a) cnt[ai]++;
	auto sa = a.dup;
	sort(sa);
	auto ua = sa.uniq.array;
	int[][long] poss;
	{
	int i = 0;
	foreach(j, ai; ua)
	{
		poss[ai] = null;
		foreach(k; 0 .. cnt[ai])
		{
			poss[ai] ~= i++;
		}
	}
	}
	auto used = new long[](m);
	long ans = 0;
	foreach(i, ai; a)
	{
		auto pos = poss[ai][$-1];
		poss[ai] = poss[ai][0 .. $ - 1];
		foreach(j; 0 .. pos)
		{
			ans += used[j];
		}
		used[pos] = 1;
	}
	ans.writeln;
}

//{{{
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.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

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

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

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }

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

long compress(in long[] arr, out long[long] zip, out long[] unzip)
{
	auto index = MAKE_IDX(arr);
	long last = arr[index[0]], x;
	zip[last] = x;
	unzip ~= last;
	foreach (i; index[1..$])
	{
		if (arr[i] == last) continue;
		last = arr[i];
		++x;
		zip[last] = x;
		unzip ~= last;
	}
	return x+1;
}

struct SegTree(T)
{
	T delegate(const(T), const(T)) f;
	T[] data;
	T init;
    this(T[] _data, T delegate(const(T), const(T)) _f, T _init)
    {
		f = _f; init = _init;
		size_t n = 1; while (n < _data.length) n *= 2; data.length = n*2-1;
		foreach (i; 0.._data.length) data[i+n-1] = _data[i];
		foreach (i; _data.length..n) data[i+n-1] = _init;
		foreach_reverse (i; 0..n-1) data[i] = f(data[i*2+1], data[i*2+2]);
	}
    T query(size_t l, size_t r)
	{
        size_t n = (data.length+1) / 2; l += n-1; r += n-1; T res = init;
        while (l < r)
		{
            if ((l & 1) == 0) res = f(res, data[l]);
            if ((r & 1) == 0) res = f(res, data[r-1]);
            l = l/2; r = (r-1)/2;
        }
        return res;
    }
	void update(size_t i, T v)
	{
		i += (data.length+1) / 2 - 1; data[i] = v;
		while (i != 0) { i = (i-1)/2; data[i] = f(data[i*2+1], data[i*2+2]); }
    }
}

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

		long[long] zip;
		long[] unzip;
		auto len = compress(a, zip, unzip);

		long[] b;
		foreach (i; 0..m)
		{
			b ~= zip[a[i]];
		}

		auto dummy = new long[](cast(int)len);
		auto st = new SegTree!long(dummy, (in long x, in long y)=>x+y, 0);
		foreach (i; 0..m)
		{
			ans[ti] += st.query(0, cast(int)b[i]);
			auto cnt = st.query(cast(int)b[i], cast(int)b[i]+1);
			st.update(cast(int)b[i], cnt+1);
		}
	}

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

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

int lb = -1;

struct S { int v; int[] a; };

S op(ref S x, ref S y)
{
  if (lb < 0) {
    return S(0, (x.a ~ y.a).sort.array);
  } else {
    auto cnt1 = x.a.assumeSorted.lowerBound(lb).length;
    auto cnt2 = y.a.assumeSorted.lowerBound(lb).length;
    return S(x.v + y.v + cast(int)(cnt1 + cnt2), []);
  }
}

S e()
{
  S tmp;
  return tmp;
}

void main()
{
  int t;
  readf!" %d "(t);
  while (t--) {
    int n, m;
    readf!" %d %d "(n, m);
    auto a = readln.strip.split.map!(to!int).array;
    auto b = zip(iota(n * m), a).array.sort!((x, y) => x[1] == y[1] ? x[0] > y[0] : x[1] < y[1]).array;
    S[] c;
    foreach (x ; b)
      c ~= S(0, [x[0]]);

    lb = -1;
    auto seg = c.Segtree!(S, op, e);
    long ans = 0;
    foreach (i ; 0 .. n * m) {
      lb = b[i][0];
      int cnt = seg.prod(0, i).v;
      ans += cnt;
    }

    writeln(ans);
  }
}

// Code from: https://github.com/kotet/atcoder-library-d

int celiPow2(int n) @safe pure nothrow @nogc
{
    int x = 0;
    while ((1u << x) < cast(uint)(n))
        x++;
    return x;
}

// --- segtree ---

struct Segtree(S, alias op, alias e)
{
    import std.functional : binaryFun, unaryFun;
    import std.traits : isCallable, Parameters;

    static if (is(typeof(e) : string))
    {
        auto unit()
        {
            return mixin(e);
        }
    }
    else
    {
        alias unit = e;
    }

    this(int n)
    {
        auto buf = new S[](n);
        buf[] = unit();
        this(buf);
    }

    this(S[] v)
    {
        _n = cast(int) v.length;
        log = celiPow2(_n);
        size = 1 << log;
        d = new S[](2 * size);
        d[] = unit();
        foreach (i; 0 .. _n)
            d[size + i] = v[i];
        foreach_reverse (i; 1 .. size)
            update(i);
    }

    void set(int p, S x)
    {
        assert(0 <= p && p < _n);
        p += size;
        d[p] = x;
        foreach (i; 1 .. log + 1)
            update(p >> i);
    }

    S get(int p)
    {
        assert(0 <= p && p < _n);
        return d[p + size];
    }

    S prod(int l, int r)
    {
        assert(0 <= l && l <= r && r <= _n);
        S sml = unit(), smr = unit();
        l += size;
        r += size;
        while (l < r)
        {
            if (l & 1)
                sml = binaryFun!(op)(sml, d[l++]);
            if (r & 1)
                smr = binaryFun!(op)(d[--r], smr);
            l >>= 1;
            r >>= 1;
        }
        return binaryFun!(op)(sml, smr);
    }

    S allProd()
    {
        return d[1];
    }

    int maxRight(alias f)(int l)
    {
        return maxRight(l, &unaryFun!(f));
    }

    int maxRight(F)(int l, F f) if (isCallable!F && Parameters!(F).length == 1)
    {
        assert(0 <= l && l <= _n);
        assert(f(unit()));
        if (l == _n)
            return _n;
        l += size;
        S sm = unit();
        do
        {
            while (l % 2 == 0)
                l >>= 1;
            if (!f(binaryFun!(op)(sm, d[l])))
            {
                while (l < size)
                {
                    l = 2 * l;
                    if (f(binaryFun!(op)(sm, d[l])))
                    {
                        sm = binaryFun!(op)(sm, d[l]);
                        l++;
                    }
                }
                return l - size;
            }
            sm = binaryFun!(op)(sm, d[l]);
            l++;
        }
        while ((l & -l) != l);
        return _n;
    }

    int minLeft(alias f)(int r)
    {
        return minLeft(r, &unaryFun!(f));
    }

    int minLeft(F)(int r, F f) if (isCallable!F && Parameters!(F).length == 1)
    {
        assert(0 <= r && r <= _n);
        assert(f(unit()));
        if (r == 0)
            return 0;
        r += size;
        S sm = unit();
        do
        {
            r--;
            while (r > 1 && (r % 2))
                r >>= 1;
            if (!f(binaryFun!(op)(d[r], sm)))
            {
                while (r < size)
                {
                    r = 2 * r + 1;
                    if (f(binaryFun!(op)(d[r], sm)))
                    {
                        sm = binaryFun!(op)(d[r], sm);
                        r--;
                    }
                }
                return r + 1 - size;
            }
            sm = binaryFun!(op)(d[r], sm);
        }
        while ((r & -r) != r);
        return 0;
    }

private:
    int _n = 0, size = 1, log = 0;
    S[] d = [unit(), unit()];
    void update(int k)
    {
        d[k] = binaryFun!(op)(d[2 * k], d[2 * k + 1]);
    }
}

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		int n, m;
		readf !(" %s %s") (n, m);
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		auto b = iota (n * m).map !(i => tuple (a[i], i)).array;
		b.schwartzSort !(c => tuple (c[0], -c[1]));
		debug {writeln (b);}

		long res = 0;
		auto vis = new bool [] [] (n, m);
		foreach (ref c; b)
		{
			auto row = c[1] / m;
			auto col = c[1] % m;
			foreach (d; 0..col)
			{
				res += vis[row][d];
			}
			vis[row][col] = true;
		}
		writeln (res);
	}
}


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.range;
import std.stdio;
import std.string;
import std.typecons;
import std.range.interfaces;

void main()
{
    readln;

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

    auto ans = zip(StoppingPolicy.longest, arr, arr.dropOne).filter!(t => t[1] == 1 || t[1] == 0).map!(t => t[0]).array;

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

import std.stdio;
int main()
{
	int a=0;
	readf(" %d", &a);
	int[] m = new int[a];
	for (int i=0; i<a; i++)
	{
		readf(" %d", &m[i]);
	}
	int count=0;
	for (int i=0; i<a; i++)
	{
		if (m[i]==1)
		{
			count=count+1;
		}
	}
	writeln(count);
	for (int i=1; i<a; i++)
	{
		if (m[i]==1)
		{
			writeln(m[i-1]);
		}
	}
	writeln(m[a-1]);
	return 0;
}


// Cheese-Cracker: cheese-cracker.github.io
const int INF = 10^^9 + 300;
tup[][] adj;
int[][] dis;

void djkes(int v){
    int cur;
    auto cask = rbt!tup;
    cask.insert(tup(0, v));
    dis[v][v] = 0;

    while(cask.length){
        cur = cask.front[1];
        cask.removeFront;
        foreach(ed; adj[cur]){
            int u = ed[0].to!int, w = ed[1].to!int;
            /* writeln(u, " ", w); */
            if(dis[v][u] > dis[v][cur] + w){
                cask.removeKey(tup(dis[v][u], u));
                dis[v][u] = dis[v][cur] + w;
                cask.insert(tup(dis[v][u], u));
            }
        }
    }
}

void play(){
    int n = rd!int, m = rd!int, k = rd!int;
    dis = new int[][](n+1, n+1);
    adj = new tup[][](n+1);
    tup[] routes, roads;
    int x, y, w;
    foreach(i; 0..m){
        x = rd!int;
        y = rd!int;
        w = rd!int;
        roads ~= tup(x, y);
        adj[x] ~= tup(y, w);
        adj[y] ~= tup(x, w);
    }
    foreach(i; 1..n+1){
        dis[i].fill(INF);
        djkes(i);
    }
    /* writeln(dis); */
    foreach(i; 0..k){
        routes ~= tup(rd!int, rd!int);
    }
    ll mindist = INF;
    foreach(i; 0..m){
        int st = roads[i][0];
        int end = roads[i][1];
        ll totdist = 0;
        foreach(j; 0..k){
            int rst = routes[j][0];
            int rend = routes[j][1];
            ll dist = dis[rst][rend];
            dist = min(dist, dis[rst][st] + dis[rend][end]);
            dist = min(dist, dis[rst][end] + dis[rend][st]);
            /* writeln(j, " ", dist); */
            totdist += dist;
        }
        mindist = min(totdist, mindist);
    }
    writeln(mindist);
}

int main(){
    long t = 1;
    /* t = rd;        // Toggle! */
    while(t--) play();  // Let's play!
    stdout.flush;
    return 0;
}

/**********It's A Me Mario!**********/
import std.stdio, std.conv, std.functional, std.string, std.algorithm;
import std.container, std.range, std.typecons, std.numeric, std.math, std.random;
static string[] inp;
T rd(T = long)(){while(!inp.length) inp = readln.chomp.split; string a = inp[0]; inp.popFront; return a.to!T;}
T[] rdarr(T = long)(){ auto r = readln.chomp.split.to!(T[]); return r; }
void show(A...)(A a) { debug{ a.each!(w => write(w, "| ")); writeln; } }
alias ll = long;
alias rbt = redBlackTree;
alias tup = Tuple!(int, int);
alias trip = Tuple!(int, int, int);
T max(T = long)(T a, T b){ return (a > b) ? a : b; }
T min(T = long)(T a, T b){ return (a < b) ? a : b; }

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

immutable int infinity = int.max / 2;

void main ()
{
	int n, m, k;
	while (readf !(" %s %s %s") (n, m, k) > 0)
	{
		alias Edge = Tuple !(int, q{u}, int, q{v}, int, q{w});
		auto adj = new int [] [n];
		auto edges = new Edge [m];
		foreach (int j, ref e; edges)
		{
			readf !(" %s %s %s") (e.u, e.v, e.w);
			e.u -= 1;
			e.v -= 1;
			adj[e.u] ~= j;
			adj[e.v] ~= j;
		}
		auto x = new int [k];
		auto y = new int [k];
		foreach (i; 0..k)
		{
			readf !(" %s %s") (x[i], y[i]);
		}
		x[] -= 1;
		y[] -= 1;

		int [] [] dist;
		foreach (i; 0..n)
		{
			auto d = new int [n];
			d[] = infinity;
			d[i] = 0;
			alias Record = Tuple !(int, q{d}, int, q{v});
			auto t = redBlackTree !(Record);
			t.insert (Record (d[i], i));
			while (!t.empty)
			{
				auto v = t.front.v;
				t.removeFront ();
				foreach (j; adj[v])
				{
					auto e = edges[j];
					auto u = e.u ^ e.v ^ v;
					if (d[u] > d[v] + e.w)
					{
						t.removeKey (Record (d[u], u));
						d[u] = d[v] + e.w;
						t.insert (Record (d[u], u));
					}
				}
			}
			dist ~= d;
		}

		int res = infinity;
		foreach (e; edges)
		{
			int cur = 0;
			foreach (i; 0..k)
			{
				auto temp = dist[x[i]][y[i]];
				temp = min (temp,
				    dist[x[i]][e.u] + dist[y[i]][e.v]);
				temp = min (temp,
				    dist[x[i]][e.v] + dist[y[i]][e.u]);
				cur += temp;
			}
			res = min (res, cur);
		}

		writeln (res);
	}
}

// Cheese-Cracker: cheese-cracker.github.io
const int INF = 10^^8;
tup[][] adj;
int[][] dis;

void djkes(int v){
    int cur;
    auto cask = rbt!tup;
    cask.insert(tup(0, v));
    dis[v][v] = 0;

    while(cask.length){
        cur = cask.front[1];
        cask.removeFront;
        foreach(ed; adj[cur]){
            int u = ed[0].to!int, w = ed[1].to!int;
            /* writeln(u, " ", w); */
            if(dis[v][u] > dis[v][cur] + w){
                cask.removeKey(tup(dis[v][u], u));
                dis[v][u] = dis[v][cur] + w;
                cask.insert(tup(dis[v][u], u));
            }
        }
    }
}

void play(){
    int n = rd!int, m = rd!int, k = rd!int;
    dis = new int[][](n+1, n+1);
    adj = new tup[][](n+1);
    tup[] routes, roads;
    int x, y, w;
    foreach(i; 0..m){
        x = rd!int;
        y = rd!int;
        w = rd!int;
        roads ~= tup(x, y);
        adj[x] ~= tup(y, w);
        adj[y] ~= tup(x, w);
    }
    foreach(i; 1..n+1){
        dis[i].fill(INF);
        djkes(i);
    }
    /* writeln(dis); */
    foreach(i; 0..k){
        routes ~= tup(rd!int, rd!int);
    }
    ll mindist = INF;
    foreach(i; 0..m){
        int st = roads[i][0];
        int end = roads[i][1];
        ll totdist = 0;
        foreach(j; 0..k){
            int rst = routes[j][0];
            int rend = routes[j][1];
            ll dist = dis[rst][rend];
            dist = min(dist, dis[rst][st] + dis[rend][end]);
            dist = min(dist, dis[rst][end] + dis[rend][st]);
            /* writeln(j, " ", dist); */
            totdist += dist;
        }
        mindist = min(totdist, mindist);
    }
    writeln(mindist);
}

int main(){
    long t = 1;
    /* t = rd;        // Toggle! */
    while(t--) play();  // Let's play!
    stdout.flush;
    return 0;
}

/**********It's A Me Mario!**********/
import std.stdio, std.conv, std.functional, std.string, std.algorithm;
import std.container, std.range, std.typecons, std.numeric, std.math, std.random;
static string[] inp;
T rd(T = long)(){while(!inp.length) inp = readln.chomp.split; string a = inp[0]; inp.popFront; return a.to!T;}
T[] rdarr(T = long)(){ auto r = readln.chomp.split.to!(T[]); return r; }
void show(A...)(A a) { debug{ a.each!(w => write(w, "| ")); writeln; } }
alias ll = long;
alias rbt = redBlackTree;
alias tup = Tuple!(int, int);
alias trip = Tuple!(int, int, int);
T max(T = long)(T a, T b){ return (a > b) ? a : b; }
T min(T = long)(T a, T b){ return (a < b) ? a : b; }


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 totalsum = a.sum;
    writeln(totalsum % n == 0 ? 0 : 1);
  }
}

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto a = readln.splitter.map !(to !(int)).array;
		auto s = a.sum;
		writefln !("%d") (!!(s % n));
	}
}

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

        const sumA = A.sum;
        writeln((sumA % N == 0) ? 0 : 1);
      }
    }
  } catch (EOFException e) {
  }
}

immutable multi = true;

void solve(int tc)
{
  auto n = readInt!int;
  auto a = ma(n, readInt!int);
  auto s = a.sum;
  if (s % n == 0) return writeln(0);
  return writeln(1);
}

// 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.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

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

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

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
double euDist(T)(T[] a, T[] b) { auto c = a.dup; c[] -= b[]; c[] *= c[]; return sqrt(cast(double)c.sum); }
double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }
double norm(double[] vec) { return sqrt(reduce!((a,b)=>a+b*b)(0.0, vec)); }
double dotProd(double[] a, double[] b) { auto r = a.dup; r[] *= b[]; return r.sum; }

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

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

		auto tot = a.sum;
		ans[ti] = tot % n ? 1 : 0;
	}

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


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

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

    auto mxle = tuple(-1, -1);
    auto mnr = tuple(10 ^^ 9, 10 ^^ 9);
    Tuple!(int, int)[] segs;
    foreach (_; 0 .. n) {
        int a, b;
        readf("%s %s", &a, &b);
        readln;
        segs ~= tuple(a, b);

        if (a > mxle[0]) mxle[1] = mxle[0], mxle[0] = a;
        else if (a > mxle[1]) mxle[1] = a;

        if (b < mnr[0]) mnr[1] = mnr[0], mnr[0] = b;
        else if (b < mnr[1]) mnr[1] = b;
    }

    debug { segs.writeln; writeln(mxle, ' ', mnr); }

    auto ans = 0;
    foreach (sg; segs) {
        auto x = sg[0], y = sg[1];
        auto cur = (y != mnr[0] ? mnr[0] : mnr[1]) - (x != mxle[0] ? mxle[0] : mxle[1]);
        if (cur < 0) cur = 0;
        ans = max(ans, cur);
    }

    ans.writeln;
}

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

  int n; rd(n);
  struct T{int l, r;}
  auto s=new T[](n);
  foreach(i; 0..n) rd(s[i].l, s[i].r);

  int mxlen=0;
  s.sort!"a.l==b.l ? a.r<b.r : a.l>b.l";
  // writeln(s[0]);
  mxlen=max(mxlen, s[1..$].minElement!("a.r").r-s[1..$].maxElement!("a.l").l);
  s.sort!"a.r==b.r ? a.l>b.l : a.r<b.r";
  // writeln(s[0]);
  mxlen=max(mxlen, s[1..$].minElement!("a.r").r-s[1..$].maxElement!("a.l").l);
  writeln(mxlen);
}

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


import std.conv;
import std.stdio;

void main() {
    int a, b, n;

    readf("%s %s %s", &a, &b, &n);

    if( a%b != 0 ) {
        --n;
        auto tmp = a * 10;
        bool f;
        for( int i = 0; i < 10; ++i ) {
            if( ( tmp + i ) % b == 0 )
            {
                a = tmp + i;
                f = true;
                break;
            }
        }
        if(!f) {
            writeln("-1");
            return;
        }
    }

    string as = to!string(a);

    for( int i; i < n; ++i ) {
        bool f;
        /*foreach(x; 1..10) {
            if( x % b == 0 ) {
                as ~= to!string(x);
                f = true;
                break;
            }
        }
        if(!f) {
            i = n;
            as = "-1";
        }*/
        as ~= "0";
    }

    writeln(as);

}

module cf_260A;

import std.stdio;
import std.conv;
import std.algorithm;

void main() {
    int a, b, n;

    readf("%d %d %d", &a, &b, &n);

    int mod = a % b;
    int ops = 0;

    string resultNumber = to!string(a);
    for (int i = 0; i < n; ++i) {
        for (int j = min(9, b); j >= 0; --j) {
            if ((mod * 10 + j) % b == 0) {
                resultNumber ~= j + '0';
                mod = 0;
                ++ops;
                break;
            }
        }
    }

    writeln(ops == n? resultNumber: "-1");
}

import std.conv;
import std.stdio;

void main() {
    int a, b, n;

    readf("%s %s %s", &a, &b, &n);

    if( a%b != 0 ) {
        --n;
        auto tmp = a * 10;
        bool f;
        for( int i = 1; i < 10; ++i ) {
            if( ( tmp + i ) % b == 0 )
            {
                a = tmp + i;
                f = true;
                break;
            }
        }
        if(!f) {
            writeln("-1");
            return;
        }
    }

    string as = to!string(a);

    for( int i; i < n; ++i ) {
        bool f;
        /*foreach(x; 1..10) {
            if( x % b == 0 ) {
                as ~= to!string(x);
                f = true;
                break;
            }
        }
        if(!f) {
            i = n;
            as = "-1";
        }*/
        as ~= "0";
    }

    writeln(as);

}

import std.conv;
import std.stdio;

void main() {
    int a, b, n;

    readf("%s %s %s", &a, &b, &n);

    if( a%b != 0 ) {
        --n;
        auto tmp = a * 10;
        bool f;
        for( int i = 1; i < 10; ++i ) {
            if( ( tmp + i ) % b == 0 )
            {
                a = tmp + i;
                f = true;
                break;
            }
        }
        if(!f) {
            writeln("-1");
            return;
        }
    }

    string as = to!string(a);

    for( int i; i < n; ++i ) {
        bool f;
        foreach(x; 1..10) {
            if( x % b == 0 ) {
                as ~= to!string(x);
                f = true;
                break;
            }
        }
        if(!f) {
            i = n;
            as = "-1";
        }
    }

    writeln(as);

}


// Try Codeforces
// author: Leonardone @ NEETSDKASU
import std.stdio, std.array, std.conv, std.string, std.algorithm;

auto gets() { return readln().chomp(); }
auto getV(T)() { return to!T(readln().chomp()); }
auto getVals(T)() { return to!(T[])(readln().chomp().split(" ")); }

auto isOdd(int p) { return p % 2 != 0; }

void main() {
    int n = getV!int();
    auto xs = getVals!int();
    auto odds = 0;
    foreach (x ; xs) {
        if (isOdd(x)) {
            odds++;
        }
    }
    if (odds == 0) {
        writeln("Second");
        return;
    }
    writeln("First");
}

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

      bool ans;
      if (A.sum % 2 != 0) {
        ans = true;
      } else {
        if (A.any!"a % 2 != 0") {
          ans = true;
        } else {
          ans = false;
        }
      }
      writeln(ans ? "First" : "Second");
    }
  } catch (EOFException e) {
  }
}


import std.stdio;
import std.algorithm;
import core.stdcpp.allocator;

const int N = cast(int)2e5 + 10;

int n;
int [N] lst;

void main() {
  int t;
  readf(" %s", &t);
  while(t-- > 0) {
    readf(" %s", &n);
    for(int i = 0; i < n; ++i) {
      lst[i] = -1;
    }
    int bst = n + 1;
    for(int i = 0; i < n; ++i) {
      int x;
      readf(" %s", &x); x--;
      if(lst[x] != -1) bst = min(bst, i - lst[x] + 1);
      lst[x] = i;
    }
    if(bst == n + 1) bst = -1;
    writefln("%s", bst);
  }
}

import std.stdio;
import std.algorithm;
import core.stdcpp.allocator;

int n;
int [int] lst;

void main() {
  int t;
  readf(" %s", &t);
  while(t-- > 0) {
    readf(" %s", &n);
    for(int i = 0; i < n; ++i) {
      lst[i] = -1;
    }
    int bst = n + 1;
    for(int i = 0; i < n; ++i) {
      int x;
      readf(" %s", &x); x--;
      if(lst[x] != -1) bst = min(bst, i - lst[x] + 1);
      lst[x] = i;
    }
    if(bst == n + 1) bst = -1;
    writefln("%s", bst);
  }
}

import std.stdio;
import std.algorithm;

void main() {
  int t;
  readf(" %s", &t);
  while(t-- > 0) {
    int n;
    readf(" %s", &n);
    int [] lst = new int[n];
    for(int i = 0; i < n; ++i) {
      lst[i] = -1;
    }
    int bst = n + 1;
    for(int i = 0; i < n; ++i) {
      int x;
      readf(" %s", &x); x--;
      if(lst[x] != -1) bst = min(bst, i - lst[x] + 1);
      lst[x] = i;
    }
    if(bst == n + 1) bst = -1;
    writefln("%s", bst);
  }
}

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

string FMT_F = "%.10f";

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

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

bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * (y / gcd(x, y)); }

//long mod = 10^^9 + 7;
long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }

void main()
{
	auto t = RD!int;
	auto ans = new long[](t);
	foreach (i; 0..t)
	{
		auto n = RD!int;
		auto a = RDA(-1);
		auto pos = new size_t[](n);
		pos[] = -1;
		ans[i] = long.max;
		foreach (j, e; a)
		{
			if (pos[e] != -1)
			{
				ans[i] = min(ans[i], j - pos[e] + 1);
			}
			pos[e] = j;
		}
		if (ans[i] == long.max)
			ans[i] = -1;
	}

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


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

string FMT_F = "%.10f";

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

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

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

//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { 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 q = RD!int;
		auto a = RDA;

		auto dp = new long[][](n+1, 2);
		foreach (i; 0..n)
		{
			dp[i+1][0].chmax(dp[i][0]);
			dp[i+1][1].chmax(dp[i][1]);
			dp[i+1][0].chmax(dp[i][1] - a[i]);
			dp[i+1][1].chmax(dp[i][0] + a[i]);
		}
		ans[ti] = max(dp[n][0], dp[n][1]);
	}

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

// Cheese-Cracker: cheese-cracker.github.io
import std.stdio, std.conv, std.functional, std.string, std.algorithm;
import std.container, std.range, std.typecons, std.numeric, std.math, std.random;

alias ll = long;
alias rbt = redBlackTree;
alias tup = Tuple!(long, long);
static string[] inp;
T rd(T = long)(){while(!inp.length) inp = readln.chomp.split; string a = inp[0]; inp.popFront; return a.to!T;}
T[] rdarr(T = long)(T fix = 0){ auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
void show(A...)(A a) { debug{ foreach(t; a){ write(t, "| "); } writeln; } }
/*******************************It's a Me, Mario!*******************************************/

void play(){
    int n, q;
    n = rd!int;
    q = rd!int;
    ll[] arr = rdarr;
    if(n == 1){ writeln(arr[0]); return;}
    ll[] res;
    bool side = (arr[1] > arr[0]), nside;
    if(!side){ res ~= arr[0]; }
    foreach(i; 1..n){
        if(arr[i] > arr[i-1]){
            nside = 1;
        }else{
            nside = 0;
        }
        if(nside != side){
            res ~= arr[i-1];
            side = nside;
        }
    }
    if(side){ res ~= arr[n-1]; }
    /* writeln(res); */
    ll pow = 0;
    foreach(i; 0..res.length){
        if(i % 2 == 0){
            pow += res[i];
        }else if(i != res.length-1){
            pow -= res[i];
        }
    }
    writeln(pow);
}

int main(){
    long t = 1;
    t = rd;        // Toggle!
    while(t--) play();  // Let's play!
    stdout.flush;
    return 0;
}

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) {
            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 solve()
{
    int N, Q; get(N, Q);
    long[] AS; get(AS);
    int p;
    foreach (i, a; AS) if (a == N) {
        p = i.to!int;
        break;
    }
    long sum_a = N;
    long min_a, max_a;
    void update(long a) {
        if (min_a == -1) {
            if (a < max_a) {
                sum_a += max_a;
                max_a = -1;
                min_a = a;
            } else {
                max_a = a;
            }
        } else {
            if (a > min_a) {
                sum_a -= min_a;
                min_a = -1;
                max_a = a;
            } else {
                min_a = a;
            }
        }
    }
    if (p < N-1) {
        max_a = -1; min_a = AS[p+1];
        foreach (i; p+2..N) update(AS[i]);
        if (max_a != -1) sum_a += max_a;
    }
    if (p > 0) {
        max_a = -1; min_a = AS[p-1];
        foreach_reverse (i; 0..p-1) update(AS[i]);
        if (max_a != -1) sum_a += max_a;
    }
    writeln(sum_a);
}

void main()
{
    int T; get(T);
    foreach (_; 0..T) solve();
}

import std.algorithm;
import std.container;
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 n, q;
		readf !(" %s %s") (n, q);
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		long total = 0;

		void mark (int i, int delta)
		{
			if (i < 0 || i >= n)
			{
				return;
			}
			if ((i == 0 || a[i] > a[i - 1]) &&
			    (i == n - 1 || a[i] > a[i + 1]))
			{
				total += a[i] * delta;
			}
			if ((i > 0 && a[i] < a[i - 1]) &&
			    (i < n - 1 && a[i] < a[i + 1]))
			{
				total -= a[i] * delta;
			}
		}

		foreach (i; 0..n)
		{
			mark (i, +1);
		}
		writeln (total);
		foreach (s; 0..q)
		{
			int l, r;
			readf !(" %s %s") (l, r);
			readln;
			l -= 1;
			r -= 1;
			auto p = chain (iota (l - 1, l + 2),
			    iota (max (l + 2, r - 1), r + 2)).array;
			foreach (i; p)
			{
				mark (i, -1);
			}
			swap (a[l], a[r]);
			foreach (i; p)
			{
				mark (i, +1);
			}
			writeln (total);
		}
	}
}

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;

void main(string[] args)
{
  auto t = next!int;
  foreach(tc; 0 .. t)
    {
      auto n = next!int;
      auto q = next!int;
      auto a = new int[](n); foreach(ref ai; a) ai = next!int;
      assert(q == 0);
      long maxval = a[0], minval = a[0];
      long globalmax = a[0];
      foreach(ai; a[1 ..$])
	{
	  long localmax = ai;
	  if (minval < 0) localmax -= minval;
	  long localmin = ai;
	  if (maxval > 0) localmin -= maxval;
	  maxval = max(localmax, maxval);
	  minval = min(localmin, minval);
	}
      maxval.writeln;
    }
}
ulong bitCnt(long x)
{
  ulong cnt = 0;
  while(x) {cnt += x & 1; x>>=1;}
  return cnt;
}
alias Arr(T) = Mat!(T, 1);
struct Mat(T, size_t dimension)
{
  T[] _baseArray;
  T[] _slice;
  size_t[dimension] _sizes; size_t dim(size_t i) {return _sizes[i];}
  size_t[dimension] _indexer;
  this(T[] baseArray, size_t[dimension] sizes)
  {
    _baseArray = baseArray;
    _sizes = sizes;
    _indexer[dimension - 1] = 1;
    static foreach_reverse(i; 0 .. dimension - 1)
      _indexer[i] = _indexer[i + 1] * _sizes[i + 1];
    auto requiredSize = _indexer[0] * _sizes[0];
    debug assert(_baseArray.length >= requiredSize);
    _slice = _baseArray[0 .. requiredSize];
  }
  this(size_t[dimension] sizes)
  {
    size_t reqSize = 1;
    static foreach(i; 0 .. dimension)
      reqSize *= sizes[i];
    this(new T[](reqSize), sizes);
  }
  ref auto opIndex()
  {
    pragma(inline, true);
    return _slice[];
  }
  ref auto opIndex(I...)(I i)
  {
    pragma(inline, true);
    debug assert(i.length <= dimension);
    size_t index = mixin(iota(0, i.length).map!(j => text("i[", j, "]*_indexer[", j, "]")).join("+"));
    static if (i.length == dimension)
      {
	return _slice[index];
      }
    else
      {
	enum sliceDimension = dimension - i.length;
	size_t[sliceDimension] remSizes = _sizes[i.length .. $];
	auto basePortion = _slice[index .. index + _indexer[i.length - 1]];
 	return Mat!(T, sliceDimension)(basePortion, remSizes);
      }
  }
  static if (dimension == 2)
    {
      auto identity()
      {
	debug assert(dim(0) == dim(1));
	auto n = dim(0);
	auto id = Mat!(T, 2)([n, n]);
	foreach(k; 0 .. n)
	  id[k, k] = T(1);
	return id;
      }
      bool canMultiply(Mat!(T, 2) b)
      {
	return this.dim(1) == b.dim(0);
      }
      auto opBinary(string op)(Mat!(T, 2) b)
	if (op == "*")
	  {
	    debug assert(canMultiply(b));
	    auto res = Mat!(T, 2)([this.dim(0), b.dim(1)]);
	    foreach(i; 0 .. res.dim(0))
	      foreach(k; 0 .. this.dim(1))
		foreach(j; 0 .. res.dim(1))
		  res[i, j] = res[i, j] + this[i, k] * b[k, j];
	    return res;
	  }
      auto opBinary(string op)(ulong exp)
	if (op == "^^")
	  {
	    return this.pow(identity, exp);
	  }
      mixin interiorBinPow;
    }
}

struct Mod(T, ulong mod)
{
  alias This = Mod!(T, mod);
  T _rep; T rep() {return _rep;}
  this(W)(W w)
  {
    _rep = ((cast(T) w)%mod + mod) % mod;
  }
  static auto plainConstruct(T t)
  {
    pragma(inline, true);
    debug assert(t >= 0 && t < mod);
    auto res = Mod!(T, mod).init;
    res._rep = t;
    return res;
  }
  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.plainConstruct(resrep);
      }
    else static if (op == "-")
      {
	T resrep = _rep - b._rep;
	if (resrep < 0) resrep += mod;
	return This.plainConstruct(resrep);
      }
    else static if (op == "*")
      {
	return This(_rep * b._rep);
      }
  }
}

mixin template interiorBinPow()
{
  alias ThisType = typeof(this);
  auto pow(ThisType res, ulong exp)
    {
      if (exp < 0) debug assert(0);
      auto pow = this;
      while(exp)
	{
	  if (exp & 1)
	    res = res * pow;
	  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 next(T)()
{
  static if (is(T == int) || is(T == long) || is(T == short))
    {
      import std.ascii: isDigit;
      T res;
      while(!frontChar.isDigit)
	popChar;
      while(frontChar.isDigit)
	{
	  res *= 10;
	  res += frontChar - '0';
	  popChar;
	}
      return 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;
    }
}

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;

void main(string[] args)
{
  inputFile = stdin;
  debug inputFile = File(args[1]);
  auto t = next!int;
  foreach(tc; 0 .. t)
    {
      auto n = next!int;
      auto q = next!int;
      auto a = next!int(n);
      assert(q == 0);
      long maxval = a[0], minval = a[0];
      long globalmax = a[0];
      foreach(ai; a[1 ..$])
	{
	  long localmax = ai;
	  if (minval < 0) localmax -= minval;
	  long localmin = ai;
	  if (maxval > 0) localmin -= maxval;
	  maxval = max(localmax, maxval);
	  minval = min(localmin, minval);
	}
      maxval.writeln;
    }
}
ulong bitCnt(long x)
{
  ulong cnt = 0;
  while(x) {cnt += x & 1; x>>=1;}
  return cnt;
}
alias Arr(T) = Mat!(T, 1);
struct Mat(T, size_t dimension)
{
  T[] _baseArray;
  T[] _slice;
  size_t[dimension] _sizes; size_t dim(size_t i) {return _sizes[i];}
  size_t[dimension] _indexer;
  this(T[] baseArray, size_t[dimension] sizes)
  {
    _baseArray = baseArray;
    _sizes = sizes;
    _indexer[dimension - 1] = 1;
    static foreach_reverse(i; 0 .. dimension - 1)
      _indexer[i] = _indexer[i + 1] * _sizes[i + 1];
    auto requiredSize = _indexer[0] * _sizes[0];
    debug assert(_baseArray.length >= requiredSize);
    _slice = _baseArray[0 .. requiredSize];
  }
  this(size_t[dimension] sizes)
  {
    size_t reqSize = 1;
    static foreach(i; 0 .. dimension)
      reqSize *= sizes[i];
    this(new T[](reqSize), sizes);
  }
  ref auto opIndex()
  {
    pragma(inline, true);
    return _slice[];
  }
  ref auto opIndex(I...)(I i)
  {
    pragma(inline, true);
    debug assert(i.length <= dimension);
    size_t index = mixin(iota(0, i.length).map!(j => text("i[", j, "]*_indexer[", j, "]")).join("+"));
    static if (i.length == dimension)
      {
	return _slice[index];
      }
    else
      {
	enum sliceDimension = dimension - i.length;
	size_t[sliceDimension] remSizes = _sizes[i.length .. $];
	auto basePortion = _slice[index .. index + _indexer[i.length - 1]];
 	return Mat!(T, sliceDimension)(basePortion, remSizes);
      }
  }
  static if (dimension == 2)
    {
      auto identity()
      {
	debug assert(dim(0) == dim(1));
	auto n = dim(0);
	auto id = Mat!(T, 2)([n, n]);
	foreach(k; 0 .. n)
	  id[k, k] = T(1);
	return id;
      }
      bool canMultiply(Mat!(T, 2) b)
      {
	return this.dim(1) == b.dim(0);
      }
      auto opBinary(string op)(Mat!(T, 2) b)
	if (op == "*")
	  {
	    debug assert(canMultiply(b));
	    auto res = Mat!(T, 2)([this.dim(0), b.dim(1)]);
	    foreach(i; 0 .. res.dim(0))
	      foreach(k; 0 .. this.dim(1))
		foreach(j; 0 .. res.dim(1))
		  res[i, j] = res[i, j] + this[i, k] * b[k, j];
	    return res;
	  }
      auto opBinary(string op)(ulong exp)
	if (op == "^^")
	  {
	    return this.pow(identity, exp);
	  }
      mixin interiorBinPow;
    }
}

struct Mod(T, ulong mod)
{
  alias This = Mod!(T, mod);
  T _rep; T rep() {return _rep;}
  this(W)(W w)
  {
    _rep = ((cast(T) w)%mod + mod) % mod;
  }
  static auto plainConstruct(T t)
  {
    pragma(inline, true);
    debug assert(t >= 0 && t < mod);
    auto res = Mod!(T, mod).init;
    res._rep = t;
    return res;
  }
  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.plainConstruct(resrep);
      }
    else static if (op == "-")
      {
	T resrep = _rep - b._rep;
	if (resrep < 0) resrep += mod;
	return This.plainConstruct(resrep);
      }
    else static if (op == "*")
      {
	return This(_rep * b._rep);
      }
  }
}

mixin template interiorBinPow()
{
  alias ThisType = typeof(this);
  auto pow(ThisType res, ulong exp)
    {
      if (exp < 0) debug assert(0);
      auto pow = this;
      while(exp)
	{
	  if (exp & 1)
	    res = res * pow;
	  pow = pow * pow;
	  exp >>= 1;
	}
      return res;
    }
}

// INPUT
File inputFile;
DList!string _words;
void _wordsFill()
{
  while (_words.empty) _words.insertBack(inputFile.readln.split);
}
string popWord()
{
  _wordsFill;
  auto word = _words.front;
  _words.removeFront;
  return word;
}
string peekWord()
{
  _wordsFill;
  return _words.front;
}
T next(T)()
{
  return to!T(popWord);
}
T peek(T)()
{
  return to!T(peekWord);
}
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, std.conv, std.functional, std.string, std.algorithm;
import std.container, std.range, std.typecons, std.numeric, std.math, std.random;

alias ll = long;
alias rbt = redBlackTree;
alias tup = Tuple!(long, long);
static string[] inp;
T rd(T = long)(){while(!inp.length) inp = readln.chomp.split; string a = inp[0]; inp.popFront; return a.to!T;}
T[] rdarr(T = long)(T fix = 0){ auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
void show(A...)(A a) { debug{ foreach(t; a){ write(t, "| "); } writeln; } }
/*******************************It's a Me, Mario!*******************************************/

void play(){
    int n, q;
    n = rd!int;
    q = rd!int;
    ll[] arr = rdarr;
    if(n == 1){ writeln(arr[0]); return;}
    ll[] res;
    bool side = (arr[1] > arr[0]), nside;
    foreach(i; 1..n){
        if(arr[i] > arr[i-1]){
            nside = 1;
        }else{
            nside = 0;
        }
        if(nside != side){
            res ~= arr[i-1];
            side = nside;
        }
    }
    if(side){ res ~= arr[n-1]; }
    /* writeln(res); */
    ll pow = 0;
    foreach(i; 0..res.length){
        if(i % 2 == 0){
            pow += res[i];
        }else if(i != res.length-1){
            pow -= res[i];
        }
    }
    writeln(pow);
}

int main(){
    long t = 1;
    t = rd;        // Toggle!
    while(t--) play();  // Let's play!
    stdout.flush;
    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!int;
		auto q = RD!int;
		auto a = RDA;

		long[] b = [a[0]];
		foreach (i; 1..n-1)
		{
			if (a[i] < a[i-1] && a[i] < a[i+1])
				b ~= a[i];
			else if (a[i] > a[i-1] && a[i] > a[i+1])
				b ~= a[i];
		}
		b ~= a[n-1];

		int j = -1;
		foreach (i; 1..b.length)
		{
			if (b[i] > b[i-1])
			{
				ans[ti] += b[i];
				j = cast(int)i;
				break;
			}
		}

		if (j == -1)
			ans[ti] = b[0];
		else
		{
			foreach (i; j+2..b.length)
			{
				if (b[i] > b[i-1])
				{
					ans[ti] += b[i] - b[i-1];
				}
			}
		}
	}

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

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

string FMT_F = "%.10f";

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

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

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

//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { 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 q = RD!int;
		auto a = RDA;

		long[] b = [a[0]];
		foreach (i; 1..n-1)
		{
			if (a[i] <= a[i-1] && a[i] <= a[i+1])
				b ~= a[i];
			else if (a[i] >= a[i-1] && a[i] >= a[i+1])
				b ~= a[i];
		}
		b ~= a[n-1];

		if (b.length >= 2)
		{
			if (b[0] >= b[1])
				ans[ti] += b[0];
		}
		int j = -1;
		foreach (i; 1..b.length)
		{
			if (b[i] > b[i-1])
			{
				ans[ti] += b[i];
				j = cast(int)i;
				break;
			}
		}

		if (j == -1)
			ans[ti] = b[0];
		else
		{
			foreach (i; j+2..b.length)
			{
				if (b[i] > b[i-1])
				{
					ans[ti] += b[i] - b[i-1];
				}
			}
		}
	}

	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 int mod = 10 ^^ 9 + 7;

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto adj = new int [] [n];
		foreach (i; 0..n - 1)
		{
			int u, v;
			readf !(" %s %s") (u, v);
			u -= 1;
			v -= 1;
			adj[u] ~= v;
			adj[v] ~= u;
		}
		readln;
		auto m = readln.strip.to !(int);
		auto q = readln.splitter.map !(to !(int)).array;

		sort (q);
		while (q.length > n - 1)
		{
			q[$ - 2] = (q[$ - 2] * 1L * q[$ - 1]) % mod;
			q.length -= 1;
		}
		reverse (q);
		while (q.length < n - 1)
		{
			q ~= 1;
		}

		auto d = new int [n];
		long [] nums;

		void recur (int v, int p)
		{
			d[v] = 1;
			foreach (u; adj[v])
			{
				if (u != p)
				{
					recur (u, v);
					nums ~= d[u] * 1L * (n - d[u]);
					d[v] += d[u];
				}
			}
		}

		recur (0, -1);
		sort (nums);
		reverse (nums);
		nums[] %= mod;

		int res = 0;
		foreach (i; 0..n - 1)
		{
			res = (res + nums[i] * q[i]) % mod;
		}
		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 long[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto edges = new int[][](n);
		foreach (i; 0..n-1)
		{
			auto u = RD!int-1;
			auto v = RD!int-1;
			edges[u] ~= v;
			edges[v] ~= u;
		}
		auto m = RD!int;
		auto p = RDA;
		p.sort!"a > b"();
		while (p.length > n-1)
		{
			p[1].modm(p[0]);
			p.popFront;
		}

		long[] list;
		long dfs(int pos, int last)
		{
			long cnt;
			foreach (v; edges[pos])
			{
				if (v == last) continue;
				cnt += dfs(v, pos);
			}
			++cnt;
			auto x = cnt;
			x *= (n-cnt);
			list ~= x;
			return cnt;
		}
		dfs(0, -1);
		list.sort!"a > b"();
		debug writeln(list);

		foreach (i; 0..n-1)
		{
			long pp = 1;
			if (!p.empty)
			{
				pp = p.front; p.popFront;
			}
			pp.modm(list[i]);
			ans[ti].moda(pp);
		}
	}

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

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

string FMT_F = "%.10f";

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

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

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

long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { 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 edges = new int[][](n);
		foreach (i; 0..n-1)
		{
			auto u = RD!int-1;
			auto v = RD!int-1;
			edges[u] ~= v;
			edges[v] ~= u;
		}
		auto m = RD!int;
		auto p = RDA;
		p.sort!"a > b"();

		long[] list;
		int dfs(int pos, int last)
		{
			int cnt;
			foreach (v; edges[pos])
			{
				if (v == last) continue;
				cnt += dfs(v, pos);
			}
			++cnt;
			list ~= cnt * (n-cnt);
			return cnt;
		}
		dfs(0, -1);
		list.sort!"a > b"();

		foreach (i; 0..n-1)
		{
			long pp = 1;
			if (!p.empty)
			{
				pp = p.front; p.popFront;
			}
			pp *= list[i];
			ans[ti].moda(pp);
		}
	}

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

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

string FMT_F = "%.10f";

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

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

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

long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { 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 edges = new int[][](n);
		foreach (i; 0..n-1)
		{
			auto u = RD!int-1;
			auto v = RD!int-1;
			edges[u] ~= v;
			edges[v] ~= u;
		}
		auto m = RD!int;
		auto p = RDA;
		p.sort!"a > b"();

		long[] list;
		long dfs(int pos, int last)
		{
			long cnt;
			foreach (v; edges[pos])
			{
				if (v == last) continue;
				cnt += dfs(v, pos);
			}
			++cnt;
			list ~= cnt * (n-cnt);
			return cnt;
		}
		dfs(0, -1);
		list.sort!"a > b"();

		foreach (i; 0..n-1)
		{
			long pp = 1;
			if (!p.empty)
			{
				pp = p.front; p.popFront;
			}
			pp *= list[i];
			ans[ti].moda(pp);
		}
	}

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

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

string FMT_F = "%.10f";

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

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

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

long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { 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 edges = new int[][](n);
		foreach (i; 0..n-1)
		{
			auto u = RD!int-1;
			auto v = RD!int-1;
			edges[u] ~= v;
			edges[v] ~= u;
		}
		auto m = RD!int;
		auto p = RDA;
		p.sort!"a > b"();
		while (p.length > n-1)
		{
			p[1].modm(p[0]);
			p.popFront;
		}

		long[] list;
		long dfs(int pos, int last)
		{
			long cnt;
			foreach (v; edges[pos])
			{
				if (v == last) continue;
				cnt += dfs(v, pos);
			}
			++cnt;
			auto x = cnt;
			x.modm(n-cnt);
			list ~= x;
			return cnt;
		}
		dfs(0, -1);
		list.sort!"a > b"();
		debug writeln(list);

		foreach (i; 0..n-1)
		{
			long pp = 1;
			if (!p.empty)
			{
				pp = p.front; p.popFront;
			}
			pp.modm(list[i]);
			ans[ti].moda(pp);
		}
	}

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

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

string FMT_F = "%.10f";

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

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

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

long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { 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 edges = new int[][](n);
		foreach (i; 0..n-1)
		{
			auto u = RD!int-1;
			auto v = RD!int-1;
			edges[u] ~= v;
			edges[v] ~= u;
		}
		auto m = RD!int;
		auto p = RDA;
		p.sort!"a > b";

		long[] list;
		int dfs(int pos, int last)
		{
			int cnt;
			foreach (v; edges[pos])
			{
				if (v == last) continue;
				cnt += dfs(v, pos);
			}
			++cnt;
			list ~= cnt * (n-cnt);
			return cnt;
		}
		dfs(0, -1);
		list.sort!"a > b"();

		foreach (i; 0..n-1)
		{
			long pp = 1;
			if (!p.empty)
			{
				pp = p.front; p.popFront;
			}
			pp.modm(list[i]);
			ans[ti].moda(pp);
		}
	}

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

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

string FMT_F = "%.10f";

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

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

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

long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { 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 edges = new int[][](n);
		foreach (i; 0..n-1)
		{
			auto u = RD!int-1;
			auto v = RD!int-1;
			edges[u] ~= v;
			edges[v] ~= u;
		}
		auto m = RD!int;
		auto p = RDA;
		p.sort!"a > b"();

		long[] list;
		long dfs(int pos, int last)
		{
			long cnt;
			foreach (v; edges[pos])
			{
				if (v == last) continue;
				cnt += dfs(v, pos);
			}
			++cnt;
			list ~= cnt * (n-cnt);
			return cnt;
		}
		dfs(0, -1);
		list.sort!"a > b"();

		foreach (i; 0..n-1)
		{
			long pp = 1;
			if (!p.empty)
			{
				pp = p.front; p.popFront;
			}
			pp.modm(list[i]);
			ans[ti].moda(pp);
		}
	}

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

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

string FMT_F = "%.10f";

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

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

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

long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { 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 edges = new int[][](n);
		foreach (i; 0..n-1)
		{
			auto u = RD!int-1;
			auto v = RD!int-1;
			edges[u] ~= v;
			edges[v] ~= u;
		}
		auto m = RD!int;
		auto p = RDA;
		p.sort!"a > b"();

		long[] list;
		long dfs(int pos, int last)
		{
			long cnt;
			foreach (v; edges[pos])
			{
				if (v == last) continue;
				cnt += dfs(v, pos);
			}
			++cnt;
			auto x = cnt;
			x.modm(n-cnt);
			list ~= x;
			return cnt;
		}
		dfs(0, -1);
		list.sort!"a > b"();
		debug writeln(list);

		foreach (i; 0..n-1)
		{
			long pp = 1;
			if (!p.empty)
			{
				pp = p.front; p.popFront;
			}
			pp.modm(list[i]);
			ans[ti].moda(pp);
		}
	}

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


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

string FMT_F = "%.10f";

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

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

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }

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

		auto index = a.MAKE_IDX;
		foreach (ii, i; index)
		{
			a[i] = ii;
		}

		long cnt;
		foreach (i; 0..n-1)
		{
			if (a[i]+1 != a[i+1])
				++cnt;
		}
		ans[ti] = cnt < k;
	}

	foreach (e; ans)
		writeln(e ? "Yes" : "No");
	stdout.flush;
	debug readln;
}

immutable multi = true;

void solve(int tc)
{
	auto n = readInt!int;
	auto k = readInt!int;
	auto a = ma(n, readInt!int);
	auto oa = a.dup;
	int[int] rnk;
	sort(a);
	foreach(i, ai; a) rnk[ai] = cast(int)i;
	oa = oa.map!(ai => rnk[ai]).array;
	int chunks = 0;
	int i = 0;
	while (i < n)
	{
		chunks++;
		while (i + 1 < n && oa[i + 1] == oa[i] + 1) i++;
		i++;
	}
	if (chunks <= k) writeln("YES"); else writeln("NO");
}

// 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;
}
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.numeric, std.stdio;
immutable H = 1 << 19, T = H * 2;
int [T] t;
int n, q, u, v, w, z;
void set (int p, int v) {
	for (t[p] = v, p >>= 1; p; p >>= 1) t[p] = gcd (t[p * 2], t[p * 2 + 1]);
}
int ver (int p, int v) {
	while (p < H) {
		int x = t[p * 2] % v, y = t[p * 2 + 1] % v;
		if (x && y) return 2;
		p = p * 2 + !x;
	}
	return 1;
}
int ask (int lo, int hi, int v) {
	for (int f; lo < hi; lo >>= 1, hi >>= 1) {
		if (lo & 1) f += (t[lo] % v) ? ver (lo, v) : 0, lo += 1;
		if (hi & 1) hi -= 1, f += (t[hi] % v) ? ver (hi, v) : 0;
		if (f > 1) return 0;
	}
	return 1;
}
void main () {
	readf (" %s", &n);
	foreach (i; 0..n) readf (" %s", &t[i + H]);
	foreach_reverse (i; 1..H) t[i] = gcd (t[i * 2], t[i * 2 + 1]);
	readf (" %s", &q);
	foreach (r; 0..q) {
		readf (" %s %s %s", &z, &u, &v);
		if (z < 2) readf (" %s", &w), writeln (ask (u - 1 + H, v + H, w) ? "YES" : "NO");
		else set (u - 1 + H, v);
	}
}

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

// T: monoid
// op: T * T -> T
// query(a, b): returns t_a ... t_{b-1}
class SegmentTree(T, alias op) {
  import std.functional : binaryFun;
  alias opFun = binaryFun!op;
  const(T) idT;

  int n;
  T[] ts;
  this(int n_, const(T) idT) {
    this.idT = idT;
    for (n = 1; n < n_; n <<= 1) {}
    ts = new T[n << 1];
    ts[] = idT;
  }
  this(inout(T)[] ts_, const(T) idT) {
    this.idT = idT;
    const n_ = cast(int)(ts_.length);
    for (n = 1; n < n_; n <<= 1) {}
    ts = new T[n << 1];
    ts[0 .. n] = idT;
    ts[n .. n + n_] = ts_[];
    ts[n + n_ .. n << 1] = idT;
    build();
  }
  ref T at(int a) {
    return ts[n + a];
  }
  void build() {
    foreach_reverse (a; 1 .. n) ts[a] = opFun(ts[a << 1], ts[a << 1 | 1]);
  }
  void set(int a, const(T) val) {
    ts[a += n] = val;
    for (; a >>= 1; ) ts[a] = opFun(ts[a << 1], ts[a << 1 | 1]);
  }
  void mulL(int a, const(T) val) {
    set(a, opFun(val, ts[a + n]));
  }
  void mulR(int a, const(T) val) {
    set(a, opFun(ts[a + n], val));
  }
  T query(int a, int b) const {
    T prodL = idT, prodR = idT;
    for (a += n, b += n; a < b; a >>= 1, b >>= 1) {
      if (a & 1) prodL = opFun(prodL, ts[a++]);
      if (b & 1) prodR = opFun(ts[--b], prodR);
    }
    return opFun(prodL, prodR);
  }
}

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

      auto seg = new SegmentTree!(int, gcd)(A, 0);
      bool solve(int l, int r, int x) {
        for (; ; ) {
          debug {
            // writeln(l, " ", r, " ", x);
          }
          if (r - l <= 1) {
            return true;
          }
          const mid = (l + r) / 2;
          debug {
            writefln("l = %s, r = %s, x = %s", l, r, x);
            writeln("seg.query(l, mid) = ", seg.query(l, mid));
            writeln("seg.query(mid, r) = ", seg.query(mid, r));
          }
          if (seg.query(l, mid) % x == 0) {
            l = mid;
          } else if (seg.query(mid, r) % x == 0) {
            r = mid;
          } else {
            return false;
          }
        }
      }

      debug {
        auto as = A.dup;
      }

      const Q = readInt();
      foreach (q; 0 .. Q) {
        const typ = readInt();
        switch (typ) {
          case 1: {
            const l = readInt() - 1;
            const r = readInt();
            const x = readInt();
            const ans = solve(l, r, x);
            writeln(ans ? "YES" : "NO");
            debug {
              const brt = (as[l .. r].count!(a => (a % x != 0)) <= 1);
              assert(brt == ans, format("%s %s %s %s; brt = %s, ans = %s", as, l, r, x, brt, ans));
            }
          } break;
          case 2: {
            const i = readInt() - 1;
            const y = readInt();
            seg.set(i, y);
            debug {
              as[i] = y;
            }
          } break;
          default: assert(false);
        }
      }
    }
  } catch (EOFException e) {
  }
}

import std.algorithm, std.array, std.conv, std.numeric, std.stdio, std.string;
immutable int logHalf = 19, half = 1 << logHalf, limit = half << 1;
void main () {
	int n = readln.strip.to!int;
	auto a = readln.splitter.map !(to!int).array;
	auto t = new int [limit];
	foreach (i, c; a) t[i + half] = c;
	foreach_reverse (i; 1..half) t[i] = gcd (t[i * 2 + 0], t[i * 2 + 1]);

	void change (int pos, int v) {
		for (pos += half, t[pos] = v, pos >>= 1; pos; pos >>= 1)
			t[pos] = gcd (t[pos * 2 + 0], t[pos * 2 + 1]);
	}

	bool checkVertex (int pos, int v) {
		while (pos < half) {
			bool x = (t[pos * 2 + 0] % v != 0);
			bool y = (t[pos * 2 + 1] % v != 0);
			if (x && y) return false;
			pos = pos * 2 + y;
		}
		return true;
	}

	bool checkSegment (int lo, int hi, int v) {
		bool found = false;
		for (lo += half, hi += half; lo < hi; lo >>= 1, hi >>= 1) {
			if (lo & 1) {
				if (t[lo] % v != 0) {
					if (found || !checkVertex (lo, v)) return false;
					found = true;
				}
				lo += 1;
			}
			if (hi & 1) {
				hi -= 1;
				if (t[hi] % v != 0) {
					if (found || !checkVertex (hi, v)) return false;
					found = true;
				}
			}
		}
		return true;
	}

	int q = readln.strip.to!int;
	foreach (r; 0..q) {
		auto s = readln.splitter.map !(to!int).array;
		if (s[0] == 1) writeln (checkSegment (s[1] - 1, s[2] - 0, s[3]) ? "YES" : "NO");
		else change (s[1] - 1, s[2]);
	}
}

import std.algorithm, std.array, std.conv, std.numeric, std.stdio, std.string;
immutable H = 1 << 19, T = H * 2;
int [T] t;
void main () {
	int n, q, u, v, w, z;
	readf (" %s", &n);
	foreach (i; 0..n) readf (" %s", &t[i + H]);
	foreach_reverse (i; 1..H) t[i] = gcd (t[i * 2], t[i * 2 + 1]);

	void set (int p, int v) {
		for (t[p] = v, p >>= 1; p; p >>= 1)
			t[p] = gcd (t[p * 2], t[p * 2 + 1]);
	}

	int ver (int p, int v) {
		while (p < H) {
			int x = t[p * 2] % v, y = t[p * 2 + 1] % v;
			if (x && y) return 0;
			p = p * 2 + !x;
		}
		return 1;
	}

	int ask (int lo, int hi, int v) {
		for (int f; lo < hi; lo >>= 1, hi >>= 1) {
			if (lo & 1) {
				if (t[lo] % v) {
					if (f || !ver (lo, v)) return 0;
					f = 1;
				}
				lo += 1;
			}
			if (hi & 1) {
				hi -= 1;
				if (t[hi] % v) {
					if (f || !ver (hi, v)) return 0;
					f = 1;
				}
			}
		}
		return 1;
	}

	readf (" %s", &q);
	foreach (r; 0..q) {
		readf (" %s %s %s", &z, &u, &v);
		if (z < 2) {
			readf (" %s", &w);
			writeln (ask (u - 1 + H, v + H, w) ? "YES" : "NO");
		} else set (u - 1 + H, v);
	}
}

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

immutable int logHalf = 19;
immutable int half = 1 << logHalf;
immutable int limit = half << 1;

int gcd (int a, int b)
{
	while (a && b)
	{
		a %= b;
		if (a)
		{
			b %= a;
		}
	}
	return a + b;
}

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		auto t = new int [limit];
		foreach (i, c; a)
		{
			t[i + half] = c;
		}
		foreach_reverse (i; 1..half)
		{
			t[i] = gcd (t[i * 2 + 0], t[i * 2 + 1]);
		}

		void change (int pos, int v)
		{
			pos += half;
			t[pos] = v;
			for (pos >>= 1; pos; pos >>= 1)
			{
				t[pos] = gcd (t[pos * 2 + 0], t[pos * 2 + 1]);
			}
		}

		bool checkVertex (int pos, int v)
		{
			while (pos < half)
			{
				bool x = (t[pos * 2 + 0] % v != 0);
				bool y = (t[pos * 2 + 1] % v != 0);
				if (x && y)
				{
					return false;
				}
				pos = pos * 2 + y;
			}
			return true;
		}

		bool checkSegment (int lo, int hi, int v)
		{
			bool found = false;
			for (lo += half, hi += half; lo < hi;
			    lo >>= 1, hi >>= 1)
			{
				if (lo & 1)
				{
					if (t[lo] % v != 0)
					{
						if (found ||
						    !checkVertex (lo, v))
						{
							return false;
						}
						found = true;
					}
					lo += 1;
				}
				if (hi & 1)
				{
					hi -= 1;
					if (t[hi] % v != 0)
					{
						if (found ||
						    !checkVertex (hi, v))
						{
							return false;
						}
						found = true;
					}
				}
			}
			return true;
		}

		auto q = readln.strip.to !(int);
		foreach (r; 0..q)
		{
			auto s = readln.splitter.map !(to !(int)).array;
			if (s[0] == 1)
			{
				writeln (checkSegment
				    (s[1] - 1, s[2] - 0, s[3]) ? "YES" : "NO");
			}
			else
			{
				change (s[1] - 1, s[2]);
			}
		}
	}
}

import std.numeric, std.stdio;
immutable H = 1 << 19, T = H * 2;
int [T] t;
int n, q, u, v, w, z;
void set (int p, int v) {
	for (t[p] = v, p >>= 1; p; p >>= 1) t[p] = gcd (t[p * 2], t[p * 2 + 1]);
}
int ver (int p, int v) {
	while (p < H) {
		int x = t[p * 2] % v, y = t[p * 2 + 1] % v;
		if (x && y) return 2;
		p = p * 2 + !x;
	}
	return 1;
}
int ask (int lo, int hi, int v) {
	for (int f; lo < hi; lo >>= 1, hi >>= 1) {
		if (lo & 1) f += (t[lo] % v) && ver (lo, v), lo += 1;
		if (hi & 1) hi -= 1, f += (t[hi] % v) && ver (hi, v);
		if (f > 1) return 0;
	}
	return 1;
}
void main () {
	readf (" %s", &n);
	foreach (i; 0..n) readf (" %s", &t[i + H]);
	foreach_reverse (i; 1..H) t[i] = gcd (t[i * 2], t[i * 2 + 1]);
	readf (" %s", &q);
	foreach (r; 0..q) {
		readf (" %s %s %s", &z, &u, &v);
		if (z < 2) readf (" %s", &w), writeln (ask (u - 1 + H, v + H, w) ? "YES" : "NO");
		else set (u - 1 + H, v);
	}
}

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

// T: monoid
// op: T * T -> T
// query(a, b): returns t_a ... t_{b-1}
class SegmentTree(T, alias op) {
  import std.functional : binaryFun;
  alias opFun = binaryFun!op;
  const(T) idT;

  int n;
  T[] ts;
  this(int n_, const(T) idT) {
    this.idT = idT;
    for (n = 1; n < n_; n <<= 1) {}
    ts = new T[n << 1];
    ts[] = idT;
  }
  this(inout(T)[] ts_, const(T) idT) {
    this.idT = idT;
    const n_ = cast(int)(ts_.length);
    for (n = 1; n < n_; n <<= 1) {}
    ts = new T[n << 1];
    ts[0 .. n] = idT;
    ts[n .. n + n_] = ts_[];
    ts[n + n_ .. n << 1] = idT;
    build();
  }
  ref T at(int a) {
    return ts[n + a];
  }
  void build() {
    foreach_reverse (a; 1 .. n) ts[a] = opFun(ts[a << 1], ts[a << 1 | 1]);
  }
  void set(int a, const(T) val) {
    ts[a += n] = val;
    for (; a >>= 1; ) ts[a] = opFun(ts[a << 1], ts[a << 1 | 1]);
  }
  void mulL(int a, const(T) val) {
    set(a, opFun(val, ts[a + n]));
  }
  void mulR(int a, const(T) val) {
    set(a, opFun(ts[a + n], val));
  }
  T query(int a, int b) const {
    T prodL = idT, prodR = idT;
    for (a += n, b += n; a < b; a >>= 1, b >>= 1) {
      if (a & 1) prodL = opFun(prodL, ts[a++]);
      if (b & 1) prodR = opFun(ts[--b], prodR);
    }
    return opFun(prodL, prodR);
  }
}

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

      auto seg = new SegmentTree!(int, gcd)(A, 0);
      bool solve(int l, int r, int x) {
        for (; ; ) {
          debug {
            writeln(l, " ", r, " ", x);
          }
          if (r - l <= 1) {
            return true;
          }
          const mid = (l + r) / 2;
          if (seg.query(l, mid) % x == 0) {
            r = mid;
          } else if (seg.query(mid, r) % x == 0) {
            l = mid;
          } else {
            return false;
          }
        }
      }

      const Q = readInt();
      foreach (q; 0 .. Q) {
        const typ = readInt();
        switch (typ) {
          case 1: {
            const l = readInt() - 1;
            const r = readInt();
            const x = readInt();
            const ans = solve(l, r, x);
            writeln(ans ? "YES" : "NO");
          } break;
          case 2: {
            const i = readInt() - 1;
            const y = readInt();
            seg.set(i, y);
          } break;
          default: assert(false);
        }
      }
    }
  } catch (EOFException e) {
  }
}