import core.bitop;
import core.checkedint;
import core.simd;
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.stdio;
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));
}

immutable class SegTree {
    SegTree left, right;
    bool inv;
    int size, count;

    this(bool value) {
        left = right = null;
        inv = false;
        size = 1;
        count = value;
    }

    this(immutable SegTree left, immutable SegTree right, bool inv) {
        this.left = left;
        this.right = right;
        this.inv = inv;
        size = getSize(left) + getSize(right);
        count = getCount(left) + getCount(right);
    }

    static int getSize(immutable SegTree t) {
        return t is null? 0 : t.size;
    }

    static int getCount(immutable SegTree t) {
        return t is null? 0 : t.inv? t.size - t.count : t.count;
    }

    static auto invertOne(immutable SegTree t) {
        if (t is null)
            return null;
        if (t.left is null && t.right is null)
            return new immutable SegTree(!t.count);
        return new immutable SegTree(t.left, t.right, !t.inv);
    }

    auto push() {
        return inv? new immutable SegTree(invertOne(left), invertOne(right), false) : this;
    }

    auto update(int i, bool value) {
        assert(size >= 1, "Size is %s".format(size));
        if (size == 1) {
            assert(!i);
            return new immutable SegTree(value);
        }
        auto t = push();
        if (i < getSize(left))
            return new immutable SegTree(t.left.update(i, value), t.right, false);
        return new immutable SegTree(t.left, t.right.update(i - getSize(left), value), false);
    }

    auto invert(int lb, int rb) {
        if (!lb && rb == size)
            return invertOne(this);
        int mid = getSize(left);
        auto t = push();
        auto newLeft = lb >= mid? t.left : t.left.invert(lb, min(mid, rb));
        auto newRight = rb <= mid? t.right : t.right.invert(max(lb - mid, 0), rb - mid);
        return new immutable SegTree(newLeft, newRight, false);
    }
}

auto construct(int n) {
    assert(n >= 1);
    if (n == 1)
        return new immutable SegTree(false);
    return new immutable SegTree(construct(n >> 1), construct(n - (n >> 1)), false);
}

void main() {
    int n, m, q;
    while (read(&n, &m, &q)) {
        auto roots = [construct(n * m)];
        roots.reserve(q + 1);
        while (q--) {
            char c;
            int a, b;
            read(&c, &a);
            final switch (c) {
                case '1': case '2': {
                    read(&b);
                    roots ~= roots.back.update((a - 1) * m + b - 1, c == '1');
                    break;
                }
                case '3': {
                    roots ~= roots.back.invert((a - 1) * m, a * m);
                    break;
                }
                case '4': {
                    roots ~= roots[a];
                    break;
                }
            }
            writeln(SegTree.getCount(roots.back));
        }
        debug writeln();
    }
}

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

immutable class SegTree {
    SegTree left, right;
    bool inv;
    int size, count;

    this(bool value) {
        left = right = null;
        inv = false;
        size = 1;
        count = value;
    }

    this(immutable SegTree left, immutable SegTree right, bool inv) {
        this.left = left;
        this.right = right;
        this.inv = inv;
        size = getSize(left) + getSize(right);
        count = getCount(left) + getCount(right);
    }

    static int getSize(immutable SegTree t) {
        return t is null? 0 : t.size;
    }

    static int getCount(immutable SegTree t) {
        return t is null? 0 : t.inv? t.size - t.count : t.count;
    }

    static auto invertOne(immutable SegTree t) {
        if (t is null)
            return null;
        if (t.left is null && t.right is null)
            return new immutable SegTree(!t.count);
        return new immutable SegTree(t.left, t.right, !t.inv);
    }

    auto push() {
        return inv? new immutable SegTree(invertOne(left), invertOne(right), false) : this;
    }

    auto update(int i, bool value) {
        assert(size >= 1, "Size is %s".format(size));
        if (size == 1) {
            assert(!i);
            return new immutable SegTree(value);
        }
        auto t = push();
        if (i < getSize(left))
            return new immutable SegTree(t.left.update(i, value), t.right, false);
        return new immutable SegTree(t.left, t.right.update(i - getSize(left), value), false);
    }

    auto invert(int lb, int rb) {
        if (!lb && rb == size)
            return invertOne(this);
        int mid = getSize(left);
        auto t = push();
        auto newLeft = lb >= mid? t.left : t.left.invert(lb, min(mid, rb));
        auto newRight = rb <= mid? t.right : t.right.invert(max(lb - mid, 0), rb - mid);
        return new immutable SegTree(newLeft, newRight, false);
    }
}

auto construct(int n) {
    assert(n >= 1);
    if (n == 1)
        return new immutable SegTree(false);
    return new immutable SegTree(construct(n >> 1), construct(n - (n >> 1)), false);
}

void main() {
    int n, m, q;
    while (scanf("%d%d%d", &n, &m, &q) == 3) {
        auto roots = [construct(n * m)];
        roots.reserve(q + 1);
        while (q--) {
            char c;
            int a, b;
            scanf(" %c%d", &c, &a);
            final switch (c) {
                case '1': case '2': {
                    scanf("%d", &b);
                    roots ~= roots.back.update((a - 1) * m + b - 1, c == '1');
                    break;
                }
                case '3': {
                    roots ~= roots.back.invert((a - 1) * m, a * m);
                    break;
                }
                case '4': {
                    roots ~= roots[a];
                    break;
                }
            }
            printf("%d\n", SegTree.getCount(roots.back));
        }
        debug putchar('\n');
    }
}

import core.bitop;
import core.checkedint;
import core.simd;
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.functional;
import std.math;
import std.meta;
import std.numeric;
import std.random;
import std.regex;
import std.stdio;
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;
}

final class SegTree {
    int size;
    int sum;
    bool inv;
    SegTree left, right;

    invariant {
        assert(size > 0);
        assert(sum >= 0);
        assert(sum <= size);
        assert((left is null) == (right is null));
    }

    this(int size, int sum, bool inv) {
        this(size, sum, inv, null, null);
    }

    this(int size, int sum, bool inv, SegTree left, SegTree right) {
        this.size = size;
        this.sum = sum;
        this.inv = inv;
        this.left = left;
        this.right = right;
    }

    static SegTree build(int n) {
        assert(n > 0);
        if (n == 1)
            return new SegTree(1, 0, false);
        return new SegTree(n, 0, false, build(n >>> 1), build(n - (n >>> 1)));
    }

    static int getSum(const SegTree sgt) {
        return sgt is null ? 0 : sgt.inv ? sgt.size - sgt.sum : sgt.sum;
    }

    private void _push() {
        if (inv) {
            if (left !is null) {
                left.inv ^= true;
                right.inv ^= true;
            }
            sum = size - sum;
            inv = false;
        }
    }

    SegTree set(int i, bool value) {
        if (left is null)
            return new SegTree(1, value, false);
        _push();
        if (i < left.size) {
            auto newLeft = left.set(i, value);
            return new SegTree(size, getSum(newLeft) + getSum(right), false, newLeft, right);
        } else {
            auto newRight = right.set(i - left.size, value);
            return new SegTree(size, getSum(left) + getSum(newRight), false, left, newRight);
        }
    }

    SegTree invert(int lb, int rb) {
        if (lb >= rb)
            return this;
        _push();
        if (!lb && rb == size)
            return new SegTree(size, sum, !inv, left, right);
        assert(left !is null);
        auto newLeft = left.invert(lb, min(left.size, rb));
        auto newRight = right.invert(max(lb - left.size, 0), rb - left.size);
        return new SegTree(size, getSum(newLeft) + getSum(newRight), false, newLeft, newRight);
    }
}

void main() {
    int n, m, q;
    while (read(&n, &m, &q)) {
        Array!SegTree roots;
        roots.length = q + 1;
        roots[0] = SegTree.build(n * m);
        foreach (cur; 0 .. q) {
            char cmd;
            int i, j;
            read(&cmd);
            switch (cmd) {
                case '1': {
                    read(&i, &j);
                    i--;
                    j--;
                    roots[cur + 1] = roots[cur].set(i * m + j, true);
                    break;
                }
                case '2': {
                    read(&i, &j);
                    i--;
                    j--;
                    roots[cur + 1] = roots[cur].set(i * m + j, false);
                    break;
                }
                case '3': {
                    read(&i);
                    roots[cur + 1] = roots[cur].invert((i - 1) * m, i * m);
                    break;
                }
                case '4': {
                    read(&i);
                    roots[cur + 1] = roots[i];
                    break;
                }
                default: assert(false);
            }
            writeln(SegTree.getSum(roots[cur + 1]));
        }
        debug writeln();
    }
}

import core.bitop;
import core.checkedint;
import core.simd;
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.functional;
import std.math;
import std.meta;
import std.numeric;
import std.random;
import std.regex;
import std.stdio;
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;
}

final class SegTree {
    SegTree left, right;
    int size;
    int sum;
    bool inv;

    this(int size, int sum, bool inv) {
        this(size, sum, inv, null, null);
    }

    this(int size, int sum, bool inv, SegTree left, SegTree right) {
        this.size = size;
        this.sum = sum;
        this.inv = inv;
        this.left = left;
        this.right = right;
    }

    static SegTree build(int n) {
        assert(n > 0);
        if (n == 1)
            return new SegTree(1, 0, false);
        return new SegTree(n, 0, false, build(n >>> 1), build(n - (n >>> 1)));
    }

    static int getSum(const SegTree sgt) {
        return sgt is null ? 0 : sgt.sum;
    }

    private void _push() {
        if (inv) {
            if (left) {
                left.sum = left.size - left.sum;
                left.inv ^= true;
            }
            if (right) {
                right.sum = right.size - right.sum;
                right.inv ^= true;
            }
            inv = false;
        }
    }

    SegTree add(int i) {
        if (left is null)
            return new SegTree(1, 1, false);
        _push();
        if (i < left.size) {
            auto newLeft = left.add(i);
            return new SegTree(size, newLeft.sum + right.sum, false, newLeft, right);
        } else {
            auto newRight = right.add(i - left.size);
            return new SegTree(size, left.sum + newRight.sum, false, left, newRight);
        }
    }

    SegTree remove(int i) {
        if (left is null) {
            assert(right is null);
            return new SegTree(1, 0, false);
        }
        _push();
        if (i < left.size) {
            auto newLeft = left.remove(i);
            return new SegTree(size, newLeft.sum + right.sum, false, newLeft, right);
        } else {
            auto newRight = right.remove(i - left.size);
            return new SegTree(size, left.sum + newRight.sum, false, left, newRight);
        }
    }

