import core.bitop;
import core.checkedint;
import core.simd;
import core.stdc.stdio;
import core.stdc.stdlib;
import core.stdc.string;
import std.algorithm;
import std.array;
import std.ascii;
import std.bigint;
import std.bitmanip;
import std.complex;
import std.container;
import std.conv;
import std.datetime;
import std.format;
import std.functional;
import std.math;
import std.meta;
import std.numeric;
import std.random;
import std.range;
import std.regex;
import std.string;
import std.typecons;
import std.variant;

__gshared:

bool read(T...)(T ptrs) if (ptrs.length > 0) {
    return readf(' ' ~ replicate("%s ", ptrs.length), ptrs) == ptrs.length;
}

T[ ] allocate(T)(size_t n) {
    return (cast(T*)malloc(n * T.sizeof))[0 .. n];
}

auto pairwise(R)(R range) if (isForwardRange!R) {
    return lockstep(range, dropOne(range));
}

struct Lca(Node, int n, Flag!`checkBackReferences` checkBackReferences = No.checkBackReferences,
        Flag!`assignLineIndices` assignLineIndices = No.assignLineIndices) {
    Node*[n + 1] lp;
    int[n + 1] height;

    void init(Node* root) {
        int gid = 1;

        static if (checkBackReferences)
            alias PrevType = AliasSeq!(Node*);
        else
            alias PrevType = AliasSeq!();

        int dfs0(Node* self, in PrevType prev) {
            with (self) {
                id = deepestId = gid++;
                int h = bsf(id);
                foreach (node; adj) {
                    static if (checkBackReferences) {
                        if (node is prev[0])
                            continue;
                        const t = dfs0(node, self);
                    } else
                        const t = dfs0(node);
                    if (t > h) {
                        h = t;
                        deepestId = node.deepestId;
                    }
                    lp[node.deepestId] = self;
                }
                height[id] = h;
                return h;
            }
        }

        void dfs1(Node* self, in PrevType prev, int index) {
            with (self) {
                static if (assignLineIndices)
                    indexInLine = index;
                mask |= deepestId & -deepestId;
                foreach (node; adj)
                    static if (checkBackReferences) {
                        if (node !is prev[0]) {
                            node.mask = mask;
                            dfs1(node, self, deepestId == node.deepestId? index + 1 : 0);
                        }
                    } else {
                        node.mask = mask;
                        dfs1(node, deepestId == node.deepestId? index + 1 : 0);
                    }
            }
        }

        root.mask = 0x0;
        static if (checkBackReferences) {
            dfs0(root, null);
            dfs1(root, null, 0);
        } else {
            dfs0(root);
            dfs1(root, 0);
        }
    }

    Node* get(Node* x, Node* y) {
        const dpx = x.deepestId, dpy = y.deepestId;
        if (dpx != dpy) {
            const k = bsr(dpx ^ dpy);
            const zh = bsf(x.mask & y.mask & ~0 << height[(dpx >> k | 0x1) << k]);

            Node* raise(int deepestId, int mask) {
                const k = bsr(mask & ((1 << zh) - 1));
                return lp[(deepestId >> k | 0x1) << k];
            }

            if (bsf(x.mask) != zh)
                x = raise(dpx, x.mask);
            if (bsf(y.mask) != zh)
                y = raise(dpy, y.mask);
        }
        return x.id < y.id? x : y;
    }
}

struct AutoNode {
    int len;
    AutoNode*[char] next;
    AutoNode* link;
    AutoNode*[ ] invLinks;
    int id, deepestId, mask;

    alias adj = invLinks;

    this(this) {
        next = next.dup;
    }

    AutoNode* append(char c, AutoNode* last) {
        auto cur = createNode(last.len + 1);
        AutoNode** p;
        while ((p = c in last.next) is null) {
            last.next[c] = cur;
            last = last.link;
            if (last is null) {
                cur.link = &this;
                return cur;
            }
        }
        auto q = *p;
        if (q.len == last.len + 1) {
            cur.link = q;
            return cur;
        }
        auto clone = createNode(*q);
        clone.len = last.len + 1;
        cur.link = q.link = clone;
        do {
            *p = clone;
            last = last.link;
            if (last is null)
                break;
            p = c in last.next;
        } while (p !is null && *p == q);
        return cur;
    }
}

struct TreeNode {
    char c;
    TreeNode*[ ] adj;
    int id, deepestId, mask, indexInLine, indexInString;

    void genString(in TreeNode* prev) {
        indexInString = strSize;
        str[strSize++] = c;
        foreach (node; adj)
            if (node !is prev && node.deepestId == deepestId) {
                node.genString(&this);
                break;
            }
        foreach (node; adj)
            if (node !is prev && node.deepestId != deepestId)
                node.genString(&this);
    }
}

struct Waypoint {
    int pos, lpos, trgLpos;
    bool up;
}

int n;
TreeNode[300_000] _nodes;
int strSize;
char[300_000] str;
int auNodesSize;
AutoNode[ ] auNodes;
AutoNode*[300_000] _tdNodes, _buNodes;
AutoNode*[ ] tdNodes, buNodes;
Lca!(TreeNode, 300_000, Yes.checkBackReferences, Yes.assignLineIndices) treeLca;
Lca!(AutoNode, 1_200_000) auLca;
Waypoint[38][2] wp;

int solve(TreeNode* a, TreeNode* b, TreeNode* c, TreeNode* d) {
    auto calcWaypoints(TreeNode* a, TreeNode* b, Waypoint[ ] wp) {
        const lca = treeLca.get(a, b);
        int i = 0;

        void walkUp(const(TreeNode)* node, bool up) {
            while (node.deepestId != lca.deepestId) {
                wp[i++] = Waypoint(node.indexInString, node.indexInLine, 0, up);
                node = treeLca.lp[node.deepestId];
            }
            if (up)
                wp[i++] = Waypoint(node.indexInString, node.indexInLine, lca.indexInLine, up);
            else if (node !is lca)
                wp[i++] = Waypoint(node.indexInString, node.indexInLine, lca.indexInLine + 1, up);
        }

        walkUp(a, true);
        const mid = i;
        walkUp(b, false);
        wp[mid .. i].reverse();
        return wp[0 .. i];
    }

    auto wp0 = calcWaypoints(a, b, wp[0][ ]);
    auto wp1 = calcWaypoints(c, d, wp[1][ ]);
    int result = 0;
    while (!wp0.empty && !wp1.empty) {
        const aw = wp0.front;
        const cw = wp1.front;
        auto node0 = aw.up? buNodes[aw.pos] : tdNodes[aw.pos - aw.lpos + aw.trgLpos];
        auto node1 = cw.up? buNodes[cw.pos] : tdNodes[cw.pos - cw.lpos + cw.trgLpos];
        const aLim = aw.lpos - aw.trgLpos + 1;
        const cLim = cw.lpos - cw.trgLpos + 1;
        const lce = min(auLca.get(node0, node1).len, aLim, cLim);
        result += lce;
        if (lce < aLim && lce < cLim)
            return result;

        void update(ref Waypoint[ ] wp, int lim) {
            if (lce == lim)
                wp.popFront();
            else if (wp.front.up) {
                wp.front.pos -= lce;
                wp.front.lpos -= lce;
            } else
                wp.front.trgLpos += lce;
        }

        update(wp0, aLim);
        update(wp1, cLim);
    }

    return result;
}

auto createNode(int len) {
    auNodes[auNodesSize] = AutoNode(len);
    return &auNodes[auNodesSize++];
}

auto createNode(ref AutoNode node) {
    auNodes[auNodesSize] = node;
    return &auNodes[auNodesSize++];
}

void main() {
    auNodes = uninitializedArray!(AutoNode[ ])(1_200_000);
    while (scanf("%d ", &n) == 1) {
        auto nodes = _nodes[0 .. n];
        foreach (ref node; nodes) {
            version (LocalProject)
                node = TreeNode.init;
            node.c = cast(char)getchar();
        }
        foreach (i; 1 .. n) {
            int a, b;
            scanf("%d%d", &a, &b);
            a--;
            b--;
            nodes[a].adj ~= &nodes[b];
            nodes[b].adj ~= &nodes[a];
        }

        treeLca.init(&nodes[0]);
        strSize = 0;
        nodes[0].genString(null);
        assert(strSize == n);

        AutoNode root;
        tdNodes = _tdNodes[0 .. n];
        buNodes = _buNodes[0 .. n];
        auNodesSize = 0;
        //Build automata on reversed strings.
        auto last = &root;
        foreach_reverse (i, c; str[0 .. n])
            last = tdNodes[i] = root.append(c, last);
        foreach (i, c; str[0 .. n])
            last = buNodes[i] = root.append(c, last);
        foreach (ref node; auNodes[0 .. auNodesSize])
            node.link.invLinks ~= &node;
        auLca.init(&root);

        int q;
        scanf("%d", &q);
        while (q--) {
            int a, b, c, d;
            scanf("%d%d%d%d", &a, &b, &c, &d);
            printf("%d\n", solve(&nodes[a - 1], &nodes[b - 1], &nodes[c - 1], &nodes[d - 1]));
        }
    }
}

