import std;

void main () {
    immutable tests = (){ uint t; readf!"%s\n"(t); return t; }();

    foreach (test_index; 0 .. tests) {
        readln;

        immutable str = readln.chomp;

        immutable cut = (){
        immutable start = str[0];
        foreach (ret; 1 .. str.length)
                if (str[ret] == start || str[ret] > str[ret - 1])
                    return ret;
            return str.length;
        }();

        (
            str[0 .. cut]
            ~ str[0 .. cut]
                .byCodeUnit
                .retro
                .array
        ).writeln();
    }
}
// ""

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;

bool lexsmaller2(int idx, string rs, int[] cache)
{
  if (cache[idx] != -1)
    return cast(bool)cache[idx];
  if (idx + 2 >= rs.length)
    return true;
  assert(idx + 2 < rs.length);
  if (rs[idx] < rs[idx + 2])
    return cache[idx] = 1;
  if (rs[idx] > rs[idx + 2])
    return cache[idx] = 0;

  return (cache[idx] = lexsmaller2(idx + 1, rs, cache)) != 0;
}

bool lexsmaller(char newch, int idx, string rs, int[] cache)
{
  if (newch < rs[idx])
    return true;
  if (newch > rs[idx])
    return false;

  if (idx + 1 >= rs.length)
    return false;

  if (newch < rs[idx + 1])
    return true;
  if (newch > rs[idx + 1])
    return false;
  return lexsmaller2(idx + 2, rs, cache);
}

void main()
{
  foreach (casenum ; 1 .. readln.strip.to!int + 1) {
    int n;
    readf!" %d "(n);
    auto s = readln.strip;
    auto rs = s.dup.reverse.idup;
    int ans = 0;
    int[] cache = new int[](n);
    cache[] = -1;
    foreach (k ; 1 .. n) {
      int idx = n - k;
      char newch = s[k];
      if (lexsmaller(newch, idx, rs, cache)) {
        ans = k;
      } else {
        break;
      }
    }
    writeln(s[0 .. ans + 1] ~ s[0 .. ans + 1].dup.reverse.idup);
  }
}

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto s = readln.strip;

		alias mirror = len => s[0..len] ~ s[0..len].byChar.retro.array;

		int len = 1;
		bool first = true;
		int [] cand = [1, n];
		foreach (i; 0..n - 1)
		{
			if (s[i + 1] < s[i])
			{
				cand ~= i + 1;
			}
			if (s[i + 1] > s[i])
			{
				cand ~= i + 1;
				break;
			}
		}
		debug {writeln (cand);}
		cand.map !(mirror).minElement.writeln;
	}
}

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

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

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

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

void main() {
  try {
    for (; ; ) {
      const numCases = readInt;
      foreach (caseId; 0 .. numCases) {
        const N = readInt;
        const S = readToken;

        char[] ans;
        if (N >= 2 && S[0] == S[1]) {
          ans ~= S[0];
          ans ~= S[0];
        } else {
          char[] cs;
          foreach (i; 0 .. N) {
            if (i > 0 && S[i - 1] < S[i]) {
              break;
            }
            cs ~= S[i];
          }
          ans ~= cs;
          cs.reverse;
          ans ~= cs;
        }

        writeln(ans);
      }
    }
  } catch (EOFException e) {
  }
}

void main() { runSolver(); }

void problem() {
  auto QN = scan!long;

  auto solve() {
    auto subSolve() {
      auto N = scan!int;
      auto S = cast(char[])scan;

      int k = 1;
      char last2 = S[0];
      char last = S[0];
      int eql;
      foreach(int i; 1..N) {
        // deb([i, k], [[last], [last2], [S[i]]]);
        if (i == 1) {
          if (last <= S[i]) break;
        } else {
          if (last < S[i] || last2 < S[i]) break;
        }

        last2 = last;
        last = S[i];
        k++;
      }

      return (S[0..k] ~ S[0..k].dup.reverse).to!string;
    }

    foreach(_; 0..QN) subSolve().writeln;
  }

  outputForAtCoder(&solve);
}

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

import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric, std.traits, std.functional, std.bigint, std.datetime.stopwatch, core.time, core.bitop;
T[][] combinations(T)(T[] s, in long m) {   if (!m) return [[]];   if (s.empty) return [];   return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; }
T[] scan(T)(long n){ return n.iota.map!(i => scan!T()).array; }
void deb(T ...)(T t){ debug writeln(t); }
alias Point = Tuple!(long, "x", long, "y");
Point invert(Point p) { return Point(p.y, p.x); }
long[] divisors(long n) { long[] ret; for (long i = 1; i * i <= n; i++) { if (n % i == 0) { ret ~= i; if (i * i != n) ret ~= n / i; } } return ret.sort.array; }
bool chmin(T)(ref T a, T b) { if (b < a) { a = b; return true; } else return false; }
bool chmax(T)(ref T a, T b) { if (b > a) { a = b; return true; } else return false; }
string charSort(alias S = "a < b")(string s) { return (cast(char[])((cast(byte[])s).sort!S.array)).to!string; }
ulong comb(ulong a, ulong b) { if (b == 0) {return 1;}else{return comb(a - 1, b - 1) * a / b;}}
T[] compress(T)(T[] arr, T origin = T.init) { T[T] indecies; arr.dup.sort.uniq.enumerate(origin).each!((i, t) => indecies[t] = i); return arr.map!(t => indecies[t]).array; }
string toAnswerString(R)(R r) { return r.map!"a.to!string".joiner(" ").array.to!string; }
struct ModInt(uint MD) if (MD < int.max) {ulong v;this(string v) {this(v.to!long);}this(int v) {this(long(v));}this(long v) {this.v = (v%MD+MD)%MD;}void opAssign(long t) {v = (t%MD+MD)%MD;}static auto normS(ulong x) {return (x<MD)?x:x-MD;}static auto make(ulong x) {ModInt m; m.v = x; return m;}auto opBinary(string op:"+")(ModInt r) const {return make(normS(v+r.v));}auto opBinary(string op:"-")(ModInt r) const {return make(normS(v+MD-r.v));}auto opBinary(string op:"*")(ModInt r) const {return make((ulong(v)*r.v%MD).to!ulong);}auto opBinary(string op:"^^", T)(T r) const {long x=v;long y=1;while(r){if(r%2==1)y=(y*x)%MD;x=x^^2%MD;r/=2;} return make(y);}auto opBinary(string op:"/")(ModInt r) const {return this*memoize!inv(r);}static ModInt inv(ModInt x) {return x^^(MD-2);}string toString() const {return v.to!string;}auto opOpAssign(string op)(ModInt r) {return mixin ("this=this"~op~"r");}}
alias MInt1 = ModInt!(10^^9 + 7);
alias MInt9 = ModInt!(998_244_353);
void outputForAtCoder(T)(T delegate() fn) {
  static if (is(T == float) || is(T == double) || is(T == real)) "%.16f".writefln(fn());
  else static if (is(T == void)) fn();
  else static if (is(T == string)) fn().writeln;
  else static if (isInputRange!T) {
    static if (!is(string == ElementType!T) && isInputRange!(ElementType!T)) foreach(r; fn()) r.toAnswerString.writeln;
    else foreach(r; fn()) r.writeln;
  }
  else fn().writeln;
}
void runSolver() {
  enum BORDER = "==================================";
  debug { BORDER.writeln; while(!stdin.eof) { "<<< Process time: %s >>>".writefln(benchmark!problem(1)); BORDER.writeln; } }
  else problem();
}
enum YESNO = [true: "Yes", false: "No"];

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

K binarySearch(K)(bool delegate(K) cond, K l, K r) { return binarySearch((K k) => k, cond, l, r); }
T binarySearch(T, K)(K delegate(T) fn, bool delegate(K) cond, T l, T r) {
  auto ok = l;
  auto ng = r;
  const T TWO = 2;

  bool again() {
    static if (is(T == float) || is(T == double) || is(T == real)) {
      return !ng.approxEqual(ok, 1e-08, 1e-08);
    } else {
      return abs(ng - ok) > 1;
    }
  }

  while(again()) {
    const half = (ng + ok) / TWO;
    const halfValue = fn(half);

    if (cond(halfValue)) {
      ok = half;
    } else {
      ng = half;
    }
  }

  return ok;
}