    SegTree invert(int lb, int rb) {
        if (lb >= rb)
            return this;
        _push();
        if (!lb && rb == size)
            return new SegTree(size, size - sum, !inv, left, right);
        if (left is null)
            writeln(lb, ' ', rb);
        assert(left !is null);
        assert(right !is null);
        auto newLeft = left.invert(lb, min(left.size, rb));
        auto newRight = right.invert(max(lb - left.size, 0), rb - left.size);
        return new SegTree(size, newLeft.sum + newRight.sum, false, newLeft, newRight);
    }
}

void main() {
    int n, m, q;
    while (read(&n, &m, &q)) {
        Array!SegTree roots;
        roots.length = q + 1;
        roots[0] = SegTree.build(n * m);
        foreach (cur; 0 .. q) {
            char cmd;
            int i, j;
            read(&cmd);
            switch (cmd) {
                case '1': {
                    read(&i, &j);
                    i--;
                    j--;
                    roots[cur + 1] = roots[cur].add(i * m + j);
                    break;
                }
                case '2': {
                    read(&i, &j);
                    i--;
                    j--;
                    roots[cur + 1] = roots[cur].remove(i * m + j);
                    break;
                }
                case '3': {
                    read(&i);
                    roots[cur + 1] = roots[cur].invert((i - 1) * m, i * m);
                    break;
                }
                case '4': {
                    read(&i);
                    roots[cur + 1] = roots[i];
                    break;
                }
                default: assert(false);
            }
            writeln(roots[cur + 1].sum);
        }
        debug writeln();
    }
}


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

int main() {

    auto tmp = split(strip(readln()), " ");
    auto n = to!int(tmp[0]);
    auto m = to!int(tmp[1]);
    auto x = to!int(tmp[2]);
    auto y = to!int(tmp[3]);
    auto table = new string[n];
    for (int i = 0; i < n; ++i)
        table[i] = strip(readln());

    int cntImpl(int color, int j) {
        int ans = 0;
        for (int i = 0; i < n; ++i)
            ans += table[i][j] == '.' ? color : 1 - color;
        return ans;
    }

    alias memoize!cntImpl cnt;

    int sum(int color, int end) {
        alias memoize!sum msum;
        if (end == 0)
            return cnt(color, end);
        else
            return cnt(color, end) + msum(color, end - 1);
    }

    int sum_of_color(int color, int start, int end) {
        if (start < 0) return 1000000000;
        return sum(color, end) - sum(color, start) + cnt(color, start);
    }

    int dp(int color, int num) {
        alias memoize!dp dpImpl;
        int ans = 1000000000;
        if (num < 0)
            return 0;
        for (int i = x; i <= y; ++i)
            ans = min(ans, dpImpl(1 - color, num - i) + sum_of_color(color, num - i + 1, num));
        return ans;
    }
    writeln(min(dp(0, m - 1), dp(1, m - 1)));
    return 0;
}

import std.stdio;
import std.array;
import std.algorithm;

const int max_n = 1024;
const int inf = cast(int)(1e9);

int[max_n][max_n][2] dp;

void main() {
    int n, m, x, y;
    readf("%d %d %d %d", &n, &m, &x, &y);
    int[] black = new int[m];
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            char c;
            readf(" %c", &c);
            black[j] += (c == '#');
        }
    }

    foreach (ref a; dp) {
        foreach (ref b; a) {
            b[] = inf;
        }
    }

    int get(int i, int c) {
        return (c == 0) ? black[i] : n - black[i];
    }

    for (int c = 0; c <= 1; ++c) {
        dp[c][0][1] = get(0, c);
    }

    for (int i = 1; i < m; ++i) {
        for (int c = 0; c <= 1; ++c) {
            dp[c][i][1] = get(i, c) + minPos(dp[c ^ 1][i - 1][x .. min($, y + 1)])[0];
            for (int j = 2; j <= i + 1; ++j) {
                dp[c][i][j] = get(i, c) + dp[c][i - 1][j - 1];
            }
        }
    }

    int res = inf;
    for (int c = 0; c <= 1; ++c) {
        res = min(res, minPos(dp[c][m - 1][x .. min($, y + 1)])[0]);
    }

    writeln(res);
}

import std.stdio;
import std.array;
import std.algorithm;

const int max_n = 10;
const int inf = cast(int)(1e9);

int[max_n][max_n][2] dp;

void main() {
    int n, m, x, y;
    readf("%d %d %d %d", &n, &m, &x, &y);
    int[] black = new int[m];
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            char c;
            readf(" %c", &c);
            black[j] += (c == '#');
        }
    }

    foreach (ref a; dp) {
        foreach (ref b; a) {
            b[] = inf;
        }
    }

    int get(int c) {
        return (c == 0) ? black[c] : n - black[c];
    }

    for (int c = 0; c <= 1; ++c) {
        dp[c][0][1] = get(c);
    }

    for (int i = 1; i < m; ++i) {
        for (int c = 0; c <= 1; ++c) {
            dp[c][i][1] = get(c) + minPos(dp[c ^ 1][i - 1][x .. y + 1])[0];
            for (int j = 2; j <= i + 1; ++j) {
                dp[c][i][j] = get(c) + dp[c][i - 1][j - 1];
            }
        }
    }

    int res = inf;
    for (int c = 0; c <= 1; ++c) {
        for (int j = x; j <= y; ++j) {
            res = min(res, dp[c][m - 1][j]);
        }
    }

    writeln(res);
}

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

int main() {

    auto tmp = split(strip(readln()), " ");
    auto n = to!int(tmp[0]);
    auto m = to!int(tmp[1]);
    auto x = to!int(tmp[2]);
    auto y = to!int(tmp[3]);
    string[] table = [];
    for (int i = 0; i < n; ++i)
        table ~= strip(readln());

    int cntImpl(int color, int j) {
        int ans = 0;
        for (int i = 0; i < n; ++i)
            ans += table[i][j] == '.' ? color : 1 - color;
        return ans;
    }

    alias memoize!cntImpl cnt;

    int sum(int color, int end) {
        alias memoize!sum msum;
        if (end < 0)
            return 1000000000;
        else if (end == 0)
            return cnt(color, end);
        else
            return cnt(color, end) + msum(color, end - 1);
    }

    int sum_of_color(int color, int start, int end) {
        return sum(color, end) - sum(color, start) + cnt(color, start);
    }

    int dp(int color, int num) {
        alias memoize!dp dpImpl;
        int ans = 1000000000;
        if (num == 0)
            return sum_of_color(color, 0, 0);
        for (int i = x; i <= y; ++i)
            if (num - i >= 0)
            	ans = min(ans, dpImpl(1 - color, num - i) + sum_of_color(color, num - i + 1, num));
        return ans;
    }
    writeln(min(dp(0, m - 1), dp(1, m - 1)));
    return 0;
}


import std.bigint,std.container,std.array,std.algorithm,std.conv,std.functional,std.math,std.numeric,std.string,std.range,std.complex,std.typecons,std.regex,
std.random,std.uni,std.traits,std.variant;
import core.stdc.stdio:freopen;
import std.stdio;
//import core.stdc.stdio;
alias RedBlackTree set;
alias redBlackTree rbt;
alias BigInt big;
alias pair!(int,int) pii;
alias pair!(long,long) pll;
alias BoyerMooreFinder strfind;
immutable int mod=1000000007;
@property pure nothrow T lcm (T)(auto ref T a,auto ref T b)
{
	return a/gcd(a,b)*b;
}
pure nothrow @property X binpow(X,Y)(X base,Y exp)
	if(is(typeof(exp&1)))
{
	if(exp<0)return X(0);
	X res=X(1);
	while(exp>0)
	{
		if(exp&1)res*=base;
		base*=base;
		exp>>=1;
	}
	return res;
}
pure nothrow @property auto binpow(X,Y,Z)(X base,Y exp,in Z mm)
	if(is(typeof(exp&1)))
{
	if(mm==0) return binpow(base,exp);
	if(exp<0)return X(0);
	auto res=X(1)%mm;
	while(exp>0)
	{
		if(exp&1)res=(res*base)%mm;
		base*=base;
		base%=mm;
		exp>>=1;
	}
	return res;
}
@property void putarr(X)(in X a,in char ch=' '){foreach(const ref i;a)write(i,ch);}
@property void putarr(X)(X a,in int n,in int m)
{
	foreach(i;0..n)
	{
		foreach(j;0..m)write(a[i][j],' ');
		writeln;
	}
}
@property bool getarr(X)(X a,in int n){bool b=1; foreach(ref i;a[0..n])b=input(&i);return b;}
bool input(T...)(T ptrs) if (ptrs.length > 0) {return readf(replicate(" %s", ptrs.length), ptrs)==ptrs.length;}
struct pair(X,Y)
{
	X first;
	Y second;
	alias first fi;
	alias second se;
	this(X a_,Y b_){
		fi=a_;
		se=b_;
		}
	this(this){fi=fi;se=se;}
	this(A,B)(auto ref pair!(A,B) p)
	{
		fi=p.fi;
		se=p.se;
	}
	int opCmp(in pair!(X,Y) s_) const pure nothrow @safe
	{
		int cmp=0-(fi<s_.fi)+(fi>s_.fi);
		if(cmp!=0)return cmp;
		else return (0-(se<s_.se)+(se>s_.se));
	}
};
pair!(X,Y) mp(X,Y)(in X x_,in Y y_) pure nothrow @safe
{
	pair!(X,Y) pp;
	pp.fi=x_;
	pp.se=y_;
	return pp;
}
X sqr(X)(in X a_) pure nothrow @property @safe @nogc {return a_*a_;}
X cub(X)(in X a_) pure nothrow @property @safe @nogc {return a_*a_*a_;}
nothrow @nogc @system void inf(in char* name) {freopen(name,"r",core.stdc.stdio.stdin);}
nothrow @nogc @system void ouf(in char* name) {freopen(name,"w",core.stdc.stdio.stdout);}
auto arread(T)(){return readln.split.map!(to!(T)).array;}
nothrow @property pure size_t lowb(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
{
	size_t l=0,r=a.length;
	while(r-l>1)
	{
		auto m=(l+r)>>1;
		if(!(a[m]<g))r=m;
		else l=m;
	}
	return (!(a[l]<g))?l:r;
}
pure @property nothrow size_t upb(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
{
	size_t l=0,r=a.length;
	while(r-l>1)
	{
		auto m=(l+r)>>1;
		if(g<a[m])r=m;
		else l=m;
	}
	return (g<a[l])?l:r;
}
pure @property nothrow size_t binf(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
{
	size_t l=0,r=a.length;
	while(r-l>1)
	{
		auto m=(l+r)>>1;
		if(!(a[m]<g))r=m;
		else l=m;
	}
	return (g==a[l])?l:a.length;
}
pure @property nothrow bool binary_search(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
{
	return binf(a,g)!=a.length;
}
//split strip isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin cumulativeFold schwartzSort multiSort partialSort heapify join filter
//---------------------------------------------------------------program beginning-----------------------------------------------------------------

void main()
{
	int n;
	while(input(&n))
	{
		readln;
		auto s=readln.strip;
		string ans;
		foreach(i;0..s.length)
		{
			if(n&1)
			{
				if((i&1)==0)ans~=s[i];
				else ans=s[i]~ans;
			}
			else
			{
				if((i&1)==0)ans=s[i]~ans;
				else ans~=s[i];
			}
		}
		writeln(ans);
	}
}

//ahmat
import core.stdc.stdio:scanf,printf,puts;
import core.bitop;
import std.stdio;
import std.string;
import std.algorithm;
import std.functional;
import std.array;
import std.container;
import std.conv;
import std.container;
import std.typecons;
import std.math;
import std.numeric;
pragma(inline,true);

void main(){
    int n=readln.chomp.to!int;
    auto s=readln.strip;
    char[] a;
    foreach(i;0..s.length){
        if(n&1) a~=s[i];
        else a=s[i]~a;
        n--;
    }
    writeln(a);
}

//ahmat
import core.stdc.stdio:scanf,printf,puts;
import core.bitop;
import std.stdio;
import std.string;
import std.algorithm;
import std.functional;
import std.array;
import std.container;
import std.conv;
import std.container;
import std.typecons;
import std.math;
import std.numeric;
pragma(inline,true);

void main(){
    int n=readln.chomp.to!int;
    auto s=readln.strip;
    char[] a;
    foreach(ref c;s){
        if(n&1) a~=c;
        else a=c~a;
        n--;
    }
    writeln(a);
}


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;
		auto k = RD;
		if (n < k^^2)
			ans[ti] = false;
		else
			ans[ti] = n % 2 == k % 2;
	}

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

import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); } 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){
        long n = scan!long, k = scan!long;
        if(n % 2 != k % 2) "NO".writeln;
        else if(n < k * k) "NO".writeln;
        else "YES".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; }
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 k = RD!int;
		if (n < k^^2)
			ans[ti] = false;
		else
			ans[ti] = n % 2 == k % 2;
	}

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

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