import core.bitop;
import core.checkedint;
import core.simd;
import core.stdc.stdio;
import core.stdc.stdlib;
import core.stdc.string;
import std.algorithm;
import std.array;
import std.ascii;
import std.bigint;
import std.bitmanip;
import std.complex;
import std.container;
import std.conv;
import std.datetime;
import std.format;
import std.functional;
import std.math;
import std.meta;
import std.numeric;
import std.random;
import std.range;
import std.regex;
import std.string;
import std.typecons;
import std.variant;

__gshared:

bool read(T...)(T ptrs) if (ptrs.length > 0) {
    return readf(' ' ~ replicate("%s ", ptrs.length), ptrs) == ptrs.length;
}

T[ ] allocate(T)(size_t n) {
    return (cast(T*)calloc(n, T.sizeof))[0 .. n];
}

auto pairwise(R)(R range) if (isForwardRange!R) {
    return lockstep(range, dropOne(range));
}

struct Lca(Node, int n, Flag!`checkBackReferences` checkBackReferences = No.checkBackReferences,
        Flag!`assignLineIndices` assignLineIndices = No.assignLineIndices) {
    Node*[n + 1] lp;
    int[n + 1] height;

    void init(Node* root) {
        int gid = 1;

        static if (checkBackReferences)
            alias PrevType = AliasSeq!(Node*);
        else
            alias PrevType = AliasSeq!();

        int dfs0(Node* self, in PrevType prev) {
            with (self) {
                id = deepestId = gid++;
                int h = bsf(id);
                foreach (node; adj) {
                    static if (checkBackReferences) {
                        if (node is prev[0])
                            continue;
                        const t = dfs0(node, self);
                    } else
                        const t = dfs0(node);
                    if (t > h) {
                        h = t;
                        deepestId = node.deepestId;
                    }
                    lp[node.deepestId] = self;
                }
                height[id] = h;
                return h;
            }
        }

        void dfs1(Node* self, in PrevType prev, int index) {
            with (self) {
                static if (assignLineIndices)
                    indexInLine = index;
                mask |= deepestId & -deepestId;
                foreach (node; adj)
                    static if (checkBackReferences) {
                        if (node !is prev[0]) {
                            node.mask = mask;
                            dfs1(node, self, deepestId == node.deepestId? index + 1 : 0);
                        }
                    } else {
                        node.mask = mask;
                        dfs1(node, deepestId == node.deepestId? index + 1 : 0);
                    }
            }
        }

        root.mask = 0x0;
        static if (checkBackReferences) {
            dfs0(root, null);
            dfs1(root, null, 0);
        } else {
            dfs0(root);
            dfs1(root, 0);
        }
    }

    Node* get(Node* x, Node* y) {
        const dpx = x.deepestId, dpy = y.deepestId;
        if (dpx != dpy) {
            const k = bsr(dpx ^ dpy);
            const zh = bsf(x.mask & y.mask & ~0 << height[(dpx >> k | 0x1) << k]);

            Node* raise(int deepestId, int mask) {
                const k = bsr(mask & ((1 << zh) - 1));
                return lp[(deepestId >> k | 0x1) << k];
            }

            if (bsf(x.mask) != zh)
                x = raise(dpx, x.mask);
            if (bsf(y.mask) != zh)
                y = raise(dpy, y.mask);
        }
        return x.id < y.id? x : y;
    }
}

struct AutoNode {
    int len;
    //AutoNode*[char] next;
    AutoNode*[26] next;
    AutoNode* link;
    AutoNode*[ ] invLinks;
    int id, deepestId, mask;

    alias adj = invLinks;

    /+
    this(this) {
        next = next.dup;
    }
    +/

    AutoNode* append(int c, AutoNode* last) {
        /+
        while (auNodesSize >= 1_200_000) { }
        auNodes[auNodesSize] = AutoNode(last.len + 1);
        auto cur = &auNodes[auNodesSize++];
        +/
        auto cur = new AutoNode(last.len + 1);
        /+
        AutoNode** p;
        while ((p = c in last.next) is null) {
            last.next[c] = cur;
            last = last.link;
            if (last is null) {
                cur.link = &this;
                return cur;
            }
        }
        while (p is null) { }
        +/
        for (; last !is null && last.next[c] is null; last = last.link)
            last.next[c] = cur;
        if (last is null) {
            cur.link = &this;
            return cur;
        }
        while (last is null) { }
        auto q = /+*p+/ last.next[c];
        while (q is null) { }
        if (q.len == last.len + 1) {
            cur.link = q;
            return cur;
        }
        /+
        while (auNodesSize >= 1_200_000) { }
        auNodes[auNodesSize] = *q;
        auto clone = &auNodes[auNodesSize++];
        +/
        auto clone = new AutoNode;
        *clone = *q;
        clone.len = last.len + 1;
        //clone.next = clone.next.dup;
        cur.link = q.link = clone;
        for (; last !is null && last.next[c] == q; last = last.link)
            last.next[c] = clone;
        /+
        do {
            while (p is null) { }
            *p = clone;
            last = last.link;
            if (last is null)
                break;
            p = c in last.next;
        } while (p !is null && *p == q);
        +/
        return cur;
    }
}

struct TreeNode {
    char c;
    TreeNode*[ ] adj;
    int id, deepestId, mask, indexInLine, indexInString;

    void genString(in TreeNode* prev) {
        indexInString = strSize;
        str[strSize++] = c;
        foreach (node; adj)
            if (node !is prev && node.deepestId == deepestId) {
                node.genString(&this);
                break;
            }
        foreach (node; adj)
            if (node !is prev && node.deepestId != deepestId)
                node.genString(&this);
    }
}

struct Waypoint {
    int pos, lpos, trgLpos;
    bool up;
}

int n;
TreeNode[300_000] _nodes;
int strSize;
char[300_000] str;
int auNodesSize;
AutoNode[ ] auNodes;
AutoNode*[300_000] _tdNodes, _buNodes;
AutoNode*[ ] tdNodes, buNodes;
Lca!(TreeNode, 300_000, Yes.checkBackReferences, Yes.assignLineIndices) treeLca;
Lca!(AutoNode, 1_200_000) auLca;
Waypoint[38][2] wp;

int solve(TreeNode* a, TreeNode* b, TreeNode* c, TreeNode* d) {
    auto calcWaypoints(TreeNode* a, TreeNode* b, Waypoint[ ] wp) {
        const lca = treeLca.get(a, b);
        int i = 0;

        void walkUp(const(TreeNode)* node, bool up) {
            while (node.deepestId != lca.deepestId) {
                wp[i++] = Waypoint(node.indexInString, node.indexInLine, 0, up);
                node = treeLca.lp[node.deepestId];
            }
            if (up)
                wp[i++] = Waypoint(node.indexInString, node.indexInLine, lca.indexInLine, up);
            else if (node !is lca)
                wp[i++] = Waypoint(node.indexInString, node.indexInLine, lca.indexInLine + 1, up);
        }

        walkUp(a, true);
        const mid = i;
        walkUp(b, false);
        wp[mid .. i].reverse();
        return wp[0 .. i];
    }

    auto wp0 = calcWaypoints(a, b, wp[0][ ]);
    auto wp1 = calcWaypoints(c, d, wp[1][ ]);
    int result = 0;
    while (!wp0.empty && !wp1.empty) {
        const aw = wp0.front;
        const cw = wp1.front;
        auto node0 = aw.up? buNodes[aw.pos] : tdNodes[aw.pos - aw.lpos + aw.trgLpos];
        auto node1 = cw.up? buNodes[cw.pos] : tdNodes[cw.pos - cw.lpos + cw.trgLpos];
        const aLim = aw.lpos - aw.trgLpos + 1;
        const cLim = cw.lpos - cw.trgLpos + 1;
        const lce = min(auLca.get(node0, node1).len, aLim, cLim);
        result += lce;
        if (lce < aLim && lce < cLim)
            return result;

        void update(ref Waypoint[ ] wp, int lim) {
            if (lce == lim)
                wp.popFront();
            else if (wp.front.up) {
                wp.front.pos -= lce;
                wp.front.lpos -= lce;
            } else
                wp.front.trgLpos += lce;
        }

        update(wp0, aLim);
        update(wp1, cLim);
    }

    return result;
}

