#include <bits/stdc++.h>
using namespace std;
const long long INF = 0x3f3f3f3f3f3f3f3f;
const long long inf = 0xc0c0c0c0c0c0c0c0;
const double pi = acos(-1);
const int MAXL = 1e6 + 7;
char in[MAXL];
int pc[MAXL];
int c[MAXL];
int res[MAXL];
int N;
int K, D;
void get_pc() {
  int sz = 0;
  for (int i = 0; i < D; i++)
    for (int j = i; j < K; j += D) pc[sz++] = j;
  for (int i = K; i < N; i++) pc[i] = i;
  int temp = pc[0];
  for (int i = 0; i < N - 1; i++) pc[i] = pc[i + 1];
  pc[N - 1] = temp;
}
void merege(int *old, int *now) {
  for (int i = 0; i < N; i++) c[i] = old[now[i]];
  for (int i = 0; i < N; i++) old[i] = c[i];
}
void solve() {
  get_pc();
  int p = N - K + 1;
  while (p) {
    if (p & 1) merege(res, pc);
    p >>= 1;
    merege(pc, pc);
  }
  int f = N - K + 1;
  for (int i = 0; i < N; i++) c[i] = res[(i - f + N) % N];
  for (int i = 0; i < N; i++) res[i] = c[i];
}
int main() {
  scanf("%s", in);
  for (N = 0; in[N]; N++) res[N] = N;
  int Q;
  scanf("%d", &Q);
  while (Q--) {
    scanf("%d%d", &K, &D);
    solve();
    for (int i = 0; i < N; i++) putchar(in[res[i]]);
    putchar('\n');
  }
  return 0;
}