string FMT_F = "%.10f";

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

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

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

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

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

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

string FMT_F = "%.10f";

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

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

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

//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { 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 k = RD!int;
		if (k > n)
			ans[ti] = false;
		else
			ans[ti] = n % 2 == k % 2;
	}

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


import std.stdio: writeln, readln;
import std.conv: to;
import std.array: split, array;
import std.algorithm: map, filter, any;
import std.string: chomp;

void main() {
	int t = readln.chomp.to!int;
	foreach (int _; 0..t) {
		int k = readln.chomp.split(" ").map!(to!int).array[1];
		long[] a = readln.chomp.split(" ").map!(to!long).array;

		string ans = "YES";
		while (a.any!((long x) => x != 0)) {
			long[] b = a.map!((long x) => x%k).filter!((long x) => x != 0).array;
			if (b > [1L]) {
				ans = "NO";
				break;
			}
			a = a.map!((long x) => x/k).array;
		}
		ans.writeln;
	}
}

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

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

bool test (in ulong[] a, uint k) {
  debug stderr.writeln (a);
  bool[int] m;
  foreach (x; a) {
    ulong y = x;
    int o;
    debug stderr.writeln ("x = ", x);
    while (y > 0) {
      uint t = (y % k).to!uint;
      debug stderr.writeln (y, ' ', t);
      if (t > 1) return false;
      if (t == 1) {
        if (o in m) return false;
        m[o] = true;
      }
      y /= k;
      ++o;
    }
  }
  return true;
}

void main() {
  auto r = new InputReader ();
  immutable nt = r.next!uint ();
  foreach (tid; 0 .. nt) {
    const n = r.next!uint;
    const k = r.next!uint;
    auto a = r.nextA!ulong (n);
    writeln (test (a, k) ? "YES" : "NO");
  }
}

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

string FMT_F = "%.10f";

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

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

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

//long mod = 10^^9 + 7;
long mod = 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 k = RD;
		auto a = RDA;
		auto cnt = new int[](60);
		foreach (i; 0..n)
		{
			int j;
			while (a[i] != 0)
			{
				cnt[j] += a[i] % k;
				a[i] /= k;
				++j;
			}
		}
		bool ok = true;
		foreach (e; cnt)
		{
			if (e > 1)
				ok = false;
		}
		ans[ti] = ok;
	}

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


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

bool solve(int n, Tuple!(int, int, int)[] edges, int mask, ref int ans)
{
  auto uf = Dsu(n);
  foreach (e ; edges) {
    if (!uf.same(e[0], e[1]) && !(e[2] & mask)) {
      uf.merge(e[0], e[1]);
      ans |= e[2];
    }
  }
  return uf.size(0) == n;
}

void main()
{
  foreach (casenum ; 1 .. readln.strip.to!int + 1) {
    int n, m;
    readf!" %d %d "(n, m);
    Tuple!(int, int, int)[] edges;
    foreach (i ; 0 .. m) {
      int u, v, w;
      readf!" %d %d %d "(v, u, w);
      v--;
      u--;
      edges ~= tuple(v, u, w);
    }
    edges.sort!((x, y) => x[2] < y[2]);
    int mask = 0;
    int best_ans = int.max;
    foreach_reverse(maskbit ; 0 .. 31) {
      int ans = 0;
      if (solve(n, edges, mask | (1 << maskbit), ans)) {
        best_ans = min(best_ans, ans);
        mask |= (1 << maskbit);
      }
    }

    writeln(best_ans);
  }
}

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

struct Dsu
{
public:
    this(long n) @safe nothrow
    {
        _n = cast(int) n, parent_or_size = new int[](cast(int)n);
        parent_or_size[] = -1;
    }

    int merge(long a, long b) @safe nothrow @nogc
    {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        int x = leader(a), y = leader(b);
        if (x == y)
            return x;
        if (-parent_or_size[x] < -parent_or_size[y])
        {
            auto tmp = x;
            x = y;
            y = tmp;
        }
        parent_or_size[x] += parent_or_size[y];
        parent_or_size[y] = x;
        return x;
    }

    bool same(long a, long b) @safe nothrow @nogc
    {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        return leader(a) == leader(b);
    }

    int leader(long a) @safe nothrow @nogc
    {
        assert(0 <= a && a < _n);
        if (parent_or_size[cast(int)a] < 0)
            return cast(int) a;
        return parent_or_size[cast(int)a] = leader(parent_or_size[cast(int)a]);
    }

    int size(long a) @safe nothrow @nogc
    {
        assert(0 <= a && a < _n);
        return -parent_or_size[leader(a)];
    }

    int[][] groups() @safe nothrow
    {
        auto leader_buf = new int[](_n), group_size = new int[](_n);
        foreach (i; 0 .. _n)
        {
            leader_buf[i] = leader(i);
            group_size[leader_buf[i]]++;
        }
        auto result = new int[][](_n);
        foreach (i; 0 .. _n)
            result[i].reserve(group_size[i]);
        foreach (i; 0 .. _n)
            result[leader_buf[i]] ~= i;
        int[][] filtered;
        foreach (r; result)
            if (r.length != 0)
                filtered ~= r;
        return filtered;
    }

private:
    int _n;
    int[] parent_or_size;
}

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() {
    auto T = read!int;
    foreach (t; 0 .. T) solve();
}

alias Edge = Tuple!(int, "to", long, "cost");

void solve() {
    readln;
    int N, M; readf("%d %d\n", &N, &M);
    auto G = new Edge[][N];
    foreach (i; 0 .. M) {
        int u, v; long w; readf("%d %d %d\n", &u, &v, &w);
        u--; v--;
        G[u] ~= Edge(v, w);
        G[v] ~= Edge(u, w);
    }

    long ans = 0;
    for (int k = 31; k >= 0; k--) {
        long mask = ans | ((1L<<k) - 1);
        //writefln("k = %s, mask = %s", k, mask);
        bool C() {
            auto uf = UnionFind(N);
            foreach (u; 0 .. N) {
                foreach (e; G[u]) {
                    if ((e.cost & mask) == e.cost) {
                        uf.merge(u, e.to);
                    }
                }
            }
            return uf.ngroups == 1;
        }
        if (C()) {
            // k-th bit can be zero.
        } else {
            // k-th bit cannot be zero.
            ans |= (1L<<k);
        }
    }
    writeln(ans);
}

struct UnionFind {
    int N;
    int[] parent;
    int[] size;
    int ngroups;
    this(int N) {
        this.N = N;
        this.parent = new int[N];
        this.parent[] = -1;
        this.size = new int[N];
        this.size[] = 1;
        this.ngroups = N;
    }
    int root(int x) {
        if (parent[x] < 0) return x;
        return parent[x] = root(parent[x]);
    }
    void merge(int x, int y) {
        x = root(x);
        y = root(y);
        if (x == y) return;
        if (size[x] < size[y]) {
            parent[x] = y;
            size[x] += size[y];
        } else {
            parent[y] = x;
            size[y] += size[x];
        }
        ngroups--;
    }
}

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() {
    auto T = read!int;
    foreach (t; 0 .. T) solve();
}

alias Edge = Tuple!(int, "to", long, "cost");

void solve() {
    readln;
    int N, M; readf("%d %d\n", &N, &M);
    auto G = new Edge[][N];
    foreach (i; 0 .. M) {
        int u, v; long w; readf("%d %d %d\n", &u, &v, &w);
        u--; v--;
        G[u] ~= Edge(v, w);
        G[v] ~= Edge(u, w);
    }

    long ans = 0;
    for (int k = 31; k >= 0; k--) {
        auto sG = new Edge[][N];
        long mask = ans | ((1L<<k) - 1);
        //writefln("k = %s, mask = %s", k, mask);
        bool C() {
            auto uf = UnionFind(N);
            foreach (u; 0 .. N) {
                foreach (e; G[u]) {
                    if ((e.cost & mask) == e.cost) {
                        uf.merge(u, e.to);
                    }
                }
            }
            return uf.ngroups == 1;
        }
        if (C()) {
            // k-th bit can be zero.
        } else {
            // k-th bit cannot be zero.
            ans |= (1L<<k);
        }
    }
    writeln(ans);
}

