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

DList!string _words;
void read(T)(out T t)
{
  import std.stdio: readln;
  while(_words.empty)
    {
      import std.array: split;
      foreach(word; readln.split)
        {
          _words.insertBack(word);
        }
    }
  import std.conv: to;
  string word = _words.front;
  _words.removeFront;
  t = to!T(word);
}

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

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

void main()
{
  int t;
  read(t);
  while(t--)
    {
      long n, m;
      read(n), read(m);
      auto p = new long[cast(size_t)n];
      foreach(ref pi; p)
        read(pi);
      auto h = new long[cast(size_t)n];
      foreach(ref hi; h)
        read(hi);
      auto g = new long[cast(size_t)n];
      auto adj = new Appender!(long[])[cast(size_t)n];
      foreach(ref list; adj)
        list = appender!(long[])();
      foreach(i; 0 .. n - 1)
        {
          long x, y;
          read(x), x--;
          read(y), y--;
          adj.at(x).put(y);
          adj.at(y).put(x);
        }
      auto totalp = new long[cast(size_t)n];
      class Impossible: Throwable
      {
        this()
        {
          super("");
        }
      }
      bool isImpossible = false;
      void check(long node, long parent)
      {
        totalp.at(node) = p.at(node);
        long hsum = 0;
        long gsum = 0;
        bool hasChildren = true;
        foreach(w; adj[cast(size_t)node][])
          if (w != parent)
            {
              check(w, node);
              totalp.at(node) += totalp.at(w);
              gsum += g.at(w);
              hasChildren = true;
            }
        // s
        if ((h.at(node) + totalp.at(node)) % 2 != 0)
          {
            version(none) writeln(2, " ", node + 1, " ", parent + 1);
            isImpossible = true;
          }
        g.at(node) = (totalp.at(node) + h.at(node))/2;
        bool condition1 = adj[cast(size_t)node][].length > 1 && hsum > h[cast(size_t)node] + p[cast(size_t)node];
        bool condition1p = hasChildren && gsum > g.at(node);
        if(condition1p)
          {
            version(none) writeln(1, " ", node + 1, " ", parent + 1);
            isImpossible = true;
          }
        bool condition2 = h[cast(size_t)node] > totalp[cast(size_t)node] || h[cast(size_t)node] < -totalp[cast(size_t)node];
        bool condition2p = g.at(node) < 0 || g.at(node) > totalp.at(node);
        if (condition2p)
          {
            version(none) writeln(3, " ", node + 1, " ", parent + 1);
            isImpossible = true;
          }
      }
      check(0, -1);
      if (!isImpossible)
        writeln("YES");
      else
        writeln("NO");
    }
}

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)
	{
		readln;
		auto p = readln.splitter.map !(to !(int)).array;
		auto n = p.length.to !(int);
		auto h = readln.splitter.map !(to !(int)).array;
		auto adj = new int [] [n];
		foreach (j; 0..n - 1)
		{
			auto v = readln.splitter.map !(to !(int)).array;
			v[] -= 1;
			adj[v[0]] ~= v[1];
			adj[v[1]] ~= v[0];
		}

		auto d = new int [n];
		bool ok = true;

		void recur (int v, int w)
		{
			d[v] = p[v];
			foreach (u; adj[v])
			{
				if (u != w)
				{
					recur (u, v);
					d[v] += d[u];
				}
			}
			ok &= (d[v] >= h[v] && (d[v] & 1) == (h[v] & 1));
			h[v] = (d[v] - h[v]) / 2;
		}

		recur (0, -1);

		bool good (int v, int w)
		{
			int cur = 0;
			foreach (u; adj[v])
			{
				if (u != w)
				{
					cur += h[u];
					if (!good (u, v))
					{
						return false;
					}
				}
			}
			return (cur >= h[v] - p[v]);
		}

		writeln (ok && good (0, -1) ? "YES" : "NO");
	}
}

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

DList!string _words;
void read(T)(out T t)
{
  import std.stdio: readln;
  while(_words.empty)
    {
      import std.array: split;
      foreach(word; readln.split)
        {
          _words.insertBack(word);
        }
    }
  import std.conv: to;
  string word = _words.front;
  _words.removeFront;
  t = to!T(word);
}

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

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

void main()
{
  int t;
  read(t);
  while(t--)
    {
      long n, m;
      read(n), read(m);
      auto p = new long[cast(size_t)n];
      foreach(ref pi; p)
        read(pi);
      auto h = new long[cast(size_t)n];
      foreach(ref hi; h)
        read(hi);
      auto g = new long[cast(size_t)n];
      auto adj = new Appender!(long[])[cast(size_t)n];
      foreach(ref list; adj)
        list = appender!(long[])();
      foreach(i; 0 .. n - 1)
        {
          long x, y;
          read(x), x--;
          read(y), y--;
          adj.at(x).put(y);
          adj.at(y).put(x);
        }
      auto totalp = new long[cast(size_t)n];
      class Impossible: Throwable
      {
        this()
        {
          super("");
        }
      }
      bool isImpossible = false;
      void check(long node, long parent)
      {
        totalp.at(node) = p.at(node);
        long hsum = 0;
        long gsum = 0;
        foreach(w; adj[cast(size_t)node][])
          if (w != parent)
            {
              check(w, node);
              totalp.at(node) += totalp.at(w);
              gsum += g.at(w);
            }
        // s
        if ((h.at(node) + totalp.at(node)) % 2 != 0)
          {
            version(none) writeln(2, " ", node + 1, " ", parent + 1);
            isImpossible = true;
          }
        g.at(node) = (totalp.at(node) + h.at(node))/2;
        bool condition1 = adj[cast(size_t)node][].length > 1 && hsum > h[cast(size_t)node] + p[cast(size_t)node];
        bool condition1p = gsum > g.at(node);
        if(condition1p)
          {
            version(none) writeln(1, " ", node + 1, " ", parent + 1);
            isImpossible = true;
          }
        bool condition2 = h[cast(size_t)node] > totalp[cast(size_t)node] || h[cast(size_t)node] < -totalp[cast(size_t)node];
        bool condition2p = g.at(node) < 0 || g.at(node) > totalp.at(node);
        if (condition2p)
          {
            version(none) writeln(3, " ", node + 1, " ", parent + 1);
            isImpossible = true;
          }
      }
      check(0, -1);
      if (!isImpossible)
        writeln("YES");
      else
        writeln("NO");
    }
}

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

DList!string _words;
void read(T)(out T t)
{
  import std.stdio: readln;
  while(_words.empty)
    {
      import std.array: split;
      foreach(word; readln.split)
        {
          _words.insertBack(word);
        }
    }
  import std.conv: to;
  string word = _words.front;
  _words.removeFront;
  t = to!T(word);
}

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

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

