Rusty String

Grigory loves strings. Recently he found a metal strip on a loft. The strip had length n and consisted of letters “V” and “K”. Unfortunately, rust has eaten some of the letters so that it’s now impossible to understand which letter was written.

Grigory couldn’t understand for a long time what these letters remind him of, so he became interested in the following question: if we put a letter “V” or “K” on each unreadable position, which values can the period of the resulting string be equal to?

A period of a string is such an integer d from 1 to the length of the string that if we put the string shifted by d positions to the right on itself, then all overlapping letters coincide. For example, 3 and 5 are periods of “VKKVK”.

Input

There are several (at least one) test cases in the input. The first line contains single integer — the number of test cases.

There is an empty line before each test case. Each test case is described in two lines: the first line contains single integer n (1 ≤ n ≤ 5·105) — the length of the string, the second line contains the string of length n, consisting of letters “V”, “K” and characters “?”. The latter means the letter on its position is unreadable.

It is guaranteed that the sum of lengths among all test cases doesn’t exceed 5·105.

For hacks you can only use tests with one test case.

Output

For each test case print two lines. In the first line print the number of possible periods after we replace each unreadable letter with “V” or “K”. In the next line print all these values in increasing order.

Example

input

3
 
5
V??VK
 
6
??????
 
4
?VK?

output

2
3 5
6
1 2 3 4 5 6
3
2 3 4

Note

In the first test case from example we can obtain, for example, “VKKVK”, which has periods 3 and 5.

In the second test case we can obtain “VVVVVV” which has all periods from 1 to 6.

In the third test case string “KVKV” has periods 2 and 4, and string “KVKK” has periods 3 and 4.

Solution:

#include <bits/stdc++.h>

using namespace std;

namespace fft {
typedef double dbl;

struct num {
dbl x, y;
num() { x = y = 0; }
num(dbl x, dbl y) : x(x), y(y) { }
};

inline num operator+(num a, num b) { return num(a.x + b.x, a.y + b.y); }
inline num operator-(num a, num b) { return num(a.x - b.x, a.y - b.y); }
inline num operator*(num a, num b) { return num(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x); }
inline num conj(num a) { return num(a.x, -a.y); }

int base = 1;
vector<num> roots = {{0, 0}, {1, 0}};
vector<int> rev = {0, 1};

const dbl PI = acosl(-1.0);

void ensure_base(int nbase) {
if (nbase <= base) {
      return;
    }
    rev.resize(1 << nbase);
    for (int i = 0; i < (1 << nbase); i++) {
      rev[i] = (rev[i >> 1] >> 1) + ((i & 1) << (nbase - 1));
    }
    roots.resize(1 << nbase);
    while (base < nbase) {
      dbl angle = 2 * PI / (1 << (base + 1));
//      num z(cos(angle), sin(angle));
      for (int i = 1 << (base - 1); i < (1 << base); i++) {
        roots[i << 1] = roots[i];
//        roots[(i << 1) + 1] = roots[i] * z;
        dbl angle_i = angle * (2 * i + 1 - (1 << base));
        roots[(i << 1) + 1] = num(cos(angle_i), sin(angle_i));
      }
      base++;
    }
  }

  void fft(vector<num> &a, int n = -1) {
if (n == -1) {
n = a.size();
}
assert((n & (n - 1)) == 0);
int zeros = __builtin_ctz(n);
ensure_base(zeros);
int shift = base - zeros;
for (int i = 0; i < n; i++) {
      if (i < (rev[i] >> shift)) {
swap(a[i], a[rev[i] >> shift]);
}
}
/*    for (int k = 1; k < n; k <<= 1) {
      for (int i = 0; i < n; i += 2 * k) {
        for (int j = 0; j < k; j++) {
          num z = a[i + j + k] * roots[j + k];
          a[i + j + k] = a[i + j] - z;
          a[i + j] = a[i + j] + z;
        }
      }
    }*/
    for (int len = 1; len < n; len <<= 1) {
      for (int i = 0; i < n; i += 2 * len) {
        for (int j = i, k = i + len; j < i + len; j++, k++) {
          num z = a[k] * roots[k - i];
          a[k] = a[j] - z;
          a[j] = a[j] + z;
        }
      }
    }
  }