void main() {
    //auNodes = allocate!AutoNode(1_200_000);
    while (scanf("%d ", &n) == 1) {
        auto nodes = _nodes[0 .. n];
        foreach (ref node; nodes) {
            version (LocalProject)
                node = TreeNode.init;
            node.c = cast(char)getchar();
        }
        foreach (i; 1 .. n) {
            int a, b;
            scanf("%d%d", &a, &b);
            a--;
            b--;
            nodes[a].adj ~= &nodes[b];
            nodes[b].adj ~= &nodes[a];
        }

        treeLca.init(&nodes[0]);
        strSize = 0;
        nodes[0].genString(null);
        while (strSize != n) { }

        AutoNode root;
        while (root.len) { }
        while (root.link !is null) { }
        tdNodes = _tdNodes[0 .. n];
        buNodes = _buNodes[0 .. n];
        auNodesSize = 0;
        //Build automata on reversed strings.
        auto last = &root;
        foreach_reverse (i, c; str[0 .. n]) {
            while (i < 0 || i >= tdNodes.length) { }
            last = tdNodes[i] = root.append(c - 'a', last);
        }
        foreach (i, c; str[0 .. n]) {
            while (i < 0 || i >= buNodes.length) { }
            last = buNodes[i] = root.append(c - 'a', last);
        }
        /+
        foreach (ref node; auNodes[0 .. auNodesSize]) {
            while (node.link is null) { }
            node.link.invLinks ~= &node;
        }
        +/

        void traverse(AutoNode* node) {
            if (node.link !is null) {
                node.link.invLinks ~= node;
                node.link = null;
                foreach (next; node.next)
                    if (next !is null)
                        traverse(next);
            }
        }

        foreach (node; root.next)
            if (node !is null)
                traverse(node);
        auLca.init(&root);

        int q;
        scanf("%d", &q);
        while (q--) {
            int a, b, c, d;
            scanf("%d%d%d%d", &a, &b, &c, &d);
            printf("%d\n", solve(&nodes[a - 1], &nodes[b - 1], &nodes[c - 1], &nodes[d - 1]));
        }
    }
}

import core.bitop;
import core.checkedint;
import core.simd;
import core.stdc.stdio;
import core.stdc.stdlib;
import core.stdc.string;
import std.algorithm;
import std.array;
import std.ascii;
import std.bigint;
import std.bitmanip;
import std.complex;
import std.container;
import std.conv;
import std.datetime;
import std.format;
import std.functional;
import std.math;
import std.meta;
import std.numeric;
import std.random;
import std.range;
import std.regex;
import std.string;
import std.typecons;
import std.variant;

__gshared:

bool read(T...)(T ptrs) if (ptrs.length > 0) {
    return readf(' ' ~ replicate("%s ", ptrs.length), ptrs) == ptrs.length;
}

T[ ] allocate(T)(size_t n) {
    return (cast(T*)calloc(n, T.sizeof))[0 .. n];
}

auto pairwise(R)(R range) if (isForwardRange!R) {
    return lockstep(range, dropOne(range));
}

struct Lca(Node, int n, Flag!`checkBackReferences` checkBackReferences = No.checkBackReferences,
        Flag!`assignLineIndices` assignLineIndices = No.assignLineIndices) {
    Node*[n + 1] lp;
    int[n + 1] height;

    void init(Node* root) {
        int gid = 1;

        static if (checkBackReferences)
            alias PrevType = AliasSeq!(Node*);
        else
            alias PrevType = AliasSeq!();

        int dfs0(Node* self, in PrevType prev) {
            with (self) {
                id = deepestId = gid++;
                int h = bsf(id);
                foreach (node; adj) {
                    static if (checkBackReferences) {
                        if (node is prev[0])
                            continue;
                        const t = dfs0(node, self);
                    } else
                        const t = dfs0(node);
                    if (t > h) {
                        h = t;
                        deepestId = node.deepestId;
                    }
                    lp[node.deepestId] = self;
                }
                height[id] = h;
                return h;
            }
        }

        void dfs1(Node* self, in PrevType prev, int index) {
            with (self) {
                static if (assignLineIndices)
                    indexInLine = index;
                mask |= deepestId & -deepestId;
                foreach (node; adj)
                    static if (checkBackReferences) {
                        if (node !is prev[0]) {
                            node.mask = mask;
                            dfs1(node, self, deepestId == node.deepestId? index + 1 : 0);
                        }
                    } else {
                        node.mask = mask;
                        dfs1(node, deepestId == node.deepestId? index + 1 : 0);
                    }
            }
        }

        root.mask = 0x0;
        static if (checkBackReferences) {
            dfs0(root, null);
            dfs1(root, null, 0);
        } else {
            dfs0(root);
            dfs1(root, 0);
        }
    }

    Node* get(Node* x, Node* y) {
        const dpx = x.deepestId, dpy = y.deepestId;
        if (dpx != dpy) {
            const k = bsr(dpx ^ dpy);
            const zh = bsf(x.mask & y.mask & ~0 << height[(dpx >> k | 0x1) << k]);

            Node* raise(int deepestId, int mask) {
                const k = bsr(mask & ((1 << zh) - 1));
                return lp[(deepestId >> k | 0x1) << k];
            }

            if (bsf(x.mask) != zh)
                x = raise(dpx, x.mask);
            if (bsf(y.mask) != zh)
                y = raise(dpy, y.mask);
        }
        return x.id < y.id? x : y;
    }
}

struct AutoNode {
    int len;
    //AutoNode*[char] next;
    AutoNode*[26] next;
    AutoNode* link;
    AutoNode*[ ] invLinks;
    int id, deepestId, mask;

    alias adj = invLinks;

    /+
    this(this) {
        next = next.dup;
    }
    +/

    AutoNode* append(int c, AutoNode* last) {
        while (auNodesSize >= 1_200_000) { }
        auNodes[auNodesSize] = AutoNode(last.len + 1);
        auto cur = &auNodes[auNodesSize++];
        /+
        AutoNode** p;
        while ((p = c in last.next) is null) {
            last.next[c] = cur;
            last = last.link;
            if (last is null) {
                cur.link = &this;
                return cur;
            }
        }
        while (p is null) { }
        +/
        for (; last !is null && last.next[c] is null; last = last.link)
            last.next[c] = cur;
        if (last is null) {
            cur.link = &this;
            return cur;
        }
        while (last is null) { }
        auto q = /+*p+/ last.next[c];
        while (q is null) { }
        if (q.len == last.len + 1) {
            cur.link = q;
            return cur;
        }
        while (auNodesSize >= 1_200_000) { }
        auNodes[auNodesSize] = *q;
        auto clone = &auNodes[auNodesSize++];
        clone.len = last.len + 1;
        //clone.next = clone.next.dup;
        cur.link = q.link = clone;
        for (; last !is null && last.next[c] == q; last = last.link)
            last.next[c] = clone;
        /+
        do {
            while (p is null) { }
            *p = clone;
            last = last.link;
            if (last is null)
                break;
            p = c in last.next;
        } while (p !is null && *p == q);
        +/
        return cur;
    }
}

struct TreeNode {
    char c;
    TreeNode*[ ] adj;
    int id, deepestId, mask, indexInLine, indexInString;

    void genString(in TreeNode* prev) {
        indexInString = strSize;
        str[strSize++] = c;
        foreach (node; adj)
            if (node !is prev && node.deepestId == deepestId) {
                node.genString(&this);
                break;
            }
        foreach (node; adj)
            if (node !is prev && node.deepestId != deepestId)
                node.genString(&this);
    }
}

struct Waypoint {
    int pos, lpos, trgLpos;
    bool up;
}

int n;
TreeNode[300_000] _nodes;
int strSize;
char[300_000] str;
int auNodesSize;
AutoNode[ ] auNodes;
AutoNode*[300_000] _tdNodes, _buNodes;
AutoNode*[ ] tdNodes, buNodes;
Lca!(TreeNode, 300_000, Yes.checkBackReferences, Yes.assignLineIndices) treeLca;
Lca!(AutoNode, 1_200_000) auLca;
Waypoint[38][2] wp;