void main()
{
  int t;
  read(t);
  while(t--)
    {
      long n, m;
      read(n), read(m);
      auto p = Array!long();
      p.length = cast(size_t)n;
      foreach(ref pi; p)
        read(pi);
      auto h = Array!long();
      h.length = cast(size_t)n;
      foreach(ref hi; h)
        read(hi);
      auto g = Array!long();
      g.length = cast(size_t)n;
      auto adj = Array!(Appender!(long[]))();
      adj.length = cast(size_t)n;
      foreach(ref list; adj)
        list = appender!(long[])();
      foreach(i; 0 .. n - 1)
        {
          long x, y;
          read(x), x--;
          read(y), y--;
          adj.at(x).put(y);
          adj.at(y).put(x);
        }
      auto totalp = new long[cast(size_t)n];
      class Impossible: Throwable
      {
        this()
        {
          super(null);
        }
      }
      void check(long node, long parent)
      {
        totalp.at(node) = p.at(node);
        long hsum = 0;
        bool hasChildren = true;
        foreach(w; adj.at(node)[])
          if (w != parent)
            {
              check(w, node);
              totalp.at(node) += totalp.at(w);
              hsum += h.at(w);
              hasChildren = true;
            }
        if ((h.at(node) + totalp.at(node)) % 2 != 0
            || hasChildren && hsum > h.at(node) + p.at(node)
            || h.at(node) > totalp.at(node) || h.at(node) < -totalp.at(node))
          throw new Impossible();
      }
      try
        check(0, -1);
      catch(Impossible)
        goto no;
    yes:
      writeln("YES");
      continue;
    no:
      writeln("NO");
    }
}

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

DList!string _words;
void read(T)(out T t)
{
  import std.stdio: readln;
  while(_words.empty)
    {
      import std.array: split;
      foreach(word; readln.split)
        {
          _words.insertBack(word);
        }
    }
  import std.conv: to;
  string word = _words.front;
  _words.removeFront;
  t = to!T(word);
}

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

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

void main()
{
  int t;
  read(t);
  while(t--)
    {
      long n, m;
      read(n), read(m);
      auto p = new long[cast(size_t)n];
      foreach(ref pi; p)
        read(pi);
      auto h = new long[cast(size_t)n];
      foreach(ref hi; h)
        read(hi);
      auto g = new long[cast(size_t)n];
      auto adj = new Appender!(long[])[cast(size_t)n];
      foreach(ref list; adj)
        list = appender!(long[])();
      foreach(i; 0 .. n - 1)
        {
          long x, y;
          read(x), x--;
          read(y), y--;
          adj.at(x).put(y);
          adj.at(y).put(x);
        }
      auto totalp = new long[cast(size_t)n];
      class Impossible: Throwable
      {
        this()
        {
          super("");
        }
      }
      void check(long node, long parent)
      {
        totalp.at(node) = p.at(node);
        long hsum = 0;
        bool hasChildren = true;
        foreach(w; adj.at(node)[])
          if (w != parent)
            {
              check(w, node);
              totalp.at(node) += totalp.at(w);
              hsum += h.at(w);
              hasChildren = true;
            }
        if ((h.at(node) + totalp.at(node)) % 2 != 0
            || hasChildren && hsum > h.at(node) + p.at(node)
            || h.at(node) > totalp.at(node) || h.at(node) < -totalp.at(node))
          throw new Impossible();
      }
      try
        check(0, -1);
      catch(Impossible)
        goto no;
    yes:
      writeln("YES");
      continue;
    no:
      writeln("NO");
    }
}

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

DList!string _words;
void read(T)(out T t)
{
  import std.stdio: readln;
  while(_words.empty)
    {
      import std.array: split;
      foreach(word; readln.split)
        {
          _words.insertBack(word);
        }
    }
  import std.conv: to;
  string word = _words.front;
  _words.removeFront;
  t = to!T(word);
}

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

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

void main()
{
  int t;
  read(t);
  while(t--)
    {
      long n, m;
      read(n), read(m);
      auto p = new long[cast(size_t)n];
      foreach(ref pi; p)
        read(pi);
      auto h = new long[cast(size_t)n];
      foreach(ref hi; h)
        read(hi);
      auto g = new long[cast(size_t)n];
      auto adj = new Appender!(long[])[cast(size_t)n];
      foreach(ref list; adj)
        list = appender!(long[])();
      foreach(i; 0 .. n - 1)
        {
          long x, y;
          read(x), x--;
          read(y), y--;
          adj.at(x).put(y);
          adj.at(y).put(x);
        }
      auto totalp = new long[cast(size_t)n];
      class Impossible: Throwable
      {
        this()
        {
          super("");
        }
      }
      bool isImpossible = false;
      void check(long node, long parent)
      {
        totalp.at(node) = p.at(node);
        long hsum = 0;
        long gsum = 0;
        bool hasChildren = true;
        foreach(w; adj[cast(size_t)node][])
          if (w != parent)
            {
              check(w, node);
              totalp.at(node) += totalp.at(w);
              gsum += g.at(w);
              hsum += h.at(w);
              hasChildren = true;
            }
        // s
        if ((h.at(node) + totalp.at(node)) % 2 != 0)
          {
            version(none) writeln(2, " ", node + 1, " ", parent + 1);
            isImpossible = true;
          }
        g.at(node) = (totalp.at(node) + h.at(node))/2;
        bool condition1 = hasChildren && hsum > h.at(node) + p.at(node);
        bool condition1p = hasChildren && gsum > g.at(node);
        if(condition1)
          {
            version(none) writeln(1, " ", node + 1, " ", parent + 1);
            isImpossible = true;
          }
        bool condition2 = h[cast(size_t)node] > totalp[cast(size_t)node] || h[cast(size_t)node] < -totalp[cast(size_t)node];
        bool condition2p = g.at(node) < 0 || g.at(node) > totalp.at(node);
        if (condition2)
          {
            version(none) writeln(3, " ", node + 1, " ", parent + 1);
            isImpossible = true;
          }
      }
      check(0, -1);
      if (!isImpossible)
        writeln("YES");
      else
        writeln("NO");
    }
}

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 bool[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto m = RD!int;
		auto p = RDA!int;
		auto h = RDA!int;
		auto edges = new int[][](n);
		foreach (i; 0..n-1)
		{
			auto x = RD!int-1;
			auto y = RD!int-1;
			edges[x] ~= y;
			edges[y] ~= x;
		}

		bool ok = true;
		long[2] dfs(int pos, int last)
		{
			long cnt0, cnt1;
			foreach (to; edges[pos])
			{
				if (to == last) continue;
				auto r = dfs(to, pos);
				cnt0 += r[0];
				cnt1 += r[1];
			}
			auto d = cnt0 - cnt1;
			auto v = h[pos] - d;
			auto remain = p[pos] - abs(v);
			if (remain < 0 && v > 0)
			{
				debug writeln("d:", d, " v:", v, " remain:", remain);
				debug writeln("cnt:", cnt0, " ", cnt1);
				cnt0 += p[pos];
				v -= p[pos];
				auto u = min(cnt1, abs(remain/2));
				cnt0 += u;
				cnt1 -= u;
				v -= u*2;
				remain += u*2;
				debug writeln("d:", d, " v:", v, " remain:", remain);
				debug writeln("cnt:", cnt0, " ", cnt1);
			}
			if (remain < 0 || remain % 2)
			{
				debug writeln("false pos:", pos, " remain:", remain);
				ok = false;
			}
			if (v > 0)
				cnt0 += v;
			else
				cnt1 += abs(v);
			cnt0 += remain / 2;
			cnt1 += remain / 2;
			return [cnt0, cnt1];
		}
		auto r = dfs(0, -1);
		debug writeln("m:", m, " ", r);
		if (r[0] + r[1] != m)
			ok = false;

		ans[ti] = ok;
	}

	foreach (e; ans)
	{
		writeln(e ? "YES" : "NO");
	}
	stdout.flush;
	debug readln;
}

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		readln;
		auto p = readln.splitter.map !(to !(int)).array;
		auto n = p.length.to !(int);
		auto m = p.sum;
		auto h = readln.splitter.map !(to !(int)).array;
		auto adj = new int [] [n];
		foreach (j; 0..n - 1)
		{
			auto v = readln.splitter.map !(to !(int)).array;
			v[] -= 1;
			adj[v[0]] ~= v[1];
			adj[v[1]] ~= v[0];
		}

		auto d = new int [n];
		bool ok = true;

		void recur (int v, int w)
		{
			d[v] = p[v];
			foreach (u; adj[v])
			{
				if (u != w)
				{
					recur (u, v);
					d[v] += d[u];
				}
			}
			ok &= ((d[v] & 1) == (h[v] & 1));
			h[v] = (d[v] - h[v]) / 2;
		}

		recur (0, -1);

		bool good (int v, int w)
		{
			int cur = 0;
			foreach (u; adj[v])
			{
				if (u != w)
				{
					cur += h[u];
					if (!good (u, v))
					{
						return false;
					}
				}
			}
			return (cur >= h[v] - p[v]);
		}

		writeln (ok && good (0, -1) ? "YES" : "NO");
	}
}

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

