#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000006;
int v[MAXN], r[MAXN];
char buff[MAXN];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  string s;
  while (cin >> s) {
    int m, n = int((s).size());
    cin >> m;
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < n + 5; ++j) r[j] = j + 1;
      int k, d;
      cin >> k >> d;
      int cnt = -1;
      for (int j = 0; j < d; ++j) {
        for (int l = j; l < k; l += d) {
          r[cnt + 1] = l + 1;
          v[l] = cnt++;
        }
      }
      string res(n - k + 1, '0');
      int val = 0;
      for (int j = 0; j < n - k + 1; ++j) {
        res[j] = s[r[val] - 1];
        val = r[val];
      }
      string nova(k - 1, 4);
      int y = r[0];
      while (y < k) {
        nova[y - 1] = s[r[val] - 1];
        val = r[val];
        y = r[y];
      }
      int op = n - k + 1;
      for (int j = 0; j < k; ++j) {
        if (nova[j] != 4) continue;
        int cyclelen = 1, start = j + 1, now = r[j + 1];
        while (start != now) {
          cyclelen++;
          now = r[now];
        }
        int x = op % cyclelen;
        for (int l = 0; l < x; ++l) now = r[now];
        for (int l = 0; l < cyclelen; ++l) {
          nova[start - 1] = s[now - 1];
          start = r[start], now = r[now];
        }
      }
      if (!nova.empty()) res += nova;
      printf("%s\n", res.c_str());
      s = res;
    }
  }
}