struct UnionFind {
    int N;
    int[] parent;
    int[] size;
    int ngroups;
    this(int N) {
        this.N = N;
        this.parent = new int[N];
        this.parent[] = -1;
        this.size = new int[N];
        this.size[] = 1;
        this.ngroups = N;
    }
    int root(int x) {
        if (parent[x] < 0) return x;
        return parent[x] = root(parent[x]);
    }
    void merge(int x, int y) {
        x = root(x);
        y = root(y);
        if (x == y) return;
        if (size[x] < size[y]) {
            parent[x] = y;
            size[x] += size[y];
        } else {
            parent[y] = x;
            size[y] += size[x];
        }
        ngroups--;
    }
}


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

void main() {
    auto N = readln.chomp.to!int;
    auto A = readln.split.map!(to!long).array;
    A.sort();
    int ans = 0;

    for (int i = 1, p = 0; i <= N && p < N; ++i, ++p) {
        while (p < N && A[p] < i) ++p;
        if (p >= N) break;
        ans += 1;
    }

    ans.writeln;
}

import std.stdio, std.conv, std.string;
import std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }

int DEBUG_LEVEL = 0;
void print()(){ writeln(""); }
void print(T, A ...)(T t, lazy A a){ write(t), print(a); }
void print(int level, T, A ...)(T t, lazy A a){ if(level <= DEBUG_LEVEL) print(t, a); }

void main(string[] args){
	if(args.length > 1 && args[1] == "-debug"){
		if(args.length > 2 && args[2].isNumeric) DEBUG_LEVEL = args[2].to!int;
		else DEBUG_LEVEL = 1;
	}

	int n = read.to!int;

	int[] as;
	foreach(i; 0 .. n) as ~= read.to!int;

	as.sort!();

	int ans = 0;
	for(int i = 0, j = 0; i < n && j < n; ){
		if(as[j] < i + 1){
			j += 1;
		}
		else{
			ans += 1;
			i += 1, j += 1;
		}
	}
	ans.writeln;

}

import std.stdio : writef, readf, readln, writeln, writefln;
import std.array : array, split;
import std.algorithm;
import std.conv : to;
import std.range : iota, tee;
import std.algorithm.comparison;
import std.container.rbtree : RBT = RedBlackTree;
import std.string : strip;
import core.memory : GC;
import std.typecons : tuple;

alias Tree = RBT!int;

void main() {
    GC.disable();

    uint n;
    readf(" %s", n);
    auto a = new long[n];
    iota(0, n).each!(i => readf(" %s", a[i]));

    sort(a);
    uint s = 0;
    long k = 1;
    long d = 0;
    // writeln(a);
    while(s < n) {
      while(s < n && a[s] < k) s++;
      if (s >= n) break;
      // writeln(k, " " , s, " ", a[s]);
      if (k <= a[s]) { d++; }
      k += 1;
      s++;
    }

    writeln(d);
}

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

string FMT_F = "%.10f";

static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

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

long mod = 10^^9 + 7;
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;
	auto a = RDR.ARR;
	a.sort();

	long ans;
	foreach (i; 0..N)
	{
		if (a[i] > ans)
			++ans;
	}

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

import std.stdio : writef, readf, readln, writeln, writefln;
import std.array : array, split;
import std.algorithm;
import std.conv : to;
import std.range : iota, tee;
import std.algorithm.comparison;
import std.container.rbtree : RBT = RedBlackTree;
import std.string : strip;
import core.memory : GC;
import std.typecons : tuple;

alias Tree = RBT!int;

void main() {
    GC.disable();

    uint n;
    readf(" %s", n);
    auto a = new long[n];
    iota(0, n).each!(i => readf(" %s", a[i]));

    sort(a);
    uint s = 0;
    long k = 1;
    long d = 0;
    // writeln(a);
    while(s < n) {
      while(s < n && a[s] < k) s++;
      if (s >= n) break;
      // writeln(k, " " , s, " ", a[s]);
      if (k <= a[s]) { d++; }
      k *= 2;
      s++;
    }

    writeln(d);
}

import std.stdio : writef, readf, readln, writeln, writefln;
import std.array : array, split;
import std.algorithm;
import std.conv : to;
import std.range : iota, tee;
import std.algorithm.comparison;
import std.container.rbtree : RBT = RedBlackTree;
import std.string : strip;
import core.memory : GC;
import std.typecons : tuple;

alias Tree = RBT!int;

void main() {
    GC.disable();

    uint n;
    readf(" %s", n);
    auto a = new int[n];
    iota(0, n).each!(i => readf(" %s", a[i]));

    sort(a);
    uint s = 0;
    uint k = 1;
    uint d = 1;
    while(true) {
      while(s < n && a[s] < k) s++;
      if (s >= n) {writeln(d); return;}
      // writeln(s, " " , a[s], " ",  k);
      d ++;
      k *= 2;
      s++;
      if (s >= n) {writeln(d-1); return ; }
    }

    assert(false);
}

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

string FMT_F = "%.10f";

static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

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

long mod = 10^^9 + 7;
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;
	auto a = RDR.ARR;
	a.sort();

	debug writeln(a.uniq);
	writeln(a.uniq.array.length);
	stdout.flush();
	debug readln();
}


import std.algorithm, std.conv, std.range, std.stdio;
int g (int x) {return (x < 2) ? x : (x < 5) ? (x - 2) : (x & 1) ? 0 : (1 + (g (x / 2) == 1));}
int h (int x) {return (x < 3) ? x : !(x & 1);}
void main () {
	int n, k;
	readf (" %s %s", &n, &k);
	k %= 2;
	readln;
	writeln (readln.split.map !(to !(int)).map !(x => k ? g (x) : h (x)).reduce !(q{a ^ b}) ? "Kevin" : "Nicky");
}

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

pure int g (int x)
{
	if (x < 5)
	{
		return [0, 1, 0, 1, 2][x];
	}
	if (x % 2 != 0)
	{
		return 0;
	}
	int v = g (x / 2);
	return 1 + (v == 1);
}

pure int h (int x)
{
	if (x < 3)
	{
		return [0, 1, 2][x];
	}
	if (x % 2 != 0)
	{
		return 0;
	}
	return 1;
}

void main ()
{
	int n, k;
	while (readf (" %s %s", &n, &k) > 0)
	{
		readln;
		auto a = readln.split.map !(to !(int)).array;
		k %= 2;
		int res;
		if (k)
		{
			res = a.map !(x => g (x)).reduce !(q{a ^ b});
		}
		else
		{
			res = a.map !(x => h (x)).reduce !(q{a ^ b});
		}
		debug {writeln (n, ' ', k, ' ', a[0], ' ', res);}
		writeln (res ? "Kevin" : "Nicky");
	}
}

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

pure int g (int x)
{
	if (x < 5)
	{
		return [0, 1, 0, 1, 2][x];
	}
	if (x % 2 != 0)
	{
		return 0;
	}
	return 3 ^ g (x / 2);
}

pure int h (int x)
{
	if (x < 3)
	{
		return [0, 1, 2][x];
	}
	if (x % 2 != 0)
	{
		return 0;
	}
	return 1;
}

void main ()
{
	int n, k;
	while (readf (" %s %s", &n, &k) > 0)
	{
		readln;
		auto a = readln.split.map !(to !(int)).array;
		k %= 2;
		int res;
		if (k)
		{
			res = a.map !(x => g (x)).reduce !(q{a ^ b});
		}
		else
		{
			res = a.map !(x => h (x)).reduce !(q{a ^ b});
		}
		writeln (res ? "Kevin" : "Nicky");
	}
}


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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		int n, m;
		readf !(" %s %s") (n, m);
		readln;
		auto c = new long [] [n];
		auto w = new long [n];
		foreach (i; 0..n)
		{
			c[i] = readln.splitter.map !(to !(long)).array;
			foreach (j; 0..m)
			{
				w[i] += c[i][j] * j;
			}
		}
		auto p = w.length - w.maxPos.length;
		writeln (p + 1, " ", w[p] - w[!p]);
	}
}

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

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

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

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

void main() {
  try {
    for (; ; ) {
      const numCases = readInt;
      foreach (caseId; 0 .. numCases) {
        const N = readInt;
        const M = readInt;
        auto C = new long[][](N, M);
        foreach (i; 0 .. N) {
          foreach (j; 0 .. M) {
            C[i][j] = readLong;
          }
        }

        auto fs = new long[N];
        foreach (i; 0 .. N) {
          foreach (j; 0 .. M) {
            fs[i] += j * C[i][j];
          }
        }
        debug {
          writeln("fs = ", fs);
        }
        const minF = fs.minElement;
        const maxF = fs.maxElement;

        int im = -1;
        foreach (i; 0 .. N) {
          if (maxF == fs[i]) {
            im = i;
            break;
          }
        }
        writeln(im + 1, " ", maxF - minF);
      }
    }
  } catch (EOFException e) {
  }
}

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		int n, m;
		readf !(" %s %s") (n, m);
		readln;
		auto c = new int [] [n];
		auto w = new long [n];
		foreach (i; 0..n)
		{
			c[i] = readln.splitter.map !(to !(int)).array;
			foreach (j; 0..m)
			{
				w[i] += c[i][j] * j;
			}
		}
		auto p = w.length - w.maxPos.length;
		writeln (p + 1, " ", w[p] - w[!p]);
	}
}


import std.stdio;
import std.stdio;
import std.algorithm;
import std.range;
import std.functional;
import std.conv;
import std.string;
import std.math;
import core.bitop;

ulong[] arr;
ulong[] clone;

bool check(ulong k) { // O(3n + n log n)
	foreach (i, v; arr) { // O(n)
		clone[i] = v;
	}
	foreach (ref v; clone) { // O(n)
		v ^= k;
	}
	clone.sort(); // O(n log n)
	return clone == arr; // O(n)
}