DList!string _words;
void read(T)(out T t)
{
  import std.stdio: readln;
  while(_words.empty)
    {
      import std.array: split;
      foreach(word; readln.split)
        {
          _words.insertBack(word);
        }
    }
  import std.conv: to;
  string word = _words.front;
  _words.removeFront;
  t = to!T(word);
}

void main()
{
  int t;
  read(t);
  while(t--)
    {
      long n, m;
      read(n), read(m);
      auto p = new long[cast(size_t)n];
      foreach(ref pi; p)
        read(pi);
      auto h = new long[cast(size_t)n];
      foreach(ref hi; h)
        read(hi);
      auto g = new long[cast(size_t)n];
      auto adj = new Appender!(long[])[cast(size_t)n];
      foreach(ref list; adj)
        list = appender!(long[])();
      foreach(i; 0 .. n - 1)
        {
          long x, y;
          read(x), x--;
          read(y), y--;
          adj[cast(size_t)x].put(y);
          adj[cast(size_t)y].put(x);
        }
      auto totalp = new long[cast(size_t)n];
      class Impossible: Throwable
      {
        this()
        {
          super("");
        }
      }
      bool isImpossible = false;
      void check(long node, long parent)
      {
        totalp[cast(size_t)node] = p[cast(size_t)node];
        long hsum = 0;
        long gsum = 0;
        foreach(w; adj[cast(size_t)node][])
          if (w != parent)
            {
              check(w, node);
              totalp[cast(size_t)node] += totalp[cast(size_t)w];
              hsum += h[cast(size_t)w];
              gsum += g[cast(size_t)w];
            }
        // s
        if ((h[cast(size_t)node] + totalp[cast(size_t)node]) % 2 != 0)
          {
            version(none) writeln(2, " ", node + 1, " ", parent + 1);
            isImpossible = true;
          }
        g[cast(size_t)node] = (totalp[cast(size_t)node] + h[cast(size_t)node])/2;
        bool condition1 = adj[cast(size_t)node][].length > 1 && hsum > h[cast(size_t)node] + p[cast(size_t)node];
        bool condition1p = adj[cast(size_t)node][].length > 1 && gsum > g[cast(size_t)node];
        if(condition1p)
          {
            version(none) writeln(1, " ", node + 1, " ", parent + 1);
            isImpossible = true;
          }
        bool condition2 = h[cast(size_t)node] > totalp[cast(size_t)node] || h[cast(size_t)node] < -totalp[cast(size_t)node];
        bool condition2p = g[cast(size_t)node] < 0 || g[cast(size_t)node] > totalp[cast(size_t)node];
        if (condition2p)
          {
            version(none) writeln(3, " ", node + 1, " ", parent + 1);
            isImpossible = true;
          }
      }
      check(0, -1);
      if (!isImpossible)
        writeln("YES");
      else
        writeln("NO");
    }
}

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

DList!string _words;
void read(T)(out T t)
{
  import std.stdio: readln;
  while(_words.empty)
    {
      import std.array: split;
      foreach(word; readln.split)
        {
          _words.insertBack(word);
        }
    }
  import std.conv: to;
  string word = _words.front;
  _words.removeFront;
  t = to!T(word);
}

void main()
{
  int t;
  read(t);
  while(t--)
    {
      long n, m;
      read(n), read(m);
      auto p = new long[cast(size_t)n];
      foreach(ref pi; p)
        read(pi);
      auto h = new long[cast(size_t)n];
      foreach(ref hi; h)
        read(hi);
      auto g = new long[cast(size_t)n];
      auto adj = new Appender!(long[])[cast(size_t)n];
      foreach(ref list; adj)
        list = appender!(long[])();
      foreach(i; 0 .. n - 1)
        {
          long x, y;
          read(x), x--;
          read(y), y--;
          adj[cast(size_t)x].put(y);
          adj[cast(size_t)y].put(x);
        }
      auto totalp = new long[cast(size_t)n];
      class Impossible: Throwable
      {
        this()
        {
          super("");
        }
      }
      void check(long node, long parent)
      {
        totalp[cast(size_t)node] = p[cast(size_t)node];
        long hsum = 0;
        long gsum = 0;
        foreach(w; adj[cast(size_t)node][])
          if (w != parent)
            {
              check(w, node);
              totalp[cast(size_t)node] += totalp[cast(size_t)w];
              hsum += h[cast(size_t)w];
              gsum += g[cast(size_t)w];
            }
        // s
        if ((h[cast(size_t)node] + totalp[cast(size_t)node]) % 2 != 0)
          {
            version(none) writeln(2, " ", node + 1, " ", parent + 1);
            throw new Impossible();
          }
        g[cast(size_t)node] = (totalp[cast(size_t)node] + h[cast(size_t)node])/2;
        bool condition1 = adj[cast(size_t)node][].length > 1 && hsum > h[cast(size_t)node] + p[cast(size_t)node];
        bool condition1p = adj[cast(size_t)node][].length > 1 && gsum > g[cast(size_t)node];
        assert(condition1 == condition1p);
        if(condition1p)
          {
            version(none) writeln(1, " ", node + 1, " ", parent + 1);
            throw new Impossible();
          }
        bool condition2 = h[cast(size_t)node] > totalp[cast(size_t)node] || h[cast(size_t)node] < -totalp[cast(size_t)node];
        bool condition2p = g[cast(size_t)node] < 0 || g[cast(size_t)node] > totalp[cast(size_t)node];
        if (condition2p)
          {
            version(none) writeln(3, " ", node + 1, " ", parent + 1);
            throw new Impossible();
          }
      }
      try
        {
          check(0, -1);
        }
      catch(Impossible impossible)
        {
          goto isImpossible;
        }
      writeln("YES");
      continue;
    isImpossible:
      writeln("NO");
    }
}

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

DList!string _words;
void read(T)(out T t)
{
  import std.stdio: readln;
  while(_words.empty)
    {
      import std.array: split;
      foreach(word; readln.split)
        {
          _words.insertBack(word);
        }
    }
  import std.conv: to;
  string word = _words.front;
  _words.removeFront;
  t = to!T(word);
}

