Problem: How many specific orders do you know? Ascending order, descending order, order of ascending length, order of ascending polar angle... Let's have a look at another specific order: d-sorting. This sorting is applied to the strings of length at least d, where d is some positive integer. The characters of the string are sorted in following manner: first come all the 0-th characters of the initial string, then the 1-st ones, then the 2-nd ones and so on, in the end go all the (d - 1)-th characters of the initial string. By the i-th characters we mean all the character whose positions are exactly i modulo d. If two characters stand on the positions with the same remainder of integer division by d, their relative order after the sorting shouldn't be changed. The string is zero-indexed. For example, for string 'qwerty':

Its 1-sorting is the string 'qwerty' (all characters stand on 0 positions),

Its 2-sorting is the string 'qetwry' (characters 'q', 'e' and 't' stand on 0 positions and characters 'w', 'r' and 'y' are on 1 positions),

Its 3-sorting is the string 'qrwtey' (characters 'q' and 'r' stand on 0 positions, characters 'w' and 't' stand on 1 positions and characters 'e' and 'y' stand on 2 positions),

Its 4-sorting is the string 'qtwyer',

Its 5-sorting is the string 'qywert'.

You are given string S of length n and m shuffling operations of this string. Each shuffling operation accepts two integer arguments k and d and transforms string S as follows. For each i from 0 to n - k in the increasing order we apply the operation of d-sorting to the substring S[i..i + k - 1]. Here S[a..b] represents a substring that consists of characters on positions from a to b inclusive.

After each shuffling operation you need to print string S.

Input

The first line of the input contains a non-empty string S of length n, consisting of lowercase and uppercase English letters and digits from 0 to 9. 

The second line of the input contains integer m – the number of shuffling operations (1 ≤ m·n ≤ 106). 

Following m lines contain the descriptions of the operations consisting of two integers k and d (1 ≤ d ≤ k ≤ n). 

Output

After each operation print the current state of string S.

Examples

Input

qwerty
3
4 2
6 3
5 2


Output

qertwy
qtewry
qetyrw

Note

Here is detailed explanation of the sample. The first modification is executed with arguments k = 4, d = 2. That means that you need to apply 2-sorting for each substring of length 4 one by one moving from the left to the right. The string will transform in the following manner:

qwerty →  qewrty →  qerwty →  qertwy

Thus, string S equals 'qertwy' at the end of first query.

The second modification is executed with arguments k = 6, d = 3. As a result of this operation the whole string S is replaced by its 3-sorting: 

qertwy →  qtewry

The third modification is executed with arguments k = 5, d = 2. 

qtewry →  qertwy →  qetyrw

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T bigmod(T p, T e, T M) {
  long long ret = 1;
  for (; e > 0; e >>= 1) {
    if (e & 1) ret = (ret * p) % M;
    p = (p * p) % M;
  }
  return (T)ret;
}
template <class T>
inline T gcd(T a, T b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
template <class T>
inline T modinverse(T a, T M) {
  return bigmod(a, M - 2, M);
}
string s;
int ar[1000001], vis[1000001], id[1000001], pos[1000001];
pair<int, int> pp[1000001];
vector<int> cycles[1000001];
int last[1000001], cc, line[1000001], rev[1000001];
void dfs(int c, int p) {
  vis[c] = 1;
  last[p] = c;
  if (ar[c] == 0) return;
  if (ar[c] == last[0]) {
    cc++;
    for (int i = (0); i < (p + 1); ++i) {
      cycles[cc].push_back(last[i]);
      id[last[i]] = cc;
      pos[last[i]] = i;
    }
    return;
  }
  if (vis[ar[c]]) return;
  dfs(ar[c], p + 1);
}
char out[1000001];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long a, b = 0, c, d = 0, e = 0, f = 0, g, h = 0, x, y, z = 0;
  cin >> s;
  cin >> a;
  for (int ts = (0); ts < (a); ++ts) {
    cin >> b >> c;
    for (int i = (0); i < (b); ++i) pp[i] = make_pair(i % c, i);
    sort(pp, pp + b);
    for (int i = (0); i < (b); ++i) rev[i] = 0;
    for (int i = (0); i < (b); ++i) {
      ar[pp[i].second] = i - 1;
      if (i > 0) rev[i - 1] = pp[i].second;
    }
    for (int i = (0); i < (b); ++i) {
      id[i] = 0;
      vis[i] = 0;
      cycles[i + 1].clear();
    }
    cc = 0;
    for (int i = (1); i < (b); ++i) {
      if (vis[i] == 0) dfs(i, 0);
    }
    g = 0;
    line[g++] = 0;
    for (int i = rev[0]; i != 0; i = rev[i]) {
      pos[i] = g;
      line[g++] = i;
    }
    for (int i = (1); i < (s.size()); ++i) {
      if (i < b) {
        d = i;
        e = s.size() - b + 1;
        f = 0;
      } else {
        d = b - 1;
        e = s.size() - i;
        f = i - b + 1;
      }
      if (id[d] == 0) {
        if (pos[d] <= e) {
          out[f + pos[d]] = s[i];
        } else {
          out[s.size() - b + 1 + line[pos[d] - e]] = s[i];
        }
      } else {
        out[s.size() - b + 1 +
            cycles[id[d]][(pos[d] + e) % cycles[id[d]].size()]] = s[i];
      }
    }
    for (int i = (1); i < (s.size()); ++i) s[i] = out[i];
    cout << s << "\n";
  }
}