void main() {
	int tt = readln().chomp.to!int;
	outer: foreach (t; 0 .. tt) {
		size_t n = readln().chomp.to!size_t;
		arr = readln().chomp.split(" ").map!(to!ulong).array;
		arr.sort();
		clone.length = arr.length;
		// 1026 * (3 * 1024 + 1024 log 1024)
		foreach (k; 1 .. 1026) {
			if (check(k)) {
				writeln(k);
				continue outer;
			}
		}
		writeln(-1);
	}
}

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

void main () {
	foreach (t; 0..readln.strip.to!int) {
		int n = readln.strip.to!int, v;
		auto a = readln.splitter.map !(to!int).array;
		a.sort;
		for (v = 1; v < 1024; v++) {
			auto b = a.dup;
			b[] ^= v;
			b.sort;
			if (a == b) break;
		}
		if (v == 1024) v = -1;
		v.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; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }

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

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

		ans[ti] = -1;
		foreach (i; 1..2^^10)
		{
			auto cnt = new int[](2^^10);
			foreach (j; 0..n)
			{
				++cnt[s[j]];
			}
			foreach (j; 0..n)
			{
				auto num = s[j] ^ i;
				--cnt[num];
			}
			bool ok = true;
			foreach (j; 0..2^^10)
			{
				if (cnt[j] != 0)
				{
					ok = false;
					break;
				}
			}
			if (ok)
			{
				ans[ti] = i;
				break;
			}
		}
	}

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


module main;

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

class XD
{
    public:
        this(int n)
        {
            len = n << 3;
            h = new int[][len];
            f = new bool[len];
            v = new long[len];
        }
        int getPosition(long n)
        {
            auto s = to!string(n);
            int pos = calc(s);
            while (f[pos] == true && v[pos] != n)
            {
                ++ pos;
                if (pos == len)
                {
                    pos = 0;
                }
            }
            return pos;
        }
        void insert(long n, int idx)
        {
            auto pos = getPosition(n);
            //writefln("%d %d", pos, n);
            v[pos] = n;
            f[pos] = true;
            h[pos] ~= idx;
        }
        int[] find(long n)
        {
            auto pos = getPosition(n);
            return h[pos];
        }
        int calc(string s)
        {
            long hash = 0;
            foreach (i, val; s)
            {
                hash = (hash << 4) ^ (hash >> 28) ^ val;
            }
            return (hash % len);
        }
    protected:
        int len;
        long[] v;
        bool[] f;
        int[][] h;
};

int search(int[] a, int bound)
{
    int left = 0, right = a.length - 1, mid, res = 0;
    while (left <= right)
    {
        mid = (left + right) >> 1;
        if (a[mid] >= bound)
        {
            right = mid - 1;
        }
        else
        {
            res = mid + 1;
            left = mid + 1;
        }
    }
    //writefln("%d %d", bound, res);
    return res;
}

void solve(int[] a)
{
    XD xd = new XD(a.length);
    auto s = new long[a.length];
    s[0] = a[0];
    xd.insert(s[0], 0);
    foreach (i; 1 .. a.length)
    {
        s[i] = a[i] + s[i - 1];
        xd.insert(s[i], i);
    }
    long ans = 0;
    foreach (i; 2 .. a.length)
    {
        long sum = s[a.length - 1] - s[i - 1];
        if ((s[i - 1] & 1) == 0 && s[i - 1] / 2 == sum)
        {
            auto arr = xd.find(sum);
            //foreach (j, val; arr) writef("%d ", val);
            //writeln();
            ans += search(arr, i - 1);
        }
    }
    writefln("%d", ans);
}

int main(string[] args)
{
    int n;
    while (scanf("%d", &n) == 1)
    {
        auto a = new int[n];
        foreach (i, ref v; a)
        {
            scanf("%d", &v);
        }
        solve(a);
    }
    return 0;
}

module main;

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

class XD
{
    public:
        this(int n)
        {
            len = n;
            h = new int[][len];
            f = new bool[len];
            v = new long[len];
        }
        int getPosition(long n)
        {
            auto s = to!string(n);
            int pos = calc(s);
            while (f[pos] == true && v[pos] != n)
            {
                ++ pos;
                if (pos == len)
                {
                    pos = 0;
                }
            }
            return pos;
        }
        void insert(long n, int idx)
        {
            auto pos = getPosition(n);
            //writefln("%d %d", pos, n);
            v[pos] = n;
            f[pos] = true;
            h[pos] ~= idx;
        }
        int[] find(long n)
        {
            auto pos = getPosition(n);
            return h[pos];
        }
        int calc(string s)
        {
            long hash = 0;
            foreach (i, val; s)
            {
                hash = (hash << 4) ^ (hash >> 28) ^ val;
            }
            return (hash % len);
        }
    protected:
        int len;
        long[] v;
        bool[] f;
        int[][] h;
};

int search(int[] a, int bound)
{
    int left = 0, right = a.length - 1, mid, res = 0;
    while (left <= right)
    {
        mid = (left + right) >> 1;
        if (a[mid] >= bound)
        {
            right = mid - 1;
        }
        else
        {
            res = mid + 1;
            left = mid + 1;
        }
    }
    //writefln("%d %d", bound, res);
    return res;
}

void solve(int[] a)
{
    XD xd = new XD(a.length);
    auto s = new long[a.length];
    s[0] = a[0];
    xd.insert(s[0], 0);
    foreach (i; 1 .. a.length)
    {
        s[i] = a[i] + s[i - 1];
        xd.insert(s[i], i);
    }
    long ans = 0;
    foreach (i; 2 .. a.length)
    {
        long sum = s[a.length - 1] - s[i - 1];
        if ((s[i - 1] & 1) == 0 && s[i - 1] / 2 == sum)
        {
            auto arr = xd.find(sum);
            //foreach (j, val; arr) writef("%d ", val);
            //writeln();
            ans += search(arr, i - 1);
        }
    }
    writefln("%d", ans);
}

int main(string[] args)
{
    int n;
    while (scanf("%d", &n) == 1)
    {
        auto a = new int[n];
        foreach (i, ref v; a)
        {
            scanf("%d", &v);
        }
        solve(a);
    }
    return 0;
}

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

void main() {
    auto N = readln.chomp.to!int;
    auto A = readln.split.map!(to!long).array;

    if (A.sum % 3 != 0) {
        writeln(0);
        return;
    }

    auto ave = A.sum / 3;

    auto acm_f = new long[](N);
    acm_f[0] = A[0];
    auto acm_b = new long[](N);
    acm_b[N-1] = A[N-1];

    foreach (i; 0..N-1) {
        acm_f[i+1] = acm_f[i] + A[i+1];
        acm_b[N-i-2] = acm_b[N-i-1] + A[N-i-2];
    }

    int[] f;
    int[] b;
    foreach (i; 0..N) {
        if (acm_f[i] == ave) f ~= i;
        if (acm_b[i] == ave) b ~= i;
    }

    auto B = b.assumeSorted;
    long ans = 0;

    foreach (i; f) {
        ans += B.upperBound(i+1).length;
    }

    ans.writeln;
}

module main;

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

class XD
{
    public:
        this(int n)
        {
            len = n << 4;
            h = new int[][len];
            f = new bool[len];
            v = new long[len];
        }
        int getPosition(long n)
        {
            auto s = to!string(n);
            int pos = calc(s);
            while (f[pos] == true && v[pos] != n)
            {
                ++ pos;
                if (pos == len)
                {
                    pos = 0;
                }
            }
            return pos;
        }
        void insert(long n, int idx)
        {
            auto pos = getPosition(n);
            //writefln("%d %d", pos, n);
            v[pos] = n;
            f[pos] = true;
            h[pos] ~= idx;
        }
        int[] find(long n)
        {
            auto pos = getPosition(n);
            return h[pos];
        }
        int calc(string s)
        {
            long hash = 0;
            foreach (i, val; s)
            {
                hash = (hash << 4) ^ (hash >> 28) ^ val;
            }
            return (hash % len);
        }
    protected:
        int len;
        long[] v;
        bool[] f;
        int[][] h;
};

int search(int[] a, int bound)
{
    int left = 0, right = a.length - 1, mid, res = 0;
    while (left <= right)
    {
        mid = (left + right) >> 1;
        if (a[mid] >= bound)
        {
            right = mid - 1;
        }
        else
        {
            res = mid + 1;
            left = mid + 1;
        }
    }
    //writefln("%d %d", bound, res);
    return res;
}

void solve(int[] a)
{
    XD xd = new XD(a.length);
    auto s = new long[a.length];
    s[0] = a[0];
    xd.insert(s[0], 0);
    foreach (i; 1 .. a.length)
    {
        s[i] = a[i] + s[i - 1];
        xd.insert(s[i], i);
    }
    long ans = 0;
    foreach (i; 2 .. a.length)
    {
        long sum = s[a.length - 1] - s[i - 1];
        if ((s[i - 1] & 1) == 0 && s[i - 1] / 2 == sum)
        {
            auto arr = xd.find(sum);
            //foreach (j, val; arr) writef("%d ", val);
            //writeln();
            ans += search(arr, i - 1);
        }
    }
    writefln("%d", ans);
}

int main(string[] args)
{
    int n;
    while (scanf("%d", &n) == 1)
    {
        auto a = new int[n];
        foreach (i, ref v; a)
        {
            scanf("%d", &v);
        }
        solve(a);
    }
    return 0;
}

module main;

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

class XD
{
    public:
        this(int n)
        {
            len = n << 2;
            h = new int[][len];
            f = new bool[len];
            v = new long[len];
        }
        int getPosition(long n)
        {
            auto s = to!string(n);
            int pos = calc(s);
            while (f[pos] == true && v[pos] != n)
            {
                ++ pos;
                if (pos == len)
                {
                    pos = 0;
                }
            }
            return pos;
        }
        void insert(long n, int idx)
        {
            auto pos = getPosition(n);
            //writefln("%d %d", pos, n);
            v[pos] = n;
            f[pos] = true;
            h[pos] ~= idx;
        }
        int[] find(long n)
        {
            auto pos = getPosition(n);
            return h[pos];
        }
        int calc(string s)
        {
            long hash = 0;
            foreach (i, val; s)
            {
                hash = (hash << 4) ^ (hash >> 28) ^ val;
            }
            return (hash % len);
        }
    protected:
        int len;
        long[] v;
        bool[] f;
        int[][] h;
};

int search(int[] a, int bound)
{
    int left = 0, right = a.length - 1, mid, res = 0;
    while (left <= right)
    {
        mid = (left + right) >> 1;
        if (a[mid] >= bound)
        {
            right = mid - 1;
        }
        else
        {
            res = mid + 1;
            left = mid + 1;
        }
    }
    //writefln("%d %d", bound, res);
    return res;
}