  vector<num> fa, fb;

vector<long long> multiply(vector<int> &a, vector<int> &b) {
int need = a.size() + b.size() - 1;
int nbase = 0;
while ((1 << nbase) < need) nbase++;
    ensure_base(nbase);
    int sz = 1 << nbase;
    if (sz > (int) fa.size()) {
fa.resize(sz);
}
for (int i = 0; i < sz; i++) {
      int x = (i < (int) a.size() ? a[i] : 0);
      int y = (i < (int) b.size() ? b[i] : 0);
      fa[i] = num(x, y);
    }
    fft(fa, sz);
    num r(0, -0.25 / sz);
    for (int i = 0; i <= (sz >> 1); i++) {
int j = (sz - i) & (sz - 1);
num z = (fa[j] * fa[j] - conj(fa[i] * fa[i])) * r;
if (i != j) {
fa[j] = (fa[i] * fa[i] - conj(fa[j] * fa[j])) * r;
}
fa[i] = z;
}
fft(fa, sz);
vector<long long> res(need);
for (int i = 0; i < need; i++) {
      res[i] = fa[i].x + 0.5;
    }
    return res;
  }

  vector<int> multiply_mod(vector<int> &a, vector<int> &b, int m, int eq = 0) {
int need = a.size() + b.size() - 1;
int nbase = 0;
while ((1 << nbase) < need) nbase++;
    ensure_base(nbase);
    int sz = 1 << nbase;
    if (sz > (int) fa.size()) {
fa.resize(sz);
}
for (int i = 0; i < (int) a.size(); i++) {
      int x = (a[i] % m + m) % m;
      fa[i] = num(x & ((1 << 15) - 1), x >> 15);
}
fill(fa.begin() + a.size(), fa.begin() + sz, num {0, 0});
fft(fa, sz);
if (eq) {
copy(fa.begin(), fa.begin() + sz, fb.begin());
} else {
if (sz > (int) fb.size()) {
fb.resize(sz);
}
for (int i = 0; i < (int) b.size(); i++) {
        int x = (b[i] % m + m) % m;
        fb[i] = num(x & ((1 << 15) - 1), x >> 15);
}
fill(fb.begin() + b.size(), fb.begin() + sz, num {0, 0});
fft(fb, sz);
}
dbl ratio = 0.25 / sz;
num r2(0, -1);
num r3(ratio, 0);
num r4(0, -ratio);
num r5(0, 1);
for (int i = 0; i <= (sz >> 1); i++) {
int j = (sz - i) & (sz - 1);
num a1 = (fa[i] + conj(fa[j]));
num a2 = (fa[i] - conj(fa[j])) * r2;
num b1 = (fb[i] + conj(fb[j])) * r3;
num b2 = (fb[i] - conj(fb[j])) * r4;
if (i != j) {
num c1 = (fa[j] + conj(fa[i]));
num c2 = (fa[j] - conj(fa[i])) * r2;
num d1 = (fb[j] + conj(fb[i])) * r3;
num d2 = (fb[j] - conj(fb[i])) * r4;
fa[i] = c1 * d1 + c2 * d2 * r5;
fb[i] = c1 * d2 + c2 * d1;
}
fa[j] = a1 * b1 + a2 * b2 * r5;
fb[j] = a1 * b2 + a2 * b1;
}
fft(fa, sz);
fft(fb, sz);
vector<int> res(need);
for (int i = 0; i < need; i++) {
      long long aa = fa[i].x + 0.5;
      long long bb = fb[i].x + 0.5;
      long long cc = fa[i].y + 0.5;
      res[i] = (aa + ((bb % m) << 15) + ((cc % m) << 30)) % m;
    }
    return res;
  }

  vector<int> square_mod(vector<int> &a, int m) {
return multiply_mod(a, a, m, 1);
}
// fft::multiply uses dbl, outputs vector<long long> of rounded values
// fft::multiply_mod might work for res.size() up to 2^21
// typedef long double dbl;          =>        up to 2^25 (but takes a lot of memory)
};

const int N = 1234567;

char s[N];

int main() {
int tt;
scanf("%d", &tt);
vector<int> a, b, res;
while (tt--) {
int n;
scanf("%d", &n);
scanf("%s", s);
a.resize(n);
b.resize(n);
for (int i = 0; i < n; i++) {
      a[i] = (s[i] == 'V');
      b[i] = (s[n - i - 1] == 'K');
    }
    vector<long long> c = fft::multiply(a, b);
res.clear();
for (int i = 1; i <= n; i++) {
      bool ok = true;
      for (int j = i; j < n; j += i) {
        if (c[n - 1 - j] != 0 || c[n - 1 + j] != 0) {
          ok = false;
          break;
        }
      }
      if (ok) {
        res.push_back(i);
      }
    }
    int sz = res.size();
    printf("%d\n", sz);
    for (int i = 0; i < sz; i++) {
      if (i > 0) putchar(' ');
printf("%d", res[i]);
}
printf("\n");
}
return 0;
}