import std;

void main () {
    immutable tests = (){ uint t; readf!"%s\n"(t); return t; }();

    foreach (test_index; 0 .. tests) {
        readln;

        immutable str = readln.chomp;

        immutable cut = (){
        foreach (ret; 1 .. str.length)
                if (str[ret] >= str[ret - 1])
                    return ret;
            return str.length;
        }();

        (
            str[0 .. cut]
            ~ str[0 .. cut]
                .byCodeUnit
                .retro
                .array
        ).writeln();
    }
}
// ""

void main() { runSolver(); }

void problem() {
  auto QN = scan!long;

  auto solve() {
    auto subSolve() {
      auto N = scan!int;
      auto S = cast(char[])scan;

      int k = 1;
      foreach(int i; 1..N) {
        if (S[i] >= S[i - 1]) break;
        k++;
      }

      return (S[0..k] ~ S[0..k].dup.reverse).to!string;
    }

    foreach(_; 0..QN) subSolve().writeln;
  }

  outputForAtCoder(&solve);
}

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

import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric, std.traits, std.functional, std.bigint, std.datetime.stopwatch, core.time, core.bitop;
T[][] combinations(T)(T[] s, in long m) {   if (!m) return [[]];   if (s.empty) return [];   return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; }
T[] scan(T)(long n){ return n.iota.map!(i => scan!T()).array; }
void deb(T ...)(T t){ debug writeln(t); }
alias Point = Tuple!(long, "x", long, "y");
Point invert(Point p) { return Point(p.y, p.x); }
long[] divisors(long n) { long[] ret; for (long i = 1; i * i <= n; i++) { if (n % i == 0) { ret ~= i; if (i * i != n) ret ~= n / i; } } return ret.sort.array; }
bool chmin(T)(ref T a, T b) { if (b < a) { a = b; return true; } else return false; }
bool chmax(T)(ref T a, T b) { if (b > a) { a = b; return true; } else return false; }
string charSort(alias S = "a < b")(string s) { return (cast(char[])((cast(byte[])s).sort!S.array)).to!string; }
ulong comb(ulong a, ulong b) { if (b == 0) {return 1;}else{return comb(a - 1, b - 1) * a / b;}}
T[] compress(T)(T[] arr, T origin = T.init) { T[T] indecies; arr.dup.sort.uniq.enumerate(origin).each!((i, t) => indecies[t] = i); return arr.map!(t => indecies[t]).array; }
string toAnswerString(R)(R r) { return r.map!"a.to!string".joiner(" ").array.to!string; }
struct ModInt(uint MD) if (MD < int.max) {ulong v;this(string v) {this(v.to!long);}this(int v) {this(long(v));}this(long v) {this.v = (v%MD+MD)%MD;}void opAssign(long t) {v = (t%MD+MD)%MD;}static auto normS(ulong x) {return (x<MD)?x:x-MD;}static auto make(ulong x) {ModInt m; m.v = x; return m;}auto opBinary(string op:"+")(ModInt r) const {return make(normS(v+r.v));}auto opBinary(string op:"-")(ModInt r) const {return make(normS(v+MD-r.v));}auto opBinary(string op:"*")(ModInt r) const {return make((ulong(v)*r.v%MD).to!ulong);}auto opBinary(string op:"^^", T)(T r) const {long x=v;long y=1;while(r){if(r%2==1)y=(y*x)%MD;x=x^^2%MD;r/=2;} return make(y);}auto opBinary(string op:"/")(ModInt r) const {return this*memoize!inv(r);}static ModInt inv(ModInt x) {return x^^(MD-2);}string toString() const {return v.to!string;}auto opOpAssign(string op)(ModInt r) {return mixin ("this=this"~op~"r");}}
alias MInt1 = ModInt!(10^^9 + 7);
alias MInt9 = ModInt!(998_244_353);
void outputForAtCoder(T)(T delegate() fn) {
  static if (is(T == float) || is(T == double) || is(T == real)) "%.16f".writefln(fn());
  else static if (is(T == void)) fn();
  else static if (is(T == string)) fn().writeln;
  else static if (isInputRange!T) {
    static if (!is(string == ElementType!T) && isInputRange!(ElementType!T)) foreach(r; fn()) r.toAnswerString.writeln;
    else foreach(r; fn()) r.writeln;
  }
  else fn().writeln;
}
void runSolver() {
  enum BORDER = "==================================";
  debug { BORDER.writeln; while(!stdin.eof) { "<<< Process time: %s >>>".writefln(benchmark!problem(1)); BORDER.writeln; } }
  else problem();
}
enum YESNO = [true: "Yes", false: "No"];

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

K binarySearch(K)(bool delegate(K) cond, K l, K r) { return binarySearch((K k) => k, cond, l, r); }
T binarySearch(T, K)(K delegate(T) fn, bool delegate(K) cond, T l, T r) {
  auto ok = l;
  auto ng = r;
  const T TWO = 2;

  bool again() {
    static if (is(T == float) || is(T == double) || is(T == real)) {
      return !ng.approxEqual(ok, 1e-08, 1e-08);
    } else {
      return abs(ng - ok) > 1;
    }
  }

  while(again()) {
    const half = (ng + ok) / TWO;
    const halfValue = fn(half);

    if (cond(halfValue)) {
      ok = half;
    } else {
      ng = half;
    }
  }

  return ok;
}

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto s = readln.strip;

		alias mirror = len => s[0..len] ~ s[0..len].byChar.retro.array;

		int len = 1;
		bool first = true;
		int [] cand = [1, n];
		foreach (i; 0..n - 2)
		{
			if (s[i + 1] < s[i])
			{
				cand ~= i + 2;
			}
			if (s[i + 1] > s[i])
			{
				cand ~= i + 1;
				break;
			}
		}
		debug {writeln (cand);}
		cand.map !(mirror).minElement.writeln;
	}
}


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

const int[] dx = [1, 0, -1, 0];
const int[] dy = [0, 1, 0, -1];

void main() {
    int n; readf("%d\n", &n);
    char[][] F = new char[][n];
    for (int i = 0; i < n; i++) F[i] = new char[n];
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            F[i][j] = '?';
    int c = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (F[i][j] == '?') {
                F[i][j] = 'C';
                c++;
                for (int k = 0; k < 4; k++) {
                    if (0 <= i + dy[k] && i + dy[k] < n && 0 <= j + dx[k] && j + dx[k] < n)
                        F[i + dy[k]][j + dx[k]] = '.';
                }
            }
        }
    }
    c.writeln;
    F.join("\n").writeln;
}

/** CodeForces Problem 384.A (1)
 * ACM summer training contest (3)
 * Noein
 */

import std.stdio;

T mut(T)(in T inval) {
    import std.traits;

    return cast(Unqual!T) inval;
}

T imut(T)(in T inval) {
    //import std.traits;

    return cast(immutable) inval;
}

void main() {
    int n;

    readf("%s\n", &n);

    const bool odd = cast(bool) (n % 2);

    auto r = odd? n+1 : n;

    const result = (r * r / 2) - ( (odd) ? n : 0 );

    auto bitf = odd.mut;

    auto repr_str = "";

    for( int y; y < n; ++ y ) {
        for( int x; x < n; ++x ) {
            repr_str ~= ( (bitf) ? "C" : "." );
            bitf ^= true;
        }
        repr_str ~= "\n";
        if(!odd) bitf ^= true;
    }

    writeln(result, "\n", repr_str);
}

/** CodeForces Problem 384.A (1)
 * ACM summer training contest (3)
 * Noein
 */

import std.stdio;

void main() {
    int n;

    readf("%s\n", &n);

    const bool odd = cast(bool) (n % 2);

    const result = (n * n / 2) - ( (odd) ? n : 0 );

    auto bitf = false;

    auto repr_str = "";

    for( int y; y < n; ++ y ) {
        for( int x; x < n; ++x ) {
            repr_str ~= ( (bitf) ? "C" : "." );
            bitf ^= true;
        }
        repr_str ~= "\n";
        if(!odd) bitf ^= true;
    }

    writeln(repr_str);
}

