#include <bits/stdc++.h>
using namespace std;
int ed, la, n, m, t1, i, j, cnt, mx, x, a[1000010], b[1000010][21], d;
char s[1000010], ss[1000010];
int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  scanf("%d", &t1);
  while (t1--) {
    scanf("%d%d", &m, &d);
    cnt = 0;
    for (i = 1; i <= d; i++)
      for (j = i; j <= m; j += d) b[j][0] = cnt++;
    for (j = 1; j <= 20; j++)
      for (i = 1; i <= m; i++) b[i][j] = b[b[i][j - 1]][j - 1];
    for (i = 1; i <= n; i++) {
      if (i <= m)
        x = i, la = 0;
      else
        x = m, la = i - m;
      cnt = 0;
      ed = i + m - x;
      mx = n - ed + 1;
      for (j = 20; j >= 0; j--)
        if (mx >= (1 << j) && b[x][j]) x = b[x][j], cnt += 1 << j, mx -= 1 << j;
      a[x + cnt + la] = i;
    }
    for (i = 1; i <= n; i++) ss[i] = s[a[i]];
    for (i = 1; i <= n; i++) {
      s[i] = ss[i];
      printf("%c", s[i]);
    }
    puts("");
  }
}