int solve(TreeNode* a, TreeNode* b, TreeNode* c, TreeNode* d) {
    auto calcWaypoints(TreeNode* a, TreeNode* b, Waypoint[ ] wp) {
        const lca = treeLca.get(a, b);
        int i = 0;

        void walkUp(const(TreeNode)* node, bool up) {
            while (node.deepestId != lca.deepestId) {
                wp[i++] = Waypoint(node.indexInString, node.indexInLine, 0, up);
                node = treeLca.lp[node.deepestId];
            }
            if (up)
                wp[i++] = Waypoint(node.indexInString, node.indexInLine, lca.indexInLine, up);
            else if (node !is lca)
                wp[i++] = Waypoint(node.indexInString, node.indexInLine, lca.indexInLine + 1, up);
        }

        walkUp(a, true);
        const mid = i;
        walkUp(b, false);
        wp[mid .. i].reverse();
        return wp[0 .. i];
    }

    auto wp0 = calcWaypoints(a, b, wp[0][ ]);
    auto wp1 = calcWaypoints(c, d, wp[1][ ]);
    int result = 0;
    while (!wp0.empty && !wp1.empty) {
        const aw = wp0.front;
        const cw = wp1.front;
        auto node0 = aw.up? buNodes[aw.pos] : tdNodes[aw.pos - aw.lpos + aw.trgLpos];
        auto node1 = cw.up? buNodes[cw.pos] : tdNodes[cw.pos - cw.lpos + cw.trgLpos];
        const aLim = aw.lpos - aw.trgLpos + 1;
        const cLim = cw.lpos - cw.trgLpos + 1;
        const lce = min(auLca.get(node0, node1).len, aLim, cLim);
        result += lce;
        if (lce < aLim && lce < cLim)
            return result;

        void update(ref Waypoint[ ] wp, int lim) {
            if (lce == lim)
                wp.popFront();
            else if (wp.front.up) {
                wp.front.pos -= lce;
                wp.front.lpos -= lce;
            } else
                wp.front.trgLpos += lce;
        }

        update(wp0, aLim);
        update(wp1, cLim);
    }

    return result;
}

void main() {
    auNodes = new AutoNode[1_200_000];
    while (auNodes.ptr is null) { }
    while (scanf("%d ", &n) == 1) {
        auto nodes = _nodes[0 .. n];
        foreach (ref node; nodes) {
            version (LocalProject)
                node = TreeNode.init;
            node.c = cast(char)getchar();
        }
        foreach (i; 1 .. n) {
            int a, b;
            scanf("%d%d", &a, &b);
            a--;
            b--;
            nodes[a].adj ~= &nodes[b];
            nodes[b].adj ~= &nodes[a];
        }

        treeLca.init(&nodes[0]);
        strSize = 0;
        nodes[0].genString(null);
        while (strSize != n) { }

        AutoNode root;
        while (root.len) { }
        while (root.link !is null) { }
        tdNodes = _tdNodes[0 .. n];
        buNodes = _buNodes[0 .. n];
        auNodesSize = 0;
        //Build automata on reversed strings.
        auto last = &root;
        foreach_reverse (i, c; str[0 .. n]) {
            while (i < 0 || i >= tdNodes.length) { }
            last = tdNodes[i] = root.append(c - 'a', last);
        }
        foreach (i, c; str[0 .. n]) {
            while (i < 0 || i >= buNodes.length) { }
            last = buNodes[i] = root.append(c - 'a', last);
        }
        /+
        foreach (ref node; auNodes[0 .. auNodesSize]) {
            while (node.link is null) { }
            node.link.invLinks ~= &node;
        }
        +/

        void traverse(AutoNode* node) {
            if (node.link !is null) {
                node.link.invLinks ~= node;
                node.link = null;
                foreach (next; node.next)
                    if (next !is null)
                        traverse(next);
            }
        }

        foreach (node; root.next)
            if (node !is null)
                traverse(node);
        auLca.init(&root);

        int q;
        scanf("%d", &q);
        while (q--) {
            int a, b, c, d;
            scanf("%d%d%d%d", &a, &b, &c, &d);
            printf("%d\n", solve(&nodes[a - 1], &nodes[b - 1], &nodes[c - 1], &nodes[d - 1]));
        }
    }
}


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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto t = RD!int;
	auto ans = new bool[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto a = RDA;
		auto b = RDA;
		bool add, sub;
		ans[ti] = true;
		foreach (i; 0..n)
		{
			if (a[i] > b[i])
			{
				if (!sub)
				{
					ans[ti] = false;
					break;
				}
			}
			else if (a[i] < b[i])
			{
				if (!add)
				{
					ans[ti] = false;
					break;
				}
			}
			if (a[i] > 0)
				add = true;
			else if (a[i] < 0)
				sub = true;
		}
	}

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

//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!uint;
    auto a = r.nextA!int (n);
    auto b = r.nextA!int (n);
    auto mask = uninitializedArray!(uint[])(n);
    int flags = 0;
    foreach (i; 0 .. n) {
      if (a[i] < 0) flags |= 1;
      if (a[i] > 0) flags |= 2;
      mask[i] = flags;
    }
    bool test () {
      foreach_reverse (j; 0 .. n) {
        if (b[j] == a[j]) continue;
        const f = j > 0 ? mask[j-1] : 0;
        if (b[j] > a[j] && (f & 2)) continue;
        if (b[j] < a[j] && (f & 1)) continue;
        return false;
      }
      return true;
    }
    writeln (test() ? "YES" : "NO");
  }
}

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(); } bool DEBUG = 0;
void log(A ...)(lazy A a){ if(DEBUG) print(a); }
void print(){ writeln(""); } void print(T)(T t){ writeln(t); } void print(T, A ...)(T t, A a){ write(t, " "), print(a); }
string unsplit(T)(T xs){ return xs.array.to!(string[]).join(" "); }
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)(int n){ return n.iota.map!(i => scan!T()).array; }
T lowerTo(T)(ref T x, T y){ if(x > y) x = y; return x; } T raiseTo(T)(ref T x, T y){ if(x < y) x = y; return x; }

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

void solve(){
    foreach(_; 0 .. scan!int){
        int n = scan!int;
        long[] as = scan!long(n), bs = scan!long(n);
        bool f, g;
        bool isOK = 1;
        foreach(i; 0 .. n){
            long a = as[i], b = bs[i];
            if(a < b && ! f) isOK = 0;
            if(a > b && ! g) isOK = 0;

            if(a > 0) f = 1;
            if(a < 0) g = 1;
        }
        (isOK? "YES": "NO").writeln;
    }
}

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto a = readln.splitter.map !(to !(int)).array;
		auto b = readln.splitter.map !(to !(int)).array;
		bool havePos = false;
		bool haveNeg = false;
		bool canDo = true;
		foreach (i; 0..n)
		{
			if (a[i] < b[i])
			{
				canDo &= havePos;
			}
			if (a[i] > b[i])
			{
				canDo &= haveNeg;
			}
			havePos |= (a[i] > 0);
			haveNeg |= (a[i] < 0);
		}
		writeln (canDo ? "YES" : "NO");
	}
}


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;

// zero-prefixed array
long solvez(long[] a, long s)
{
  long[long] freq;
  freq[s]++;
  foreach (i ; 1 .. a.length) {
    if (a[i] == 0) {
      long best = 0;
      long ans = 0;
      foreach (k, v ; freq) {
        if (v > best) {
          best = v;
        }
      }
      return best + solvez(a[i .. $], s);
    }
    s += a[i];
    freq[s]++;
  }
  long best = 0;
  long ans = 0;
  foreach (k, v ; freq) {
    if (v > best) {
      best = v;
    }
  }
  return best;
}

long solve(long[] a)
{
  long ans = 0;
  long s = 0;
  foreach (i ; 0 .. a.length) {
    s += a[i];
    if (a[i] == 0)
      return ans + solvez(a[i .. $], s);
    if (s == 0)
      ans++;
  }
  return ans;
}

void main()
{
  foreach (casenum ; 1 .. readln.strip.to!int + 1) {
    auto n = readln.strip.to!long;
    auto a = readln.splitter.map!(to!long).array;
    writeln(solve(a));
  }
}

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

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

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 solve() {
    int n = read!int;
    auto s = readarray!long;
    long[long] d;
    long cur_sum, res;
    int i = 0;
    bool flag = false;
    while (i < s.length) {
        cur_sum += s[i];
        //writeln("sum", cur_sum);
        if (flag) {
            if (s[i] == 0) {
                auto m_key = d.byPair.maxElement!(p => p.value);
                cur_sum += m_key.key;
                res += m_key.value;
                d.clear();
            }
            d[cur_sum] = 1 + d.get(cur_sum, 0);
        }
        else if (s[i] == 0) {
            flag = true;
            d[cur_sum] = 1 + d.get(cur_sum, 0);
        }
        else if (cur_sum == 0)
            res++;
        i++;
    }
    if (!d.empty) {
        auto m_key = d.byPair.maxElement!(p => p.value);
        cur_sum += m_key.key;
        res += m_key.value;
    }
    writeln(res);
}

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


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

