#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 100;
const int M = 17;
const long long mod = 1e9 + 7;
const long long MOD = 998244353;
const long double eps = 0.000000001;
const int P = 1336;
const int inf = 1e18 + 7;
mt19937 gen(chrono::high_resolution_clock::now().time_since_epoch().count());
vector<int> g[N];
int u[N], kol[N];
int get(int nom, int i, int j) {
  i = kol[i];
  if (i + j < g[nom].size()) return g[nom][i + j];
  int sz = g[nom].size();
  j %= sz;
  return g[nom][(i + j) % sz];
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  srand(time(0));
  string s;
  cin >> s;
  int n = s.size();
  int q;
  cin >> q;
  while (q--) {
    int m, k;
    cin >> m >> k;
    vector<int> a;
    string ans;
    for (int i = 0; i < k; i++) {
      for (int j = i; j < m; j += k) {
        a.push_back(j);
        u[j] = -1;
      }
    }
    for (int i = m; i < n; i++) {
      a.push_back(i);
      u[i] = -1;
    }
    int me = 0;
    for (int i = 0; i < n; i++) {
      if (u[i] == -1) {
        g[me].clear();
        int j = i, ko = 0;
        while (j <= n && u[j] != me) {
          g[me].push_back(a[j]);
          u[j] = me;
          kol[j] = ko;
          j = a[j] + 1;
          ko++;
        }
        me++;
      }
    }
    for (int i = 0; i < n; i++) {
      if (i < n - m)
        ans += s[get(u[0], 0, i)];
      else
        ans += s[get(u[i - (n - m)], i - (n - m), n - m)];
    }
    cout << ans << '\n';
    s = ans;
  }
}