void solve(int[] a)
{
    XD xd = new XD(a.length);
    auto s = new long[a.length];
    s[0] = a[0];
    xd.insert(s[0], 0);
    foreach (i; 1 .. a.length)
    {
        s[i] = a[i] + s[i - 1];
        xd.insert(s[i], i);
    }
    long ans = 0;
    foreach (i; 2 .. a.length)
    {
        long sum = s[a.length - 1] - s[i - 1];
        if ((s[i - 1] & 1) == 0 && s[i - 1] / 2 == sum)
        {
            auto arr = xd.find(sum);
            //foreach (j, val; arr) writef("%d ", val);
            //writeln();
            ans += search(arr, i - 1);
        }
    }
    writefln("%d", ans);
}

int main(string[] args)
{
    int n;
    while (scanf("%d", &n) == 1)
    {
        auto a = new int[n];
        foreach (i, ref v; a)
        {
            scanf("%d", &v);
        }
        solve(a);
    }
    return 0;
}

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

void main() {
    auto N = readln.chomp.to!int;
    auto A = readln.split.map!(to!int).array;

    if (A.sum % 3 != 0) {
        writeln(0);
        return;
    }

    auto ave = A.sum / 3;

    auto acm_f = new int[](N);
    acm_f[0] = A[0];
    auto acm_b = new int[](N);
    acm_b[N-1] = A[N-1];

    foreach (i; 0..N-1) {
        acm_f[i+1] = acm_f[i] + A[i+1];
        acm_b[N-i-2] = acm_b[N-i-1] + A[N-i-2];
    }

    int[] f;
    int[] b;
    foreach (i; 0..N) {
        if (acm_f[i] == ave) f ~= i;
        if (acm_b[i] == ave) b ~= i;
    }

    auto B = b.assumeSorted;
    long ans = 0;

    foreach (i; f) {
        ans += B.upperBound(i+1).length;
    }

    ans.writeln;
}

module main;

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

class XD
{
    public:
        this(int n)
        {
            len = n << 3;
            h = new int[][n << 3];
            f = new bool[n << 3];
            v = new long[n << 3];
        }
        int getPosition(long n)
        {
            auto s = to!string(n);
            int pos = calc(s);
            while (f[pos] == true && v[pos] != n)
            {
                ++ pos;
                if (pos == len)
                {
                    pos = 0;
                }
            }
            return pos;
        }
        void insert(long n, int idx)
        {
            auto pos = getPosition(n);
            v[pos] = n;
            f[pos] = true;
            h[pos] ~= idx;
        }
        int[] find(long n)
        {
            auto pos = getPosition(n);
            return h[pos];
        }
        int calc(string s)
        {
            long hash = 0;
            foreach (i, val; s)
            {
                hash = (hash << 4) ^ (hash >> 28) ^ val;
            }
            return (hash % len);
        }
    protected:
        int len;
        long[] v;
        bool[] f;
        int[][] h;
};

int search(int[] a, int bound)
{
    int left = 0, right = a.length - 1, mid, res = 0;
    while (left <= right)
    {
        mid = (left + right) >> 1;
        if (a[mid] >= bound)
        {
            right = mid - 1;
        }
        else
        {
            res = mid - left + 1;
            left = mid + 1;
        }
    }
    return res;
}

void solve(int[] a)
{
    XD xd = new XD(a.length);
    auto s = new long[a.length];
    s[0] = a[0];
    xd.insert(s[0], 0);
    foreach (i; 1 .. a.length)
    {
        s[i] = a[i] + s[i - 1];
        xd.insert(s[i], i);
    }
    long ans = 0;
    foreach (i; 2 .. a.length)
    {
        long sum = s[a.length - 1] - s[i - 1];
        if ((s[i - 1] & 1) == 0 && s[i - 1] / 2 == sum)
        {
            auto arr = xd.find(sum);
            ans += search(arr, i - 1);
        }
    }
    writefln("%d", ans);
}

int main(string[] args)
{
    int n;
    while (scanf("%d", &n) == 1)
    {
        auto a = new int[n];
        foreach (i, ref v; a)
        {
            scanf("%d", &v);
        }
        solve(a);
    }
    return 0;
}

module main;

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

class XD
{
    public:
        this(int n)
        {
            len = n;
            h = new int[][n << 3];
            f = new bool[n << 3];
            v = new long[n << 3];
        }
        int getPosition(long n)
        {
            auto s = to!string(n);
            int pos = calc(s);
            while (f[pos] == true && v[pos] != n)
            {
                ++ pos;
                if (pos == len)
                {
                    pos = 0;
                }
            }
            return pos;
        }
        void insert(long n, int idx)
        {
            auto pos = getPosition(n);
            v[pos] = n;
            f[pos] = true;
            h[pos] ~= idx;
        }
        int[] find(long n)
        {
            auto pos = getPosition(n);
            return h[pos];
        }
        int calc(string s)
        {
            long hash = 0;
            foreach (i, val; s)
            {
                hash = (hash << 4) ^ (hash >> 28) ^ val;
            }
            return (hash % len);
        }
    protected:
        int len;
        long[] v;
        bool[] f;
        int[][] h;
};

int search(int[] a, int bound)
{
    int left = 0, right = a.length - 1, mid, res = 0;
    while (left <= right)
    {
        mid = (left + right) >> 1;
        if (a[mid] >= bound)
        {
            right = mid - 1;
        }
        else
        {
            res = mid - left + 1;
            left = mid + 1;
        }
    }
    return res;
}

void solve(int[] a)
{
    XD xd = new XD(a.length);
    auto s = new long[a.length];
    s[0] = a[0];
    xd.insert(s[0], 0);
    foreach (i; 1 .. a.length)
    {
        s[i] = a[i] + s[i - 1];
        xd.insert(s[i], i);
    }
    long ans = 0;
    foreach (i; 2 .. a.length)
    {
        long sum = s[a.length - 1] - s[i - 1];
        auto arr = xd.find(sum);
        ans += search(arr, i - 1);
    }
    writefln("%d", ans);
}

int main(string[] args)
{
    int n;
    while (scanf("%d", &n) == 1)
    {
        auto a = new int[n];
        foreach (i, ref v; a)
        {
            scanf("%d", &v);
        }
        solve(a);
    }
    return 0;
}

module main;

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

class XD
{
    public:
        this(int n)
        {
            len = n;
            h = new int[][n << 3];
            f = new bool[n << 3];
            v = new long[n << 3];
        }
        int getPosition(long n)
        {
            auto s = to!string(n);
            int pos = calc(s);
            while (f[pos] == true && v[pos] != n)
            {
                ++ pos;
                if (pos == len)
                {
                    pos = 0;
                }
            }
            return pos;
        }
        void insert(long n, int idx)
        {
            auto pos = getPosition(n);
            v[pos] = n;
            f[pos] = true;
            h[pos] ~= idx;
        }
        int[] find(long n)
        {
            auto pos = getPosition(n);
            return h[pos];
        }
        int calc(string s)
        {
            long hash = 0;
            foreach (i, val; s)
            {
                hash = (hash << 4) ^ (hash >> 28) ^ val;
            }
            return (hash % len);
        }
    protected:
        int len;
        long[] v;
        bool[] f;
        int[][] h;
};

int search(int[] a, int bound)
{
    int left = 0, right = a.length - 1, mid, res = 0;
    while (left <= right)
    {
        mid = (left + right) >> 1;
        if (a[mid] >= bound)
        {
            right = mid - 1;
        }
        else
        {
            res = mid - left + 1;
            left = mid + 1;
        }
    }
    return res;
}

void solve(int[] a)
{
    XD xd = new XD(a.length);
    auto s = new long[a.length];
    s[0] = a[0];
    xd.insert(s[0], 0);
    foreach (i; 1 .. a.length)
    {
        s[i] = a[i] + s[i - 1];
        xd.insert(s[i], i);
    }
    long ans = 0;
    foreach (i; 2 .. a.length)
    {
        long sum = s[a.length - 1] - s[i - 1];
        if (s[i - 1] / 2 == sum)
        {
            auto arr = xd.find(sum);
            ans += search(arr, i - 1);
        }
    }
    writefln("%d", ans);
}

int main(string[] args)
{
    int n;
    while (scanf("%d", &n) == 1)
    {
        auto a = new int[n];
        foreach (i, ref v; a)
        {
            scanf("%d", &v);
        }
        solve(a);
    }
    return 0;
}


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

