### Prompt

Your task is to create a Cpp solution to the following problem:

Peter got a new snow blower as a New Year present. Of course, Peter decided to try it immediately. After reading the instructions he realized that it does not work like regular snow blowing machines. In order to make it work, you need to tie it to some point that it does not cover, and then switch it on. As a result it will go along a circle around this point and will remove all the snow from its path.

Formally, we assume that Peter's machine is a polygon on a plane. Then, after the machine is switched on, it will make a circle around the point to which Peter tied it (this point lies strictly outside the polygon). That is, each of the points lying within or on the border of the polygon will move along the circular trajectory, with the center of the circle at the point to which Peter tied his machine.

Peter decided to tie his car to point P and now he is wondering what is the area of the region that will be cleared from snow. Help him.

Input

The first line of the input contains three integers — the number of vertices of the polygon n (<image>), and coordinates of point P.

Each of the next n lines contains two integers — coordinates of the vertices of the polygon in the clockwise or counterclockwise order. It is guaranteed that no three consecutive vertices lie on a common straight line.

All the numbers in the input are integers that do not exceed 1 000 000 in their absolute value.

Output

Print a single real value number — the area of the region that will be cleared. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.

Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.

Examples

Input

3 0 0
0 1
-1 2
1 2

Output

12.566370614359172464

Input

4 1 -1
0 0
1 2
2 0
1 1

Output

21.991148575128551812

Note

In the first sample snow will be removed from that area:

<image>

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
vector<pair<double, double>> v;
double dist(pair<double, double> a, pair<double, double> b) {
  double aux = a.first - b.first;
  aux *= aux;
  double aux2 = a.second - b.second;
  aux2 *= aux2;
  return sqrt(aux + aux2);
}
double Dot(pair<double, double> a, pair<double, double> b) {
  return a.first * b.first + a.second * b.second;
}
double cross(pair<double, double> a, pair<double, double> b) {
  return a.first * b.second - a.second * b.first;
}
double dist_line(pair<double, double> p, pair<double, double> a,
                 pair<double, double> b) {
  pair<double, double> u, v, w, z;
  u = {b.first - a.first, b.second - a.second};
  v = {p.first - a.first, p.second - a.second};
  w = {p.first - b.first, p.second - b.second};
  z = {a.first - b.first, a.second - b.second};
  if (Dot(u, w) > 0) return dist(p, b);
  if (Dot(z, v) > 0) return dist(p, a);
  return fabs(cross(u, v)) / dist(a, b);
}
int main() {
  ios_base::sync_with_stdio(0);
  int n, i;
  double maxD = 0, minD = 1e9;
  pair<double, double> aux, p;
  cin >> n >> p.first >> p.second;
  for (i = 0; i < n; i++) {
    cin >> aux.first >> aux.second;
    v.emplace_back(aux);
  }
  for (int i = 0; i < n; i++) {
    double d = dist(p, v[i]);
    maxD = max(maxD, d);
    minD = min(minD, d);
  }
  for (i = 0; i < n; i++) {
    double d = dist_line(p, v[i], v[(i + 1) % n]);
    maxD = max(maxD, d);
    minD = min(minD, d);
  }
  cout.precision(10);
  cout << fixed << (maxD * maxD - minD * minD) * 3.14159265358979323846 << "\n";
  return 0;
}

```


### Prompt

Please create a solution in cpp to the following problem:

Everybody seems to think that the Martians are green, but it turns out they are metallic pink and fat. Ajs has two bags of distinct nonnegative integers. The bags are disjoint, and the union of the sets of numbers in the bags is \{0,1,…,M-1\}, for some positive integer M. Ajs draws a number from the first bag and a number from the second bag, and then sums them modulo M.

What are the residues modulo M that Ajs cannot obtain with this action?

Input

The first line contains two positive integer N (1 ≤ N ≤ 200 000) and M (N+1 ≤ M ≤ 10^{9}), denoting the number of the elements in the first bag and the modulus, respectively.

The second line contains N nonnegative integers a_1,a_2,…,a_N (0 ≤ a_1<a_2< …< a_N<M), the contents of the first bag.

Output

In the first line, output the cardinality K of the set of residues modulo M which Ajs cannot obtain.

In the second line of the output, print K space-separated integers greater or equal than zero and less than M, which represent the residues Ajs cannot obtain. The outputs should be sorted in increasing order of magnitude. If K=0, do not output the second line.

Examples

Input

2 5
3 4

Output

1
2

Input

4 1000000000
5 25 125 625

Output

0

Input

2 4
1 3

Output

2
0 2

Note

In the first sample, the first bag and the second bag contain \{3,4\} and \{0,1,2\}, respectively. Ajs can obtain every residue modulo 5 except the residue 2:  4+1 ≡ 0,   4+2 ≡ 1,   3+0 ≡ 3,   3+1 ≡ 4  modulo 5. One can check that there is no choice of elements from the first and the second bag which sum to 2 modulo 5.

In the second sample, the contents of the first bag are \{5,25,125,625\}, while the second bag contains all other nonnegative integers with at most 9 decimal digits. Every residue modulo 1 000 000 000 can be obtained as a sum of an element in the first bag and an element in the second bag.

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
int a[200000];
int b[200000];
int main(int argc, char *argv[]) {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; ++i) scanf("%d", a + i);
  for (int i = 0; i < n - 1; ++i) b[i] = a[i + 1] - a[i];
  int t = n;
  for (int i = 1; i < n; ++i) {
    bool flag = true;
    for (int j = 0; j < n - i - 1; ++j) {
      if (b[j] != b[j + i]) {
        flag = false;
        break;
      }
    }
    if (flag) {
      t = i;
      break;
    }
  }
  int ans = -1;
  for (int i = 0; i < n; ++i) {
    int v = a[0] + a[i];
    if (v >= m && i < n - 1) continue;
    bool flag = false;
    for (int j = 1; j <= (i >> 1); ++j) {
      if (a[j] + a[i - j] != v) {
        flag = true;
        break;
      }
    }
    if (!flag) {
      for (int j = i + 1; j <= ((n + i) >> 1); ++j) {
        if (a[j] + a[n - j + i] != v + m) {
          flag = true;
          break;
        }
      }
    }
    if (!flag) {
      ans = i;
      break;
    }
  }
  if (ans == -1)
    cout << 0 << endl;
  else {
    int cnt = (n - 1 - ans) / t;
    cout << cnt + 1 << endl;
    int *res = new int[cnt + 1];
    for (int i = 0; i <= cnt; ++i) {
      res[i] = (a[0] + a[ans + i * t]) % m;
    }
    sort(res, res + cnt + 1);
    for (int i = 0; i < cnt; ++i) printf("%d ", res[i]);
    cout << res[cnt] << endl;
  }
  return 0;
}

```


### Prompt

Please formulate a cpp solution to the following problem:

Malek has recently found a treasure map. While he was looking for a treasure he found a locked door. There was a string s written on the door consisting of characters '(', ')' and '#'. Below there was a manual on how to open the door. After spending a long time Malek managed to decode the manual and found out that the goal is to replace each '#' with one or more ')' characters so that the final string becomes beautiful.

Below there was also written that a string is called beautiful if for each i (1 ≤ i ≤ |s|) there are no more ')' characters than '(' characters among the first i characters of s and also the total number of '(' characters is equal to the total number of ')' characters.

Help Malek open the door by telling him for each '#' character how many ')' characters he must replace it with.

Input

The first line of the input contains a string s (1 ≤ |s| ≤ 105). Each character of this string is one of the characters '(', ')' or '#'. It is guaranteed that s contains at least one '#' character.

Output

If there is no way of replacing '#' characters which leads to a beautiful string print  - 1. Otherwise for each character '#' print a separate line containing a positive integer, the number of ')' characters this character must be replaced with.

If there are several possible answers, you may output any of them.

Examples

Input

