#include <bits/stdc++.h>
using namespace std;
template <typename T>
void showArr(T *arr, int n) {
  for (int i = 0; i < n; i++) cout << arr[i] << ' ';
  puts("");
}
const int MAXN = 1000007;
const int MOD = 1000000007;
const int SZ = 21;
char s[2][MAXN];
int p[MAXN][SZ];
int bk[MAXN], dis[MAXN];
int main() {
  memset(p[0], 0, sizeof(p[0]));
  while (~scanf("%s", s[0])) {
    int q;
    scanf("%d", &q);
    while (q--) {
      int k, d;
      scanf("%d%d", &k, &d);
      for (int index = -1, i = 0; i < d; i++) {
        for (int j = i; j < k; j += d, index++) {
          p[j][0] = index;
          if (index >= 0) bk[index] = j;
        }
      }
      for (int i = 1; i < SZ; i++) {
        for (int j = 0; j < k; j++) {
          int nxt = p[j][i - 1];
          p[j][i] = (nxt >= 0 ? p[nxt][i - 1] : nxt);
        }
      }
      for (int i = 0; i < k; i++) dis[i] = MOD;
      for (int i = 0, step = 0; dis[i] == MOD; step++, i = bk[i]) {
        dis[i] = step;
        if (i == k - 1) break;
      }
      int n = strlen(s[0]);
      for (int i = 0; i < n; i++) {
        int tm = min(n - i, n - k + 1);
        int b = min(i, k - 1);
        if (dis[b] <= tm) {
          s[1][i - b + dis[b]] = s[0][i];
        } else {
          int tt = tm;
          int np = b;
          int bit = 0;
          while (tt) {
            if (tt % 2) np = p[np][bit];
            tt /= 2;
            bit++;
          }
          s[1][i - b + tm + np] = s[0][i];
        }
      }
      s[1][n] = 0;
      strcpy(s[0], s[1]);
      puts(s[0]);
    }
  }
  return 0;
}