void main()
{
  int t;
  read(t);
  while(t--)
    {
      long n, m;
      read(n), read(m);
      auto p = new long[cast(size_t)n];
      foreach(ref pi; p)
        read(pi);
      auto h = new long[cast(size_t)n];
      foreach(ref hi; h)
        read(hi);
      auto g = new long[cast(size_t)n];
      auto adj = new Appender!(long[])[cast(size_t)n];
      foreach(ref list; adj)
        list = appender!(long[])();
      foreach(i; 0 .. n - 1)
        {
          long x, y;
          read(x), x--;
          read(y), y--;
          adj[cast(size_t)x].put(y);
          adj[cast(size_t)y].put(x);
        }
      auto totalp = new long[cast(size_t)n];
      class Impossible: Throwable
      {
        this()
        {
          super("");
        }
      }
      void check(long node, long parent)
      {
        totalp[cast(size_t)node] = p[cast(size_t)node];
        long hsum = 0;
        long gsum = 0;
        foreach(w; adj[cast(size_t)node][])
          if (w != parent)
            {
              check(w, node);
              totalp[cast(size_t)node] += totalp[cast(size_t)w];
              hsum += h[cast(size_t)w];
              gsum += g[cast(size_t)w];
            }
        // s
        if ((h[cast(size_t)node] + totalp[cast(size_t)node]) % 2 != 0)
          {
            version(none) writeln(2, " ", node + 1, " ", parent + 1);
            throw new Impossible();
          }
        g[cast(size_t)node] = (totalp[cast(size_t)node] + h[cast(size_t)node])/2;
        bool condition1 = adj[cast(size_t)node][].length > 1 && hsum > h[cast(size_t)node] + p[cast(size_t)node];
        bool condition1p = adj[cast(size_t)node][].length > 1 && gsum > g[cast(size_t)node];
        assert(condition1 == condition1p);
        if(condition1)
          {
            version(none) writeln(1, " ", node + 1, " ", parent + 1);
            throw new Impossible();
          }
        bool condition2 = h[cast(size_t)node] > totalp[cast(size_t)node] || h[cast(size_t)node] < -totalp[cast(size_t)node];
        bool condition2p = g[cast(size_t)node] < 0 || g[cast(size_t)node] > totalp[cast(size_t)node];
        if (condition2)
          {
            version(none) writeln(3, " ", node + 1, " ", parent + 1);
            throw new Impossible();
          }
      }
      try
        {
          check(0, -1);
        }
      catch(Impossible impossible)
        {
          goto isImpossible;
        }
      writeln("YES");
      continue;
    isImpossible:
      writeln("NO");
    }
}

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

DList!string _words;
void read(T)(out T t)
{
  import std.stdio: readln;
  while(_words.empty)
    {
      import std.array: split;
      foreach(word; readln.split)
        {
          _words.insertBack(word);
        }
    }
  import std.conv: to;
  string word = _words.front;
  _words.removeFront;
  t = to!T(word);
}

void main()
{
  int t;
  read(t);
  while(t--)
    {
      long n, m;
      read(n), read(m);
      auto p = new long[cast(size_t)n];
      foreach(ref pi; p)
        read(pi);
      auto h = new long[cast(size_t)n];
      foreach(ref hi; h)
        read(hi);
      writeln("h = ", h);
      auto adj = new Appender!(long[])[cast(size_t)n];
      foreach(ref list; adj)
        list = appender!(long[])();
      foreach(i; 0 .. n - 1)
        {
          long x, y;
          read(x), x--;
          read(y), y--;
          adj[cast(size_t)x].put(y);
          adj[cast(size_t)y].put(x);
        }
      auto totalp = new long[cast(size_t)n];
      class Impossible: Throwable
      {
        this()
        {
          super("");
        }
      }
      void check(long node, long parent)
      {
        totalp[cast(size_t)node] = p[cast(size_t)node];
        long hsum = 0;
        foreach(w; adj[cast(size_t)node][])
          if (w != parent)
            {
              check(w, node);
              totalp[cast(size_t)node] += totalp[cast(size_t)w];
              hsum += h[cast(size_t)w];
            }
        if(adj[cast(size_t)node][].length > 1 && hsum > h[cast(size_t)node] + p[cast(size_t)node])
          {
            version(none) writeln(1, " ", node + 1, " ", parent + 1);
            throw new Impossible();
          }
        if ((h[cast(size_t)node] + totalp[cast(size_t)node]) % 2 != 0)
          {
            version(none) writeln(2, " ", node + 1, " ", parent + 1);
            throw new Impossible();
          }
        if (h[cast(size_t)node] > totalp[cast(size_t)node] || h[cast(size_t)node] < -totalp[cast(size_t)node])
          {
            version(none) writeln(3, " ", node + 1, " ", parent + 1);
            throw new Impossible();
          }
      }
      try
        {
          check(0, -1);
        }
      catch(Impossible impossible)
        {
          goto isImpossible;
        }
      writeln("YES");
      continue;
    isImpossible:
      writeln("NO");
    }
}

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

DList!string _words;
void read(T)(out T t)
{
  import std.stdio: readln;
  while(_words.empty)
    {
      import std.array: split;
      foreach(word; readln.split)
        {
          _words.insertBack(word);
        }
    }
  import std.conv: to;
  string word = _words.front;
  _words.removeFront;
  t = to!T(word);
}

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

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

void main()
{
  int t;
  read(t);
  while(t--)
    {
      long n, m;
      read(n), read(m);
      auto p = new long[cast(size_t)n];
      foreach(ref pi; p)
        read(pi);
      auto h = new long[cast(size_t)n];
      foreach(ref hi; h)
        read(hi);
      auto g = new long[cast(size_t)n];
      auto adj = new Appender!(long[])[cast(size_t)n];
      foreach(ref list; adj)
        list = appender!(long[])();
      foreach(i; 0 .. n - 1)
        {
          long x, y;
          read(x), x--;
          read(y), y--;
          adj.at(x).put(y);
          adj.at(y).put(x);
        }
      auto totalp = new long[cast(size_t)n];
      class Impossible: Throwable
      {
        this()
        {
          super("");
        }
      }
      bool isImpossible = false;
      void check(long node, long parent)
      {
        totalp.at(node) = p.at(node);
        long hsum = 0;
        long gsum = 0;
        foreach(w; adj[cast(size_t)node][])
          if (w != parent)
            {
              check(w, node);
              totalp.at(node) += totalp.at(w);
              gsum += g.at(w);
            }
        // s
        if ((h.at(node) + totalp.at(node)) % 2 != 0)
          {
            version(none) writeln(2, " ", node + 1, " ", parent + 1);
            isImpossible = true;
          }
        g.at(node) = (totalp.at(node) + h.at(node))/2;
        bool condition1 = adj[cast(size_t)node][].length > 1 && hsum > h[cast(size_t)node] + p[cast(size_t)node];
        bool condition1p = adj.at(node)[].length > 1 && gsum > g.at(node);
        if(condition1p)
          {
            version(none) writeln(1, " ", node + 1, " ", parent + 1);
            isImpossible = true;
          }
        bool condition2 = h[cast(size_t)node] > totalp[cast(size_t)node] || h[cast(size_t)node] < -totalp[cast(size_t)node];
        bool condition2p = g.at(node) < 0 || g.at(node) > totalp.at(node);
        if (condition2p)
          {
            version(none) writeln(3, " ", node + 1, " ", parent + 1);
            isImpossible = true;
          }
      }
      check(0, -1);
      if (!isImpossible)
        writeln("YES");
      else
        writeln("NO");
    }
}

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

DList!string _words;
void read(T)(out T t)
{
  import std.stdio: readln;
  while(_words.empty)
    {
      import std.array: split;
      foreach(word; readln.split)
        {
          _words.insertBack(word);
        }
    }
  import std.conv: to;
  string word = _words.front;
  _words.removeFront;
  t = to!T(word);
}