string FMT_F = "%.10f";

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

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

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

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

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

		auto l = a.MIN_POS;
		auto r = a.MIN_POS!"a > b";
		ans[ti] = [l+1, r+1];
	}

	foreach (e; ans)
		writeln(e[0], " ", e[1]);
	stdout.flush;
	debug readln;
}

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto a = readln.splitter.map !(to !(int)).array;
		auto i = n - a.minPos.length.to !(int);
		auto j = n - a.maxPos.length.to !(int);
		writeln (i + 1, " ", j + 1);
	}
}


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

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

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

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

void main() {
  try {
    for (; ; ) {
      const numCases = readInt();
      foreach (caseId; 0 .. numCases) {
        const S = readToken();
        int sum, zero, even;
        foreach (c; S) {
          const d = c - '0';
          sum += d;
          if (d == 0) {
            ++zero;
          }
          if (d % 2 == 0) {
            ++even;
          }
        }
        const ans = (sum % 3 == 0 && zero >= 1 && even >= 2);
        writeln(ans ? "red" : "cyan");
      }
    }
  } catch (EOFException e) {
  }
}

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

void main() {
    auto T = readln.chomp.to!int;
    auto cnt = new int[](10);
    while (T--) {
        cnt[] = 0;
        auto S = readln.chomp;
        auto N = S.length.to!int;
        int zero = 0;
        int even = 0;
        int div3 = 0;
        foreach (i; 0..N) {
            int d = S[i]-'0';
            if (d == 0) zero += 1;
            if (d % 2 == 0) even += 1;
            div3 = (div3 + d) % 3;
        }
        if (zero >= 1 && even >= 2 && div3 == 0) {
            writeln("red");
        } else {
            writeln("cyan");
        }
    }
}

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

bool solve (string s)
{
	auto t = s.map !(q{a - '0'}).array;
	sort (t);
	if (t[0] != 0)
	{
		return false;
	}
	if (t.sum % 3 != 0)
	{
		return false;
	}
	if (!t.drop (1).canFind !(x => x % 2 == 0))
	{
		return false;
	}
	return true;
}

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto s = readln.strip;
		writeln (solve (s) ? "red" : "cyan");
	}
}

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, std.regex;
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(){
	int n = rint;
	string[] as = rtypes!string(n);

	foreach(a; as){
		string ans;
		int[] ks = a.split("").map!(to!int).array;
		int[] cnt = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
		foreach(k; ks) cnt[k] += 1;
		if(cnt[0] == 0) ans = "cyan";
		else if(cnt[0] + cnt[2] + cnt[4] + cnt[6] + cnt[8] <= 1) ans = "cyan";
		else if(ks.sum % 3 != 0) ans = "cyan";
		else ans = "red";
		ans.writeln;
	}
}

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 main()
{
	auto n = RD!int;
	foreach (i; 0..n)
	{
		auto s = RD!string;
		auto cnt = new int[](10);
		foreach (c; s)
		{
			auto x = c - '0';
			++cnt[x];
		}
		int x;
		foreach (j; 0..10)
			x += cnt[j] * j;

		if (cnt[0] == 0 || x % 3)
			writeln("cyan");
		/*else if (s.length <= 3)
		{
			bool ok;
			if (cnt[6] >= 1)
				ok = true;
			else if (cnt[1] == 1 && cnt[2]+cnt[8] == 1)
				ok = true;
			else if (cnt[4] == 1 && cnt[2]+cnt[5]+cnt[8] == 1)
				ok = true;
			else if (cnt[7] == 1 && cnt[8]+cnt[2] == 1)
				ok = true;
			writeln(ok ? "red" : "cyan");
		}*/
		else if (cnt[0] == 1)
		{
			bool ok;
			foreach (j; 1..5)
			{
				if (cnt[j*2])
					ok = true;
			}
			writeln(ok ? "red" : "cyan");
		}
		else
		{
			writeln("red");
		}
	}

	stdout.flush;
	debug readln;
}

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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto n = RD!int;
	foreach (i; 0..n)
	{
		auto s = RD!string;
		auto cnt = new int[](10);
		foreach (c; s)
		{
			auto x = c - '0';
			++cnt[x];
		}
		int x;
		foreach (j; 0..10)
			x += cnt[j] * j;
		if (x % 3)
			writeln("cyan");
		else
		{
			if ((cnt[0] >= 1 && cnt[6] >= 1) || (cnt[0] >= 2))
				writeln("red");
			else
				writeln("cyan");
		}
	}

	stdout.flush;
	debug readln;
}

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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto n = RD!int;
	foreach (i; 0..n)
	{
		auto s = RD!string;
		auto cnt = new int[](10);
		foreach (c; s)
		{
			auto x = c - '0';
			++cnt[x];
		}
		int x;
		foreach (j; 0..10)
			x += cnt[j] * j;

		if (s.length <= 3)
		{
			if (cnt[0] == 0)
				writeln("cyan");
			else
			{
				bool ok;
				if (cnt[6] >= 1)
					ok = true;
				else if (cnt[1] == 1 && cnt[2]+cnt[8] == 1)
					ok = true;
				else if (cnt[4] == 1 && cnt[2]+cnt[5]+cnt[8] == 1)
					ok = true;
				else if (cnt[7] == 1 && cnt[8]+cnt[2] == 1)
					ok = true;
				writeln(ok ? "red" : "cyan");
			}
		}
		else
		{
			writeln(x % 3 || (cnt[0] >= 1 && cnt[0]+cnt[6] <= 1) ? "cyan" : "red");
		}
	}

	stdout.flush;
	debug readln;
}

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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto n = RD!int;
	foreach (i; 0..n)
	{
		auto s = RD!string;
		auto cnt = new int[](10);
		foreach (c; s)
		{
			auto x = c - '0';
			++cnt[x];
		}
		int x;
		foreach (j; 0..10)
			x += cnt[j] * j;

		if (s.length <= 3)
		{
			if (cnt[0] == 0)
				writeln("cyan");
			else
			{
				bool ok;
				if (cnt[6] >= 1)
					ok = true;
				else if (cnt[1] == 1 && cnt[2]+cnt[8] == 1)
					ok = true;
				else if (cnt[4] == 1 && cnt[2]+cnt[5]+cnt[8] == 1)
					ok = true;
				else if (cnt[7] == 1 && cnt[8]+cnt[2] == 1)
					ok = true;
				writeln(ok ? "red" : "cyan");
			}
		}
		else
		{
			writeln(x % 3 || (cnt[0] == 0 || cnt[0]+cnt[6] <= 1) ? "cyan" : "red");
		}
	}

	stdout.flush;
	debug readln;
}


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

string FMT_F = "%.10f";

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

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

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

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

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

		ans[ti] = n / 10;
		if (n % 10 == 9)
			++ans[ti];
	}

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

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		writeln ((n + 1) / 10);
	}
}

// code by cutx64

module main;

import std.string, std.array, std.container, std.typecons;
import std.stdio, std.format, std.file, std.outbuffer;
import std.algorithm, std.conv, std.functional, std.range, core.bitop, std.random;
// import std.bigint, std.complex, std.bitmanip;
// import std.math, std.mathspecial, std.numeric;

void main() {

  int tests; readf!"%s\n"(tests);
  while(tests--) {
    int n; readf!"%s\n"(n);
    ((n + 1) / 10).writeln;
  }

} // main

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

int main()
{
	int t = readInt!int;
	while (t--)
	{
		int n = readInt!int;
		if (n % 10 == 9)
		{
			n++;
			(n/10).writeln;
		}
		else
		{
			n -= n%10;
			(n/10).writeln;
		}
	}
	return 0;
}

/* INPUT ROUTINES */
int currChar;
static this()
{
	currChar = getchar();
}

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

void popChar()
{
	currChar = getchar();
}

auto readInt(T)()
{
	T num = 0;
	int sgn = 0;
	while (topChar.isWhite)
		popChar;
	while (topChar == '-' || topChar == '+')
	{
		sgn ^= (topChar == '-');
		popChar;
	}
	while (topChar.isDigit)
	{
		num *= 10;
		num += (topChar - '0');
		popChar;
	}
	if (sgn)
		return -num;
	return num;
}

string readString()
{
	string res = "";
	while (topChar.isWhite)
		popChar;
	while (!topChar.isWhite)
	{
		res ~= topChar;
		popChar;
	}
	return res;
}
/**MODULAR SYSTEM*/
struct Z(immutable long m)
{
	long rep;
	this(long num)
	{
		rep = num;
	}