/** CodeForces Problem 384.A (1)
 * ACM summer training contest (3)
 * Noein
 */

import std.stdio;

void main() {
    int n;

    readf("%s\n", &n);

    const bool odd = cast(bool) (n % 2);

    const result = (n * n / 2) - ( (odd) ? n : 0 );

    auto bitf = false;

    auto repr_str = "";

    for( int y; y < n; ++ y ) {
        for( int x; x < n; ++x ) {
            repr_str ~= ( (bitf) ? "C" : "." );
            bitf ^= true;
        }
        repr_str ~= "\n";
        if(!odd) bitf ^= true;
    }

    writeln(result, "\n", repr_str);
}


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

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

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

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

    auto rbt = make!(RedBlackTree!(int, "a < b", true))(b);

    debug { rbt.writeln; }

    int[] ans;
    foreach (e; a) {
        auto mn = (n - e - 1 + n) % n;
        auto upper = rbt.upperBound(mn);
        auto up = upper.empty() ? rbt.front : upper.front;
        debug { writeln(e, ' ', mn, ' ', upper, ' ', rbt.front, ' ', up); }

        ans ~= (e + up) % n;
        rbt.removeKey(up);
    }

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

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

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		auto b = readln.splitter.map !(to !(int)).array;
		auto t = redBlackTree !(q{a < b}, true) (b);
		debug {writeln (t);}

		int [] c;
		foreach (i; 0..n)
		{
			auto f = t.upperBound ((n - a[i]) % n - 1);
			if (f.empty)
			{
				f = t.upperBound (0 - 1);
			}
			auto v = f.front;
			c ~= (a[i] + v) % n;
			t.removeKey (v);
		}
		writefln ("%(%s %)", c);
	}
}


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

void main() {
  int a = 0, b = 0;
  int less = -2;
  foreach(i; iota(29,-1,-1)) {
    debug {writeln(i);}
    int query(int x, int y) {
      x <<= i; y <<= i;
      x ^= a; y ^= b;
      writefln!"? %d %d" (x,y);
      stdout.flush;
      return readln.strip.to!int;
    }
    void set(int x, int y) {
      x <<= i; y <<= i;
      a ^= x; b ^= y;
    }
    if(less == -2) less = query(0,0);
    int k = query(1,1);
    if(less != k) {
      set(less > 0, less < 0);
      less = -2;
      continue;
    }
    k = query(0,1);
    set(k > 0, k > 0);
  }
  writefln!"! %d %d" (a,b);
}

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;

immutable int M = 30;
long x, y;
int cnt = 0;

int ask(long c, long d) {
    debug {
        cnt += 1;
        if ((x^c) > (y^d)) return 1;
        else if ((x^c) == (y^d)) return 0;
        else return -1;
    }

    writeln("? ", c, " ", d);
    stdout.flush;
    return readln.chomp.to!int;
}

void answer(long a, long b) {
    writeln("! ", a, " ", b);
    stdout.flush;
}

void main() {
    debug {
        auto s = readln.split.map!(to!long);
        x = s[0];
        y = s[1];
    }

    int f = ask(0, 0);
    int ff = f;

    if (f == 0) {
        long a = 0;
        foreach_reverse (i; 0..M) {
            long c = (1L << (i + 1)) - 1;
            long d = (1L << i) - 1;
            c += a;
            d += a;
            f = ask(c, d);
            if (f == -1) {
                a += 1L << i;
            }
        }
        answer(a, a);
        return;
    }

    bool a_large = f == 1;
    long a = 0;
    long b = 0;
    auto same = new bool[](M);

    foreach_reverse (i; 0..M) {
        long c = 1L << i;
        f = ask(a+c, b+c);
        if (a_large && f == -1) {
            a += c;
            f = ask(a, b);
            a_large = f == 1;
        } else if (!a_large && f == 1) {
            b += c;
            f = ask(a, b);
            a_large = f == 1;
        } else {
            same[i] = true;
        }
    }

    foreach (i; 0..M) {
        if (!same[i]) {
            continue;
        }
        long c = 1L << i;
        f = ask(a+c, b);
        if (f == -1) {
            a += c;
            b += c;
        }
    }

    answer(a, b);
    debug{cnt.writeln;}
}


import std.stdio;

void main() {
    uint t, x, y, n, b;

    scanf("%d", &t);

    while(t--) {
        uint ans;
        scanf("%d %d %d", &x, &y, &n);

        if(x > n) ans = 0;

        b = n % x;
        if(b == y)
            ans = n;
        else if(b > y)
            ans = n - (b -y);
        else
            ans = n - b - (x - y);

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

import std.stdio;

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

  foreach (i; 0..t) {
    long x, y, n;
    readf!"%d %d %d\n"(x, y, n);

    auto r = n - n % x + y;
    if (r > n) {
      r -= x;
    }

    writeln(r);
  }
}

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 x, y, n;
		readf !(" %s %s %s") (x, y, n);
		n -= y;
		n -= n % x;
		n += y;
		writeln (n);
	}
}


// xxxxx
import std.random;
import std.stdio;

void main ()
{
	rndGen.seed (unpredictableSeed);
	writeln (uniform (0, 2) ? "Even" : "Odd");
}

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

void main(){

	writeln("Odd");

}

import std.stdio;

void main ()
{
	writeln ("Even");
}

import std.stdio;

void main ()
{
	writeln (25);
}

// xxx
import std.random;
import std.stdio;

void main ()
{
	rndGen.seed (unpredictableSeed);
	writeln (uniform (0, 2) ? "Even" : "Odd");
}

import std.stdio;

void main ()
{
	writeln ("Odd");
}

// xxxx
import std.random;
import std.stdio;

void main ()
{
	rndGen.seed (unpredictableSeed);
	writeln (uniform (0, 2) ? "Even" : "Odd");
}

// x
import std.random;
import std.stdio;

void main ()
{
	rndGen.seed (unpredictableSeed);
	writeln (uniform (0, 2) ? "Even" : "Odd");
}

// xx
import std.random;
import std.stdio;

void main ()
{
	rndGen.seed (unpredictableSeed);
	writeln (uniform (0, 2) ? "Even" : "Odd");
}

import std.stdio;
import std.string;

void main ()
{
	readln.strip.writeln;
}

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

void main(){

	writeln("Even");

}


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; }
long lcm(long x, long y) { return x * (y / gcd(x, y)); }

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

void main()
{
	auto n = RD!int;
	double ans = 0.0;
	foreach (i; 0..n)
	{
		ans += 1.0 / (n-i);
	}

	writefln(FMT_F, ans);
	stdout.flush;
	debug readln;
}

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

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

    double ans = 0;
    foreach (e; (n+1).iota.dropOne) {
        ans += 1.0 / e;
    }

    writefln("%.10f", ans);
}

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

DList!string _words;

void read(T...)(ref T t)
{
  void singleRead(W)(ref W w)
  {
    import std.stdio: readln;
    while(_words.empty)
      {
        import std.array: split;
        foreach(word; readln.split)
          {
            _words.insertBack(word);
          }
      }
    import std.conv: to;
    string word = _words.front;
    _words.removeFront;
    w = to!W(word);
  }
  static foreach(i; 0 .. T.length)
    {
      static if (is(typeof({foreach(ref e; t[i]){}})) && !is(T[i] == string))
        {
          foreach(ref element; t[i])
            read(element);
        }
      else
        {
          singleRead(t[i]);
        }
    }
}

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

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

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

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

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

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

    }
}

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

void main()
{
  writeln(iota(long(1), long(next!long + 1)).map!(k => real(1) / real(k)).sum);
}


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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto t = RD!int;
	auto ans = new bool[](t);
	foreach (ti; 0..t)
	{
		auto a = RD!string;
		auto b = RD!string;
		auto c = RD!string;
		bool ok = true;
		foreach (i; 0..a.length)
		{
			if (a[i] == c[i] || b[i] == c[i]) continue;
			ok = false;
			break;
		}
		ans[ti] = ok;
	}

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