void main()
{
  int t;
  read(t);
  while(t--)
    {
      long n, m;
      read(n), read(m);
      auto p = new long[cast(size_t)n];
      foreach(ref pi; p)
        read(pi);
      auto h = new long[cast(size_t)n];
      foreach(ref hi; h)
        read(hi);
      auto adj = new Appender!(long[])[cast(size_t)n];
      foreach(ref list; adj)
        list = appender!(long[])();
      foreach(i; 0 .. n - 1)
        {
          long x, y;
          read(x), x--;
          read(y), y--;
          adj[cast(size_t)x].put(y);
          adj[cast(size_t)y].put(x);
        }
      auto totalp = new long[cast(size_t)n];
      class Impossible: Throwable
      {
        this()
        {
          super("");
        }
      }
      void check(long node, long parent)
      {
        totalp[cast(size_t)node] = p[cast(size_t)node];
        long hsum = 0;
        foreach(w; adj[cast(size_t)node][])
          if (w != parent)
            {
              check(w, node);
              totalp[cast(size_t)node] += totalp[cast(size_t)w];
              hsum += h[cast(size_t)w];
            }
        if(adj[cast(size_t)node][].length > 1 && hsum > h[cast(size_t)node] + p[cast(size_t)node])
          {
            version(none) writeln(1, " ", node + 1, " ", parent + 1);
            throw new Impossible();
          }
        if ((h[cast(size_t)node] + totalp[cast(size_t)node]) % 2 != 0)
          {
            version(none) writeln(2, " ", node + 1, " ", parent + 1);
            throw new Impossible();
          }
        if (h[cast(size_t)node] > totalp[cast(size_t)node] || h[cast(size_t)node] < -totalp[cast(size_t)node])
          {
            version(none) writeln(3, " ", node + 1, " ", parent + 1);
            throw new Impossible();
          }
      }
      try
        {
          check(0, -1);
        }
      catch(Impossible impossible)
        {
          goto isImpossible;
        }
      writeln("YES");
      continue;
    isImpossible:
      writeln("NO");
    }
}

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 bool[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto m = RD!int;
		auto p = RDA!int;
		auto h = RDA!int;
		auto edges = new int[][](n);
		foreach (i; 0..n-1)
		{
			auto x = RD!int-1;
			auto y = RD!int-1;
			edges[x] ~= y;
			edges[y] ~= x;
		}

		bool ok = true;
		long[2] dfs(int pos, int last)
		{
			long cnt0, cnt1;
			foreach (to; edges[pos])
			{
				if (to == last) continue;
				auto r = dfs(to, pos);
				cnt0 += r[0];
				cnt1 += r[1];
			}
			auto d = cnt0 - cnt1;
			auto d2 = h[pos] - d;
			auto remain = p[pos] - abs(d2);
			if (remain < 0 && d2 > 0)
			{
				cnt0 -= remain;
				cnt1 += remain;
				d2 += remain;
				remain = 0;
			}
			if (remain < 0 || remain % 2 || cnt0 < 0)
			{
				debug writeln("false pos:", pos, " remain:", remain);
				ok = false;
			}
			if (d2 > 0)
				cnt0 += d2;
			else
				cnt1 += abs(d2);
			cnt0 += remain / 2;
			cnt1 += remain / 2;
			return [cnt0, cnt1];
		}
		auto r = dfs(0, -1);
		debug writeln("m:", m, " ", r);
		if (r[0] + r[1] != m)
			ok = false;

		ans[ti] = ok;
	}

	foreach (e; ans)
	{
		writeln(e ? "YES" : "NO");
	}
	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 bool[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto m = RD!int;
		auto p = RDA!int;
		auto h = RDA!int;
		auto edges = new int[][](n);
		foreach (i; 0..n-1)
		{
			auto x = RD!int-1;
			auto y = RD!int-1;
			edges[x] ~= y;
			edges[y] ~= x;
		}

		bool ok = true;
		long dfs(int pos, int last)
		{
			long cnt = p[pos];
			foreach (to; edges[pos])
			{
				if (to == last) continue;
				cnt += dfs(to, pos);
			}
			auto d = abs(cnt - h[pos]);
			if (d % 2 || abs(h[pos]) > cnt)
				ok = false;
			return cnt;
		}
		if (dfs(0, -1) != m)
			ok = false;

		ans[ti] = ok;
	}

	foreach (e; ans)
	{
		writeln(e ? "YES" : "NO");
	}
	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 bool[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto m = RD!int;
		auto p = RDA!int;
		auto h = RDA!int;
		auto edges = new int[][](n);
		foreach (i; 0..n-1)
		{
			auto x = RD!int-1;
			auto y = RD!int-1;
			edges[x] ~= y;
			edges[y] ~= x;
		}

		bool ok = true;
		int dfs(int pos, int last)
		{
			int cnt = p[pos];
			foreach (to; edges[pos])
			{
				if (to == last) continue;
				cnt += dfs(to, pos);
			}
			auto d = abs(cnt - h[pos]);
			if (d % 2 || abs(h[pos]) > cnt)
				ok = false;
			return cnt;
		}
		dfs(0, -1);

		ans[ti] = ok;
	}

	foreach (e; ans)
	{
		writeln(e ? "YES" : "NO");
	}
	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 bool[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto m = RD!int;
		auto p = RDA!int;
		auto h = RDA!int;
		auto edges = new int[][](n);
		foreach (i; 0..n-1)
		{
			auto x = RD!int-1;
			auto y = RD!int-1;
			edges[x] ~= y;
			edges[y] ~= x;
		}

		bool ok = true;
		int dfs(int pos, int last)
		{
			int cnt = p[pos];
			foreach (to; edges[pos])
			{
				if (to == last) continue;
				cnt += dfs(to, pos);
			}
			auto d = abs(cnt - h[pos]);
			if (d % 2 || abs(h[pos]) > cnt)
				ok = false;
			return cnt;
		}
		if (dfs(0, -1) != m)
			ok = false;

		ans[ti] = ok;
	}

	foreach (e; ans)
	{
		writeln(e ? "YES" : "NO");
	}
	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 bool[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto m = RD!int;
		auto p = RDA!int;
		auto h = RDA!int;
		auto edges = new int[][](n);
		foreach (i; 0..n-1)
		{
			auto x = RD!int-1;
			auto y = RD!int-1;
			edges[x] ~= y;
			edges[y] ~= x;
		}

		bool ok = true;
		long[2] dfs(int pos, int last)
		{
			long cnt0, cnt1;
			foreach (to; edges[pos])
			{
				if (to == last) continue;
				auto r = dfs(to, pos);
				cnt0 += r[0];
				cnt1 += r[1];
			}
			auto d = cnt0 - cnt1;
			auto v = h[pos] - d;
			auto remain = p[pos] - abs(v);
			if (remain < 0 && v > 0)
			{
				cnt0 -= remain;
				cnt1 += remain;
				v += remain;
				remain = 0;
			}
			if (remain < 0 || remain % 2 || cnt1 < 0)
			{
				debug writeln("false pos:", pos, " remain:", remain);
				ok = false;
			}
			if (v > 0)
				cnt0 += v;
			else
				cnt1 += abs(v);
			cnt0 += remain / 2;
			cnt1 += remain / 2;
			return [cnt0, cnt1];
		}
		auto r = dfs(0, -1);
		debug writeln("m:", m, " ", r);
		if (r[0] + r[1] != m)
			ok = false;

		ans[ti] = ok;
	}

	foreach (e; ans)
	{
		writeln(e ? "YES" : "NO");
	}
	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 bool[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto m = RD!int;
		auto p = RDA!int;
		auto h = RDA!int;
		auto edges = new int[][](n);
		foreach (i; 0..n-1)
		{
			auto x = RD!int-1;
			auto y = RD!int-1;
			edges[x] ~= y;
			edges[y] ~= x;
		}

		bool ok = true;
		long dfs(int pos, int last)
		{
			long cnt = p[pos];
			foreach (to; edges[pos])
			{
				if (to == last) continue;
				cnt += dfs(to, pos);
			}
			if ((cnt % 2 != abs(h[pos]) % 2) || (abs(h[pos]) > cnt))
				ok = false;
			return cnt;
		}
		if (dfs(0, -1) != m)
			ok = false;

		ans[ti] = ok;
	}

	foreach (e; ans)
	{
		writeln(e ? "YES" : "NO");
	}
	stdout.flush;
	debug readln;
}


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) {
    int m, n;
    readf!" %d %d "(m, n);
    int[] frjoy = new int[](n);
    int[][] joy;
    int sharedshopjoy;
    foreach (i ; 0 .. m) {
      joy ~= readln.splitter.map!(to!int).array;
      foreach (j ; 0 .. n)
        frjoy[j] = max(frjoy[j], joy.back[j]);
      joy.back.sort;
      sharedshopjoy = max(sharedshopjoy, joy.back[$ - 2]);
    }
    int ans = frjoy.minElement;
    if (n - 1 < m)
      ans = min(ans, sharedshopjoy);
    writeln(ans);
  }
}

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

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

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

