#include <bits/stdc++.h>
using namespace std;
int get() {
  char c;
  while (c = getchar(), (c < '0' || c > '9') && (c != '-'))
    ;
  bool flag = (c == '-');
  if (flag) c = getchar();
  int x = 0;
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - 48;
    c = getchar();
  }
  return flag ? -x : x;
}
void output(int x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  int len = 0, data[10];
  while (x) {
    data[len++] = x % 10;
    x /= 10;
  }
  if (!len) data[len++] = 0;
  while (len--) putchar(data[len] + 48);
  putchar('\n');
}
const int maxn = 1000000;
string s;
int dp[20][maxn];
char ans[maxn];
int jump(int x, int p) {
  for (int i = 0; i < 20 && x != -1; i++)
    if (p & (1 << i)) x = dp[i][x];
  return x;
}
int main() {
  string s;
  cin >> s;
  int n = s.size(), q = get();
  while (q--) {
    int m = get(), k = get(), counter = -1;
    for (int i = 0; i < k; i++)
      for (int j = i; j < m; j += k) dp[0][j] = counter++;
    for (int i = 1; i < 20; i++)
      for (int j = 0; j < m; j++)
        dp[i][j] = (dp[i - 1][j] == -1) ? -1 : dp[i - 1][dp[i - 1][j]];
    for (int i = 0; i < n; i++) {
      int start = min(i, m - 1), len = n - max(i, m - 1), t = jump(start, len);
      if (t != -1)
        ans[n - m + t + 1] = s[i];
      else {
        int p = start, step = 0;
        for (int j = 19; j >= 0; j--)
          if (dp[j][p] != -1) {
            p = dp[j][p];
            step += 1 << j;
          }
        if (i < m - 1)
          p = 0;
        else
          p = i - m + 1;
        ans[p + step] = s[i];
      }
    }
    for (int i = 0; i < n; i++) putchar(s[i] = ans[i]);
    putchar('\n');
  }
  return 0;
}