	Z!m opBinary(string operator)(Z!m rhs)
	{
		static if (operator == "+")
		{
			long result = rhs.rep + this.rep;
			if (result >= m)
				result -= m;
			return Z!m(result);
		}
		else static if (operator == "-")
		{
			long result = this.rep - rhs.rep;
			if (result < 0)
				result += m;
			return Z!m(result);
		}
		else static if (operator == "*")
		{
			long result = this.rep * rhs.rep;
			if (result >= m)
				result %= m;
			return Z!m(result);
		}
		else
			static assert(text("Operator ", operator, " not supported"));
	}

	Z!m opBinary(string operator)(long exponent) if (operator == "^^")
	{
		assert(exponent >= 0);
		Z!m base = this;
		Z!m result = 1;
		while (exponent)
		{
			if (exponent & 1)
				result = result * base;
			base = base * base;
			exponent >>= 1;
		}
		return result;
	}

	invariant
	{
		assert(rep >= 0 && rep < m);
	}
}

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

void main()
{
  long t;
  readf!" %d "(t);
  while (t--) {
    long n;
    readf!" %d "(n);
    writeln((n + 1) / 10);
  }
}


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

void main ()
{
	bool [long] cubes;
	for (long s = 1; s <= 10 ^^ 4; s++)
	{
		cubes[s ^^ 3] = true;
	}

	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(long);
		bool ok = false;
		for (long s = 1; s ^^ 3 <= n; s++)
		{
			ok |= ((n - s ^^ 3) in cubes) !is null;
		}
		writeln (ok ? "YES" : "NO");
	}
}

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

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

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

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

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

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

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

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

void main()
{
    bool[long] memo;
    for (long b = 1; b^^3 <= 10L^^12; ++b) memo[b^^3] = true;
    int T; get(T); while (T--) {
        long X; get(X);
        for (long a = 1; a^^3 <= X; ++a) if (X - a^^3 in memo) {
            writeln("YES");
            goto ok;
        }
        writeln("NO");
        ok:
    }
}

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

string FMT_F = "%.10f";

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

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

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

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

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

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

		for (long i = 1; i^^3 < x; ++i)
		{
			auto a = i^^3;
			auto y = x - a;
			bool f(long j)
			{
				if (j > y/j) return false;
				if (j*j > y/j) return false;
				return j^^3 <= y;
			}
			auto r = binarySearch!(f)(1, y+1);
			if (i == 5779)
				debug writeln("i:", i, " r:", r);
			ans[ti] = true;
			foreach (j; 0..3)
			{
				if (y % r)
				{
					if (i == 5779)
						debug writeln("y:", y, " r:", r);
					ans[ti] = false;
					break;
				}
				y /= r;
			}
			if (ans[ti] && y == 1)
				break;
			else
				ans[ti] = false;

		}
	}

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


import std.algorithm;
import std.stdio;

immutable int MED_N = 10;
immutable int MAX_N = MED_N << 1;
immutable int POW_N = 1 << MAX_N;

int [POW_N] a;
int n;

void main ()
{
    while (scanf (" %d", &n) != EOF)
    {
        foreach (i; 0..n)
        {
            scanf (" %d", &a[i]);
        }
        sort (a[0..n]);
        reverse (a[0..n]);

        long k = 0;
        for (int p = n; p > 0; p >>= 2)
        {
            k++;
        }
        int cur = 1, total = 3;

        long res = 0;
        for (int i = 0; i < n; i++)
        {
            cur--;
            res += k * a[i];
            if (cur == 0)
            {
                k--;
                cur = total;
                total *= 4;
            }
        }

        writeln (res);
    }
}

import std.algorithm;
import std.stdio;

immutable int MED_N = 10;
immutable int MAX_N = MED_N << 1;
immutable int POW_N = 1 << MAX_N;

int [POW_N] a;
int n;

void main ()
{
    while (scanf (" %d", &n) > 0)
    {
        foreach (i; 0..n)
        {
            scanf (" %d", &a[i]);
        }
        sort (a[0..n]);
        reverse (a[0..n]);

        long k = 0;
        for (int p = n; p > 0; p >>= 2)
        {
            k++;
        }
        int cur = 1, total = 3;

        long res = 0;
        for (int i = 0; i < n; i++)
        {
            cur--;
            res += k * a[i];
            if (cur == 0)
            {
                k--;
                cur = total;
                total *= 4;
            }
        }

        writeln (res);
    }
}

import std.stdio: readln, writeln;
import std.conv: to;
import std.string: strip;
import std.array: split;
import std.algorithm: sort, reverse;

/**
 * User: Jior
 * Date: 04.06.13
 * Time: 17:55
 */

void main(string[] args) {
	int countEls = to!int(strip(readln()));
	long[] els = to!(long[])(strip(readln()).split());
	sort(els);
	reverse(els);

	foreach (i, ref long el; els[1..$]) {
		el += els[i];
	}

	long result;
	while (countEls > 0) {
		result += els[countEls-1];
		countEls /= 4;
	}
	writeln(result);
}

import std.stdio: readln, writeln;
import std.conv: to;
import std.string: strip;
import std.array: split;
import std.algorithm: sort, reverse;

/**
 * User: Jior
 * Date: 04.06.13
 * Time: 17:55
 */

void main(string[] args) {
	int countEls = to!int(strip(readln()));
	int[] els = to!(int[])(strip(readln()).split());
	sort(els);
	reverse(els);

	foreach (i, ref int el; els[1..$]) {
		el += els[i];
	}

	long result;
	while (countEls > 0) {
		result += els[countEls-1];
		countEls /= 4;
	}
	writeln(result);
}

import std.stdio: readln, writeln;
import std.conv: to;
import std.string: strip;
import std.array: split;

import std.math: sqrt;
import std.algorithm: max;

/**
 * User: Jior
 * Date: 04.06.13
 * Time: 17:55
 */

int beauty(int[] mx) {
	int n = cast(int)sqrt(cast(float)mx.length);
	if (n == 1)
		return mx[0];

	int m = mx[0];
	foreach(int l; mx[1..$]) {
		m = max(m, l);
	}

	int[] left, right;
	foreach (int i; 0..n) {
		left ~= mx[i*n..((i+1)*n)-n/2];
		right ~= mx[i*n+n/2..(i+1)*n];
	}

	return beauty(left[0..$/2]) + beauty(left[$/2..$]) + beauty(right[0..$/2]) + beauty(right[$/2..$]) + m;
}

void main(string[] args) {
	int countEls = to!int(strip(readln()));
	int[] els = to!(int[])(strip(readln()).split());
	writeln(beauty(els));
}


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

long mex(long[] a)
{
  foreach (i ; 0L .. 10L) {
    bool good = true;
    foreach (x ; a) {
      if (x == i)
        good = false;
    }
    if (good)
      return i;
  }
  return -1L;
}

void main()
{
  long t;
  readf!" %d "(t);

  long[] cache;
  long acc = 0;
  foreach (x ; 0L .. 3 * 100000 + 5) {
    acc ^= x;
    cache ~= acc;
  }

  assert(cache[4 - 1] == 0);
  assert(cache[5 - 1] == 4);

  while (t--) {
    long a, b;
    readf!" %d %d "(a, b);
//    writefln("a=%d b=%d", a, b);
/*    if (mex([0L, b]) == a)
      writeln(2);
    else */ {
      long x = cache[cast(int)a - 1];
      if (x == b)
        writefln("%d", a);
      else if ((x ^ b) != a)
        writefln("%d", a + 1);
      else
        writefln("%d", a + 2);
    }
  }
}

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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto t = RD!int;
	auto ans = new long[](t);
	auto arr = new long[]((10^^5) * 3);
	foreach (i; 1..(10^^5) * 3)
	{
		arr[i] = arr[i-1] ^ i;
	}
	foreach (ti; 0..t)
	{
		auto a = RD!int;
		auto b = RD!int;

		auto x = arr[a-1] ^ b;

		if (x == 0)
			ans[ti] = a;
		else if (x == a)
			ans[ti] = a + 2;
		else
			ans[ti] = a + 1;
	}

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

immutable multi = true;

int[3_00_000 + 3] cumulativeXor;
static this()
{
	cumulativeXor[0] = 0;
	foreach(i; 1 .. cumulativeXor.length)
		cumulativeXor[i] = cast(int)i ^ cumulativeXor[i-1];
}

void solve(int tc)
{
	auto a = readInt!int;
	auto b = readInt!int;
	if (cumulativeXor[a-1] == b)
	{
		writeln(a);
	}
	else
	{
		auto req = cumulativeXor[a-1] ^ b;
		if (req < a)
		{
			writeln(a+1);
		}
		else if (req > a)
		{
			writeln(a+1);
		}
		else
		{
			writeln(a+2);
		}
	}
}

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

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