void solve() {
    readln;
    int M, N; readf("%d %d\n", &M, &N);
    auto P = new int[][M];
    foreach (ref L; P) L = readarray!int;

    bool C(int x) {
        bool[int] s;
        for (int j = 0; j < N; j++) {
            int c = 0;
            for (int i = 0; i < M; i++) {
                if (P[i][j] >= x) {
                    c++;
                }
            }
            if (c == 0) return false;
        }
        for (int j = 0; j < N; j++) {
            for (int i = 0; i < M; i++) {
                if (P[i][j] >= x) {
                    if (i in s) return true;
                    s[i] = true;
                }
            }
        }
        return false;
    }
    int lb = 0, ub = 1_000_000_010;
    while (lb + 1 < ub) {
        int mid = (lb + ub) / 2;
        (C(mid) ? lb : ub) = mid;
    }
    writeln(lb);
}


import std.stdio;
import std.string : split, strip;
import std.algorithm : any, count, map, sort;
import std.conv : to;
import std.array;
import std.range : slide;

static bool even(ulong a) { return (a & 1) == 0; }

void main()
{
  int t;
  readf!"%d\n"(t);

  foreach (i; 0..t) {
    int n;
    readf!"%d\n"(n);
    auto a = readln.strip.split.map!(to!uint).array;
    a.sort;

    auto d = a.slide(2).any!"a[1] - a[0] == 1";
    auto c = a.count!even;

    if (even(c) || d) {
      writeln("YES");
    } else {
      writeln("NO");
    }
  }
}

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

string FMT_F = "%.10f";

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

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

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

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

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

		long cnt;
		foreach (i; 0..n)
		{
			if (a[i] % 2)
				++cnt;
		}
		if (cnt % 2 == 0 && (n-cnt) % 2 == 0)
			ans[ti] = true;
		else if (cnt % 2 && (n-cnt % 2))
		{
			a.sort();
			foreach (i; 1..n)
			{
				if (a[i]-1 == a[i-1])
					ans[ti] = true;
			}
		}
	}

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

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

alias type = multi;
struct TestCase
{
  mixin prettyPrinting;

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

  void solve(long tc = -1)
  {
    sort(a);
    long p = a.count!(ai => ai % 2 == 0), i = a.count!(ai => ai % 2 == 1);
    bool has1p = iota(0, n - 1).map!(i => abs(a.at(i) - a.at(i + 1))).canFind(1);
    if ((p + i) % 2 == 0)
      {
        if (p % 2 == 0)
          {
            writeln("YES");
          }
        else
          {
            if (has1p)
              {
                writeln("YES");
              }
            else
              {
                writeln("NO");
              }
          }
      }
    else
      {
        writeln("NO");
      }
  }
}

struct Interval
{
  long l, h;

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

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

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

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

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

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

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

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

DList!string _words;

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

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

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

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

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

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

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

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

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

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

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

    }
}

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

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

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

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

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

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

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

void main()
{
  type;
}


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

  int t;
  rd(t);
  while (t--) {
    int a;
    rd(a);
    bool ok = false;
    for (int n = 3; n <= 360; n++) {
      if (360 % n == 0) {
        auto x = 180 - 360 / n;
        if (a * (n - 2) % x == 0) {
          if (a * (n - 2) / x <= (n - 2)) {
            writeln(n);
            ok = true;
            break;
          }
        }
      }
    }
    enforce(ok);
  }

}

/*

3 60 60
4 90 45
5 108 36
6 120 30
7 180-360/7 x/5
8 135 135/6
.
.
.
n 180-360/n:=x x/(n-2)

*/

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

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

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

alias Tree = RBT!int;
alias ll = long;

void main() {
  GC.disable();

  int[] cc = new int[200];
  cc[] = 0;

  foreach(n; 3 .. 2000) {
    foreach(i; 0 .. n-1) {
      if ( (i*180) % n == 0) {
        uint a = i*180 / n;
        if (cc[a] == 0) {
          cc[a] = n;
        }
      }
    }
  }

  uint T;
  readf(" %s", T);
  foreach(i; 0 .. T) {
    uint ang;
    readf(" %s", ang);
    if (cc[ang] != 0) writeln(cc[ang]);
    else writeln(-1);
  }

}

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

void main() {
    auto T = readln.chomp.to!int;
    while (T--) {
        auto X = readln.chomp.to!long;
        auto Y = 180L;
        auto G = gcd(X, Y);
        X /= G;
        Y /= G;
        if (Y - X == 1) {
            Y *= 2;
        }
        writeln(Y);
    }
}

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

alias Tree = RBT!int;
alias ll = long;

void main() {
  GC.disable();

  int[] cc = new int[200];
  cc[] = 0;

  foreach(n; 3 .. 200) {
    foreach(i; 0 .. n-1) {
      if ( (i*180) % n == 0) {
        uint a = i*180 / n;
        if (cc[a] == 0) {
          cc[a] = n;
        }
      }
    }
  }

  uint T;
  readf(" %s", T);
  foreach(i; 0 .. T) {
    uint ang;
    readf(" %s", ang);
    if (cc[ang] != 0) writeln(cc[ang]);
    else writeln(-1);
  }

}


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

int n;
int[] a;

void main() {
    scan(n);
    a = readln.split.to!(int[]);

    int f = a[0], s = a[1], t = a[2];

    if (f > s) swap(f, s);
    if (s > t) swap(s, t);
    if (f > s) swap(f, s);

    int tmp1, tmp2;

    foreach (i ; 3 .. n) {
        if (a[i] < f) {
            tmp1 = f, tmp2 = s;
            f = a[i], s = tmp1, t = tmp2;
        }
        else if (a[i] < s) {
            tmp1 = s;
            s = a[i], t = tmp1;
        }
        else if (a[i] < t) {
            t = a[i];
        }
    }

    debug {
        stderr.writeln(f, " ", s, " ", t);
    }

    long x = a.count(t), ans;

    if (f < s) {
        if (s < t) {
            ans = x;
        }
        else {
            ans = x * (x - 1) / 2;
        }
    }
    else if (s < t) {
        ans = x;
    }
    else {
        ans = x * (x - 1) * (x - 2) / 6;
    }

    writeln(ans);
}

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

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