// M: prime, G: primitive root
class Fft(int M_, int G, int K) {
  import std.algorithm : reverse;
  import std.traits : isIntegral;
  alias M = M_;
  // 1, 1/4, 1/8, 3/8, 1/16, 5/16, 3/16, 7/16, ...
  int[] gs;
  this() {
    static assert(2 <= K && K <= 30, "Fft: 2 <= K <= 30 must hold");
    static assert(!((M - 1) & ((1 << K) - 1)), "Fft: 2^K | M - 1 must hold");
    gs = new int[1 << (K - 1)];
    gs[0] = 1;
    long g2 = G, gg = 1;
    for (int e = (M - 1) >> K; e; e >>= 1) {
      if (e & 1) gg = (gg * g2) % M;
      g2 = (g2 * g2) % M;
    }
    gs[1 << (K - 2)] = cast(int)(gg);
    for (int l = 1 << (K - 2); l >= 2; l >>= 1) {
      gs[l >> 1] = cast(int)((cast(long)(gs[l]) * gs[l]) % M);
    }
    assert((cast(long)(gs[1]) * gs[1]) % M == M - 1,
           "Fft: g^(2^(K-1)) == -1 (mod M) must hold");
    for (int l = 2; l <= 1 << (K - 2); l <<= 1) {
      foreach (i; 1 .. l) {
        gs[l + i] = cast(int)((cast(long)(gs[l]) * gs[i]) % M);
      }
    }
  }
  void fft(int[] xs) const {
    const n = cast(int)(xs.length);
    assert(!(n & (n - 1)), "Fft.fft: |xs| must be a power of two");
    assert(n <= 1 << K, "Fft.fft: |xs| <= 2^K must hold");
    for (int l = n; l >>= 1; ) {
      foreach (i; 0 .. (n >> 1) / l) {
        const(long) g = gs[i];
        foreach (j; (i << 1) * l .. (i << 1 | 1) * l) {
          const t = cast(int)((g * xs[j + l]) % M);
          if ((xs[j + l] = xs[j] - t) < 0) xs[j + l] += M;
          if ((xs[j] += t) >= M) xs[j] -= M;
        }
      }
    }
  }
  void invFft(int[] xs) const {
    const n = cast(int)(xs.length);
    assert(!(n & (n - 1)), "Fft.invFft: |xs| must be a power of two");
    assert(n <= 1 << K, "Fft.invFft: |xs| <= 2^K must hold");
    for (int l = 1; l < n; l <<= 1) reverse(xs[l .. l << 1]);
    for (int l = 1; l < n; l <<= 1) {
      foreach (i; 0 .. (n >> 1) / l) {
        const(long) g = gs[i];
        foreach (j; (i << 1) * l .. (i << 1 | 1) * l) {
          int t = cast(int)((g * (xs[j] - xs[j + l])) % M);
          if (t < 0) t += M;
          if ((xs[j] += xs[j + l]) >= M) xs[j] -= M;
          xs[j + l] = t;
        }
      }
    }
  }
  T[] convolute(T)(inout(T)[] as, inout(T)[] bs) const if (isIntegral!T) {
    const na = cast(int)(as.length), nb = cast(int)(bs.length);
    int n, invN = 1;
    for (n = 1; n < na + nb - 1; n <<= 1) {
      invN = ((invN & 1) ? (invN + M) : invN) >> 1;
    }
    auto xs = new int[n], ys = new int[n];
    foreach (i; 0 .. na) if ((xs[i] = cast(int)(as[i] % M)) < 0) xs[i] += M;
    foreach (i; 0 .. nb) if ((ys[i] = cast(int)(bs[i] % M)) < 0) ys[i] += M;
    fft(xs);
    fft(ys);
    foreach (i; 0 .. n) {
      xs[i] = cast(int)((((cast(long)(xs[i]) * ys[i]) % M) * invN) % M);
    }
    invFft(xs);
    auto cs = new T[na + nb - 1];
    foreach (i; 0 .. na + nb - 1) cs[i] = cast(T)(xs[i]);
    return cs;
  }
}

alias Fft0 = Fft!(998244353, 3, 20);

void main() {
  const FFT = new Fft0;

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

        auto fs = new int[N + 1];
        auto gs = new int[N + 1];
        foreach (i; 0 .. N) {
          switch (S[i]) {
            case 'V': ++fs[i]; break;
            case 'K': ++gs[N - i]; break;
            default: {}
          }
        }
        const hs = FFT.convolute(fs, gs);
        debug {
          writeln("hs = ", hs);
        }

        auto has = new bool[N + 1];
        foreach (x; -N .. +N + 1) {
          if (hs[N + x]) {
            has[abs(x)] = true;
          }
        }

        int[] ans;
        foreach (k; 1 .. N + 1) {
          bool ok = true;
          for (int l = k; l <= N; l += k) {
            ok = ok && !has[l];
          }
          if (ok) {
            ans ~= k;
          }
        }
        writeln(ans.length);
        foreach (index, k; ans) {
          if (index > 0) write(" ");
          write(k);
        }
        writeln;
      }
    }
  } catch (EOFException e) {
  }
}

import core.bitop, std.algorithm, std.bigint, std.compiler, std.container,
	std.complex, std.bitmanip, std.conv, std.math;
import std.numeric : Fft, fft, gcd, inverseFft;
import std.random, std.range, std.regex, std.stdio, std.string, std.traits,
	std.typecons, std.uni;

alias pair(X, Y) = Tuple!(X, "fi", Y, "se");
alias mp = make_pair;
alias pii = pair!(int, int);
alias pll = pair!(long, long);
// alias gcd = std.numeric.gcd;
alias lint = long;
alias rbt = RedBlackTree;
///modulo
const int mod = 10 ^^ 9 + 7, mod2 = mod + 2;
public
{
	pure nothrow
	{
		///make_pair
		pair!(X, Y) make_pair(X, Y)(in X x_, in Y y_)
		{
			return tuple!(X, "fi", Y, "se")(x_, y_);
		}
		///square
		@property X sqr(X)(in X a_)
		{
			return a_ * a_;
		}
		///cube
		@property X cub(X)(in X a_)
		{
			return a_ * a_ * a_;
		}
		///posicion lower bound
		size_t lowb(T, X)(T a, auto ref X g)
				if (isRandomAccessRange!T && hasLength!T && is(typeof(g < a[0]) == bool))
		{
			return assumeSorted(a).lowerBound(g).length;
		}
		///posicion upper bound
		size_t upb(T, X)(T a, auto ref X g)
				if (isRandomAccessRange!T && hasLength!T && is(typeof(g < a[0]) == bool))
		{
			return a.length - assumeSorted(a).upperBound(g).length;
		}
		///posicion binary search
		size_t binf(T, X)(T a, auto ref X g)
				if (isRandomAccessRange!T && hasLength!T && is(typeof(g < a[0]) == bool))
		{
			auto pos = lowb(a, g);
			return (g == a[pos]) ? pos : a.length;
		}
		///binary search
		bool binary_search(T, X)(T a, auto ref X g)
				if (isRandomAccessRange!T && hasLength!T && is(typeof(g < a[0]) == bool))
		{
			return assumeSorted(a).contains(g);
		}
	}
	static if (version_minor >= 74)
	{
		///read without format
		bool input(T...)(auto ref T ptrs) if (ptrs.length > 0)
		{
			return readf!(replicate(" %s", ptrs.length))(ptrs) == ptrs.length;
		}
		///readln without format
		bool read(T...)(auto ref T ptrs) if (ptrs.length > 0)
		{
			const bool fl = readf!(replicate(" %s", ptrs.length))(ptrs) == ptrs.length;
			readln;
			return fl;
		}
	}
	else
	{
		///read without format
		bool input(T...)(ref T ptrs) if (ptrs.length > 0)
		{
			size_t s;
			foreach (ref e; ptrs)
			{
				if (isSomeChar!(Unqual!(typeof(e))))
					s += readf(" %c", &e);
				else
					s += readf(" %s", &e);
			}
			return s == ptrs.length;
		}
		///readln without format
		bool read(T...)(ref T ptrs) if (ptrs.length > 0)
		{
			size_t s;
			foreach (ref e; ptrs)
			{
				if (isSomeChar!(Unqual!(typeof(e))))
					s += readf(" %c", &e);
				else
					s += readf(" %s", &e);
			}
			readln;
			return s == ptrs.length;
		}
	}
	///opening file
	nothrow auto inf(in char* name)
	{
		return freopen(name, "r", core.stdc.stdio.stdin);
	}
	///closing file
	nothrow auto ouf(in char* name)
	{
		return freopen(name, "w", core.stdc.stdio.stdout);
	}
	///parsing array from line
	@property auto arread(T)()
	{
		return readln.splitter.map!(to!(T)).array;
	}
	///outbuffer
	void enter(T...)(T args)
	{
		writeln(args);
		stdout.flush;
	}
}
//foreach_reverse isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin
//cumulativeFold return static heapify join filter foreach
//------------------------------------------program beginning--------------------------
void main()
{
	version (home)
	{
		assert(freopen("input.txt", "r", core.stdc.stdio.stdin));
	}
	int tt;
	read(tt);
	auto a = new Complex!double[1 << 20], b = new Complex!double[1 << 20];
	auto ca = a.dup, cb = b.dup;
	auto d = new int[1 << 20];
	auto r = new int[500_000];
	Fft f = new Fft(1 << 20);
	string s;
	foreach (ii; 0 .. tt)
	{
		s = readln;
		int n;
		read(n);
		s = readln.strip;
		int st = 1;
		while (st <= n)
			st *= 2;
		st *= 2;
		auto z = Complex!double(0, 0);
		a[0 .. st] = z;
		b[0 .. st] = z;
		ca[0 .. st] = z;
		cb[0 .. st] = z;
		foreach (i; 0 .. n)
		{
			if (s[i] == 'V')
				a[i] = 1;
			else if (s[i] == 'K')
				b[n - 1 - i] = 1;
		}
		f.fft(a[0 .. st], ca[0 .. st]);
		f.fft(b[0 .. st], cb[0 .. st]);
		ca[0 .. st] *= cb[0 .. st];
		cb[0 .. st] = z;
		debug writeln(ca[0 .. st]);
		f.inverseFft(ca[0 .. st], cb[0 .. st]);
		foreach (i; 0 .. st)
		{
			d[i] = roundTo!int(cb[i].re);
		}
		r[n - 1] = d[n - 1];
		foreach (i; 1 .. n)
		{
			r[i] = d[n - 1 - i] + d[n - 1 + i];
		}
		int[] ans;
		foreach (i; 1 .. n)
		{
			bool flag = true;
			foreach (j; 1 .. (n - 1) / i + 1)
			{
				if (r[i * j])
				{
					flag = false;
					break;
				}
			}
			if (flag)
			{
				ans ~= i;
			}
		}
		ans ~= n;
		writeln(ans.length);
		foreach (x; ans)
		{
			write(x, ' ');
		}
		writeln;
	}
}

import core.bitop, std.algorithm, std.bigint, std.compiler, std.container,
	std.complex, std.bitmanip, std.conv, std.math;
import std.numeric : Fft, fft, gcd, inverseFft;
import std.random, std.range, std.regex, std.stdio, std.string, std.traits,
	std.typecons, std.uni;