void popChar()
{
	import core.stdc.stdio;
	currChar = getchar();
}

auto readInt(T)()
{
	T num = 0;
	int sgn = 0;
	while (topChar.isWhite)
	{
		popChar;
	}
	while (topChar == '-' || topChar == '+')
	{
		sgn ^= (topChar == '-');
		popChar;
	}
	while (topChar.isDigit)
	{
		num *= 10;
		num += (topChar - '0');
		popChar;
	}
	if (sgn)
		return -num;
	return num;
}

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

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

long mex(long[] a)
{
  foreach (i ; 0L .. 10L) {
    bool good = true;
    foreach (x ; a) {
      if (x == i)
        good = false;
    }
    if (good)
      return i;
  }
  return -1L;
}

void main()
{
  long t;
  readf!" %d "(t);

  long[] cache;
  long acc = 0;
  foreach (x ; 0L .. 3 * 100000 + 5) {
    acc ^= x;
    cache ~= acc;
  }

  assert(cache[4 - 1] == 0);
  assert(cache[5 - 1] == 4);

  while (t--) {
    long a, b;
    readf!" %d %d "(a, b);
//    writefln("a=%d b=%d", a, b);
    if (mex([0L, b]) == a)
      writeln(2);
    else {
      long x = cache[cast(int)a - 1];
      if (x == b)
        writefln("%d", a);
      else if ((x ^ b) != a)
        writefln("%d", a + 1);
      else
        writefln("%d", a + 2);
    }
  }
}

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

long mex(long[] a)
{
  foreach (i ; 0L .. 10L) {
    bool good = true;
    foreach (x ; a) {
      if (x == i)
        good = false;
    }
    if (good)
      return i;
  }
  return -1L;
}

void main()
{
  long t;
  readf!" %d "(t);
  while (t--) {
    long a, b;
    readf!" %d %d "(a, b);
    if (mex([0L, b]) != a)
      writeln(3);
    else
      writeln(2);
  }
}


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

void main()
{
  long t;
  readf!" %d "(t);
  while (t--) {
    long n;
    readf!" %d "(n);
    int[] a;
    for (int i = 0; i + 1 < n; i += 2) {
      a ~= (i + 1) + 1;
      a ~= (i + 0) + 1;
    }
    if (n % 2 == 1) {
      a ~= cast(int)a.length - 1;
      a[$ - 2] = cast(int)a.length;
    }
    writeln(a.map!text.joiner(" "));
  }
}

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

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

while(t--) {
    int n;
    readf("%s\n", &n);
    if(n == 1) {
        "1".writeln;
        continue;
    }
    if(n % 2 == 0) {
        for(int i = 1; i <= n; ++i) {
            if(i % 2 == 1) {
                writef("%s ", i + 1);
            } else {
                writef("%s ", i - 1);
            }
        }
        "".writeln;
        continue;
    }
    "2 3 1 ".write;
    for(int i = 4; i <= n; ++i) {
        if(i % 2 == 0) {
            writef("%s ", i + 1);
        } else {
            writef("%s ", i - 1);
        }
    }
    "".writeln;
}
}

import std.stdio;

void print(int start, int n) {
  if (start > n) return;
  write(start + 1, ' ', start,' ');
  print(start + 2, n);
}

void main() {
  int t, n;

  readf!"%d"(t);

  while (t--) {
    readf!" %d"(n);

    int start = 0;

    if (n >= 3 && n % 2 == 1) {
      write("3 1 2 ");
      print(4, n);
    } else {
      print(1, n);
    }
    writeln;
  }
}

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

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

while(t--) {
    int n;
    readf("%s\n", &n);
    writef("%s ", n);
    for(int i = 1; i < n; ++i) {
        writef("%s ", i);
    } "".writeln;
}
}


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

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

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

void main() {
    foreach(t; 0 .. to!int(readln.strip)) {
        int n = to!int(readln.strip);
        auto ar = readln.split.map!(to!int).array;
        ar.partition!"a%2";
        writeln(ar.map!(to!string).join(" "));
    }
}


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

struct ModInt(uint M_) {
  import std.conv : to;
  alias M = M_;
  uint x;
  this(ModInt a) { x = a.x; }
  this(uint x_) { x = x_ % M; }
  this(ulong x_) { x = cast(uint)(x_ % M); }
  this(int x_) { x = ((x_ %= cast(int)(M)) < 0) ? (x_ + cast(int)(M)) : x_; }
  this(long x_) { x = cast(uint)(((x_ %= cast(long)(M)) < 0) ? (x_ + cast(long)(M)) : x_); }
  ref ModInt opAssign(T)(inout(T) a) if (is(T == uint) || is(T == ulong) || is(T == int) || is(T == long)) { return this = ModInt(a); }
  ref ModInt opOpAssign(string op, T)(T a) {
    static if (is(T == ModInt)) {
      static if (op == "+") { x = ((x += a.x) >= M) ? (x - M) : x; }
      else static if (op == "-") { x = ((x -= a.x) >= M) ? (x + M) : x; }
      else static if (op == "*") { x = cast(uint)((cast(ulong)(x) * a.x) % M); }
      else static if (op == "/") { this *= a.inv(); }
      else static assert(false);
      return this;
    } else static if (op == "^^") {
      if (a < 0) return this = inv()^^(-a);
      ModInt b = this, c = 1U;
      for (long e = a; e; e >>= 1) { if (e & 1) c *= b; b *= b; }
      return this = c;
    } else {
      return mixin("this " ~ op ~ "= ModInt(a)");
    }
  }
  ModInt inv() const {
    uint a = M, b = x; int y = 0, z = 1;
    for (; b; ) { const q = a / b; const c = a - q * b; a = b; b = c; const w = y - cast(int)(q) * z; y = z; z = w; }
    assert(a == 1); return ModInt(y);
  }
  ModInt opUnary(string op)() const {
    static if (op == "+") { return this; }
    else static if (op == "-") { ModInt a; a.x = x ? (M - x) : 0U; return a; }
    else static assert(false);
  }
  ModInt opBinary(string op, T)(T a) const { return mixin("ModInt(this) " ~ op ~ "= a"); }
  ModInt opBinaryRight(string op, T)(T a) const { return mixin("ModInt(a) " ~ op ~ "= this"); }
  bool opCast(T: bool)() const { return (x != 0U); }
  string toString() const { return x.to!string; }
}

enum MO = 998244353;
alias Mint = ModInt!MO;

enum LIM = 2 * 10^^6 + 10;
Mint[] inv, fac, invFac;
void prepare() {
  inv = new Mint[LIM];
  fac = new Mint[LIM];
  invFac = new Mint[LIM];
  inv[1] = 1;
  foreach (i; 2 .. LIM) {
    inv[i] = -((Mint.M / i) * inv[cast(size_t)(Mint.M % i)]);
  }
  fac[0] = invFac[0] = 1;
  foreach (i; 1 .. LIM) {
    fac[i] = fac[i - 1] * i;
    invFac[i] = invFac[i - 1] * inv[i];
  }
}
Mint binom(long n, long k) {
  if (n < 0) {
    if (k >= 0) {
      return (-1)^^(k & 1) * binom(-n + k - 1, k);
    } else if (n - k >= 0) {
      return (-1)^^((n - k) & 1) * binom(-k - 1, n - k);
    } else {
      return Mint(0);
    }
  } else {
    if (0 <= k && k <= n) {
      assert(n < LIM);
      return fac[cast(size_t)(n)] * invFac[cast(size_t)(k)] * invFac[cast(size_t)(n - k)];
    } else {
      return Mint(0);
    }
  }
}