immutable int mod = 10^^9 + 7;

int n;
int[] a;

void main() {
    n = readln.chomp.to!int;
    a = readln.split.to!(int[]);

    a.sort();
    long ans, x;

    if (a[0] < a[1]) {
        if (a[1] < a[2]) {
            ans = a.count(a[2]);
        }
        else if (a[1] == a[2] && a[2] < a[3]) {
            ans = 1;
        }
        else {
            x = a.count(a[1]);
            ans = x * (x - 1) / 2;
        }
    }
    else if (a[0] == a[1] && a[1] < a[2]) {
        if (a[2] < a[3]) {
            ans = 1;
        }
        else {
            ans = a.count(a[2]);
        }
    }
    else {
        x = a.count(a[0]);
        ans = x * (x - 1) * (x - 2) / 6;
    }

    writeln(ans);
}

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

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

int n;
int[] a;

void main() {
    scan(n);
    a = readln.split.to!(int[]);

    a.sort();

    long ans, x;

    x = a.count(a[2]);

    if (a[0] < a[1]) {
        if (a[1] < a[2]) {
            ans = x;
        }
        else {
            ans = x * (x - 1) / 2;
        }
    }
    else if (a[1] < a[2]) {
        ans = x;
    }
    else {
        ans = x * (x - 1) * (x - 2) / 6;
    }

    writeln(ans);
}

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


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

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

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

        string[] tokens;
}

void main() {
        IO cin;
        int t = 1;
        // int t = cin.read_int;
        while (t--) {
                int n = cin.read_int;
                char[][] mat = new char[][n];

                int comb = 0;
                //row-major traversal
                for (int i = 0; i < n; i++) {
                        int count = 0;
                        mat[i] = new char[n];
                        mat[i] = cin.read_string.dup;
                        for (int j = 0; j < n; j++) {
                                if (mat[i][j] == 'C') count++;
                        }
                        comb += (count * (count - 1)) / 2;
                }

                //column-major traversal
                for (int i = 0; i < n; i++) {
                        int count = 0;
                        for (int j = 0; j < n; j++) {
                                if (mat[j][i] == 'C') count++;
                        }
                        comb += (count * (count - 1)) / 2;
                }

                writeln(comb);
        }
}

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

void main() {
    int n = readln.chomp.to!int;
    string[] cake;
    foreach (i; 0..n) {
        cake ~= readln.chomp;
    }

    int ans = 0;
    foreach (row; cake) {
        auto c = row.count!("a == 'C'");
        if (c > 1) {
            ans += c * (c - 1) / 2;
        }
    }

    foreach (col; cake.transposed) {
        auto c = col.count!("a == 'C'");
        if (c > 1) {
            ans += c * (c - 1) / 2;
        }
    }

    ans.writeln;
}

import std.stdio;

void main()
{
	int n;

	scanf("%d", &n);

	char[][] m = new char[][n];

	for (int i = 0; i < n; i++) {
		m[i] = new char[n + 1];
		scanf("%s", m[i].ptr);
	}

	int res = 0;
	for (int i = 0; i < n; i++) {
		int r = 0;
		int c = 0;
		for (int j = 0; j < n; j++) {
			if (m[j][i] == 'C')
				c++;
			if (m[i][j] == 'C')
				r++;
		}

		res += (r - 1) * r / 2;
		res += (c - 1) * c / 2;
	}

	printf("%d\n", res);
}

import std.stdio;
import std.conv;
import std.string;
import std.array: Appender;
import std.datetime;
import std.algorithm;

void main()
{

	auto n = to!int(strip(readln));
	int[] r = new int[n];
	int[] c = new int[n];
	for (auto i = 0; i < n; i++)
	{
		auto str = strip(readln);
		foreach (j, ch; str)
		{
			if (ch == 'C')
			{
				r[i]++;
				c[j]++;
			}
		}
	}
	int count;
	foreach (i, ele; r)
	{
		count += (r[i] * (r[i] - 1)) / 2 + (c[i] * (c[i] - 1)) / 2;
	}

	writeln(count);
}


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

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

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

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

    s.sort();

    if (s.fold!gcd != s[0]) {
        writeln(-1);
        return;
    }

    int[] ans;
    foreach (e; s) {
        ans ~= e;
        ans ~= s[0];
    }

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

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

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

    foreach (j ; 1 .. m) {
        if (s[j] % s[0]) {
            writeln(-1);
            return;
        }
    }

    auto ans = new int[](0);
    ans ~= s[0];

    foreach (i ; 1 .. m) {
        ans ~= s[i];
        ans ~= s[0];
    }

    int n = ans.length.to!int;

    writeln(n);
    writefln("%(%s %)",ans);
}

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

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

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

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

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

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

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

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

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

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


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)
	{
		auto n = readln.strip.to !(int);
		auto a = readln.splitter.map !(to !(int)).array;
		auto c = n.iota.array;

		alias Edge = Tuple !(int, q{u}, int, q{v});
		Edge [] answer;
		foreach (i; 0..n)
		{
			foreach (j; i + 1..n)
			{
				if (a[i] != a[j] && c[i] != c[j])
				{
					answer ~= Edge (i, j);
					int u = c[i];
					foreach (k; 0..n)
					{
						if (c[k] == u)
						{
							c[k] = c[j];
						}
					}
				}
			}
		}
		if (answer.length >= n - 1)
		{
			writeln ("YES");
			foreach (ref e; answer)
			{
				writeln (e.u + 1, " ", e.v + 1);
			}
		}
		else
		{
			writeln ("NO");
		}
	}
}

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

ll[]  arr;
bool[] vis;
int[] par;

int dfs(int v){
    vis[v] = 1;
    int summ = 1;
    foreach(i; 0..arr.length){
        if(!vis[i] && arr[i] != arr[v]){
            par[i] = v;
            summ += dfs(i.to!int);
        }
    }
    return summ;
}

void play(){
    int n;
    n = rd!int;
    arr = rdarr;
    par = new int[n];
    vis = new bool[n];
    foreach(i; 0..n){
        par[] = 0;
        vis[] = 0;
        par[i] = -1;
        int found = dfs(i);
        if(found == n){

            writeln("YES");
            foreach(k; 0..n){
                if(par[k] != -1){
                    writeln(k+1, " ", par[k] + 1);
                }
            }
            return;
        }
    }
    writeln("NO");
}

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

string FMT_F = "%.10f";

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

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

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

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

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

		bool ok;
		foreach (i; 0..n)
		{
			if (a[i] != a[0])
			{
				ok = true;
				break;
			}
		}
		if (!ok) continue;

		auto used = new bool[](n*n);
		foreach (i; 0..n)
		{
			foreach (j; 0..n)
			{
				if (a[i] != a[j])
				{
					auto u = min(i, j);
					auto v = max(i, j);
					if (!used[u*n+v])
					{
						used[u*n+v] = true;
						ans[ti] ~= [u, v];
					}
					break;
				}
			}
		}
	}

	foreach (e; ans)
	{
		if (e.empty)
			writeln("NO");
		else
		{
			writeln("YES");
			foreach (ee; e)
			{
				writeln(ee[0]+1, " ", ee[1]+1);
			}
		}
	}
	stdout.flush;
	debug readln;
}

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