alias pair(X, Y) = Tuple!(X, "fi", Y, "se");
alias mp = make_pair;
alias pii = pair!(int, int);
alias pll = pair!(long, long);
// alias gcd = std.numeric.gcd;
alias lint = long;
alias rbt = RedBlackTree;
///modulo
const int mod = 10 ^^ 9 + 7, mod2 = mod + 2;
public
{
	pure nothrow
	{
		///make_pair
		pair!(X, Y) make_pair(X, Y)(in X x_, in Y y_)
		{
			return tuple!(X, "fi", Y, "se")(x_, y_);
		}
		///square
		@property X sqr(X)(in X a_)
		{
			return a_ * a_;
		}
		///cube
		@property X cub(X)(in X a_)
		{
			return a_ * a_ * a_;
		}
		///posicion lower bound
		size_t lowb(T, X)(T a, auto ref X g)
				if (isRandomAccessRange!T && hasLength!T && is(typeof(g < a[0]) == bool))
		{
			return assumeSorted(a).lowerBound(g).length;
		}
		///posicion upper bound
		size_t upb(T, X)(T a, auto ref X g)
				if (isRandomAccessRange!T && hasLength!T && is(typeof(g < a[0]) == bool))
		{
			return a.length - assumeSorted(a).upperBound(g).length;
		}
		///posicion binary search
		size_t binf(T, X)(T a, auto ref X g)
				if (isRandomAccessRange!T && hasLength!T && is(typeof(g < a[0]) == bool))
		{
			auto pos = lowb(a, g);
			return (g == a[pos]) ? pos : a.length;
		}
		///binary search
		bool binary_search(T, X)(T a, auto ref X g)
				if (isRandomAccessRange!T && hasLength!T && is(typeof(g < a[0]) == bool))
		{
			return assumeSorted(a).contains(g);
		}
	}
	static if (version_minor >= 74)
	{
		///read without format
		bool input(T...)(auto ref T ptrs) if (ptrs.length > 0)
		{
			return readf!(replicate(" %s", ptrs.length))(ptrs) == ptrs.length;
		}
		///readln without format
		bool read(T...)(auto ref T ptrs) if (ptrs.length > 0)
		{
			const bool fl = readf!(replicate(" %s", ptrs.length))(ptrs) == ptrs.length;
			readln;
			return fl;
		}
	}
	else
	{
		///read without format
		bool input(T...)(ref T ptrs) if (ptrs.length > 0)
		{
			size_t s;
			foreach (ref e; ptrs)
			{
				if (isSomeChar!(Unqual!(typeof(e))))
					s += readf(" %c", &e);
				else
					s += readf(" %s", &e);
			}
			return s == ptrs.length;
		}
		///readln without format
		bool read(T...)(ref T ptrs) if (ptrs.length > 0)
		{
			size_t s;
			foreach (ref e; ptrs)
			{
				if (isSomeChar!(Unqual!(typeof(e))))
					s += readf(" %c", &e);
				else
					s += readf(" %s", &e);
			}
			readln;
			return s == ptrs.length;
		}
	}
	///opening file
	nothrow auto inf(in char* name)
	{
		return freopen(name, "r", core.stdc.stdio.stdin);
	}
	///closing file
	nothrow auto ouf(in char* name)
	{
		return freopen(name, "w", core.stdc.stdio.stdout);
	}
	///parsing array from line
	@property auto arread(T)()
	{
		return readln.splitter.map!(to!(T)).array;
	}
	///outbuffer
	void enter(T...)(T args)
	{
		writeln(args);
		stdout.flush;
	}
}
//foreach_reverse isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin
//cumulativeFold return static heapify join filter foreach
//------------------------------------------program beginning--------------------------
void main()
{
	version (home)
	{
		assert(freopen("input.txt", "r", core.stdc.stdio.stdin));
	}
	int tt;
	read(tt);
	auto a = new Complex!double[1 << 20], b = new Complex!double[1 << 20];
	auto ca = a.dup, cb = b.dup;
	auto d = new int[1 << 20];
	auto r = new int[500_000];
	Fft f = new Fft(1 << 20);
	string s;
	foreach (ii; 0 .. tt)
	{
		s = readln;
		int n;
		read(n);
		s = readln.strip;
		int st = 1;
		while (st <= n)
			st *= 2;
		st *= 2;
		auto z = Complex!double(0, 0);
		a[0 .. st] = z;
		b[0 .. st] = z;
		ca[0 .. st] = z;
		cb[0 .. st] = z;
		foreach (i; 0 .. n)
		{
			if (s[i] == 'V')
				a[i] = 1;
			else if (s[i] == 'K')
				b[n - 1 - i] = 1;
		}
		f.fft(a[0 .. st], ca[0 .. st]);
		f.fft(b[0 .. st], cb[0 .. st]);
		ca[0 .. st] *= cb[0 .. st];
		cb[0 .. st] = z;
		debug writeln(ca[0 .. st]);
		f.inverseFft(ca[0 .. st], cb[0 .. st]);
		foreach (i; 0 .. st)
		{
			d[i] = roundTo!int(cb[i].re);
		}
		r[n - 1] = d[n - 1];
		foreach (i; 1 .. n)
		{
			r[i] = d[n - 1 - i] + d[n - 1 + i];
		}
		int[] ans;
		foreach (i; 1 .. n)
		{
			bool flag = true;
			foreach (j; 1 .. (n - 1) / i + 1)
			{
				if (r[i * j])
				{
					flag = false;
					break;
				}
			}
			if (flag)
			{
				ans ~= i;
			}
		}
		ans ~= n;
		writeln(ans.length);
		foreach (x; ans)
		{
			write(x, ' ');
		}
	}
}

import core.bitop, std.algorithm, std.bigint, std.compiler, std.container,
	std.complex, std.bitmanip, std.conv, std.math;
import std.numeric : Fft, fft, gcd, inverseFft;
import std.random, std.range, std.regex, std.stdio, std.string, std.traits,
	std.typecons, std.uni;

alias pair(X, Y) = Tuple!(X, "fi", Y, "se");
alias mp = make_pair;
alias pii = pair!(int, int);
alias pll = pair!(long, long);
// alias gcd = std.numeric.gcd;
alias lint = long;
alias rbt = RedBlackTree;
///modulo
const int mod = 10 ^^ 9 + 7, mod2 = mod + 2;
public
{
	pure nothrow
	{
		///make_pair
		pair!(X, Y) make_pair(X, Y)(in X x_, in Y y_)
		{
			return tuple!(X, "fi", Y, "se")(x_, y_);
		}
		///square
		@property X sqr(X)(in X a_)
		{
			return a_ * a_;
		}
		///cube
		@property X cub(X)(in X a_)
		{
			return a_ * a_ * a_;
		}
		///posicion lower bound
		size_t lowb(T, X)(T a, auto ref X g)
				if (isRandomAccessRange!T && hasLength!T && is(typeof(g < a[0]) == bool))
		{
			return assumeSorted(a).lowerBound(g).length;
		}
		///posicion upper bound
		size_t upb(T, X)(T a, auto ref X g)
				if (isRandomAccessRange!T && hasLength!T && is(typeof(g < a[0]) == bool))
		{
			return a.length - assumeSorted(a).upperBound(g).length;
		}
		///posicion binary search
		size_t binf(T, X)(T a, auto ref X g)
				if (isRandomAccessRange!T && hasLength!T && is(typeof(g < a[0]) == bool))
		{
			auto pos = lowb(a, g);
			return (g == a[pos]) ? pos : a.length;
		}
		///binary search
		bool binary_search(T, X)(T a, auto ref X g)
				if (isRandomAccessRange!T && hasLength!T && is(typeof(g < a[0]) == bool))
		{
			return assumeSorted(a).contains(g);
		}
	}
	static if (version_minor >= 74)
	{
		///read without format
		bool input(T...)(auto ref T ptrs) if (ptrs.length > 0)
		{
			return readf!(replicate(" %s", ptrs.length))(ptrs) == ptrs.length;
		}
		///readln without format
		bool read(T...)(auto ref T ptrs) if (ptrs.length > 0)
		{
			const bool fl = readf!(replicate(" %s", ptrs.length))(ptrs) == ptrs.length;
			readln;
			return fl;
		}
	}
	else
	{
		///read without format
		bool input(T...)(ref T ptrs) if (ptrs.length > 0)
		{
			size_t s;
			foreach (ref e; ptrs)
			{
				if (isSomeChar!(Unqual!(typeof(e))))
					s += readf(" %c", &e);
				else
					s += readf(" %s", &e);
			}
			return s == ptrs.length;
		}
		///readln without format
		bool read(T...)(ref T ptrs) if (ptrs.length > 0)
		{
			size_t s;
			foreach (ref e; ptrs)
			{
				if (isSomeChar!(Unqual!(typeof(e))))
					s += readf(" %c", &e);
				else
					s += readf(" %s", &e);
			}
			readln;
			return s == ptrs.length;
		}
	}
	///opening file
	nothrow auto inf(in char* name)
	{
		return freopen(name, "r", core.stdc.stdio.stdin);
	}
	///closing file
	nothrow auto ouf(in char* name)
	{
		return freopen(name, "w", core.stdc.stdio.stdout);
	}
	///parsing array from line
	@property auto arread(T)()
	{
		return readln.splitter.map!(to!(T)).array;
	}
	///outbuffer
	void enter(T...)(T args)
	{
		writeln(args);
		stdout.flush;
	}
}
//foreach_reverse isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin
//cumulativeFold return static heapify join filter foreach
//------------------------------------------program beginning--------------------------
void main()
{
	version (home)
	{
		assert(freopen("input.txt", "r", core.stdc.stdio.stdin));
	}
	int tt;
	read(tt);
	auto a = new Complex!double[1 << 20], b = new Complex!double[1 << 20];
	auto ca = a.dup, cb = b.dup;
	auto d = new int[1 << 20];
	auto r = new int[500_000];
	Fft f = new Fft(1 << 20);
	foreach (ii; 0 .. tt)
	{
		auto s = readln;
		int n;
		read(n);
		s = readln.strip;
		int st = 1;
		while (st <= n)
			st *= 2;
		st *= 2;
		auto z = Complex!double(0, 0);
		a[0 .. st] = z;
		b[0 .. st] = z;
		ca[0 .. st] = z;
		cb[0 .. st] = z;
		foreach (i; 0 .. n)
		{
			if (s[i] == 'V')
				a[i] = 1;
			else if (s[i] == 'K')
				b[n - 1 - i] = 1;
		}
		f.fft(a[0 .. st], ca[0 .. st]);
		f.fft(b[0 .. st], cb[0 .. st]);
		ca[0 .. st] *= cb[0 .. st];
		cb[0 .. st] = z;
		debug writeln(ca[0 .. st]);
		f.inverseFft(ca[0 .. st], cb[0 .. st]);
		foreach (i; 0 .. st)
		{
			d[i] = roundTo!int(cb[i].re);
		}
		r[n - 1] = d[n - 1];
		foreach (i; 1 .. n)
		{
			r[i] = d[n - 1 - i] + d[n - 1 + i];
		}
		int[] ans;
		foreach (i; 1 .. n)
		{
			bool flag = true;
			foreach (j; 1 .. (n - 1) / i + 1)
			{
				if (r[i * j])
				{
					flag = false;
					break;
				}
			}
			if (flag)
			{
				ans ~= i;
			}
		}
		ans ~= n;
		writeln(ans.length);
		foreach (x; ans)
		{
			write(x, ' ');
		}
	}
}