void main() {
  prepare;

  /*
  debug {
    foreach (n; 1 .. 5 + 1) {
      auto freq = new int[string][n];
      auto ps = iota(n).array;
      do {
        int[] qs = ps.dup;
        int[][] qss = [qs.dup];
        foreach (k; 0 .. n - 1) {
          foreach (i; 0 .. n - 1) {
            if (qs[i] > qs[i + 1]) {
              swap(qs[i], qs[i + 1]);
            }
          }
          qss ~= qs.dup;
        }
        auto rss = new int[][](n, n);
        foreach (k; 0 .. n) {
          foreach (i; 0 .. n) {
            foreach (j; 0 .. i) if (qss[k][j] > qss[k][i]) {
              ++rss[k][i];
            }
          }
        }
        writeln(qss, " ", rss);
        foreach (k; 0 .. n - 1) {
          auto fs = rss[k].dup;
          auto gs = rss[k + 1].dup;
          fs.sort;
          gs.sort;
          foreach (i; 0 .. n) {
            assert(max(fs[i] - 1, 0) == gs[i]);
          }
        }
        foreach (k; 0 .. n) {
          ++freq[k][rss[k].to!string];
        }
      } while (ps.nextPermutation);
      foreach (k; 0 .. n) {
        foreach (rs, val; freq[k]) {
          writeln(k, " ", rs, ": ", val);
        }
      }
    }
    // return;
  }
  */

  try {
    for (; ; ) {
      const numCases = readInt;
      foreach (caseId; 0 .. numCases) {
        const N = readInt;
        const K = readInt;
        auto V = new int[N];
        foreach (i; 0 .. N) {
          V[i] = readInt;
        }

        Mint ans = 1;
        foreach (i; 0 .. N - K) {
          if (~V[i]) {
            if (V[i] == 0) {
              ans *= (K + 1);
            }
          } else {
            ans *= ((K + 1) + i);
          }
        }
        foreach (i; N - K .. N) {
          if (~V[i]) {
            if (V[i] != 0) {
              ans = 0;
            }
          }
          ans *= (N - i);
        }
        writeln(ans);
      }
    }
  } catch (EOFException e) {
  }
}

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

immutable int mod = 998_244_353;

int solve (int [] a, int n, int k)
{
	foreach (i; 0..n - k)
	{
		if (a[i] > i)
		{
			return 0;
		}
	}

	foreach (i; n - k..n)
	{
		if (a[i] > 0)
		{
			return 0;
		}
	}

	int res = 1;
	foreach (i; 0..k)
	{
		res = (res * 1L * (i + 1)) % mod;
	}
	foreach (i; 0..n - k)
	{
		if (a[i] == -1)
		{
			res = (res * 1L * (i + k + 1)) % mod;
		}
		else if (a[i] == 0)
		{
			res = (res * 1L * (k + 1)) % mod;
		}
	}
	return res;
}

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


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 (_; 0..T) {
        auto N = readln.chomp.to!int;
        auto as = readln.split.to!(int[]);
        auto xs = new int[](5001);
        auto ls = new int[](5001);
        foreach (i; 1..N+1) {
            auto a = as[i-1];
            if (xs[a]) {
                if (xs[a] > 1) goto ok;
                if (ls[a] != i-1) goto ok;
            }
            ++xs[a];
            ls[a] = i;
        }
        writeln("NO");
        continue;
        ok:
        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 = 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 modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }

void main()
{
	auto t = RD!int;
	auto ans = new bool[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto a = RDA(-1);
		auto cnt = new int[][](n);
		foreach (i; 0..n)
		{
			cnt[a[i]] ~= i;
		}
		foreach (i; 0..n)
		{
			if (cnt[i].length <= 1) continue;
			if (cnt[i].length >= 3)
			{
				ans[ti] = true;
				break;
			}
			else
			{
				if (cnt[i][0]+1 != cnt[i][1])
				{
					ans[ti] = true;
					break;
				}
			}
		}
	}

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

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 (_; 0..T) {
        auto N = readln.chomp.to!int;
        auto as = readln.split.to!(int[]);
        foreach (i; 0..N) {
            if (i+2 < N && as[i] == as[i+2]) goto ok;
            if (i+3 < N && as[i] == as[i+3] && as[i+1] == as[i+2]) goto ok;
        }
        writeln("NO");
        continue;
        ok:
        writeln("YES");
    }
}


immutable multi = true;

void solve(int tc)
{
  auto n = readInt!long;
  auto isPrime(long m)
  {
    for (long d = 2; d * d <= m; d++)
      if (m % d == 0) return false;
    return true;
  }
  n--;
  for (long d = 2;; d++)
    {
      if (isPrime(d) && n % d != 0)
	{
	  writeln(n - d, " ", d, " ", 1);
	  return;
	}
    }
}

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

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

void popChar()
{
	import core.stdc.stdio;
	currChar = getchar();
}

auto readInt(T)()
{
	T num = 0;
	int sgn = 0;
	while (topChar.isWhite)
		popChar;
	while (topChar == '-' || topChar == '+')
	{
		sgn ^= (topChar == '-');
		popChar;
	}
	while (topChar.isDigit)
	{
		num *= 10;
		num += (topChar - '0');
		popChar;
	}
	if (sgn)
		return -num;
	return num;
}

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

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		int c = 1;
		int a = n - n / 2;
		int b = n - a - c;
		if (gcd (a, b) != c)
		{
			a += 1;
			b -= 1;
		}
		if (gcd (a, b) != c)
		{
			assert (false);
		}
		writeln (a, " ", b, " ", c);
	}
}

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

string FMT_F = "%.10f";

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

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

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

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

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

		if (n % 2)
		{
			--n;
			auto x = n / 2;
			auto d = x % 2 ? 2 : 1;
			ans[ti] = [x-d, x+d, 1];
		}
		else
		{
			ans[ti] = [n/2, n/2-1, 1];
		}
	}

	foreach (e; ans)
		writeln(e[0], " ", e[1], " ", e[2]);
	stdout.flush;
	debug readln;
}

import std;

T read (T, string ending = "", string beginning = "") () @trusted {
    scope T x;
    readf!(beginning ~ "%s" ~ ending)(x);
    return x;
}

void main () {
    immutable tests = readln.chomp.to!uint;
    TEST_LOOP: foreach (test_index; 0 .. tests) {
        immutable n =
            readln().chomp()
            .to!uint;

        if (n % 2 == 0) {
            writeln(n - 3, " 2 1");
        } else {
            immutable half = n / 2;

            immutable first = half - (half % 2 == 0 ? 1 : 2);
            immutable second = half + (half % 2 == 0 ? 1 : 2);

            writeln(first, ' ', second, ' ', 1);
        }
    }
}

// "" `` '' () {} []


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

T read(T)() { return readln.chomp.to!T; }
T[] readarray(T)() { return readln.chomp.split(" ").map!(to!T).array; }
void chmin(T)(ref T a, in T b) { a = min(a, b); }
void chmax(T)(ref T a, in T b) { a = max(a, b); }

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

void solve() {
    int N = read!int;
    auto A = readarray!int;

    bool check(int[] xs) {
        if (xs.length <= 2) return true;
        auto n = xs.length;
        for (int i = 0; i < n; i++) {
            for (int j = i+1; j < n; j++) {
                for (int k = j+1; k < n; k++) {
                    if (xs[i] <= xs[j] && xs[j] <= xs[k]) return false;
                    if (xs[i] >= xs[j] && xs[j] >= xs[k]) return false;
                }
            }
        }
        return true;
    }

    int ans = 0;
    for (int l = 0; l < N; l++) {
        for (int r = l + 1; r <= min(l + 4, N); r++) {
            if (check(A[l..r])) {
                //writeln([l, r]);
                ans++;
            }
        }
    }
    writeln(ans);
}

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

string FMT_F = "%.10f";

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

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

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

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

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

		if (n == 1)
		{
			ans[ti] = 1;
			continue;
		}

		ans[ti] = 3;
		bool f(int i, int j, int k)
		{
			return inside(a[j], min(a[i], a[k]), max(a[i], a[k])+1);
		}
		foreach (i; 0..n-2)
		{
			if (f(i, i+1, i+2))
				ans[ti] += 2;
			else
			{
				ans[ti] += 3;
				if (i + 3 < n)
				{
					bool ok = true;
					(){
					foreach (l; i..i+2)
					{
						foreach (m; l+1..i+4)
						{
							foreach (r; m+1..i+4)
							{
								if (f(l, m, r))
								{
									ok = false;
									return;
								}
							}
						}
					}}();
					if (ok)
						++ans[ti];
				}
			}
		}
	}

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

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

void main()
{
  int t;
  readf!" %d "(t);
  while (t--) {
    int n;
    readf!" %d "(n);
    auto a = readln.strip.split.map!(to!int).array;
    long result = 0;
    foreach (i ; 0 .. n) {
      if (i + 2 < n) {
        int x1 = a[i];
        int x2 = a[i + 1];
        int x3 = a[i + 2];
        if ((x2 > x1 && x2 > x3) || (x2 < x1 && x2 < x3))
          result++;
      }
      if (i + 3 < n) {
        int x1 = a[i];
        int x2 = a[i + 1];
        int x3 = a[i + 2];
        int x4 = a[i + 3];
        if ((x2 > x1 && x2 > x4) && (x3 < x1 && x3 < x4))
          result++;
        else if ((x2 < x1 && x2 < x4) && (x3 > x1 && x3 > x4))
          result++;
      }
    }
    writeln(result + n + n - 1);
  }
}