void play(){
    int n;
    n = rd!int;
    ll[] arr = rdarr;
    auto par = new ll[n];
    auto vis = new bool[n];
    vis[0] = 1; par[0] = -1;
    foreach(i; 0..n){
        if(!vis[i]){
            foreach(j; 0..n){
                if(vis[j] && arr[j] != arr[i]){
                    par[i] = j; vis[i] = 1;
                    break;
                }
            }
        }
        if(!vis[i]){
            writeln("NO");
            return;
        }
    }
    writeln("YES");
    foreach(i; 0..n){
        if(par[i] != -1){
            writeln(i+1, " ", par[i] + 1);
        }
    }
}

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!(long, long);
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.conv;
import std.range;
import std.stdio;
import std.string;

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		int n, k;
		readf !(" %s %s") (n, k);
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		writeln (a.canFind (1) ? "YES" : "NO");
	}
}

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;
        const K = readInt;
        auto A = new int[N];
        foreach (i; 0 .. N) {
          A[i] = readInt;
        }

        bool ans;
        foreach (i; 0 .. N) {
          ans = ans || (A[i] == 1);
        }
        writeln(ans ? "YES" : "NO");
      }
    }
  } catch (EOFException e) {
  }
}


import std.stdio, std.string, std.conv;
import std.typecons;
import std.algorithm;
import std.math;
void main(){
  int n = readln.chomp.to!int;
  auto ls = readln.chomp.split.map!(to!long);

  long s = 0;
  foreach(l; ls){
    s += l;
  }

  long m = 0;
  foreach(l; ls){
    m = max(m, l);
  }
  long res = abs(2*m-s)+1;

  writeln(res);

}

module main;

import std.stdio;
import std.algorithm;
import std.math;
import std.random;
import std.conv;

void main ()
{
    int n,sum = 0,add;
    readf(" %s", &n);
    int [] a =  new int [](n);
    for ( int i = 0; i < n; i++)
    {
        readf(" %s", &a[i]);
        sum += a[i];
    }
    for ( int i = 0; i< n; i++)
        if ( a[i] >= sum - a[i]) add = (abs(sum - 2*a[i])+ 1);
    writeln(add);

}

import std.stdio, std.string, std.conv;
import std.typecons;
import std.algorithm;
import std.math;
void main(){
  int n = readln.chomp.to!int;
  auto ls = readln.chomp.split.map!(to!long);

  long res = 0;
  foreach(long l; ls){
    res = abs (res - l);
  }

  writeln(res+1);

}


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

void main()
{
  int t;
  readf!" %d "(t);
  while (t--) {
    auto a = readln.strip.split("").map!((x) => (x[0] == '?') ? 3 : (x[0] == '0' ? 1 : 2));
//    writeln(a);
    long[][2] dp;
    bool[] avail;
    dp[0] = new long[](a.length);
    dp[1] = new long[](a.length);
    dp[0][0] = (a[0] & 1) ? 1 : 0;
    dp[1][0] = (a[0] & 2) ? 1 : 0;
    foreach (i ; 1 .. a.length) {
      if (a[i] & 1)
        dp[0][i] = dp[1][i - 1] + 1;
      else
        dp[0][i] = 0;
      if (a[i] & 2)
        dp[1][i] = dp[0][i - 1] + 1;
      else
        dp[1][i] = 0;
    }
//    writeln(dp);
    long result = 0;
    foreach (i ; 0 .. a.length) {
      result += max(dp[0][i], dp[1][i]);
    }
    writeln(result);
  }
}

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 s = readln.strip;
		auto n = s.length.to !(int);
		int prev = -1;
		int mark = -1;
		int odd = -3;
		long res = 0;
		foreach (i, c; s)
		{
			odd ^= 1;
			if (c != '?')
			{
				if (odd >= 0 && odd != c - '0')
				{
					prev = mark;
				}
				odd = c - '0';
				mark = i;
			}
			res += i - prev;
		}
		writeln (res);
	}
}

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

int main()
{
    int t = readInt!int;
    foreach(ti; 0 .. t)
    {
        string str = readString;
        auto a = new int[](str.length);
        foreach(i, ref ai; a)
            switch(str[i])
            {
            case '0': ai = 0 ^ (i & 1); continue;
            case '1': ai = 1 ^ (i & 1); continue;
            default: ai = -1; continue;
            }
        debug writeln(a);
        int c0 = 0;
        int c1 = 0;
        long result = 0;
        foreach(i, ai; a)
        {
            if (ai == 0) c0++, c1 = 0;
            if (ai == 1) c1++, c0 = 0;
            if (ai == -1) c0++, c1++;
            if (ai == 0) result += c0;
            if (ai == 1) result += c1;
            if (ai == -1) result += max(c0, c1);
        }
        result.writeln;
    }
    return 0;
}

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

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

void main()
{
  int t;
  readf!" %d "(t);
  while (t--) {
    auto a = readln.strip.split("").map!((x) => (x[0] == '?') ? 3 : (x[0] == '0' ? 1 : 2));
    auto dp = new long[][](a.length, 2);
    dp[0][0] = (a[0] & 1) ? 1 : 0;
    dp[0][1] = (a[0] & 2) ? 1 : 0;
    foreach (i ; 1 .. a.length) {
      dp[i][0] = (a[i] & 1) ? dp[i - 1][1] + 1 : 0;
      dp[i][1] = (a[i] & 2) ? dp[i - 1][0] + 1 : 0;
    }
    writeln(dp.fold!"a + b.maxElement"(0L));
  }
}


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

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

//alias T = Tuple!(int, int);
//alias TR = RedBlackTree!(T);
alias TR = RedBlackTree!(int, "a<b", true);

void main() {
  auto r = new InputReader ();
  const n = r.next!uint;
  const k = r.next!uint;
  auto _m = r.nextA!uint(n);
  auto m = assumeUnique (_m);
  auto _c = r.nextA!uint(k);
  auto c = assumeUnique (_c);
  int j = k - 1;
  auto x = new int[n+1];
  x[0] = k;
  foreach (t; 1 .. n + 1) {
    while (j >= 0 && c[j] == t) --j;
    x[t] = j + 1;
  }
  debug stderr.writeln (x);
  auto t = new TR;
  foreach (i, z; m) {
    //t.insert (T (z, 1 + i.to!int));
    t.insert (z);
  }
  int[][] ans;
  while (!t.empty) {
    int cur = 1;
    int[] b;
    while (cur <= n) {
      //auto rng = t.upperBound (T (x[cur], -1));
      auto rng = t.lowerBound (x[cur - 1] + 1);
      if (rng.empty) break;
      //auto q = m[rng.back];
      auto q = rng.back;
      b ~= q;
      t.removeKey (q);
      ++cur;
    }
    ans ~= b;
  }
  writeln (ans.length);
  foreach (const ref q; ans) {
    writefln!("%d %(%s %)")(q.length, q);
  }

}

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

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

		sort !(q{a > b}) (m);
		int [] [] answer;
		foreach (x; m)
		{
			int p = 0;
			int lo = 0;
			int hi = answer.length;
			while (lo < hi)
			{
				int me = (lo + hi) / 2;
				if (answer[me].length < c[x])
				{
					hi = me;
				}
				else
				{
					lo = me + 1;
				}
			}
			if (lo == answer.length)
			{
				answer ~= null;
			}
			answer[lo] ~= x;
		}

		writeln (answer.length);
		foreach (ref line; answer)
		{
			writefln !("%s%( %s%)") (line.length, line);
		}
	}
}