#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 7;
char s1[N], s2[N];
int n, m, k, d, tp;
int P[N];
int C[N];
int X[N];
int R[N];
int D[N];
int main() {
  scanf("%s", s1);
  n = strlen(s1);
  cin >> m;
  while (m--) {
    scanf("%d%d", &k, &d);
    tp = 0;
    for (int i = 0; i < n; i++) {
      P[i] = i;
      C[i] = i - 1;
    }
    C[0] = n - 1;
    for (int i = 0; i < d; i++)
      for (int j = i; j < k; j += d) P[j] = tp++;
    for (int i = 0; i < n; i++) X[i] = C[P[i]];
    for (int i = 0; i < n; i++) R[i] = i;
    tp = n - k + 1;
    while (tp) {
      if (tp & 1) {
        for (int i = 0; i < n; i++) D[i] = X[R[i]];
        for (int i = 0; i < n; i++) R[i] = D[i];
      }
      for (int i = 0; i < n; i++) D[i] = X[X[i]];
      for (int i = 0; i < n; i++) X[i] = D[i];
      tp >>= 1;
    }
    tp = n - k + 1;
    for (int i = 0; i < n; i++) s2[(R[i] + tp) % n] = s1[i];
    for (int i = 0; i < n; i++) putchar(s1[i] = s2[i]);
    putchar('\n');
  }
  return 0;
}