import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); }
void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0;
string rstring(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T rtype(T)(){ return rstring.to!T; } alias rint = rtype!int, rlong = rtype!long, rreal = rtype!real;
T[] rtypes(T)(int n){ return n.iota.map!(i => rtype!T()).array; } alias rint = rtypes!int, rlong = rtypes!long, rreal = rtypes!real;
T[][] rtypess(T)(int n, int m){ return n.iota.map!(i => rtypes!T(m)).array; } alias rint = rtypess!int, rlong = rtypess!long, rreal = rtypess!real;
T chmin(T)(ref T x, T y){ if(x > y) x = y; return x; } T chmax(T)(ref T x, T y){ if(x < y) x = y; return x; }

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

void solve(){
	A: foreach(_; 0 .. rint){
		string a = readln.chomp, b = readln.chomp, c = readln.chomp;
		foreach(i; 0 .. a.length){
			if(a[i] == c[i] || b[i] == c[i]) continue;
			"NO".writeln;
			continue A;
		}
		"YES".writeln;
	}
}

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;

  string a, b, c;

  void solve(long tc = -1)
  {
    auto n = a.length;
    if (iota(0, n).all!(i => c[i] == b[i] || c[i] == a[i]))
      writeln("YES");
    else
      writeln("NO");
  }
}

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 baseType(W)
{
  static if (isDynamicArray!W)
    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 = size;
      return res;
    }
  else
    {
      typeof(makeSlice!E(otherSizes))[] res;
      res.length = 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, string))
              {
                static assert(isDynamicArray!(typeOfField)
                              , " A field with dimension initialization UDA must be a dynamic array.");
                enum uda = getUDAs!(fieldSymbol, string)[0];
                mixin(fieldName) = mixin(q{makeSlice!(baseType!typeOfField)(}, uda, q{)});
              }
            else static if (isDynamicArray!typeOfField)
              {
                pragma(msg, "Warning: You probably want to add dimension to input ", 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;
}


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

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

void main() {
  auto r = new InputReader ();
  immutable nt = r.next!uint ();
  foreach (tid; 0 .. nt) {
    const n = r.next!int;
    const int a = r.next!uint;
    const int b = r.next!uint;
    const int c = r.next!uint;
    const int d = r.next!uint;
    const u1 = n * (a - b), v1 = n * (a + b);
    const u2 = c - d, v2 = c + d;
    const u = max (u1, u2), v = min (v1, v2);
    writeln ((u <= v) ? "Yes" : "No");
  }
}

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

void main()
{
    auto T = readln.chomp.to!int;
    foreach (_t; 0..T) {
        auto nn = readln.split.to!(int[]);
        auto N = nn[0];
        auto A = nn[1];
        auto B = nn[2];
        auto C = nn[3];
        auto D = nn[4];
        if ((A-B)*N > (C+D) || (A+B)*N < (C-D)) {
            writeln("No");
        } else {
            writeln("Yes");
        }
    }
}

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

		if ((a-b)*n > c+d) continue;
		if ((a+b)*n < c-d) continue;
		ans[ti] = true;
	}

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


import std.stdio;
import std.algorithm;
import std.container;

immutable int inf = cast(int)1e9;

int N;
int[] A, d;

void main() {
	readf("%d", &N);
	A = new int[N + 1];
	foreach (i; 1..N + 1) readf(" %d", &A[i]);
	d = new int[N + 1]; fill(d, cast(int)1e9);
	d[1] = 0; auto q = DList!int(1);
	while (!q.empty) {
		int v = q.front; q.removeFront();
		foreach (u; [v - 1, v + 1, A[v]]) {
			if (u < 1 || u > N) continue;
			if (d[u] > d[v] + 1) {
				d[u] = d[v] + 1;
				q.insertBack(u);
			}
		}
	}
	foreach(i; 1..N + 1) writef("%d ", d[i]); writeln("");
}

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

int main() {
    int n = readln.strip.to!int;;
    int[] a = readln.split.map!(a => a.to!int - 1).array;
    struct Edge(C) {
        int to;
        C cost;
    }
    alias E = Edge!int;
    E[][] e = new E[][](n);
    foreach (i; 0..n) {
        if (i < n-1) {
            e[i] ~= E(i+1, 1);
        }
        if (0 < i) {
            e[i] ~= E(i-1, 1);
        }
        e[i] ~= E(a[i], 1);
    }
    writeln(dijkstra(e, 0).map!(to!string).join(" "));
	return 0;
}

C[] dijkstra(E, C = typeof(E.cost))(in E[][] graph, int s) {
    import std.typecons : Tuple;
    import std.container : Array, BinaryHeap;
    alias Q = Tuple!(int, "to", C, "cost");

    size_t n = graph.length;
    C[] dist = new C[graph.length]; dist[] = E.cost.max;
    bool[] used = new bool[n];
    auto que = BinaryHeap!(Array!Q, "a.cost > b.cost")();
    dist[s] = 0;
    que.insert(Q(s, 0));
    while (!que.empty) {
        auto p = que.front.to; que.popFront;
        if (used[p]) continue;
        used[p] = true;
        foreach (E e; graph[p]) {
            if (dist[e.to] > dist[p] + e.cost) {
                dist[e.to] = dist[p] + e.cost;
                que.insert(Q(e.to, dist[e.to]));
            }
        }
    }
    return dist;
}

string readToken() {
    import std.stdio : readln;
    import std.string : split;
    static size_t pos;
    static string[] tokens;
    while (!(pos < tokens.length)) {
        pos = 0;
        tokens = readln.split;
    }
    return tokens[pos++];
}
T read(T)() {
    import std.conv : to;
    return readToken.to!T;
}

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

int main() {
    int n = readln.strip.to!int;;
    int[] a = readln.split.map!(a => a.to!int - 1).array;
    int[] d = new int[n]; d[] = 10^^9;
    d[0] = 0;
    foreach (i; 0..n) {
        if (i) {
            d[i] = min(d[i], d[i-1]+1);
        }
        d[a[i]] = min(d[a[i]], d[i]+1);
    }
    foreach_reverse (i; 0..n-1) {
        d[i] = min(d[i], d[i+1]+1);
    }
    writeln(d.map!(to!string).join(" "));
	return 0;
}

string readToken() {
    import std.stdio : readln;
    import std.string : split;
    static size_t pos;
    static string[] tokens;
    while (!(pos < tokens.length)) {
        pos = 0;
        tokens = readln.split;
    }
    return tokens[pos++];
}
T read(T)() {
    import std.conv : to;
    return readToken.to!T;
}


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

      N += 2;
      A = [0L] ~ A ~ [0L];
      auto b = new bool[][](N, N);
      foreach (i; 0 .. N) foreach (j; 0 .. N) {
        b[i][j] = (gcd(A[i], A[j]) != 1);
      }

      auto dpL = new bool[][](N, N);
      auto dpR = new bool[][](N, N);
      foreach (i; 0 .. N - 1) {
        dpL[i + 1][i] = true;
        dpR[i][i + 1] = true;
      }
      foreach (w; 2 .. N) {
        foreach (i; 0 .. N - w) {
          const j = i + w;
          foreach (k; i + 1 .. j) {
            if (dpR[i][k] && dpL[j][k]) {
              if (b[i][k]) dpL[j][i] = true;
              if (b[j][k]) dpR[i][j] = true;
            }
          }
        }
      }
      writeln(dpL[N - 1][0] ? "Yes" : "No");
    }
  } catch (EOFException e) {
  }
}

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

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

		auto g = new bool [] [] (n, n);
		foreach (i; 0..n)
		{
			foreach (j; 0..n)
			{
				g[i][j] = gcd (a[i], a[j]) > 1;
			}
		}

		auto f = new bool [2] [] [] (n + 1, n + 1);
		foreach (b; 0..2)
		{
			foreach (i; 0..n + 1)
			{
				f[i][i][b] = true;
			}
		}

		foreach (len; 1..n + 1)
		{
			foreach (i; 0..n + 1)
			{
				int j = i + len;
				if (j > n)
				{
					continue;
				}

				if (j < n)
				{
					int v = j;
					// [i..k) *k* [k+1..j) *v*
					foreach (k; i..j)
					{
						if (g[k][v] &&
						    f[i][k][0] &&
						    f[k + 1][j][1])
						{
							f[i][j][0] = true;
							break;
						}
					}
				}

				if (0 <= i - 1)
				{
					int v = i - 1;
					// *v* [i..k) *k* [k+1..j)
					foreach (k; i..j)
					{
						if (g[k][v] &&
						    f[i][k][0] &&
						    f[k + 1][j][1])
						{
							f[i][j][1] = true;
							break;
						}
					}
				}
			}
		}

		bool res = false;
		foreach (i; 0..n)
		{
			res |= f[0][i][0] && f[i + 1][n][1];
		}
		writeln (res ? "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.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 (; ; ) {
      int N = readInt();
      auto A = new long[N];
      foreach (i; 0 .. N) {
        A[i] = readInt();
      }

      N += 2;
      A = [1L] ~ A ~ [1L];
      auto b = new bool[][](N, N);
      foreach (i; 0 .. N) foreach (j; 0 .. N) {
        b[i][j] = (gcd(A[i], A[j]) != 1);
      }

      auto dp = new bool[][](N, N);
      foreach (i; 0 .. N - 1) {
        dp[i][i + 1] = true;
      }
      foreach (w; 2 .. N) {
        foreach (i; 0 .. N - w) {
          const j = i + w;
          foreach (k; i + 1 .. j) {
            if (b[i][k] || b[j][k]) {
              if (dp[i][k] && dp[k][j]) {
                dp[i][j] = true;
                break;
              }
            }
          }
        }
      }
      debug {
        writeln("dp = ", dp);
      }
      bool ans;
      foreach (i; 1 .. N - 1) {
        if (dp[0][i] && dp[i][N - 1]) {
          ans = true;
          break;
        }
      }
      writeln(ans ? "Yes" : "No");
    }
  } catch (EOFException e) {
  }
}