(((#)((#)

Output

1
2

Input

()((#((#(#()

Output

2
2
1

Input

#

Output

-1

Input

(#)

Output

-1

Note

|s| denotes the length of the string s.

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-5;
long long llinf = 1e18 + 10;
int intinf = 1e9 + 7;
const int mod = 1e9 + 7;
const int N = 1e5 + 10;
char s[N];
int main() {
  scanf("%s", s);
  int n = strlen(s);
  int run = 0;
  int lstrn, lst, num = 0;
  for (int i = 0; i < int(n); ++i) {
    if (s[i] == '(')
      run++;
    else {
      run--;
      if (s[i] == '#') num++, lst = i, lstrn = run;
    }
    if (run < 0) {
      cout << "-1\n";
      return 0;
    }
  }
  int run2 = 0;
  for (int i = n - 1; i > lst; i--) {
    if (s[i] == ')')
      run2++;
    else
      run2--;
    if (run2 < 0) {
      cout << "-1\n";
      return 0;
    }
  }
  run = lstrn;
  if (run2 > run) {
    cout << "-1\n";
    return 0;
  }
  int ans = run - run2 + 1;
  for (int i = 0; i < int(num - 1); ++i) printf("1\n");
  printf("%d\n", ans);
  return 0;
}

```


### Prompt

Your challenge is to write a cpp solution to the following problem:

Valentin participates in a show called "Shockers". The rules are quite easy: jury selects one letter which Valentin doesn't know. He should make a small speech, but every time he pronounces a word that contains the selected letter, he receives an electric shock. He can make guesses which letter is selected, but for each incorrect guess he receives an electric shock too. The show ends when Valentin guesses the selected letter correctly.

Valentin can't keep in mind everything, so he could guess the selected letter much later than it can be uniquely determined and get excessive electric shocks. Excessive electric shocks are those which Valentin got after the moment the selected letter can be uniquely determined. You should find out the number of excessive electric shocks.

Input

The first line contains a single integer n (1 ≤ n ≤ 105) — the number of actions Valentin did.

The next n lines contain descriptions of his actions, each line contains description of one action. Each action can be of one of three types:

  1. Valentin pronounced some word and didn't get an electric shock. This action is described by the string ". w" (without quotes), in which "." is a dot (ASCII-code 46), and w is the word that Valentin said.
  2. Valentin pronounced some word and got an electric shock. This action is described by the string "! w" (without quotes), in which "!" is an exclamation mark (ASCII-code 33), and w is the word that Valentin said.
  3. Valentin made a guess about the selected letter. This action is described by the string "? s" (without quotes), in which "?" is a question mark (ASCII-code 63), and s is the guess — a lowercase English letter.

All words consist only of lowercase English letters. The total length of all words does not exceed 105.

It is guaranteed that last action is a guess about the selected letter. Also, it is guaranteed that Valentin didn't make correct guesses about the selected letter before the last action. Moreover, it's guaranteed that if Valentin got an electric shock after pronouncing some word, then it contains the selected letter; and also if Valentin didn't get an electric shock after pronouncing some word, then it does not contain the selected letter.

Output

Output a single integer — the number of electric shocks that Valentin could have avoided if he had told the selected letter just after it became uniquely determined.

Examples

Input

5
! abc
. ad
. b
! cd
? c

Output

1

Input

8
! hello
! codeforces
? c
. o
? d
? h
. l
? e

Output

2

Input

7
! ababahalamaha
? a
? b
? a
? b
? a
? h

Output

0

Note

In the first test case after the first action it becomes clear that the selected letter is one of the following: a, b, c. After the second action we can note that the selected letter is not a. Valentin tells word "b" and doesn't get a shock. After that it is clear that the selected letter is c, but Valentin pronounces the word cd and gets an excessive electric shock.

In the second test case after the first two electric shocks we understand that the selected letter is e or o. Valentin tries some words consisting of these letters and after the second word it's clear that the selected letter is e, but Valentin makes 3 more actions before he makes a correct hypothesis.

In the third example the selected letter can be uniquely determined only when Valentin guesses it, so he didn't get excessive electric shocks.

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
int n;
bool vos[100];
bool soh[100];
int ok = 0;
int ans = 0;
void clea() {
  for (int i = 0; i < 100; i++) vos[i] = 0;
}
int main() {
  cin >> n;
  for (int r = 0; r < n; r++) {
    string com, q;
    cin >> com >> q;
    if (com == "!") {
      clea();
      for (int i = 0; q[i]; i++) {
        int t = q[i] - 'a';
        vos[t] = 1;
      }
      for (int i = 0; i < 26; i++) {
        if (vos[i] == 0) soh[i] = 1;
      }
      if (ok == 1) ans++;
    }
    if (com == ".") {
      for (int i = 0; q[i]; i++) {
        int t = q[i] - 'a';
        soh[t] = 1;
      }
    }
    if (com == "?") {
      int t = q[0] - 'a';
      if (soh[t] == 1 && ok == 1)
        ans++;
      else
        soh[t] = 1;
    }
    int y = 0;
    for (int i = 0; i < 26; i++)
      if (soh[i] == 0) y++;
    if (y == 1) ok = 1;
  }
  cout << ans << endl;
}

```


### Prompt

Create a solution in CPP for the following problem:

You are given a sequence a_1, a_2, ..., a_n, consisting of integers.

You can apply the following operation to this sequence: choose some integer x and move all elements equal to x either to the beginning, or to the end of a. Note that you have to move all these elements in one direction in one operation.

For example, if a = [2, 1, 3, 1, 1, 3, 2], you can get the following sequences in one operation (for convenience, denote elements equal to x as x-elements):

  * [1, 1, 1, 2, 3, 3, 2] if you move all 1-elements to the beginning;
  * [2, 3, 3, 2, 1, 1, 1] if you move all 1-elements to the end;
  * [2, 2, 1, 3, 1, 1, 3] if you move all 2-elements to the beginning;
  * [1, 3, 1, 1, 3, 2, 2] if you move all 2-elements to the end;
  * [3, 3, 2, 1, 1, 1, 2] if you move all 3-elements to the beginning;
  * [2, 1, 1, 1, 2, 3, 3] if you move all 3-elements to the end;

You have to determine the minimum number of such operations so that the sequence a becomes sorted in non-descending order. Non-descending order means that for all i from 2 to n, the condition a_{i-1} ≤ a_i is satisfied.

Note that you have to answer q independent queries.

Input

The first line contains one integer q (1 ≤ q ≤ 3 ⋅ 10^5) — the number of the queries. Each query is represented by two consecutive lines.

The first line of each query contains one integer n (1 ≤ n ≤ 3 ⋅ 10^5) — the number of elements.

The second line of each query contains n integers a_1, a_2, ... , a_n (1 ≤ a_i ≤ n) — the elements.

It is guaranteed that the sum of all n does not exceed 3 ⋅ 10^5.

Output

For each query print one integer — the minimum number of operation for sorting sequence a in non-descending order.

Example

Input

3
7
3 1 6 6 3 1 1
8
1 1 4 4 4 7 8 8
7
4 2 5 2 6 2 7

Output

2
0
1

Note

In the first query, you can move all 1-elements to the beginning (after that sequence turn into [1, 1, 1, 3, 6, 6, 3]) and then move all 6-elements to the end.

In the second query, the sequence is sorted initially, so the answer is zero.

In the third query, you have to move all 2-elements to the beginning.

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
void setIO(const string &name = "") {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  if (name.length()) {
    freopen((name + ".in").c_str(), "r", stdin);
    freopen((name + ".out").c_str(), "w", stdout);
  }
}
template <typename T>
void read(vector<T> &a) {
  for (auto &x : a) cin >> x;
}
template <typename T>
void read(vector<T> &a, long long n) {
  a.resize(n);
  for (auto &x : a) cin >> x;
}
template <class T, class U>
ostream &operator<<(ostream &out, const pair<T, U> &v) {
  out << "(";
  out << v.first << "," << v.second;
  return out << ")";
}
template <class T>
ostream &operator<<(ostream &out, const vector<T> &v) {
  out << "[";
  for (auto(i) = 0; i < (long long)(v).size(); i++) {
    if (i) out << ", ";
    out << v[i];
  }
  return out << "]";
}
template <typename T>
void print(vector<T> &a) {
  for (const auto &x : a) cout << x << ' ';
  cout << '\n';
}
void MOD(long long &x, long long m = 1000000007) {
  x %= m;
  if (x < 0) x += m;
}
template <typename T>
void dbg(const char *name, T &&arg1) {
  cout << name << " : " << arg1 << '\n';
}
template <typename T, typename... U>
void dbg(const char *names, T &&arg1, U &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  dbg(comma + 1, args...);
}
template <class T>
void read(T &x) {
  cin >> x;
}
template <class T, class... U>
void read(T &t, U &...u) {
  read(t);
  read(u...);
}
long long gcd(long long a, long long b) { return !a ? b : gcd(b % a, a); }
long long n;
vector<long long> a;
vector<long long> starting, ending;
void solve() {
  read(n);
  read(a, n);
  starting.assign(n + 2, -1);
  ending.assign(n + 2, -1);
  for (auto(i) = 0; i < n; i++) {
    if (starting[a[i]] == -1) {
      starting[a[i]] = i;
      ending[a[i]] = i;
    } else {
      ending[a[i]] = i;
    }
  }
  vector<long long> c = a;
  sort(c.begin(), c.end());
  c.erase(unique(c.begin(), c.end()), c.end());
  vector<long long> range;
  range.reserve(n);
  long long sz = 1;
  for (auto(i) = 1; i < (long long)(c).size(); i++) {
    if (starting[c[i]] < ending[c[i - 1]]) {
      range.emplace_back(sz);
      sz = 1;
    } else
      sz++;
  }
  range.emplace_back(sz);
  long long ans =
      (long long)(c).size() - *max_element(range.begin(), range.end());
  cout << ans << '\n';
}
int32_t main() {
  setIO();
  long long t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}

```


### Prompt

In cpp, your task is to solve the following problem:

The R1 company has recently bought a high rise building in the centre of Moscow for its main office. It's time to decorate the new office, and the first thing to do is to write the company's slogan above the main entrance to the building.

The slogan of the company consists of n characters, so the decorators hung a large banner, n meters wide and 1 meter high, divided into n equal squares. The first character of the slogan must be in the first square (the leftmost) of the poster, the second character must be in the second square, and so on.

Of course, the R1 programmers want to write the slogan on the poster themselves. To do this, they have a large (and a very heavy) ladder which was put exactly opposite the k-th square of the poster. To draw the i-th character of the slogan on the poster, you need to climb the ladder, standing in front of the i-th square of the poster. This action (along with climbing up and down the ladder) takes one hour for a painter. The painter is not allowed to draw characters in the adjacent squares when the ladder is in front of the i-th square because the uncomfortable position of the ladder may make the characters untidy. Besides, the programmers can move the ladder. In one hour, they can move the ladder either a meter to the right or a meter to the left.

Drawing characters and moving the ladder is very tiring, so the programmers want to finish the job in as little time as possible. Develop for them an optimal poster painting plan!

Input

The first line contains two integers, n and k (1 ≤ k ≤ n ≤ 100) — the number of characters in the slogan and the initial position of the ladder, correspondingly. The next line contains the slogan as n characters written without spaces. Each character of the slogan is either a large English letter, or digit, or one of the characters: '.', '!', ',', '?'.

Output

In t lines, print the actions the programmers need to make. In the i-th line print:

  * "LEFT" (without the quotes), if the i-th action was "move the ladder to the left";
  * "RIGHT" (without the quotes), if the i-th action was "move the ladder to the right";
  * "PRINT x" (without the quotes), if the i-th action was to "go up the ladder, paint character x, go down the ladder".

The painting time (variable t) must be minimum possible. If there are multiple optimal painting plans, you can print any of them.

Examples

Input

2 2
R1

Output

PRINT 1
LEFT
PRINT R

Input

2 1
R1

Output

PRINT R
RIGHT
PRINT 1

Input

6 4
GO?GO!

Output

RIGHT
RIGHT
PRINT !
LEFT
PRINT O
LEFT
PRINT G
LEFT
PRINT ?
LEFT
PRINT O
LEFT
PRINT G

Note

Note that the ladder cannot be shifted by less than one meter. The ladder can only stand in front of some square of the poster. For example, you cannot shift a ladder by half a meter and position it between two squares. Then go up and paint the first character and the second character.

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
char ch[105];
int main() {
  int n, m;
  scanf("%d %d\n", &n, &m);
  scanf("%s", &ch);
  if (n - m >= m) {
    for (int i = 1; i < m; i++) printf("LEFT\n");
    for (int i = 1; i < n; i++) {
      printf("PRINT %c\nRIGHT\n", ch[i - 1]);
    }
    printf("PRINT %c\n", ch[n - 1]);
  } else {
    for (int i = 1; i <= n - m; i++) printf("RIGHT\n");
    for (int i = 1; i < n; i++) {
      printf("PRINT %c\nLEFT\n", ch[n - i]);
    }
    printf("PRINT %c\n", ch[0]);
  }
  return 0;
}

```


### Prompt

Please create a solution in cpp to the following problem:

In 2300, the Life Science Division of Federal Republic of Space starts a very ambitious project to complete the genome sequencing of all living creatures in the entire universe and develop the genomic database of all space life. Thanks to scientific research over many years, it has been known that the genome of any species consists of at most 26 kinds of molecules, denoted by English capital letters (i.e. `A` to `Z`).

What will be stored into the database are plain strings consisting of English capital letters. In general, however, the genome sequences of space life include frequent repetitions and can be awfully long. So, for efficient utilization of storage, we compress N-times repetitions of a letter sequence seq into N`(`seq`)`, where N is a natural number greater than or equal to two and the length of seq is at least one. When seq consists of just one letter c, we may omit parentheses and write Nc.

For example, a fragment of a genome sequence:

> `ABABABABXYXYXYABABABABXYXYXYCCCCCCCCCC`

can be compressed into:

> `4(AB)XYXYXYABABABABXYXYXYCCCCCCCCCC`

by replacing the first occurrence of `ABABABAB` with its compressed form. Similarly, by replacing the following repetitions of `XY`, `AB`, and `C`, we get:

> `4(AB)3(XY)4(AB)3(XY)10C`

Since `C` is a single letter, parentheses are omitted in this compressed representation. Finally, we have:

> `2(4(AB)3(XY))10C`

by compressing the repetitions of `4(AB)3(XY)`. As you may notice from this example, parentheses can be nested.

Your mission is to write a program that uncompress compressed genome sequences.

Input

The input consists of multiple lines, each of which contains a character string s and an integer i separated by a single space.

The character string s, in the aforementioned manner, represents a genome sequence. You may assume that the length of s is between 1 and 100, inclusive. However, of course, the genome sequence represented by s may be much, much, and much longer than 100. You may also assume that each natural number in s representing the number of repetitions is at most 1,000.

The integer i is at least zero and at most one million.

A line containing two zeros separated by a space follows the last input line and indicates the end of the input.

Output

For each input line, your program should print a line containing the i-th letter in the genome sequence that s represents. If the genome sequence is too short to have the i-th element, it should just print a zero. No other characters should be printed in the output lines. Note that in this problem the index number begins from zero rather than one and therefore the initial letter of a sequence is its zeroth element.

Example

Input

ABC 3
ABC 0
2(4(AB)3(XY))10C 30
1000(1000(1000(1000(1000(1000(NM)))))) 999999
0 0

Output

0
A
C
M

### Response

```cpp
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <cmath>
#include <cassert>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <numeric>
#include <complex>
#include <list>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <utility>
#include <functional>
#include <iterator>

using namespace std;

#define dump(n) cerr<<"# "<<#n<<"="<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define peri(i,a,b) for(int i=int(b);i-->int(a);)
#define rep(i,n) repi(i,0,n)
#define per(i,n) peri(i,0,n)
#define iter(c) __typeof__((c).begin())
#define foreach(i,c) for(iter(c) i=(c).begin();i!=(c).end();++i)
#define all(c) (c).begin(),(c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
typedef pair<int,int> pii;

const int INFTY=1<<29;

int length(const string& s,int begin,int end)
{
	ll res=0;
	for(int i=begin;i<end;i++){
		if(isdigit(s[i])){
			int r=0;
			while(isdigit(s[i]))
				r=r*10+s[i++]-'0';
			if(s[i]=='('){
				int j=i;
				for(int k=0;j<s.size();j++){
					if(s[j]=='(') k++;
					if(s[j]==')') k--;
					if(k==0) break;
				}
				res+=(ll)r*length(s,i+1,j);
				i=j;
			}
			else
				res+=r;
		}
		else
			res++;
		res=min<ll>(res,INFTY);
	}
	return res;
}

char solve(const string& s,int index,int ci)
{
	for(;ci<s.size();){
		if(isdigit(s[ci])){
			int r=0,i=ci;
			while(isdigit(s[i]))
				r=r*10+s[i++]-'0';
			if(s[i]=='('){
				int j=i;
				for(int k=0;j<s.size();j++){
					if(s[j]=='(') k++;
					if(s[j]==')') k--;
					if(k==0) break;
				}
				int len=length(s,i+1,j);
				if((ll)r*len>index)
					return solve(s,index%len,i+1);
				else{
					index-=r*len;
					ci=j+1;
				}
			}
			else{
				if(r>index)
					return s[i];
				else{
					index-=r;
					ci=i+1;
				}
			}
		}
		else{
			if(index==0)
				return s[ci];
			else{
				index--;
				ci++;
			}
		}
	}
	return '0';
}

int main()
{
	for(string s;cin>>s && s!="0";){
		int index; cin>>index;
		cout<<solve(s,index,0)<<endl;
	}

	return 0;
}
```


### Prompt

Please provide a cpp coded solution to the problem described below:

Billy investigates the question of applying greedy algorithm to different spheres of life. At the moment he is studying the application of greedy algorithm to the problem about change. There is an amount of n coins of different face values, and the coins of each value are not limited in number. The task is to collect the sum x with the minimum amount of coins. Greedy algorithm with each its step takes the coin of the highest face value, not exceeding x. Obviously, if among the coins' face values exists the face value 1, any sum x can be collected with the help of greedy algorithm. However, greedy algorithm does not always give the optimal representation of the sum, i.e. the representation with the minimum amount of coins. For example, if there are face values {1, 3, 4} and it is asked to collect the sum 6, greedy algorithm will represent the sum as 4 + 1 + 1, while the optimal representation is 3 + 3, containing one coin less. By the given set of face values find out if there exist such a sum x that greedy algorithm will collect in a non-optimal way. If such a sum exists, find out the smallest of these sums.

Input

The first line contains an integer n (1 ≤ n ≤ 400) — the amount of the coins' face values. The second line contains n integers ai (1 ≤ ai ≤ 109), describing the face values. It is guaranteed that a1 > a2 > ... > an and an = 1.

Output

If greedy algorithm collects any sum in an optimal way, output -1. Otherwise output the smallest sum that greedy algorithm collects in a non-optimal way.

Examples

Input

5
25 10 5 2 1

Output

-1

Input

3
4 3 1

Output

6

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-10;
const double PI = acos(-1.0);
template <class T>
T gcd(T a, T b) {
  for (T c; b; c = a, a = b, b = c % b)
    ;
  return a;
}
template <class T>
void out(const vector<T> &a) {
  for (int i = 0; i < ((int)(a).size()); ++i) cout << a[i] << " ";
  cout << endl;
}
int countbit(int n) { return n == 0 ? 0 : 1 + countbit(n & (n - 1)); }
const int d8[8][2] = {{1, 0}, {-1, 0},  {0, 1},  {0, -1},
                      {1, 1}, {-1, -1}, {1, -1}, {-1, 1}};
const int d4[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
const int N = 500;
long long a[N], b[N];
int n;
long long greedy(long long m) {
  long long ret = 0;
  for (int i = 0; i < n; ++i) {
    b[i] = m / a[i];
    m %= a[i];
    ret += b[i];
  }
  return ret;
}
int main() {
  cin >> n;
  for (int i = 0; i < n; ++i) cin >> a[i];
  long long ret = LONG_LONG_MAX;
  for (int i = 1; i < n; ++i)
    for (int j = i; j < n; ++j) {
      greedy(a[i - 1] - 1);
      b[j]++;
      long long m = 0;
      long long sum = 0;
      for (int k = 0; k <= j; ++k) {
        m += a[k] * b[k];
        sum += b[k];
      }
      if (greedy(m) > sum) ret = min(ret, m);
    }
  if (ret == LONG_LONG_MAX)
    cout << -1 << endl;
  else
    cout << ret << endl;
  return 0;
}

```


### Prompt

Develop a solution in CPP to the problem described below:

Little John aspires to become a plumber! Today he has drawn a grid consisting of n rows and m columns, consisting of n × m square cells.

In each cell he will draw a pipe segment. He can only draw four types of segments numbered from 1 to 4, illustrated as follows:

<image>

Each pipe segment has two ends, illustrated by the arrows in the picture above. For example, segment 1 has ends at top and left side of it.

Little John considers the piping system to be leaking if there is at least one pipe segment inside the grid whose end is not connected to another pipe's end or to the border of the grid. The image below shows an example of leaking and non-leaking systems of size 1 × 2.

<image>

Now, you will be given the grid that has been partially filled by Little John. Each cell will either contain one of the four segments above, or be empty. Find the number of possible different non-leaking final systems after Little John finishes filling all of the empty cells with pipe segments. Print this number modulo 1000003 (106 + 3).

Note that rotations or flipping of the grid are not allowed and so two configurations that are identical only when one of them has been rotated or flipped either horizontally or vertically are considered two different configurations.

Input

The first line will contain two single-space separated integers n and m (1 ≤ n, m, n·m ≤ 5·105) — the number of rows and columns respectively. Then n lines follow, each contains exactly m characters — the description of the grid. Each character describes a cell and is either one of these:

  * "1" - "4" — a pipe segment of one of four types as described above
  * "." — an empty cell

Output

Print a single integer denoting the number of possible final non-leaking pipe systems modulo 1000003 (106 + 3). If there are no such configurations, print 0.

Examples

Input

2 2
13
..

Output

2

Input

3 1
1
4
.

Output

0

Input

2 2
3.
.1

Output

1

Note

For the first example, the initial configuration of the grid is as follows.

<image>

The only two possible final non-leaking pipe configurations are as follows:

<image> <image>

For the second example, the initial grid is already leaking, so there will be no final grid that is non-leaking.

For the final example, there's only one possible non-leaking final grid as follows.

<image>

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
int htop[500010];
int hleft[500010];
string A[500010];
long long pw(long long a, long long p) {
  if (p == 0) return 1;
  long long x = pw(a, p / 2);
  long long ans = x * x;
  ans %= 1000003;
  if (p % 2 == 1) ans *= a;
  ans %= 1000003;
  return ans;
}
int main() {
  int N, M;
  cin >> N >> M;
  for (int r = 0; r < N; r++) {
    cin >> A[r];
  }
  bool ok = true;
  for (int r = 0; r < N && ok; r++) {
    for (int c = 0; c < M && ok; c++) {
      if (A[r][c] == '.') continue;
      int q = (A[r][c] - '0');
      int hasleft = (q == 1 || q == 2) ? 1 : 2;
      int hastop = (q == 1 || q == 4) ? 1 : 2;
      hastop += r % 2;
      hastop %= 2;
      hastop += 2 * (1 - hastop);
      hasleft += c % 2;
      hasleft %= 2;
      hasleft += 2 * (1 - hasleft);
      if (htop[c] != 0 && htop[c] != hastop) ok = false;
      if (hleft[r] != 0 && hleft[r] != hasleft) ok = false;
      htop[c] = hastop;
      hleft[r] = hasleft;
    }
  }
  if (!ok) {
    cout << 0 << endl;
    return 0;
  }
  int cnt = 0;
  for (int r = 0; r < N; r++) {
    bool has = false;
    for (int c = 0; c < M && !has; c++) {
      if (A[r][c] == '.') continue;
      has = true;
    }
    if (!has) cnt++;
  }
  for (int c = 0; c < M; c++) {
    bool has = false;
    for (int r = 0; r < N && !has; r++) {
      if (A[r][c] == '.') continue;
      has = true;
    }
    if (!has) cnt++;
  }
  long long ans = pw(2, cnt);
  cout << ans << endl;
}

```


### Prompt

Your task is to create a CPP solution to the following problem:

A and B are preparing themselves for programming contests.

An important part of preparing for a competition is sharing programming knowledge from the experienced members to those who are just beginning to deal with the contests. Therefore, during the next team training A decided to make teams so that newbies are solving problems together with experienced participants.

A believes that the optimal team of three people should consist of one experienced participant and two newbies. Thus, each experienced participant can share the experience with a large number of people.

However, B believes that the optimal team should have two experienced members plus one newbie. Thus, each newbie can gain more knowledge and experience.

As a result, A and B have decided that all the teams during the training session should belong to one of the two types described above. Furthermore, they agree that the total number of teams should be as much as possible.

There are n experienced members and m newbies on the training session. Can you calculate what maximum number of teams can be formed?

Input

The first line contains two integers n and m (0 ≤ n, m ≤ 5·105) — the number of experienced participants and newbies that are present at the training session.

Output

Print the maximum number of teams that can be formed.

Examples

Input

2 6

Output

2

Input

4 5

Output

3

Note

Let's represent the experienced players as XP and newbies as NB.

In the first test the teams look as follows: (XP, NB, NB), (XP, NB, NB).

In the second test sample the teams look as follows: (XP, NB, NB), (XP, NB, NB), (XP, XP, NB).

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, ans = 0;
  scanf("%d%d", &n, &m);
  for (int i = 0; i <= m / 2 && i <= n; i++) {
    int x = m - i * 2, y = n - i;
    ans = max(ans, i + min(x, y / 2));
  }
  printf("%d\n", ans);
}

```


### Prompt

Create a solution in cpp for the following problem:

A very tense moment: n cowboys stand in a circle and each one points his colt at a neighbor. Each cowboy can point the colt to the person who follows or precedes him in clockwise direction. Human life is worthless, just like in any real western.

The picture changes each second! Every second the cowboys analyse the situation and, if a pair of cowboys realize that they aim at each other, they turn around. In a second all such pairs of neighboring cowboys aiming at each other turn around. All actions happen instantaneously and simultaneously in a second.

We'll use character "A" to denote a cowboy who aims at his neighbour in the clockwise direction, and character "B" for a cowboy who aims at his neighbour in the counter clockwise direction. Then a string of letters "A" and "B" will denote the circle of cowboys, the record is made from the first of them in a clockwise direction.

For example, a circle that looks like "ABBBABBBA" after a second transforms into "BABBBABBA" and a circle that looks like "BABBA" transforms into "ABABB".

<image> This picture illustrates how the circle "BABBA" transforms into "ABABB"

A second passed and now the cowboys' position is described by string s. Your task is to determine the number of possible states that lead to s in a second. Two states are considered distinct if there is a cowboy who aims at his clockwise neighbor in one state and at his counter clockwise neighbor in the other state.

Input

The input data consists of a single string s. Its length is from 3 to 100 characters, inclusive. Line s consists of letters "A" and "B".

Output

Print the sought number of states.

Examples

Input

BABBBABBA

Output

2

Input

ABABB

Output

2

Input

ABABAB

Output

4

Note

In the first sample the possible initial states are "ABBBABBAB" and "ABBBABBBA".

In the second sample the possible initial states are "AABBB" and "BABBA".

### Response

```cpp
#include <bits/stdc++.h>
const int maxn = 100 + 10;
char c[maxn];
int a[maxn];
long long dp[maxn][2];
long long ans;
int n;
inline int p(int x) { return x == 0 ? n - 1 : x - 1; }
inline int s(int x) { return x == n - 1 ? 0 : x + 1; }
int main() {
  scanf("%s", c);
  n = strlen(c);
  for (int i = 1; i <= n; i++) a[i] = c[i - 1] == 'B';
  if (a[1] && (!a[n])) {
    bool t = a[1];
    for (int i = 1; i < n; i++) a[i] = a[i + 1];
    a[n] = t;
  }
  dp[0][0] = 1;
  dp[0][1] = 1;
  for (int i = 1; i <= n; i++) {
    if (a[i] && a[i + 1]) {
      dp[i][0] = dp[i - 1][0];
      dp[i][1] = dp[i - 1][0];
      continue;
    }
    if (a[i] && (!a[i + 1])) {
      dp[i][0] = dp[i - 1][0];
      dp[i][1] = dp[i - 1][0];
      continue;
    }
    if ((!a[i]) && a[i + 1]) {
      dp[i][0] = a[i - 1] * dp[i - 2][1];
      dp[i][1] = dp[i - 1][0] + a[i - 1] * dp[i - 2][1];
      continue;
    }
    if (!(a[i] || a[i + 1])) {
      dp[i][0] = dp[i - 1][0] + a[i - 1] * dp[i - 2][1];
      dp[i][1] = a[i - 1] * dp[i - 2][1];
      continue;
    }
  }
  ans = dp[n][0];
  if ((!a[1]) && a[n]) {
    a[n] = 0;
    for (int i = 1; i < n; i++) a[i] = a[i + 1];
    n--;
    dp[0][0] = 1;
    dp[0][1] = 1;
    for (int i = 1; i < n; i++) {
      if (a[i] && a[i + 1]) {
        dp[i][0] = dp[i - 1][0];
        dp[i][1] = dp[i - 1][0];
        continue;
      }
      if (a[i] && (!a[i + 1])) {
        dp[i][0] = dp[i - 1][0];
        dp[i][1] = dp[i - 1][0];
        continue;
      }
      if ((!a[i]) && a[i + 1]) {
        dp[i][0] = a[i - 1] * dp[i - 2][1];
        dp[i][1] = dp[i - 1][0] + a[i - 1] * dp[i - 2][1];
        continue;
      }
      if (!(a[i] || a[i + 1])) {
        dp[i][0] = dp[i - 1][0] + a[i - 1] * dp[i - 2][1];
        dp[i][1] = a[i - 1] * dp[i - 2][1];
        continue;
      }
    }
    ans += dp[n - 1][0];
  }
  printf("%I64d\n", ans);
  return 0;
}

```


### Prompt

Your challenge is to write a cpp solution to the following problem:

Mishka's favourite experimental indie band has recently dropped a new album! Songs of that album share one gimmick. Each name s_i is one of the following types:

  * 1~c — a single lowercase Latin letter;
  * 2~j~c — name s_j (1 ≤ j < i) with a single lowercase Latin letter appended to its end.

Songs are numbered from 1 to n. It's guaranteed that the first song is always of type 1.

Vova is rather interested in the new album but he really doesn't have the time to listen to it entirely. Thus he asks Mishka some questions about it to determine if some song is worth listening to. Questions have the following format:

  * i~t — count the number of occurrences of string t in s_i (the name of the i-th song of the album) as a continuous substring, t consists only of lowercase Latin letters.

Mishka doesn't question the purpose of that information, yet he struggles to provide it. Can you please help Mishka answer all Vova's questions?

Input

The first line contains a single integer n (1 ≤ n ≤ 4 ⋅ 10^5) — the number of songs in the album.

Each of the next n lines contains the desciption of the i-th song of the album in the following format:

  * 1~c — s_i is a single lowercase Latin letter;
  * 2~j~c — s_i is the name s_j (1 ≤ j < i) with a single lowercase Latin letter appended to its end.

The next line contains a single integer m (1 ≤ m ≤ 4 ⋅ 10^5) — the number of Vova's questions.

Each of the next m lines contains the desciption of the j-th Vova's question in the following format:

  * i~t (1 ≤ i ≤ n, 1 ≤ |t| ≤ 4 ⋅ 10^5) — count the number of occurrences of string t in s_i (the name of the i-th song of the album) as a continuous substring, t consists only of lowercase Latin letters.

It's guaranteed that the total length of question strings t doesn't exceed 4 ⋅ 10^5.

Output

For each question print a single integer — the number of occurrences of the question string t in the name of the i-th song of the album as a continuous substring.

Example

Input

20
1 d
2 1 a
2 2 d
2 3 a
2 4 d
2 5 a
2 6 d
2 7 a
1 d
2 9 o
2 10 k
2 11 i
2 12 d
2 13 o
2 14 k
2 15 i
2 1 o
2 17 k
2 18 i
2 15 i
12
8 da
8 dada
8 ada
6 dada
3 dada
19 doki
19 ok
16 doki
15 doki
9 d
1 a
20 doki

Output

4
3
3
2
0
1
1
2
1
1
0
2

Note

Song names of the first example:

  1. d
  2. da
  3. dad
  4. dada
  5. dadad
  6. dadada
  7. dadadad
  8. dadadada
  9. d
  10. do
  11. dok
  12. doki
  13. dokid
  14. dokido
  15. dokidok
  16. dokidoki
  17. do
  18. dok
  19. doki
  20. dokidoki

Thus the occurrences for each question string are:

  1. string "da" starts in positions [1, 3, 5, 7] in the name "dadadada";
  2. string "dada" starts in positions [1, 3, 5] in the name "dadadada";
  3. string "ada" starts in positions [2, 4, 6] in the name "dadadada";
  4. string "dada" starts in positions [1, 3] in the name "dadada";
  5. no occurrences of string "dada" in the name "dad";
  6. string "doki" starts in position [1] in the name "doki";
  7. string "ok" starts in position [2] in the name "doki";
  8. string "doki" starts in positions [1, 5] in the name "dokidoki";
  9. string "doki" starts in position [1] in the name "dokidok";
  10. string "d" starts in position [1] in the name "d";
  11. no occurrences of string "a" in the name "d";
  12. string "doki" starts in positions [1, 5] in the name "dokidoki".

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAXC = 26;
struct Node {
  bool is_terminal = false;
  int parent;
  int parent_char;
  int depth;
  int suffix_link = -1;
  int terminal_link = -1;
  int next[MAXC];
  int go[MAXC];
  Node(int p, int pch, int d) : parent(p), parent_char(pch), depth(d) {
    memset(next, -1, sizeof(next));
    memset(go, -1, sizeof(go));
  }
};
struct Trie {
  vector<Node> nodes;
  Trie(int n) {
    nodes.reserve(n);
    nodes.emplace_back(-1, -1, 0);
  }
  Trie() : Trie(0) {}
  int convert_char(char chr) { return chr - 'a'; }
  int add(const string &word) {
    int id = 0;
    for (char chr : word) {
      int c = convert_char(chr);
      if (nodes[id].next[c] == -1) {
        nodes[id].next[c] = nodes.size();
        nodes.emplace_back(id, c, nodes[id].depth + 1);
      }
      id = nodes[id].next[c];
    }
    nodes[id].is_terminal = true;
    return id;
  }
  void build() {
    queue<int> q;
    q.push(0);
    while (!q.empty()) {
      int id = q.front();
      q.pop();
      if (id == 0 || nodes[id].parent == 0) {
        nodes[id].suffix_link = 0;
      } else {
        nodes[id].suffix_link =
            get_go(get_suffix_link(nodes[id].parent), nodes[id].parent_char);
      }
      for (int c = 0; c < MAXC; c++) {
        if (nodes[id].next[c] != -1) {
          nodes[id].go[c] = nodes[id].next[c];
        } else if (id == 0) {
          nodes[id].go[c] = 0;
        } else {
          nodes[id].go[c] = get_go(get_suffix_link(id), c);
        }
      }
      if (id == 0) {
        nodes[id].terminal_link = -1;
      } else if (nodes[nodes[id].suffix_link].is_terminal) {
        nodes[id].terminal_link = nodes[id].suffix_link;
      } else {
        nodes[id].terminal_link = get_terminal_link(get_suffix_link(id));
      }
      for (int c = 0; c < MAXC; c++) {
        if (nodes[id].next[c] != -1) {
          q.push(nodes[id].next[c]);
        }
      }
    }
  }
  int get_suffix_link(int id) { return nodes[id].suffix_link; }
  int get_go(int id, char chr) { return get_go(id, convert_char(chr)); }
  int get_go(int id, int c) { return nodes[id].go[c]; }
  int get_terminal_link(int id) { return nodes[id].terminal_link; }
};
const int N = 400005, FN = 2 * N;
vector<pair<int, char>> g[N];
int query_ids[N];
vector<int> query_indices[N];
int result[N];
Trie trie(N);
vector<int> g_suffix[N];
int tin[N], tout[N], t = 1, fenwick[FN];
void add(int i, int v) {
  while (i < FN) {
    fenwick[i] += v;
    i |= i + 1;
  }
}
int sum(int i) {
  int result = 0;
  while (i >= 0) {
    result += fenwick[i];
    i = (i & (i + 1)) - 1;
  }
  return result;
}
void dfs_suffix(int u) {
  tin[u] = t++;
  for (int v : g_suffix[u]) {
    dfs_suffix(v);
  }
  tout[u] = t++;
}
void add_to_path(int id, int v) { add(tin[id], v); }
int get_occs(int id) { return sum(tout[id]) - sum(tin[id] - 1); }
void dfs(int song_id, int node_id) {
  add_to_path(node_id, 1);
  for (int i : query_indices[song_id]) {
    result[i] = get_occs(query_ids[i]);
  }
  for (const auto &p : g[song_id]) {
    int next_song_id = p.first;
    char chr = p.second;
    int next_node_id = trie.get_go(node_id, chr);
    dfs(next_song_id, next_node_id);
  }
  add_to_path(node_id, -1);
}
void solve() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int t, j;
    char c;
    cin >> t;
    if (t == 1) {
      cin >> c;
      j = 0;
    } else {
      cin >> j >> c;
    }
    g[j].emplace_back(i, c);
  }
  int m;
  cin >> m;
  for (int i = 0; i < m; i++) {
    int song_id;
    string query;
    cin >> song_id >> query;
    query_ids[i] = trie.add(query);
    query_indices[song_id].push_back(i);
  }
  trie.build();
  for (int i = 1; i < (int)trie.nodes.size(); i++) {
    g_suffix[trie.nodes[i].suffix_link].push_back(i);
  }
  dfs_suffix(0);
  dfs(0, 0);
  for (int i = 0; i < m; i++) {
    cout << result[i] << "\n";
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  solve();
}

```


### Prompt

In CPP, your task is to solve the following problem:

    per nextum in unam tum XI conscribementis fac sic
        vestibulo perlegementum da varo.
        morde varo.
        seqis cumula varum.
    cis

    per nextum in unam tum XI conscribementis fac sic
        seqis decumulamenta da varo.
        varum privamentum fodementum da aresulto.
        varum tum III elevamentum tum V multiplicamentum da bresulto.
        aresultum tum bresultum addementum da resulto.

        si CD tum resultum non praestantiam fac sic
            dictum sic f(%d) = %.2f cis tum varum tum resultum egresso describe.
            novumversum egresso scribe.
        cis
        si CD tum resultum praestantiam fac sic
            dictum sic f(%d) = MAGNA NIMIS! cis tum varum egresso describe.
            novumversum egresso scribe.
        cis
    cis

Input

The input consists of several integers, one per line. Each integer is between -50 and 50, inclusive.

Output

As described in the problem statement.

Example

Input

0
1
-2
-3
-4
-5
-6
-7
-8
-9
10

Output

f(10) = MAGNA NIMIS!
f(-9) = -3642.00
f(-8) = -2557.17
f(-7) = -1712.35
f(-6) = -1077.55
f(-5) = -622.76
f(-4) = -318.00
f(-3) = -133.27
f(-2) = -38.59
f(1) = 6.00
f(0) = 0.00

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
int first;
double a;
vector<int> v;
int main() {
  while (scanf("%d", &first) != EOF) {
    v.push_back(first);
  }
  for (int i = v.size() - 1; i >= 0; i--) {
    a = v[i] * v[i] * v[i] * 5 + sqrt(abs(v[i]));
    if (a > 400)
      printf("f(%d) = MAGNA NIMIS!\n", v[i]);
    else
      printf("f(%d) = %.2lf\n", v[i], a);
  }
  return 0;
}

```


### Prompt

Your task is to create a cpp solution to the following problem:

Rumors say that one of Kamal-ol-molk's paintings has been altered. A rectangular brush has been moved right and down on the painting.

Consider the painting as a n × m rectangular grid. At the beginning an x × y rectangular brush is placed somewhere in the frame, with edges parallel to the frame, (1 ≤ x ≤ n, 1 ≤ y ≤ m). Then the brush is moved several times. Each time the brush is moved one unit right or down. The brush has been strictly inside the frame during the painting. The brush alters every cell it has covered at some moment.

You have found one of the old Kamal-ol-molk's paintings. You want to know if it's possible that it has been altered in described manner. If yes, you also want to know minimum possible area of the brush.

Input

The first line of input contains two integers n and m, (1 ≤ n, m ≤ 1000), denoting the height and width of the painting.

The next n lines contain the painting. Each line has m characters. Character 'X' denotes an altered cell, otherwise it's showed by '.'. There will be at least one altered cell in the painting.

Output

Print the minimum area of the brush in a line, if the painting is possibly altered, otherwise print  - 1.

Examples

Input

4 4
XX..
XX..
XXXX
XXXX

Output

4

Input

4 4
....
.XXX
.XXX
....

Output

2

Input

4 5
XXXX.
XXXX.
.XX..
.XX..

Output

-1

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
string s[1000];
int a[1000], b[1000];
char S[1000][1000];
int main() {
  int n, m;
  cin >> n >> m;
  for (int(i) = (0); i < (n); ++(i)) cin >> s[i];
  for (int(i) = (0); i < (n); ++(i)) {
    int p = 0;
    for (int(j) = (0); j < (m); ++(j)) {
      if (s[i][j] == '.')
        p = 0;
      else {
        if (a[i] == 0 || p) {
          a[i]++;
          p = 1;
        } else {
          cout << -1;
          return 0;
        }
      }
    }
  }
  for (int(i) = (0); i < (m); ++(i)) {
    int p = 0;
    for (int(j) = (0); j < (n); ++(j)) {
      if (s[j][i] == '.')
        p = 0;
      else {
        if (b[i] == 0 || p) {
          b[i]++;
          p = 1;
        } else {
          cout << -1;
          return 0;
        }
      }
    }
  }
  int xx = 0, yy = 0, b1 = 1, b2 = 1, ans1 = 10000000, ans2 = 10000000;
  while (!a[xx]) xx++;
  while (!b[yy]) yy++;
  int Axx = a[xx], Bxx = b[yy];
  if (s[xx][yy] == '.') {
    cout << -1;
    return 0;
  }
  for (int(i) = (0); i < (n); ++(i))
    if (a[i] && a[i] < a[xx]) b1 = 0;
  for (int(i) = (0); i < (m); ++(i))
    if (b[i] && b[i] < b[yy]) b2 = 0;
  if (b1) {
    int l[1000], r[1000], ll[1000], rr[1000];
    int P1 = xx, P2 = yy + Axx - 1;
    l[P1] = r[P1] = P2;
    while (true) {
      int k = 0;
      if (P2 + 1 < m && s[P1][P2 + 1] == 'X') {
        k = 1;
        P2++;
        r[P1] = P2;
      } else if (P1 + 1 < n && s[P1 + 1][P2] == 'X') {
        k = 1;
        P1++;
        l[P1] = r[P1] = P2;
      }
      if (!k) break;
    }
    int Q1 = xx, Q2 = yy;
    ll[Q1] = rr[Q1] = Q2;
    while (true) {
      int k = 0;
      if (Q1 + 1 < n && s[Q1 + 1][Q2] == 'X') {
        k = 1;
        Q1++;
        ll[Q1] = rr[Q1] = Q2;
      } else if (Q2 + 1 < m && s[Q1][Q2 + 1] == 'X') {
        k = 1;
        Q2++;
        rr[Q1] = Q2;
      }
      if (!k) break;
    }
    int len_m = 1, len_M = 1;
    while (len_M <= P1 && r[P1 - len_M] == P2) len_M++;
    while (len_m != len_M) {
      int QWE = (len_m + len_M) / 2, f = 0;
      for (int(i) = (xx); i < (P1 + 2 - QWE); ++(i))
        if (ll[i + QWE - 1] < l[i] - Axx + 1) f = 1;
      if (f)
        len_m = QWE + 1;
      else
        len_M = QWE;
    }
    int ok = 1;
    for (int(i) = (0); i < (len_M); ++(i))
      if (ll[xx + i] != l[xx] - Axx + 1) ok = 0;
    for (int(i) = (xx); i < (P1 + 2 - len_M); ++(i))
      if (ll[i + len_M - 1] != l[i] - Axx + 1) ok = 0;
    for (int(i) = (P1 + 1); i < (n); ++(i))
      if (a[i]) ok = 0;
    if (ok) ans1 = Axx * len_M;
  }
  for (int(i) = (0); i < (n); ++(i))
    for (int(j) = (0); j < (m); ++(j)) S[j][i] = s[i][j];
  swap(n, m);
  Axx = Bxx;
  swap(xx, yy);
  if (b2) {
    int l[1000], r[1000], ll[1000], rr[1000];
    int P1 = xx, P2 = yy + Axx - 1;
    l[P1] = r[P1] = P2;
    while (true) {
      int k = 0;
      if (P2 + 1 < m && S[P1][P2 + 1] == 'X') {
        k = 1;
        P2++;
        r[P1] = P2;
      } else if (P1 + 1 < n && S[P1 + 1][P2] == 'X') {
        k = 1;
        P1++;
        l[P1] = r[P1] = P2;
      }
      if (!k) break;
    }
    int Q1 = xx, Q2 = yy;
    ll[Q1] = rr[Q1] = Q2;
    while (true) {
      int k = 0;
      if (Q1 + 1 < n && S[Q1 + 1][Q2] == 'X') {
        k = 1;
        Q1++;
        ll[Q1] = rr[Q1] = Q2;
      } else if (Q2 + 1 < m && S[Q1][Q2 + 1] == 'X') {
        k = 1;
        Q2++;
        rr[Q1] = Q2;
      }
      if (!k) break;
    }
    int len_m = 1, len_M = 1;
    while (len_M <= P1 && r[P1 - len_M] == P2) len_M++;
    while (len_m != len_M) {
      int QWE = (len_m + len_M) / 2, f = 0;
      for (int(i) = (xx); i < (P1 + 2 - QWE); ++(i))
        if (ll[i + QWE - 1] < l[i] - Axx + 1) f = 1;
      if (f)
        len_m = QWE + 1;
      else
        len_M = QWE;
    }
    int ok = 1;
    for (int(i) = (0); i < (len_M); ++(i))
      if (ll[xx + i] != l[xx] - Axx + 1) ok = 0;
    for (int(i) = (xx); i < (P1 + 2 - len_M); ++(i))
      if (ll[i + len_M - 1] != l[i] - Axx + 1) ok = 0;
    for (int(i) = (P1 + 1); i < (n); ++(i))
      if (b[i]) ok = 0;
    if (ok) ans2 = Axx * len_M;
  }
  if (min(ans1, ans2) > n * m) {
    cout << -1;
    return 0;
  } else
    cout << min(ans1, ans2);
}

```


### Prompt

Generate a cpp solution to the following problem:

Fox Ciel has n boxes in her room. They have the same size and weight, but they might have different strength. The i-th box can hold at most xi boxes on its top (we'll call xi the strength of the box).

Since all the boxes have the same size, Ciel cannot put more than one box directly on the top of some box. For example, imagine Ciel has three boxes: the first has strength 2, the second has strength 1 and the third has strength 1. She cannot put the second and the third box simultaneously directly on the top of the first one. But she can put the second box directly on the top of the first one, and then the third box directly on the top of the second one. We will call such a construction of boxes a pile.

<image>

Fox Ciel wants to construct piles from all the boxes. Each pile will contain some boxes from top to bottom, and there cannot be more than xi boxes on the top of i-th box. What is the minimal number of piles she needs to construct?

Input

The first line contains an integer n (1 ≤ n ≤ 100). The next line contains n integers x1, x2, ..., xn (0 ≤ xi ≤ 100).

Output

Output a single integer — the minimal possible number of piles.

Examples

Input

3
0 0 10

Output

2

Input

5
0 1 2 3 4

Output

1

Input

4
0 0 0 0

Output

4

Input

9
0 1 0 2 0 1 1 2 10

Output

3

Note

In example 1, one optimal way is to build 2 piles: the first pile contains boxes 1 and 3 (from top to bottom), the second pile contains only box 2.

<image>

In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom).

<image>

### Response

```cpp
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:640000000")
using namespace std;
const double eps = 1e-9;
const double pi = acos(-1.0);
template <class T>
T sqr(T x) {
  return x * x;
}
int n;
int a[150];
int b[150];
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  sort(a, a + n);
  reverse(a, a + n);
  int lf = 0, rg = n;
  while (rg - lf > 1) {
    int md = (lf + rg) / 2;
    for (int i = 0; i < md; i++) {
      b[i] = a[i];
    }
    bool ok = true;
    for (int i = md, j = 0; i < n; i++) {
      int cnt = 0;
      j = i % md;
      while (cnt < md && b[(j + cnt) % md] == 0) cnt++;
      if (cnt < md) {
        b[(j + cnt) % md] = min(b[(j + cnt) % md] - 1, a[i]);
      } else {
        ok = false;
        break;
      }
    }
    if (ok)
      rg = md;
    else
      lf = md;
  }
  cout << rg << endl;
  return 0;
}

```


### Prompt

Construct a Cpp code solution to the problem outlined:

You are given two positive integers a and b. In one move you can increase a by 1 (replace a with a+1). Your task is to find the minimum number of moves you need to do in order to make a divisible by b. It is possible, that you have to make 0 moves, as a is already divisible by b. You have to answer t independent test cases.

Input

The first line of the input contains one integer t (1 ≤ t ≤ 10^4) — the number of test cases. Then t test cases follow.

The only line of the test case contains two integers a and b (1 ≤ a, b ≤ 10^9).

Output

For each test case print the answer — the minimum number of moves you need to do in order to make a divisible by b.

Example

Input

5
10 4
13 9
100 13
123 456
92 46

Output

2
5
4
333
0

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
  int t, a, b, i;
  cin >> t;
  for (i = 0; i < t; i++) {
    cin >> a >> b;
    if (a % b == 0)
      cout << 0 << endl;
    else
      cout << b - a % b << endl;
  }
}

```


### Prompt

Please formulate a cpp solution to the following problem:

Some number of chocolate pieces were prepared for a training camp. The camp had N participants and lasted for D days. The i-th participant (1 \leq i \leq N) ate one chocolate piece on each of the following days in the camp: the 1-st day, the (A_i + 1)-th day, the (2A_i + 1)-th day, and so on. As a result, there were X chocolate pieces remaining at the end of the camp. During the camp, nobody except the participants ate chocolate pieces.

Find the number of chocolate pieces prepared at the beginning of the camp.

Constraints

* 1 \leq N \leq 100
* 1 \leq D \leq 100
* 1 \leq X \leq 100
* 1 \leq A_i \leq 100 (1 \leq i \leq N)
* All input values are integers.

Input

Input is given from Standard Input in the following format:

N
D X
A_1
A_2
:
A_N

Output

Find the number of chocolate pieces prepared at the beginning of the camp.

Examples

Input

3
7 1
2
5
10

Output

8

Input

2
8 20
1
10

Output

29

Input

5
30 44
26
18
81
18
6

Output

56

### Response

```cpp
#include<cstdio>
int main()
{
	int n,d,x,ans=0;scanf("%d%d%d",&n,&d,&x);
	for(int i=1;i<=n;i++)
	{
		int a;scanf("%d",&a);
		ans+=(d-1)/a+1;
	}
	printf("%d\n",ans+x);
	return 0;
}
```


### Prompt

Generate a cpp solution to the following problem:

You are given an array of integers. Vasya can permute (change order) its integers. He wants to do it so that as many as possible integers will become on a place where a smaller integer used to stand. Help Vasya find the maximal number of such integers.

For instance, if we are given an array [10, 20, 30, 40], we can permute it so that it becomes [20, 40, 10, 30]. Then on the first and the second positions the integers became larger (20>10, 40>20) and did not on the third and the fourth, so for this permutation, the number that Vasya wants to maximize equals 2. Read the note for the first example, there is one more demonstrative test case.

Help Vasya to permute integers in such way that the number of positions in a new array, where integers are greater than in the original one, is maximal.

Input

The first line contains a single integer n (1 ≤ n ≤ 10^5) — the length of the array.

The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9) — the elements of the array.

Output

Print a single integer — the maximal number of the array's elements which after a permutation will stand on the position where a smaller element stood in the initial array.

Examples

Input

7
10 1 1 1 5 5 3

Output

4

Input

5
1 1 1 1 1

Output

0

Note

In the first sample, one of the best permutations is [1, 5, 5, 3, 10, 1, 1]. On the positions from second to fifth the elements became larger, so the answer for this permutation is 4.

In the second sample, there is no way to increase any element with a permutation, so the answer is 0.

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
const double PI = 3.14159265358979323846;
const double eps = (1e-12);
const int MOD = 1000000007;
int dcmp(double x, double y) { return fabs(x - y) <= eps ? 0 : x > y ? -1 : 1; }
int main() {
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> a(n);
  multiset<int> s;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    s.insert(a[i]);
  }
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    auto it = s.upper_bound(a[i]);
    if (it != s.end()) {
      ans++;
      s.erase(it);
    }
  }
  cout << ans << endl;
  return 0;
}

```


### Prompt

In cpp, your task is to solve the following problem:

You might have remembered Theatre square from the [problem 1A](https://codeforces.com/problemset/problem/1/A). Now it's finally getting repaved.

The square still has a rectangular shape of n × m meters. However, the picture is about to get more complicated now. Let a_{i,j} be the j-th square in the i-th row of the pavement.

You are given the picture of the squares:

  * if a_{i,j} =  "*", then the j-th square in the i-th row should be black;
  * if a_{i,j} =  ".", then the j-th square in the i-th row should be white.

The black squares are paved already. You have to pave the white squares. There are two options for pavement tiles:

  * 1 × 1 tiles — each tile costs x burles and covers exactly 1 square;
  * 1 × 2 tiles — each tile costs y burles and covers exactly 2 adjacent squares of the same row. Note that you are not allowed to rotate these tiles or cut them into 1 × 1 tiles.

You should cover all the white squares, no two tiles should overlap and no black squares should be covered by tiles.

What is the smallest total price of the tiles needed to cover all the white squares?

Input

The first line contains a single integer t (1 ≤ t ≤ 500) — the number of testcases. Then the description of t testcases follow.

The first line of each testcase contains four integers n, m, x and y (1 ≤ n ≤ 100; 1 ≤ m ≤ 1000; 1 ≤ x, y ≤ 1000) — the size of the Theatre square, the price of the 1 × 1 tile and the price of the 1 × 2 tile.

Each of the next n lines contains m characters. The j-th character in the i-th line is a_{i,j}. If a_{i,j} =  "*", then the j-th square in the i-th row should be black, and if a_{i,j} =  ".", then the j-th square in the i-th row should be white.

It's guaranteed that the sum of n × m over all testcases doesn't exceed 10^5.

Output

For each testcase print a single integer — the smallest total price of the tiles needed to cover all the white squares in burles.

Example

Input

4
1 1 10 1
.
1 2 10 1
..
2 1 10 1
.
.
3 3 3 7
..*
*..
.*.

Output

10
1
20
18

Note

In the first testcase you are required to use a single 1 × 1 tile, even though 1 × 2 tile is cheaper. So the total price is 10 burles.

In the second testcase you can either use two 1 × 1 tiles and spend 20 burles or use a single 1 × 2 tile and spend 1 burle. The second option is cheaper, thus the answer is 1.

The third testcase shows that you can't rotate 1 × 2 tiles. You still have to use two 1 × 1 tiles for the total price of 20.

In the fourth testcase the cheapest way is to use 1 × 1 tiles everywhere. The total cost is 6 ⋅ 3 = 18.

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int t;
  cin >> t;
  while (t--) {
    long long int n, m, a, b;
    cin >> n >> m >> a >> b;
    long long int sum1 = 0, sum2 = 0;
    char d[n][m];
    for (int i = 0; i < n; i++) {
      int j = 0;
      for (j = 0; j < m; j++) {
        cin >> d[i][j];
      }
    }
    for (int i = 0; i < n; i++) {
      int j = 0;
      for (j = 0; j < m - 1; j++) {
        if (d[i][j] == '.') {
          if (d[i][j + 1] == '.') {
            sum1 += 2 * a;
            sum2 += b;
            j++;
          } else {
            sum1 += a;
            sum2 += a;
            j++;
          }
        }
      }
      if (m > 1) {
        if (d[i][m - 2] == '*' || j == m - 1) {
          if (d[i][m - 1] == '.') {
            sum1 += a;
            sum2 += a;
          }
        }
      } else {
        if (d[i][m - 1] == '.') {
          sum1 += a;
          sum2 += a;
        }
      }
    }
    cout << min(sum1, sum2) << endl;
  }
  return 0;
}

```


### Prompt

Create a solution in CPP for the following problem:

An elephant decided to visit his friend. It turned out that the elephant's house is located at point 0 and his friend's house is located at point x(x > 0) of the coordinate line. In one step the elephant can move 1, 2, 3, 4 or 5 positions forward. Determine, what is the minimum number of steps he need to make in order to get to his friend's house.

Input

The first line of the input contains an integer x (1 ≤ x ≤ 1 000 000) — The coordinate of the friend's house.

Output

Print the minimum number of steps that elephant needs to make to get from point 0 to point x.

Examples

Input

5

Output

1

Input

12

Output

3

Note

In the first sample the elephant needs to make one step of length 5 to reach the point x.

In the second sample the elephant can get to point x if he moves by 3, 5 and 4. There are other ways to get the optimal answer but the elephant cannot reach x in less than three moves.

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  if (n == 5) {
    cout << 1;
    return 0;
  }
  if (n % 5 == 0) {
    cout << (n / 5) << endl;
    return 0;
  }
  int x = ((n / 5) + 1);
  cout << x;
  return 0;
}

```


### Prompt

Please create a solution in Cpp to the following problem:

Your search for Heidi is over – you finally found her at a library, dressed up as a human. In fact, she has spent so much time there that she now runs the place! Her job is to buy books and keep them at the library so that people can borrow and read them. There are n different books, numbered 1 through n.

We will look at the library's operation during n consecutive days. Heidi knows in advance that on the i-th day (1 ≤ i ≤ n) precisely one person will come to the library, request to borrow the book ai, read it in a few hours, and return the book later on the same day.

Heidi desperately wants to please all her guests, so she will make sure to always have the book ai available in the library on the i-th day. During the night before the i-th day, she has the option of going to the bookstore (which operates at nights to avoid competition with the library) and buying any book for the price of 1 CHF. Of course, if she already has a book at the library, she does not need to buy it again. Initially, the library contains no books.

There is a problem, though. The capacity of the library is k – this means that at any time, there can be at most k books at the library. If buying a new book would cause Heidi to have more than k books, she must first get rid of some book that she already has, in order to make room for the new book. If she later needs a book that she got rid of, she will need to buy that book again.

You are given k and the sequence of requests for books a1, a2, ..., an. What is the minimum cost (in CHF) of buying new books to satisfy all the requests?

Input

The first line of input will contain two integers n and k (1 ≤ n, k ≤ 80). The second line will contain n integers a1, a2, ..., an (1 ≤ ai ≤ n) – the sequence of book requests.

Output

On a single line print the minimum cost of buying books at the store so as to satisfy all requests.

Examples

Input

4 80
1 2 2 1

Output

2

Input

4 1
1 2 2 1

Output

3

Input

4 2
1 2 3 1

Output

3

Note

In the first test case, Heidi is able to keep all books forever. Therefore, she only needs to buy the book 1 before the first day and the book 2 before the second day.

In the second test case, she can only keep one book at a time. Therefore she will need to buy new books on the first, second and fourth day.

In the third test case, before buying book 3 on the third day, she must decide which of the books 1 and 2 she should get rid of. Of course, she should keep the book 1, which will be requested on the fourth day.

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
int IN() {
  int c, f, x;
  while (!isdigit(c = getchar()) && c != '-')
    ;
  c == '-' ? (f = 1, x = 0) : (f = 0, x = c - '0');
  while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + c - '0';
  return !f ? x : -x;
}
const int N = 400000 + 19;
int nxt[N], A[N], B[N], vis[N];
int n, k, res;
multiset<int> S;
int main() {
  n = IN(), k = IN();
  for (int i = 1; i < n + 1; i++) A[i] = IN();
  for (int i = 1; i < n + 1; i++) B[i] = n + 1;
  for (int i = n; i; i--) {
    nxt[i] = B[A[i]];
    B[A[i]] = i;
  }
  for (int i = 1; i < n + 1; i++) {
    if (!vis[A[i]]) {
      res++;
      if (S.size() == k) {
        int x = *--S.end();
        S.erase(S.find(x));
        vis[A[x]] = 0;
      }
      S.insert(nxt[i]);
      vis[A[i]] = 1;
    } else {
      S.erase(S.find(i));
      S.insert(nxt[i]);
    }
  }
  printf("%d\n", res);
}

```


### Prompt

Construct a cpp code solution to the problem outlined:

A correct expression of the form a+b=c was written; a, b and c are non-negative integers without leading zeros. In this expression, the plus and equally signs were lost. The task is to restore the expression. In other words, one character '+' and one character '=' should be inserted into given sequence of digits so that:

  * character'+' is placed on the left of character '=',
  * characters '+' and '=' split the sequence into three non-empty subsequences consisting of digits (let's call the left part a, the middle part — b and the right part — c),
  * all the three parts a, b and c do not contain leading zeros,
  * it is true that a+b=c.

It is guaranteed that in given tests answer always exists.

Input

The first line contains a non-empty string consisting of digits. The length of the string does not exceed 106.

Output

Output the restored expression. If there are several solutions, you can print any of them.

Note that the answer at first should contain two terms (divided with symbol '+'), and then the result of their addition, before which symbol'=' should be.

Do not separate numbers and operation signs with spaces. Strictly follow the output format given in the examples.

If you remove symbol '+' and symbol '=' from answer string you should get a string, same as string from the input data.

Examples

Input

12345168

Output

123+45=168

Input

099

Output

0+9=9

Input

199100

Output

1+99=100

Input

123123123456456456579579579

Output

123123123+456456456=579579579

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
const int mod1 = 1e9 + 7;
const int mod2 = mod1 + 2;
const int e = 10;
string s;
long long h1[1000000], h2[1000000];
long long e_pow1[1000000], e_pow2[1000000];
long long roll(long long h, int e, int val, int mod) {
  h *= e;
  if (h >= mod) h %= mod;
  h += val;
  if (h >= mod) h %= mod;
  return h;
}
long long subtract(long long a, long long b, int mod) {
  if (b >= mod) b %= mod;
  if (a < b) a += mod;
  a -= b;
  if (a >= mod) a %= mod;
  return a;
}
bool check(int p1, int p2) {
  int a = p1 + 1;
  int c = s.size() - p2 - 1;
  int b = p2 - p1;
  if (a < 1 || b < 1 || c < 1) return false;
  if (c < a || c < b) return false;
  if (b > 1 && s[p1 + 1] == '0') return false;
  if (c > 1 && s[p2 + 1] == '0') return false;
  long long a1 = h1[p1];
  long long a2 = h2[p1];
  long long b1 = subtract(h1[p2], roll(a1, e_pow1[b], 0, mod1), mod1);
  long long b2 = subtract(h2[p2], roll(a2, e_pow2[b], 0, mod2), mod2);
  long long c1 = subtract(
      h1[s.size() - 1], roll(h1[p2], e_pow1[s.size() - 1 - p2], 0, mod1), mod1);
  long long c2 = subtract(
      h2[s.size() - 1], roll(h2[p2], e_pow2[s.size() - 1 - p2], 0, mod2), mod2);
  if (c1 == (b1 + a1) % mod1 && c2 == (b2 + a2) % mod2) return true;
  return false;
}
void print(int p1, int p2) {
  for (int i = 0; i < s.size(); i++) {
    cout << s[i];
    if (i == p1) cout << "+";
    if (i == p2) cout << "=";
  }
}
int main() {
  int i;
  int p1, p2;
  cin >> s;
  e_pow1[0] = e_pow2[0] = 1;
  h1[0] = h2[0] = s[0] - '0';
  for (i = 1; i < s.size(); i++) {
    e_pow1[i] = roll(e_pow1[i - 1], e, 0, mod1);
    e_pow2[i] = roll(e_pow2[i - 1], e, 0, mod2);
    h1[i] = roll(h1[i - 1], e, s[i] - '0', mod1);
    h2[i] = roll(h2[i - 1], e, s[i] - '0', mod2);
  }
  for (i = 1; i < s.size() - 1; i++) {
    p2 = s.size() - i - 1;
    p1 = i - 2;
    if (check(p1, p2)) {
      print(p1, p2);
      return 0;
    }
    p1 = i - 1;
    if (check(p1, p2)) {
      print(p1, p2);
      return 0;
    }
    p1 = p2 - i;
    if (check(p1, p2)) {
      print(p1, p2);
      return 0;
    }
    p1 = p2 - i + 1;
    if (check(p1, p2)) {
      print(p1, p2);
      return 0;
    }
  }
  return 0;
}

```


### Prompt

Create a solution in CPP for the following problem:

Let's call beauty of an array b_1, b_2, …, b_n (n > 1) — min_{1 ≤ i < j ≤ n} |b_i - b_j|.

You're given an array a_1, a_2, … a_n and a number k. Calculate the sum of beauty over all subsequences of the array of length exactly k. As this number can be very large, output it modulo 998244353.

A sequence a is a subsequence of an array b if a can be obtained from b by deletion of several (possibly, zero or all) elements.

Input

The first line contains integers n, k (2 ≤ k ≤ n ≤ 1000).

The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^5).

Output

Output one integer — the sum of beauty over all subsequences of the array of length exactly k. As this number can be very large, output it modulo 998244353.

Examples

Input

4 3
1 7 3 5

Output

8

Input

5 5
1 10 100 1000 10000

Output

9

Note

In the first example, there are 4 subsequences of length 3 — [1, 7, 3], [1, 3, 5], [7, 3, 5], [1, 7, 5], each of which has beauty 2, so answer is 8.

In the second example, there is only one subsequence of length 5 — the whole array, which has the beauty equal to |10-1| = 9.

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
class SegTree {
 public:
  vector<long long> A;
  vector<long long> st;
  int n;
  int left(int i) { return (i << 1); }
  int right(int i) { return (i << 1) + 1; }
  void build(int p, int L, int R) {
    if (L == R)
      st[p] = L;
    else {
      build(left(p), L, (L + R) / 2);
      build(right(p), (L + R) / 2 + 1, R);
      int p1 = st[left(p)];
      int p2 = st[right(p)];
      st[p] = A[p1] <= A[p2] ? p1 : p2;
    }
  }
  SegTree(const vector<long long> &_A) {
    A = _A;
    n = _A.size();
    st.assign(4 * n, -1);
    build(1, 0, n - 1);
  }
  int rmq(int p, int L, int R, int i, int j) {
    if (i > R || j < L) return -1;
    if (L >= i && R <= j) return st[p];
    int p1 = rmq(left(p), L, (L + R) / 2, i, j);
    int p2 = rmq(right(p), (L + R) / 2 + 1, R, i, j);
    if (p1 == -1) return p2;
    if (p2 == -1) return p1;
    return (A[p1] <= A[p2] ? p1 : p2);
  }
  int rmq(int i, int j) {
    if (i < 0) i = 0;
    return rmq(1, 0, n - 1, i, j);
  }
  int getIndex(int p, int L, int R, int pos) {
    if (L == R) return p;
    if (pos <= (L + R) / 2) return getIndex(left(p), L, (L + R) / 2, pos);
    return getIndex(right(p), (L + R) / 2 + 1, R, pos);
  }
  void update(int pos, int value) {
    A[pos] = value;
    pos = getIndex(1, 0, n - 1, pos);
    pos /= 2;
    while (pos != 0) {
      int p1 = st[left(pos)];
      int p2 = st[right(pos)];
      if (p1 >= 0 && p2 >= 0) st[pos] = A[p1] <= A[p2] ? p1 : p2;
      pos /= 2;
    }
  }
};
class UnionFind {
 public:
  vector<int> rank;
  vector<pair<int, int> > p;
  UnionFind(int N) {
    rank.assign(N, 0);
    p.assign(N, pair<int, int>(0, 1));
    for (int i = 0; i < N; i++) p[i].first = i;
  }
  int findSet(int i) {
    return (p[i].first == i) ? i : (p[i].first = findSet(p[i].first));
  }
  bool isSameSet(int i, int j) { return findSet(i) == findSet(j); }
  void unionSet(int i, int j) {
    if (!isSameSet(i, j)) {
      int x = findSet(i), y = findSet(j);
      if (rank[x] > rank[y]) {
        p[y].first = x;
        p[x].second += p[y].second;
      } else {
        p[x].first = y;
        p[y].second += p[x].second;
        if (rank[x] == rank[y]) rank[y]++;
      }
    }
  }
  int getMaxId() {
    int maxIn = 0;
    for (int i = 0; i < p.size(); i++)
      if (p[i].second > p[maxIn].second) maxIn = i;
    return maxIn;
  }
  int getNum() {
    set<int> par;
    for (int i = 0; i < p.size(); i++) par.insert(p[i].first);
    return par.size();
  }
};
int main() {
  long long n, k;
  long long D = 998244353;
  cin >> n >> k;
  long long arr[n];
  for (int i = 0; i < n; i++) cin >> arr[i];
  sort(arr, arr + n);
  long long mxBu = arr[n - 1] / (k - 1);
  mxBu++;
  long long res = 0;
  long long resA[mxBu];
  for (int i = 1; i < mxBu; i++) {
    long long dp[n][k + 1];
    for (int j = 0; j < n; j++)
      for (int t = 0; t <= k; t++) dp[j][t] = 0;
    for (int j = 1, l = 0; j < n; j++) {
      while (arr[j] - arr[l] >= i && l < j) l++;
      dp[j][2] = (l + dp[j - 1][2]) % D;
      l--;
      l = max(0, l);
      for (int t = 3; t <= k && t <= (j + 1); t++) {
        while (arr[j] - arr[l] >= i && l < j) l++;
        l--;
        l = max(0, l);
        dp[j][t] = dp[j - 1][t];
        if (arr[j] - arr[l] >= i) dp[j][t] += dp[l][t - 1];
        dp[j][t] %= D;
      }
    }
    resA[i] = dp[n - 1][k];
  }
  for (int i = 1; i < mxBu - 1; i++) {
    res += (resA[i] - resA[i + 1]) * i;
    res %= D;
  }
  res += resA[mxBu - 1] * (mxBu - 1);
  res %= D;
  cout << res << endl;
}

```


### Prompt

Your task is to create a CPP solution to the following problem:

In the town of Aalam-Aara (meaning the Light of the Earth), previously there was no crime, no criminals but as the time progressed, sins started creeping into the hearts of once righteous people. Seeking solution to the problem, some of the elders found that as long as the corrupted part of population was kept away from the uncorrupted part, the crimes could be stopped. So, they are trying to set up a compound where they can keep the corrupted people. To ensure that the criminals don't escape the compound, a watchtower needs to be set up, so that they can be watched.

Since the people of Aalam-Aara aren't very rich, they met up with a merchant from some rich town who agreed to sell them a land-plot which has already a straight line fence AB along which a few points are set up where they can put up a watchtower. Your task is to help them find out the number of points on that fence where the tower can be put up, so that all the criminals can be watched from there. Only one watchtower can be set up. A criminal is watchable from the watchtower if the line of visibility from the watchtower to him doesn't cross the plot-edges at any point between him and the tower i.e. as shown in figure 1 below, points X, Y, C and A are visible from point B but the points E and D are not.

<image> Figure 1  <image> Figure 2

Assume that the land plot is in the shape of a polygon and coordinate axes have been setup such that the fence AB is parallel to x-axis and the points where the watchtower can be set up are the integer points on the line. For example, in given figure 2, watchtower can be setup on any of five integer points on AB i.e. (4, 8), (5, 8), (6, 8), (7, 8) or (8, 8). You can assume that no three consecutive points are collinear and all the corner points other than A and B, lie towards same side of fence AB. The given polygon doesn't contain self-intersections.

Input

The first line of the test case will consist of the number of vertices n (3 ≤ n ≤ 1000).

Next n lines will contain the coordinates of the vertices in the clockwise order of the polygon. On the i-th line are integers xi and yi (0 ≤ xi, yi ≤ 106) separated by a space.

The endpoints of the fence AB are the first two points, (x1, y1) and (x2, y2).

Output

Output consists of a single line containing the number of points where the watchtower can be set up.

Examples

Input

5
4 8
8 8
9 4
4 0
0 4

Output

5

Input

5
4 8
5 8
5 4
7 4
2 2

Output

0

Note

Figure 2 shows the first test case. All the points in the figure are watchable from any point on fence AB. Since, AB has 5 integer coordinates, so answer is 5.

For case two, fence CD and DE are not completely visible, thus answer is 0.

### Response

```cpp
#include <bits/stdc++.h>
int n, x[1005], y[1005];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d%d", &x[i], &y[i]);
  double L = x[1], R = x[2], w;
  if (y[3] > y[1]) L = x[2], R = x[1];
  for (int i = 3; i < n; i++) {
    if (y[i] == y[i + 1])
      w = 1e15 * ((x[i] < x[i + 1]) ^ (y[3] < y[1]) ? 1 : -1);
    else
      w = x[i] + 1. * (x[i] - x[i + 1]) / (y[i] - y[i + 1]) * (y[1] - y[i]);
    if ((y[i] < y[i + 1]))
      L = w > L ? w : L;
    else
      R = w < R ? w : R;
  }
  if (L > R + 1e-8)
    printf("0");
  else
    printf("%d\n", int(R + 1 + 1e-8) - int(L + 1 - 1e-8));
  return 0;
}

```


### Prompt

Create a solution in Cpp for the following problem:

Two pirates Polycarpus and Vasily play a very interesting game. They have n chests with coins, the chests are numbered with integers from 1 to n. Chest number i has ai coins.

Polycarpus and Vasily move in turns. Polycarpus moves first. During a move a player is allowed to choose a positive integer x (2·x + 1 ≤ n) and take a coin from each chest with numbers x, 2·x, 2·x + 1. It may turn out that some chest has no coins, in this case the player doesn't take a coin from this chest. The game finishes when all chests get emptied.

Polycarpus isn't a greedy scrooge. Polycarpys is a lazy slob. So he wonders in what minimum number of moves the game can finish. Help Polycarpus, determine the minimum number of moves in which the game can finish. Note that Polycarpus counts not only his moves, he also counts Vasily's moves.

Input

The first line contains a single integer n (1 ≤ n ≤ 100) — the number of chests with coins. The second line contains a sequence of space-separated integers: a1, a2, ..., an (1 ≤ ai ≤ 1000), where ai is the number of coins in the chest number i at the beginning of the game.

Output

Print a single integer — the minimum number of moves needed to finish the game. If no sequence of turns leads to finishing the game, print -1.

Examples

Input

1
1

Output

-1

Input

3
1 2 3

Output

3

Note

In the first test case there isn't a single move that can be made. That's why the players won't be able to empty the chests.

In the second sample there is only one possible move x = 1. This move should be repeated at least 3 times to empty the third chest.

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char *argv[]) {
  int a[111] = {0}, n;
  cin >> n;
  for (int i = 0; i < (int)(n); i++) cin >> a[i + 1];
  int tot = 0;
  for (int i = n; i > 1; i--) {
    int t = i / 2;
    if (2 * t + 1 > n) continue;
    while (a[i]) {
      if (a[2 * t + 1]) a[2 * t + 1]--;
      if (a[2 * t]) a[2 * t]--;
      if (a[t]) a[t]--;
      tot++;
    }
  }
  if (n > 2) {
    tot += a[1];
    a[1] = 0;
  }
  int f = 0;
  for (int i = 1; i <= n; i++) f |= a[i];
  if (f)
    cout << -1 << endl;
  else
    cout << tot << endl;
  return EXIT_SUCCESS;
}

```


### Prompt

Please create a solution in cpp to the following problem:

There are n cards (n is even) in the deck. Each card has a positive integer written on it. n / 2 people will play new card game. At the beginning of the game each player gets two cards, each card is given to exactly one player.

Find the way to distribute cards such that the sum of values written of the cards will be equal for each player. It is guaranteed that it is always possible.

Input

The first line of the input contains integer n (2 ≤ n ≤ 100) — the number of cards in the deck. It is guaranteed that n is even.

The second line contains the sequence of n positive integers a1, a2, ..., an (1 ≤ ai ≤ 100), where ai is equal to the number written on the i-th card.

Output

Print n / 2 pairs of integers, the i-th pair denote the cards that should be given to the i-th player. Each card should be given to exactly one player. Cards are numbered in the order they appear in the input.

It is guaranteed that solution exists. If there are several correct answers, you are allowed to print any of them.

Examples

Input

6
1 5 7 4 4 3

Output

1 3
6 2
4 5

Input

4
10 10 10 10

Output

1 2
3 4

Note

In the first sample, cards are distributed in such a way that each player has the sum of numbers written on his cards equal to 8.

In the second sample, all values ai are equal. Thus, any distribution is acceptable.

### Response

```cpp
#include <bits/stdc++.h>
int main() {
  int n, a[101], i, j, sum = 0, k, a2[101];
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    sum = sum + a[i];
  }
  k = sum / (n / 2);
  for (i = 0; i < n; i++) {
    for (j = 0; j < n; j++) {
      if (i != j && a[i] + a[j] == k) {
        printf("%d %d\n", i + 1, j + 1);
        a[i] = 0;
        a[j] = 0;
      }
    }
  }
  return 0;
}

```


### Prompt

Please create a solution in cpp to the following problem:

You are given an unweighted tree with n vertices. Then n - 1 following operations are applied to the tree. A single operation consists of the following steps:

  1. choose two leaves;
  2. add the length of the simple path between them to the answer;
  3. remove one of the chosen leaves from the tree.

Initial answer (before applying operations) is 0. Obviously after n - 1 such operations the tree will consist of a single vertex.

Calculate the maximal possible answer you can achieve, and construct a sequence of operations that allows you to achieve this answer!

Input

The first line contains one integer number n (2 ≤ n ≤ 2·105) — the number of vertices in the tree.

Next n - 1 lines describe the edges of the tree in form ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi). It is guaranteed that given graph is a tree.

Output

In the first line print one integer number — maximal possible answer.

In the next n - 1 lines print the operations in order of their applying in format ai, bi, ci, where ai, bi — pair of the leaves that are chosen in the current operation (1 ≤ ai, bi ≤ n), ci (1 ≤ ci ≤ n, ci = ai or ci = bi) — choosen leaf that is removed from the tree in the current operation.

See the examples for better understanding.

Examples

Input

3
1 2
1 3

Output

3
2 3 3
2 1 1

Input

5
1 2
1 3
2 4
2 5

Output

9
3 5 5
4 3 3
4 1 1
4 2 2

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
struct node {
  int a, b, c;
  node(int _a, int _b, int _c) : a(_a), b(_b), c(_c) {}
};
vector<int> adj[200005];
int ht[200005], far[200005];
int d[2][200005];
bool del[200005];
int dia, dv1, dv2;
int n;
void diameter(int now, int parent, int& dlen, int& v1, int& v2, int& root) {
  ht[now] = 0;
  far[now] = now;
  for (int child : adj[now]) {
    if (child == parent) continue;
    diameter(child, now, dlen, v1, v2, root);
    if (ht[child] + 1 > ht[now]) {
      ht[now] = ht[child] + 1;
      far[now] = far[child];
    }
  }
  int f1 = -1;
  int maxht = -1;
  for (int child : adj[now]) {
    if (child == parent) continue;
    if (ht[child] > maxht) {
      maxht = ht[child];
      f1 = child;
    }
  }
  int f2 = -1;
  maxht = -1;
  for (int child : adj[now]) {
    if (child == parent) continue;
    if (ht[child] > maxht && child != f1) {
      maxht = ht[child];
      f2 = child;
    }
  }
  int nowd;
  if (f1 == -1) {
    assert(f2 == -1);
    f1 = f2 = now;
    nowd = 0;
  } else if (f2 == -1) {
    f2 = now;
    nowd = ht[f1] + 1;
    f1 = far[f1];
  } else {
    nowd = ht[f1] + ht[f2] + 2;
    f1 = far[f1];
    f2 = far[f2];
  }
  if (nowd > dlen) {
    dlen = nowd;
    v1 = f1;
    v2 = f2;
    root = now;
  }
}
void dfs_dist(int res[], int now, int parent, int len) {
  res[now] = len;
  for (int child : adj[now]) {
    if (child == parent) continue;
    dfs_dist(res, child, now, len + 1);
  }
}
void dfs_solve(vector<node>& op, int now, int parent, long long int& res) {
  if (now == dv1 || now == dv2) return;
  del[now] = true;
  for (int child : adj[now]) {
    if (child == parent) continue;
    dfs_solve(op, child, now, res);
    if (!del[child]) del[now] = false;
  }
  if (del[now]) {
    int partner = (d[0][now] >= d[1][now] ? dv1 : dv2);
    res += max(d[0][now], d[1][now]);
    op.emplace_back(now, partner, now);
  }
}
int main(int argc, char* argv[]) {
  scanf("%d", &n);
  for (int i = 1; i < n; ++i) {
    int a, b;
    scanf("%d%d", &a, &b);
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  int root;
  dia = -1;
  diameter(1, -1, dia, dv1, dv2, root);
  ;
  dfs_dist(d[0], dv1, -1, 0);
  dfs_dist(d[1], dv2, -1, 0);
  assert(d[0][dv2] == dia);
  assert(d[1][dv1] == dia);
  vector<node> oseq;
  long long int ans = 0ll;
  dfs_solve(oseq, root, -1, ans);
  assert(!del[dv1]);
  assert(!del[dv2]);
  int now = dv1;
  while (now != dv2) {
    ans += dia--;
    oseq.emplace_back(now, dv2, now);
    del[now] = true;
    for (int child : adj[now]) {
      if (!del[child]) {
        now = child;
        break;
      }
    }
  }
  assert(dia == 0);
  assert((int)oseq.size() == n - 1);
  printf("%lld\n", ans);
  for (const node& nd : oseq) printf("%d %d %d\n", nd.a, nd.b, nd.c);
  return 0;
}

```


### Prompt

Generate a Cpp solution to the following problem:

Let's define the sum of two permutations p and q of numbers 0, 1, ..., (n - 1) as permutation <image>, where Perm(x) is the x-th lexicographically permutation of numbers 0, 1, ..., (n - 1) (counting from zero), and Ord(p) is the number of permutation p in the lexicographical order.

For example, Perm(0) = (0, 1, ..., n - 2, n - 1), Perm(n! - 1) = (n - 1, n - 2, ..., 1, 0)

Misha has two permutations, p and q. Your task is to find their sum.

Permutation a = (a0, a1, ..., an - 1) is called to be lexicographically smaller than permutation b = (b0, b1, ..., bn - 1), if for some k following conditions hold: a0 = b0, a1 = b1, ..., ak - 1 = bk - 1, ak < bk.

Input

The first line contains an integer n (1 ≤ n ≤ 200 000).

The second line contains n distinct integers from 0 to n - 1, separated by a space, forming permutation p.

The third line contains n distinct integers from 0 to n - 1, separated by spaces, forming permutation q.

Output

Print n distinct integers from 0 to n - 1, forming the sum of the given permutations. Separate the numbers by spaces.

Examples

Input

2
0 1
0 1

Output

0 1

Input

2
0 1
1 0

Output

1 0

Input

3
1 2 0
2 1 0

Output

1 0 2

Note

Permutations of numbers from 0 to 1 in the lexicographical order: (0, 1), (1, 0).

In the first sample Ord(p) = 0 and Ord(q) = 0, so the answer is <image>.

In the second sample Ord(p) = 0 and Ord(q) = 1, so the answer is <image>.

Permutations of numbers from 0 to 2 in the lexicographical order: (0, 1, 2), (0, 2, 1), (1, 0, 2), (1, 2, 0), (2, 0, 1), (2, 1, 0).

In the third sample Ord(p) = 3 and Ord(q) = 5, so the answer is <image>.

### Response

```cpp
#include <bits/stdc++.h>
int n, q;
int a[200001], b[200001], c[200001];
int bit[262145];
inline void I(int i, int x) {
  for (; i <= 262144; i += i & -i) bit[i] += x;
}
inline int Q(int i) {
  int x = 0;
  for (; i; i -= i & -i) x += bit[i];
  return x;
}
inline int BS(int x) {
  int sum = 0, i = 262144, ans = 262144;
  while (!(i & 1)) {
    if (sum + bit[ans - (i >> 1)] >= x)
      ans -= i >> 1;
    else
      sum += bit[ans - (i >> 1)];
    i >>= 1;
  }
  return ans;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= (n); ++i) scanf("%d", a + i), ++a[i];
  for (int i = 1; i <= (n); ++i) scanf("%d", b + i), ++b[i];
  for (int i = 1; i <= (n); ++i) I(i, 1);
  for (int i = 1; i <= (n); ++i) I(a[i], -1), a[i] = Q(a[i]);
  for (int i = 1; i <= (n); ++i) I(i, 1);
  for (int i = 1; i <= (n); ++i) I(b[i], -1), b[i] = Q(b[i]);
  int lst = 0;
  for (int i = n; i >= (1); --i) {
    c[i] = a[i] + b[i] + lst;
    if (c[i] >= n - i + 1)
      c[i] -= n - i + 1, lst = 1;
    else
      lst = 0;
  }
  for (int i = 1; i <= (n); ++i) I(i, 1);
  for (int i = 1; i <= (n); ++i) {
    c[i] = BS(c[i] + 1);
    I(c[i], -1);
  }
  for (int i = 1; i <= (n); ++i) printf("%d ", c[i] - 1);
  puts("");
  return 0;
}

```


### Prompt

Please create a solution in CPP to the following problem:

Some days ago, WJMZBMR learned how to answer the query "how many times does a string x occur in a string s" quickly by preprocessing the string s. But now he wants to make it harder.

So he wants to ask "how many consecutive substrings of s are cyclical isomorphic to a given string x". You are given string s and n strings xi, for each string xi find, how many consecutive substrings of s are cyclical isomorphic to xi.

Two strings are called cyclical isomorphic if one can rotate one string to get the other one. 'Rotate' here means 'to take some consecutive chars (maybe none) from the beginning of a string and put them back at the end of the string in the same order'. For example, string "abcde" can be rotated to string "deabc". We can take characters "abc" from the beginning and put them at the end of "de".

Input

The first line contains a non-empty string s. The length of string s is not greater than 106 characters.

The second line contains an integer n (1 ≤ n ≤ 105) — the number of queries. Then n lines follow: the i-th line contains the string xi — the string for the i-th query. The total length of xi is less than or equal to 106 characters.

In this problem, strings only consist of lowercase English letters.

Output

For each query xi print a single integer that shows how many consecutive substrings of s are cyclical isomorphic to xi. Print the answers to the queries in the order they are given in the input.

Examples

Input

baabaabaaa
5
a
ba
baa
aabaa
aaba

Output

7
5
7
3
5

Input

aabbaa
3
aa
aabb
abba

Output

2
3
3

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
inline int gi() {
  char c = getchar();
  while (c < '0' || c > '9') c = getchar();
  int sum = 0;
  while ('0' <= c && c <= '9') sum = sum * 10 + c - 48, c = getchar();
  return sum;
}
const int maxn = 2000005;
char s[maxn], t[maxn];
int n, m, q;
int rt = 1, lst = 1, tot = 1, ch[maxn][26], len[maxn], fa[maxn], siz[maxn];
int vis[maxn];
void extend(int c) {
  int pre = lst, x = ++tot;
  lst = x;
  len[x] = len[pre] + 1;
  siz[x] = 1;
  while (pre && !ch[pre][c]) ch[pre][c] = x, pre = fa[pre];
  if (!pre)
    fa[x] = rt;
  else {
    int k = ch[pre][c];
    if (len[k] == len[pre] + 1)
      fa[x] = k;
    else {
      int y = ++tot;
      memcpy(ch[y], ch[k], sizeof(ch[y]));
      len[y] = len[pre] + 1;
      fa[y] = fa[k];
      fa[k] = fa[x] = y;
      while (pre && ch[pre][c] == k) ch[pre][c] = y, pre = fa[pre];
    }
  }
}
void toposort() {
  static int cnt[maxn], a[maxn];
  for (int i = 1; i <= tot; ++i) ++cnt[len[i]];
  for (int i = 1; i <= n; ++i) cnt[i] += cnt[i - 1];
  for (int i = 1; i <= tot; ++i) a[cnt[len[i]]--] = i;
  for (int i = tot; i; --i) siz[fa[a[i]]] += siz[a[i]];
}
int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  for (int i = 1; i <= n; ++i) extend(s[i] - 'a');
  toposort();
  q = gi();
  while (q--) {
    scanf("%s", t + 1);
    m = strlen(t + 1);
    int u = rt;
    int ans = 0, L = 0;
    for (int c, i = 1; i <= (m << 1); ++i) {
      c = t[i <= m ? i : i - m] - 'a';
      while (u && !ch[u][c]) u = fa[u], L = len[u];
      if (ch[u][c]) u = ch[u][c], ++L;
      if (L > m && --L == len[fa[u]]) u = fa[u], L = len[u];
      if (L == m)
        if (vis[u] != q + 1) vis[u] = q + 1, ans += siz[u];
    }
    printf("%d\n", ans);
  }
  return 0;
}

```