// Vicfred
// https://codeforces.com/contest/1395/problem/A
// greedy
import std.stdio;

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

    while(t--) {
        long r, g, b, w;
        readf("%s %s %s %s", &r, &g, &b, &w);
        readln;

        long x = r%2 + g%2 + b%2 + w%2;

        if(x <= 1) {
            "Yes".writeln;
            continue;
        }

        if(r > 0 && g > 0 && b > 0) {
            r -= 1;
            g -= 1;
            b -= 1;
            w += 1;
        }

        x = r%2 + g%2 + b%2 + w%2;

        if(x <= 1) {
            "Yes".writeln;
            continue;
        }

        "No".writeln;
    }
}

import std.conv;
import std.stdio;
import std.string;

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		int r, g, b, w;
		readf !(" %s %s %s %s") (r, g, b, w);
		auto s = (r & 1) + (g & 1) + (b & 1) + (w & 1);
		writeln (s <= 1 || (r && g && b && s >= 3) ? "YES" : "NO");
	}
}

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 r, g, b, w;

  void solve(long tc = -1)
  {
    long p = (r % 2 == 0) + (g % 2 == 0) + (b % 2 == 0)
      + (w % 2 == 0);
    long i = 4 - p;
    long ops = min(r, g, b);
    if (p == 3 || p == 4)
      {
        writeln("Yes");
        return;
      }
    if (ops > 0)
      {
        if (p == 1 || p == 0)
          {
            writeln("Yes");
            return;
          }
      }
    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;
}

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

		int cnt;
		foreach (e; a[0..3])
		{
			if (e % 2)
				++cnt;
		}

		int cnt2 = a[3] % 2;
		if (cnt+cnt2 <= 1)
		{
			ans[ti] = true;
			continue;
		}

		if (a[0] == 0 || a[1] == 0 || a[2] == 0)
			continue;

		int cnt3 = cnt2 % 2 ? 0 : 1;
		if (3 - cnt + cnt3 <= 1)
			ans[ti] = true;
	}

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

import std.conv;
import std.stdio;
import std.string;

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		int r, g, b, w;
		readf !(" %s %s %s %s") (r, g, b, w);
		writeln ((r & 1) + (g & 1) + (b & 1) + (w & 1) == 2 ?
		    "NO" : "YES");
	}
}


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

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		if (n == 1)
		{
			writeln (1);
		}
		else if (n % 4 >= 2)
		{
			writeln (-1);
		}
		else
		{
			auto p = new int [n];
			if (n & 1)
			{
				debug {writefln ("%s", n >> 1);}
				p[n >> 1] = n >> 1;
			}
			foreach (i; 0..(n >> 2))
			{
				int j = i << 1;
				int k = n - j - 1;
				debug {writefln ("%s %s", j, k);}
				p[j] = j + 1;
				p[j + 1] = k;
				p[k] = k - 1;
				p[k - 1] = j;
			}
			p[] += 1;
			writefln ("%(%s %)", p);
		}
	}
}


import std.stdio;

byte idx;
int ans, min;

void minfoo(byte m) {
	byte temp;
	foreach (i; 0 .. m) {
		readf(" %s", &temp);
		min += temp * 5;
	}

	min += m * 15;

	if (ans > min || idx == 0)
		ans = min;

	min = 0;
}

byte[] a;

void main() {
	byte n, k;

	readf(" %s", &n);

	foreach (i; 0 .. n) {
		readf(" %s", &k);
		a ~= k;
	}

	for (idx = 0; idx < n; ++idx) {
		minfoo(a[idx]);
	}

	writeln(ans);
}

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

void main()
{
    int n = readln.chomp.to!int;
    auto cash = readln.split.map!(to!int);
    auto time = new int[](n);
    foreach (i; 0..n) {
        auto inp = readln.split.map!(to!int);
        foreach (j; 0..cash[i]) {
            time[i] += inp[j] * 5;
            time[i] += 15;
        }
    }
    time.reduce!(min).writeln;

}

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[] tk;
        string readString() {
                while (tk.empty)
                        tk = readln.split;
                auto tkt = tk.front;
                tk.popFront;
                return tkt;
        }
        int readInt() {
                return readString.to!int;
        }
        double readDouble() {
                return readString.to!double;
        }
}

void main() {
        IO cin;
        int t = 1;
        // t = cin.readInt;
        while (t--) {
                int n = cin.readInt;
                int[] c = new int[n];
                int minTime = 99999999;
                for (int i = 0; i < n; i++) {
                        c[i] = cin.readInt;
                }
                for (int i = 0; i < n; i++) {
                        int time = 0;
                        for (int j = 0; j < c[i]; j++) {
                                time += cin.readInt * 5;
                        }
                        time += c[i] * 15;
                        minTime = min(minTime, time);
                }
                writeln(minTime);
        }
}

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

void main()
{
	int n = readln.chomp.to!int;
	auto cash = readln.split.map!(to!int);
	auto time = new int[](n);
	foreach (i; 0..n) {
		auto inp = readln.split.map!(to!int);
		foreach (j; 0 .. cash[i]) {
			time[i] += inp[j] * 5;
			time[i] += 15;
		}
	}
	time.reduce!(min).writeln;
}


void main() { runSolver(); }

void problem() {
  auto QN = scan!int;

  auto solve() {
    auto subSolve() {
      auto S = scan.map!(c => c - 'a').array;

      auto acc = new long[][](S.length + 1, 26);
      foreach(li, c; S) {
        const int i = cast(int)li;

        foreach(d; 0..26) acc[i + 1][d] = acc[i][d];
        acc[i + 1][c]++;
      }

      int[26] pre;
      pre[] = 1;
      auto cs = 26.iota.filter!(c => acc[$ - 1][c] != 0).array;
      bool ans = true;

      // acc.each!deb;
      foreach(li, c; S) {
        const int i = cast(int)li + 1;
        // [i, c.to!int, acc[i][c]].deb;

        if (acc[i][c] > 1) {
          // i.deb;
          auto p = pre[c] - 1;
          auto ma = cs.map!(x => acc[i][x] - acc[p][x]).reduce!max;
          auto mi = cs.map!(x => acc[i][x] - acc[p][x]).reduce!min;
          // [ma, mi].deb;
          if (ma - mi > 1) {
            ans = false;
            break;
          }
        }

        pre[c] = i;
      }

      return YESNO[ans];
    }

    foreach(i; 0..QN) {
      subSolve().writeln;
    }
  }

  outputForAtCoder(&solve);
}

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

import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric, std.traits, std.functional, std.bigint, std.datetime.stopwatch, core.time, core.bitop;
T[][] combinations(T)(T[] s, in long m) {   if (!m) return [[]];   if (s.empty) return [];   return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; }
T[] scan(T)(long n){ return n.iota.map!(i => scan!T()).array; }
void deb(T ...)(T t){ debug writeln(t); }
alias Point = Tuple!(long, "x", long, "y");
Point invert(Point p) { return Point(p.y, p.x); }
long[] divisors(long n) { long[] ret; for (long i = 1; i * i <= n; i++) { if (n % i == 0) { ret ~= i; if (i * i != n) ret ~= n / i; } } return ret.sort.array; }
bool chmin(T)(ref T a, T b) { if (b < a) { a = b; return true; } else return false; }
bool chmax(T)(ref T a, T b) { if (b > a) { a = b; return true; } else return false; }
string charSort(alias S = "a < b")(string s) { return (cast(char[])((cast(byte[])s).sort!S.array)).to!string; }
ulong comb(ulong a, ulong b) { if (b == 0) {return 1;}else{return comb(a - 1, b - 1) * a / b;}}
T[] compress(T)(T[] arr, T origin = T.init) { T[T] indecies; arr.dup.sort.uniq.enumerate(origin).each!((i, t) => indecies[t] = i); return arr.map!(t => indecies[t]).array; }
string toAnswerString(R)(R r) { return r.map!"a.to!string".joiner(" ").array.to!string; }
struct ModInt(uint MD) if (MD < int.max) {ulong v;this(string v) {this(v.to!long);}this(int v) {this(long(v));}this(long v) {this.v = (v%MD+MD)%MD;}void opAssign(long t) {v = (t%MD+MD)%MD;}static auto normS(ulong x) {return (x<MD)?x:x-MD;}static auto make(ulong x) {ModInt m; m.v = x; return m;}auto opBinary(string op:"+")(ModInt r) const {return make(normS(v+r.v));}auto opBinary(string op:"-")(ModInt r) const {return make(normS(v+MD-r.v));}auto opBinary(string op:"*")(ModInt r) const {return make((ulong(v)*r.v%MD).to!ulong);}auto opBinary(string op:"^^", T)(T r) const {long x=v;long y=1;while(r){if(r%2==1)y=(y*x)%MD;x=x^^2%MD;r/=2;} return make(y);}auto opBinary(string op:"/")(ModInt r) const {return this*memoize!inv(r);}static ModInt inv(ModInt x) {return x^^(MD-2);}string toString() const {return v.to!string;}auto opOpAssign(string op)(ModInt r) {return mixin ("this=this"~op~"r");}}
alias MInt1 = ModInt!(10^^9 + 7);
alias MInt9 = ModInt!(998_244_353);
void outputForAtCoder(T)(T delegate() fn) {
  static if (is(T == float) || is(T == double) || is(T == real)) "%.16f".writefln(fn());
  else static if (is(T == void)) fn();
  else static if (is(T == string)) fn().writeln;
  else static if (isInputRange!T) {
    static if (!is(string == ElementType!T) && isInputRange!(ElementType!T)) foreach(r; fn()) r.toAnswerString.writeln;
    else foreach(r; fn()) r.writeln;
  }
  else fn().writeln;
}
void runSolver() {
  enum BORDER = "==================================";
  debug { BORDER.writeln; while(!stdin.eof) { "<<< Process time: %s >>>".writefln(benchmark!problem(1)); BORDER.writeln; } }
  else problem();
}
enum YESNO = [true: "Yes", false: "No"];

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

import std;

void main()
{
    int t;
    scanf("%d", &t);
    getchar();
    outer: foreach(_; 0..t)
    {
        string s = readln.strip;
        auto u = s.array.sort.uniq.array;
        int ul = cast(int) u.length;
        if (ul == 1)
        {
            writeln("yes");
            continue outer;
        }
        int[] zeros = new int[ul];
        auto tmp = assocArray(zip(u, zeros));
        foreach(int ind, ch; s.array)
        {
            if (tmp[ch] == 0)
            {
                tmp[ch] = ind + 1;
                continue;
            }
            if ((ind + 1) - tmp[ch] < ul)
            {
                writeln("no");
                continue outer;
            }
            tmp[ch] = ind + 1;
        }
        writeln("yes");
    }
}

// cheese-cracker [2022-05-03]

void solve(){
    auto word = scan!(dchar[]);
    auto seen = new int[](26);
    int uniqs = -1;
    int cntuniq = 0;
    seen[] = -1;
    for(int i = 0; i < word.length; ++i){
        int cval = (word[i] - 'a').to!int;
        if(seen[cval] != -1){
            if(uniqs != -1 && i - seen[cval] != uniqs){
                writeln("NO");
                return;
            }else{
                uniqs = (i - seen[cval]);
            }
        }else{
            ++cntuniq;
        }
        seen[cval] = i;
    }
    /* show(cntuniq, uniqs); */
    if(cntuniq == uniqs || uniqs == -1){
        writeln("YES");
    }else{
        writeln("NO");
    }
}

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

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

import std;

void main()
{
    int t;
    scanf("%d", &t);
    getchar();
    outer: foreach(_; 0..t)
    {
        string s = readln.strip;
        auto u = s.array.sort.uniq.array;
        int ul = cast(int) u.length;
        int[] zeros = new int[ul];
        auto tmp = assocArray(zip(u, zeros));
        foreach(ind, ch; s.array)
        {
            if (tmp[ch] == 0)
            {
                tmp[ch] = cast(int) ind;
                continue;
            }
            if (ul == 1)
            {
                writeln("yes");
                continue outer;
            }
            if (tmp[ch] - cast(int) ind < ul)
            {
                writeln("no");
                continue outer;
            }
            tmp[ch] = cast(int) ind;
        }
        writeln("yes");
    }
}


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

void main() {
    int n;
    scan(n);

    int ans;
    int set = (1<<26) - 1;
    bool safe;

    foreach (i ; 0 .. n-1) {
        char e;
        string s;
        scan(e, s);

        if (safe && e != '.') {
            ans++;
            continue;
        }

        int t;

        if (e == '!') {
            foreach (ch ; s) {
                t |= (1<<(ch - 'a'));
            }
        }
        else if (e == '.') {
            t = (1<<26) - 1;
            foreach (ch ; s) {
                t &= ~(1<<(ch - 'a'));
            }
        }
        else {
            t = ~(1<<(s[0] - 'a'));
        }

        set &= t;

        debug {
            writefln("%b",set);
        }

        if (set.popcnt == 1) {
            safe = 1;
        }
    }

    writeln(ans);
}

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

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

/+ dub.sdl:
    name "A"
    dependency "dunkelheit" version=">=0.9.0"
+/

import std.stdio, std.algorithm, std.range, std.conv;
// import dkh.foundation, dkh.scanner;

import core.bitop;

immutable int B = 26;

int main() {
    Scanner sc = new Scanner(stdin);
    int n;
    sc.read(n);
    int f = (1<<B) - 1;
    int ans = 0;
    foreach (i; 0..n-1) { //ignore last query
        char ty; string s;
        sc.read(ty, s);

        bool uni = popcnt(f) == 1;

        if (ty == '!') {
            //shock
            if (uni) ans++;
            int nf = 0;
            foreach (c; s) {
                nf |= (1 << (c - 'a'));
            }
            f &= nf;
        } else if (ty == '.') {
            int nf = 0;
            foreach (c; s) {
                nf |= (1 << (c - 'a'));
            }
            f &= ~nf;
        } else if (ty == '?') {
            if (uni) ans++;

            int nf = 0;
            foreach (c; s) {
                nf |= (1 << (c - 'a'));
            }
            f &= ~nf;
        }
    }
//    writeln(f, " ", popcnt(f));
    writeln(ans);
    return 0;
}
/* IMPORT /Users/yosupo/Program/dunkelheit/source/dkh/container/stackpayload.d */
// module dkh.container.stackpayload;

struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) {
    import core.exception : RangeError;

    private T* _data;
    private uint len, cap;

    @property bool empty() const { return len == 0; }
    @property size_t length() const { return len; }
    alias opDollar = length;

    inout(T)[] data() inout { return (_data) ? _data[0..len] : null; }

    ref inout(T) opIndex(size_t i) inout {
        version(assert) if (len <= i) throw new RangeError();
        return _data[i];
    }
    ref inout(T) front() inout { return this[0]; }
    ref inout(T) back() inout { return this[$-1]; }

    void reserve(size_t newCap) {
        import core.memory : GC;
        import core.stdc.string : memcpy;
        import std.conv : to;
        if (newCap <= cap) return;
        void* newData = GC.malloc(newCap * T.sizeof);
        cap = newCap.to!uint;
        if (len) memcpy(newData, _data, len * T.sizeof);
        _data = cast(T*)(newData);
    }
    void free() {
        import core.memory : GC;
        GC.free(_data);
    }

    void clear() {
        len = 0;
    }

    void insertBack(T item) {
        import std.algorithm : max;
        if (len == cap) reserve(max(cap * 2, MINCAP));
        _data[len++] = item;
    }
    alias opOpAssign(string op : "~") = insertBack;
    void removeBack() {
        assert(!empty, "StackPayload.removeBack: Stack is empty");
        len--;
    }
}

/* IMPORT /Users/yosupo/Program/dunkelheit/source/dkh/foundation.d */

// module dkh.foundation;

static if (__VERSION__ <= 2070) {
    /*
    Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d
    Copyright: Andrei Alexandrescu 2008-.
    License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
    */
    template fold(fun...) if (fun.length >= 1) {
        auto fold(R, S...)(R r, S seed) {
            import std.algorithm : reduce;
            static if (S.length < 2) {
                return reduce!fun(seed, r);
            } else {
                import std.typecons : tuple;
                return reduce!fun(tuple(seed), r);
            }
        }
    }

}
/* IMPORT /Users/yosupo/Program/dunkelheit/source/dkh/scanner.d */
// module dkh.scanner;

// import dkh.container.stackpayload;

class Scanner {
    import std.stdio : File;
    import std.conv : to;
    import std.range : front, popFront, array, ElementType;
    import std.array : split;
    import std.traits : isSomeChar, isStaticArray, isArray;
    import std.algorithm : map;
    File f;
    this(File f) {
        this.f = f;
    }
    char[512] lineBuf;
    char[] line;
    private bool succW() {
        import std.range.primitives : empty, front, popFront;
        import std.ascii : isWhite;
        while (!line.empty && line.front.isWhite) {
            line.popFront;
        }
        return !line.empty;
    }
    private bool succ() {
        import std.range.primitives : empty, front, popFront;
        import std.ascii : isWhite;
        while (true) {
            while (!line.empty && line.front.isWhite) {
                line.popFront;
            }
            if (!line.empty) break;
            line = lineBuf[];
            f.readln(line);
            if (!line.length) return false;
        }
        return true;
    }

    private bool readSingle(T)(ref T x) {
        import std.algorithm : findSplitBefore;
        import std.string : strip;
        import std.conv : parse;
        if (!succ()) return false;
        static if (isArray!T) {
            alias E = ElementType!T;
            static if (isSomeChar!E) {

                auto r = line.findSplitBefore(" ");
                x = r[0].strip.dup;
                line = r[1];
            } else static if (isStaticArray!T) {
                foreach (i; 0..T.length) {
                    bool f = succW();
                    assert(f);
                    x[i] = line.parse!E;
                }
            } else {
                StackPayload!E buf;
                while (succW()) {
                    buf ~= line.parse!E;
                }
                x = buf.data;
            }
        } else {
            x = line.parse!T;
        }
        return true;
    }
    int read(T, Args...)(ref T x, auto ref Args args) {
        if (!readSingle(x)) return 0;
        static if (args.length == 0) {
            return 1;
        } else {
            return 1 + read(args);
        }
    }
}

/*
This source code generated by dunkelheit and include dunkelheit's source code.
dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit)
dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt)
*/


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

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

    auto a = readln.split.map!(to!int).array;
    a.sort();

    if (k == 0) {
        writeln(a[0] > 1 ? 1 : -1);
    } else if (k == n) {
        writeln(a[n-1]);
    } else {
        writeln(k == n || a[k-1] != a[k] ? a[k-1] : -1);
    }
}

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

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

    auto a = readln.split.map!(to!int).array;
    a.sort();

    if (k == 0) {
        writeln(a[0] > 1 ? 1 : -1);
    } else if (k == n) {
        writeln(a[n-1]);
    } else {
        writeln(a[k-1] != a[k] ? a[k-1] : -1);
    }
}

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

auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=rdsp;foreach(ref v;t)pick(r,v);}

void main()
{
  int n, k; readV(n, k);
  int[] a; readA(n, a);

  a.sort();

  if (k == 0) {
    if (a[0] == 1)
      writeln(-1);
    else
      writeln(a[0]-1);
  } else {
    if (a[k-1] == 0 || a[k-1] == a[k])
      writeln(-1);
    else
      writeln(a[k-1]);
  }
}

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

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

    auto a = new int[n];

    foreach (ref t; a) readf(" %s", t);

    sort(a);

    if (k == 0 && a[0] > 1)
    {
        writeln(1);
    }
    else if (k == n || k > 0 && a[k-1] != a[k])
    {
        writeln(a[k-1]);
    }
    else
    {
        writeln(-1);
    }
}

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

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

    auto a = new int[n];

    foreach (ref t; a) readf(" %s", t);

    sort(a);

    if (k == n || a[k-1] != a[k])
    {
        writeln(a[k-1]);
    }
    else
    {
        writeln(-1);
    }
}

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

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

    auto a = new int[n];

    foreach (ref t; a) readf(" %s", t);

    auto sortedRange = sort(a);

    if (sortedRange.upperBound(a[k-1]).length == n-k)
    {
        writeln(a[k-1]);
    }
    else
    {
        writeln(-1);
    }
}

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

auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=rdsp;foreach(ref v;t)pick(r,v);}

void main()
{
  int n, k; readV(n, k);
  int[] a; readA(n, a);

  a.sort();
  if (a[k-1] == 0 || a[k-1] == a[k])
    writeln(-1);
  else
    writeln(a[k-1]);
}

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

auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=rdsp;foreach(ref v;t)pick(r,v);}

void main()
{
  int n, k; readV(n, k);
  int[] a; readA(n, a);

  a.sort();
  if (a[k-1] == a[k])
    writeln(-1);
  else
    writeln(a[k-1]);
}


import std.algorithm;
import std.stdio;

void main ()
{
	int n;
	while (scanf (" %d", &n) > 0)
	{
		auto a = new int [n];
		foreach (i; 0..n)
			scanf (" %d", &a[i]);
		int [] s;
		int res;
		foreach (i; 0..n)
		{
			while (s.length && s[$ - 1] < a[i])
			{
				res = max (res, s[$ - 1] ^ a[i]);
				s.length--;
			}
			if (s.length)
				res = max (res, s[$ - 1] ^ a[i]);
			s ~= a[i];
		}
		writeln (res);
	}
}

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

void main ()
{
	int n;
	while (scanf (" %d", &n) > 0)
	{
		auto a = new int [n];
		foreach (i; 0..n)
		{
			scanf (" %d", &a[i]);
		}
		int [] s;
		int res = 0;
		foreach (i; 0..n)
		{
			while (s.length >= 1 && s[$ - 1] < a[i])
			{
				res = max (res, s[$ - 1] ^ a[i]);
				s.length--;
			}
			s ~= a[i];
			if (s.length >= 2)
			{
				res = max (res, s[$ - 1] ^ s[$ - 2]);
			}
		}
		writefln ("%s", res);
	}
}


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

auto read()
{
	return readln.strip.split;
}

auto readn()
{
	return read.map!(to!int).array;
}

void main()
{
	auto ns = readn;
	auto words = read;

	int[string] weight;

	foreach(i, w; readn)
	{
		weight[words[i]] = w;
	}

	foreach(gw; ns[1].iota.map!(_ => readn))
	{
		auto ws = words.indexed(gw[1..$].map!(a => a - 1));
		auto m = ws.map!(a => weight[a]).reduce!min;

		ws.each!(a => weight[a] = m);
	}

	read.map!(a => weight[a]).reduce!`long(a)+b`.writeln;
}

import std.stdio;
import std.range;
import std.array;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.container;
import std.typecons;
import std.random;
import std.csv;
import std.regex;
import std.math;
import core.time;
import std.ascii;
import std.digest.sha;

void main()
{
	auto aaa = readln.chomp.split.map!(to!int);
	int n = aaa[0];
	int k = aaa[1];
	int m = aaa[2];
	string[] msg = readln.chomp.split;
	ulong[string] mapper;
	foreach (i, s; msg) {
		mapper[s] = i;
	}
	ulong[] cost = readln.chomp.split.map!(to!ulong).array;
	ulong[] best = new ulong[n];
	foreach (i; 0..k) {
		auto bbb = readln.chomp.split.map!(to!ulong);
		ulong x = bbb[0];
		ulong[] a = bbb[1..$].array;
		ulong ans = ulong.max;
		foreach (v; a) {
			ulong w = v - 1;
			ans = min(ans, cost[cast(uint)w]);
		}
		// stderr.writeln([x, ans]);
		foreach (v; a) {
			ulong w = v - 1;
			best[cast(uint)w] = ans;
			// stderr.writeln("\tbest : ", msg[w], " :", [w, ans]);
		}
	}
	string[] next = readln.chomp.split;
	ulong sum = 0;
	foreach (s; next) {
		sum += best[cast(uint)mapper[s]];
	}
	sum.writeln;
}

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

void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}}
void readA(T)(size_t n,ref T t){t=new T(n);auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(ElementType!T);r.popFront;}}
void readM(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=readln.splitter;foreach(ref v;t){v[i]=r.front.to!(ElementType!(typeof(v)));r.popFront;}}}
void readS(T)(size_t n,ref T t){t=new T(n);foreach(ref v;t){auto r=readln.splitter;foreach(ref j;v.tupleof){j=r.front.to!(typeof(j));r.popFront;}}}

void main()
{
  int n, k, m; readV(n, k, m);
  string[] w; readA(n, w);
  int[] a; readA(n, a);
  auto g = new int[][](k);
  foreach (i; 0..k) g[i] = readXA!int;
  foreach (ref gi; g) gi[] -= 1;
  string[] s; readA(m, s);

  auto mc = new int[](k);
  foreach (int i, gi; g)
    mc[i] = gi.map!(gij => a[gij]).fold!min;

  auto h = new int[](n);
  foreach (int i, gi; g)
    foreach (gij; gi)
      h[gij] = mc[i];

  int[string] wg;
  foreach (int i, wi; w) wg[wi] = h[i];

  writeln(s.map!(si => wg[si].to!long).sum);
}

T[] readXA(T)()
{
  auto r = readln.splitter;
  auto x = r.front.to!size_t; r.popFront;
  auto a = new T[](x);
  foreach (i; 0..x) {
    a[i] = r.front.to!T;
    r.popFront;
  }
  return a;
}

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

auto read()
{
	return readln.strip.split;
}

auto readn()
{
	return read.map!(to!int).array;
}

void main()
{
	auto ns = readn;
	auto words = read;

	int[string] weight;

	foreach(i, w; readn)
	{
		weight[words[i]] = w;
	}

	foreach(gw; ns[1].iota.map!(_ => readn))
	{
		auto ws = words.indexed(gw[1..$].map!(a => a - 1));
		auto m = ws.map!(a => weight[a]).reduce!min;

		ws.each!(a => weight[a] = m);
	}

	read.map!(a => weight[a]).reduce!`a+b`.writeln;
}

import std.stdio;
import std.range;
import std.array;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.container;
import std.typecons;
import std.random;
import std.csv;
import std.regex;
import std.math;
import core.time;
import std.ascii;
import std.digest.sha;

void main()
{
	auto aaa = readln.chomp.split.map!(to!int);
	int n = aaa[0];
	int k = aaa[1];
	int m = aaa[2];
	string[] msg = readln.chomp.split;
	ulong[string] mapper;
	foreach (i, s; msg) {
		mapper[s] = i;
	}
	int[] cost = readln.chomp.split.map!(to!int).array;
	int[] best = new int[n];
	foreach (i; 0..k) {
		auto bbb = readln.chomp.split.map!(to!int);
		int x = bbb[0];
		int[] a = bbb[1..$].array;
		int ans = int.max;
		foreach (v; a) {
			int w = v - 1;
			ans = min(ans, cost[w]);
		}
		// stderr.writeln([x, ans]);
		foreach (v; a) {
			int w = v - 1;
			best[w] = ans;
			// stderr.writeln("\tbest : ", msg[w], " :", [w, ans]);
		}
	}
	string[] next = readln.chomp.split;
	int sum = 0;
	foreach (s; next) {
		sum += best[cast(uint)mapper[s]];
	}
	sum.writeln;
}

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

void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}}
void readA(T)(size_t n,ref T t){t=new T(n);auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(ElementType!T);r.popFront;}}
void readM(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=readln.splitter;foreach(ref v;t){v[i]=r.front.to!(ElementType!(typeof(v)));r.popFront;}}}
void readS(T)(size_t n,ref T t){t=new T(n);foreach(ref v;t){auto r=readln.splitter;foreach(ref j;v.tupleof){j=r.front.to!(typeof(j));r.popFront;}}}

void main()
{
  int n, k, m; readV(n, k, m);
  string[] w; readA(n, w);
  int[] a; readA(n, a);
  auto g = new int[][](k);
  foreach (i; 0..k) g[i] = readXA!int;
  foreach (ref gi; g) gi[] -= 1;
  string[] s; readA(m, s);

  auto mc = new int[](k);
  foreach (int i, gi; g)
    mc[i] = gi.map!(gij => a[gij]).fold!min;

  auto h = new int[](n);
  foreach (int i, gi; g)
    foreach (gij; gi)
      h[gij] = mc[i];

  int[string] wg;
  foreach (int i, wi; w) wg[wi] = h[i];

  writeln(s.map!(si => wg[si]).sum);
}

T[] readXA(T)()
{
  auto r = readln.splitter;
  auto x = r.front.to!size_t; r.popFront;
  auto a = new T[](x);
  foreach (i; 0..x) {
    a[i] = r.front.to!T;
    r.popFront;
  }
  return a;
}


import std.stdio;
import std.algorithm;
import std.conv;
void main()
{
    auto total = new long[10000001];
    for(int i = 1; i < 10000001; i++) {
        for(int j = i; j < 10000001; j += i){
            total[j] += i;
        }
   }
    int[] at = new int[10000001];
    at.fill(-1);
    for(int i = 10000000;i>0;i--){
        if(total[i]<10000001){
            at[to!uint(total[i])] = i ;
    }
    }
    int n;
  readf!"%d"(n);

  while (n--) {
    int c;
    readf!" %d"(c);
    writeln(at[c]);
  }
}

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

void main() {
  auto total = new long[10000001];
  for (int i = 1; i < 10000001; i++) {
    for (int j = i; j < 10000001; j += i) {
      total[j] += i;
    }
  }

  int[] ans = new int[10000001];
  ans.fill(-1);

  for (int i = 0; i < 10000001; i++) {
    if (total[i] < 10000001 && ans[to!uint(total[i])] == -1) {
      ans[to!uint(total[i])] = i;
    }
  }

  int n;
  readf!"%d"(n);

  while (n--) {
    int c;
    readf!" %d"(c);
    writeln(ans[c]);
  }
}

import std.stdio;
import std.algorithm;

void main() {
  auto total = new long[10000001];
  for (int i = 1; i < 10000001; i++) {
    for (int j = i; j < 10000001; j += i) {
      total[j] += i;
    }
  }

  int[long] m;

  for (int i = 0; i < 50; i++) {
    if (total[i] !in m) {
      m[total[i]] = i;
    }
  }

  int n;
  readf!"%d"(n);

  while (n--) {
    int c;
    readf!" %d"(c);
    if (c !in m) {
      writeln(-1);
    } else {
      writeln(m[c]);
    }
  }

}