import java.util.*;
import java.io.*;
import java.math.*;

public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		Solver solver = new Solver();
		solver.solve(in, out);
		out.close();
	}
}

class Solver {
	int[] apply(int[] a, int[] p) {
		int[] b = new int[a.length];
		for (int i = 0; i < a.length; i++) {
			b[i] = a[p[i]];
		}
		return b;
	}
	int[] apply1(int[] a, int[] p) {
		int[] b = new int[a.length];
		for (int i = 0; i < a.length; i++) {
			b[p[i]] = a[i];
		}
		return b;
	}
	int[] cycleShift(int n, int k) {
		int[] a = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = (i + k) % n;
		}
		return a;
	}
	int[] dSort(int n, int k, int d) {
		int[] a = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = i;
		}
		int c = 0;
		for (int i = 0; i < d; i++) {
			for (int j = i; j < k; j += d) {
				a[j] = c++;
			}
		}
		return a;
	}
	int[] power(int[] p, int pow) {
		if (pow == 1)
			return p;
		if (pow % 2 == 0) {
			int[] t = power(p, pow / 2);
			return apply(t, t);
		} else {
			return apply(power(p, pow - 1), p);
		}
	}
	public void solve(InputReader in, PrintWriter out) {
		String str = in.next();
		int n = str.length();
		int[] s = new int[n];
		for (int i = 0; i < n; i++) {
			s[i] = str.charAt(i);
		}
		int m = in.nextInt();
		int[] shift = cycleShift(n, n - 1);

		for (int i = 0; i < m; i++) {
			int k = in.nextInt();
			int d = in.nextInt();
			int[] dSort = dSort(n, k, d);

			int[] perm = power(apply(shift, dSort), n - k + 1);
			perm = apply(cycleShift(n, n - k + 1), perm);
			s = apply1(s, perm);
			for (int j = 0; j < n; j++) {
				out.print((char)s[j]);
			}
			out.println();
		}

	}
}

class InputReader {
	private BufferedReader reader;
	private StringTokenizer tokenizer;

	public InputReader(InputStream stream) {
		reader = new BufferedReader(new InputStreamReader(stream));
		tokenizer = null;
	}

	public String next() {
		while (tokenizer == null || !tokenizer.hasMoreTokens()) {
			try {
				tokenizer = new StringTokenizer(reader.readLine());
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}
		return tokenizer.nextToken();
	}

	public long nextLong() {
		return Long.parseLong(next());
	}

	public int nextInt() {
		return Integer.parseInt(next());
	}

	public double nextDouble() {
		return Double.parseDouble(next());
	}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class ProblemC2 {
    BufferedReader rd;
    char[][] cur;
    char[] qq;
    int src;
    int n;

    private ProblemC2() throws IOException {
        rd = new BufferedReader(new InputStreamReader(System.in));
        compute();
    }

    private void compute() throws IOException {
        String cd = rd.readLine();
        cur = new char[2][];
        cur[0] = cd.toCharArray();
        cur[1] = cd.toCharArray();
        n = cur[0].length;
        qq = new char[n *2];
        int shuffleCount = pint();
        StringBuilder buf = new StringBuilder();
        for(int i=0;i<shuffleCount;i++) {
            int[] a = intarr();
            if(a[1] > 1 && a[1] < a[0]) {
                shuffle(a[0],a[1]);
                src = 1-src;
            }
            for(int j=0;j< n;j++) {
                buf.append(cur[src][j]);
            }
            buf.append('\n');
        }
        out(buf);
    }

    private void shuffle(int k, int d) {
        int[] p = new int[k];
        int t = 0;
        int u = 0;
        int y = 1;
        while(t < k) {
            p[t] = u;
            u += d;
            if(u >= k) {
                u = y;
                y++;
            }
            t++;
        }

        int[] od = new int[k];
        for(int i=0;i<k;i++) {
            od[p[i]] = i;
        }

        boolean[] used = new boolean[k];
        int[] queue = new int[k];
        int s = 1;
        int len = 1;
        while(len < k && s < k) {
            s = p[s];
            used[s] = true;
            queue[len] = s;
            s++;
            len++;
        }

        List<List<Integer>> cycles = new ArrayList<>();
        for(int i=1;i<k;i++) {
            if(!used[i]) {
                List<Integer> cycle = new ArrayList<>();
                cycle.add(i);
                used[i] = true;
                int g = od[i]-1;
                while(g != i) {
                    cycle.add(g);
                    used[g] = true;
                    g = od[g]-1;
                }
                cycles.add(cycle);
            }
        }

        int head = 0;
        int tail = 0;
        for(int i=0;i<len;i++) {
            qq[head++] = cur[src][queue[i]];
        }
        int dst = 1-src;
        for(int i=0;i< n -k;i++) {
            cur[dst][i] = qq[tail++];
            qq[head++] = cur[src][i+k];
        }
        for(int i=0;i<len;i++) {
            cur[dst][n -k+od[queue[i]]] = qq[tail++];
        }

        for(List<Integer> cycle: cycles) {
            int cs = cycle.size();
            int offset = (n -k) % cs;
            for(int i=0;i<cs;i++) {
                int idx = (cs+i-offset)%cs;
                cur[dst][n -k + od[cycle.get(i)]] = cur[src][cycle.get(idx)];
            }
        }
    }

    private int pint() throws IOException {
        return pint(rd.readLine());
    }

    private int pint(String s) {
        return Integer.parseInt(s);
    }

    private int[] intarr() throws IOException {
        return intarr(rd.readLine());
    }

    private int[] intarr(String s) {
        String[] q = s.split(" ");
        int n = q.length;
        int[] a = new int[n];
        for(int i=0;i<n;i++) {
            a[i] = Integer.parseInt(q[i]);
        }
        return a;
    }

    private static void out(Object x) {
        System.out.println(x);
    }

    public static void main(String[] args) throws IOException {
        new ProblemC2();
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class ProblemC2 {
    BufferedReader rd;
    char[][] cur;
    char[] qq;
    int src;

    private ProblemC2() throws IOException {
        rd = new BufferedReader(new InputStreamReader(System.in));
        compute();
    }

    private void compute() throws IOException {
        String cd = rd.readLine();
        cur = new char[2][];
        cur[0] = cd.toCharArray();
        cur[1] = cd.toCharArray();
        qq = new char[cur[0].length*2];
        int n = pint();
        StringBuilder buf = new StringBuilder();
        for(int i=0;i<n;i++) {
            int[] a = intarr();
            if(a[1] > 1 && a[1] < a[0]) {
                shuffle(a[0],a[1]);
            }
            src = 1-src;
            for(int j=0;j<cur[0].length;j++) {
                buf.append(cur[src][j]);
            }
            buf.append('\n');
        }
        out(buf);
    }

    private void shuffle(int k, int d) {
        int[] p = new int[k];
        int t = 0;
        int u = 0;
        int y = 1;
        while(t < k) {
            p[t] = u;
            u += d;
            if(u >= k) {
                u = y;
                y++;
            }
            t++;
        }

        int[] od = new int[k];
        for(int i=0;i<k;i++) {
            od[p[i]] = i;
        }

        boolean[] used = new boolean[k];
        int[] queue = new int[k];
        int s = 1;
        int len = 1;
        while(len < k && s < k) {
            s = p[s];
            used[s] = true;
            queue[len] = s;
            s++;
            len++;
        }

        List<List<Integer>> cycles = new ArrayList<>();
        for(int i=1;i<k;i++) {
            if(!used[i]) {
                List<Integer> cycle = new ArrayList<>();
                int f = i;
                cycle.add(f);
                used[f] = true;
                int g = od[f]-1;
                while(g != f) {
                    cycle.add(g);
                    used[g] = true;
                    g = od[g]-1;
                }
                cycles.add(cycle);
            }
        }

        int n = cur[0].length;
        int head = 0;
        int tail = 0;
        for(int i=0;i<len;i++) {
            qq[head++] = cur[src][queue[i]];
        }
        int dst = 1-src;
        for(int i=0;i<n-k;i++) {
            cur[dst][i] = qq[tail++];
            qq[head++] = cur[src][i+k];
        }
        for(int i=0;i<len;i++) {
            char x = qq[tail++];
            cur[dst][n-k+od[queue[i]]] = x;
        }

        for(List<Integer> cycle: cycles) {
            int cs = cycle.size();
            int offset = (n-k) % cs;
            for(int i=0;i<cs;i++) {
                int idx = (cs*100+i-offset)%cs;
                cur[dst][n-k + od[cycle.get(i)]] = cur[src][cycle.get(idx)];
            }
        }
    }

    private int pint() throws IOException {
        return pint(rd.readLine());
    }

    private int pint(String s) {
        return Integer.parseInt(s);
    }

    private int[] intarr() throws IOException {
        return intarr(rd.readLine());
    }

    private int[] intarr(String s) {
        String[] q = s.split(" ");
        int n = q.length;
        int[] a = new int[n];
        for(int i=0;i<n;i++) {
            a[i] = Integer.parseInt(q[i]);
        }
        return a;
    }

    private static void out(Object x) {
        System.out.println(x);
    }

    public static void main(String[] args) throws IOException {
        new ProblemC2();
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class ProblemC2 {
    BufferedReader rd;
    char[] c;
    int[] cur;

    private ProblemC2() throws IOException {
        rd = new BufferedReader(new InputStreamReader(System.in));
        compute();
    }

    private void compute() throws IOException {
        c = rd.readLine().toCharArray();
        cur = new int[c.length];
        for(int i=0;i<c.length;i++) {
            cur[i] = i;
        }
        int n = pint();
        for(int i=0;i<n;i++) {
            int[] a = intarr();
            if(a[1] > 1 && a[1] < a[0]) {
                shuffle(a[0],a[1]);
            }
            StringBuilder buf = new StringBuilder();
            for(int j=0;j<c.length;j++) {
                buf.append(c[cur[j]]);
            }
            out(buf);
        }
    }

    private void shuffle(int k, int d) {
        int[] p = new int[k];
        int t = 0;
        int u = 0;
        int y = 1;
        while(t < k) {
            p[t] = u;
            u += d;
            if(u >= k) {
                u = y;
                y++;
            }
            t++;
        }

        int[] od = new int[k];
        for(int i=0;i<k;i++) {
            od[p[i]] = i;
        }

        boolean[] used = new boolean[k];
        int[] queue = new int[k];
        int s = 1;
        int len = 1;
        while(len < k && s < k) {
            s = p[s];
            used[s] = true;
            queue[len] = s;
            s++;
            len++;
        }

        List<List<Integer>> cycles = new ArrayList<>();
        for(int i=1;i<k;i++) {
            if(!used[i]) {
                List<Integer> cycle = new ArrayList<>();
                int f = i;
                cycle.add(f);
                used[f] = true;
                int g = od[f]-1;
                while(g != f) {
                    cycle.add(g);
                    used[g] = true;
                    g = od[g]-1;
                }
                cycles.add(cycle);
            }
        }
        int[] pcur = Arrays.copyOf(cur, k);

        Queue<Integer> qi = new LinkedList<>();
        for(int i=0;i<len;i++) {
            qi.add(cur[queue[i]]);
        }
        int n = cur.length;
        for(int i=0;i<n-k;i++) {
            cur[i] = qi.poll();
            qi.add(cur[i+k]);
        }
        for(int i=0;i<len;i++) {
            int x = qi.poll();
            cur[n-k+od[queue[i]]] = x;
        }

        for(List<Integer> cycle: cycles) {
            int cs = cycle.size();
            int[] orig = new int[cs];
            for(int i=0;i<cs;i++) {
                orig[i] = pcur[cycle.get(i)];
            }
            int offset = (n-k) % cs;
            for(int i=0;i<cs;i++) {
                int idx = (i+offset)%cs;
                cur[n-k + od[cycle.get(i)]] = orig[idx];
            }
        }
    }

    private int pint() throws IOException {
        return pint(rd.readLine());
    }

    private int pint(String s) {
        return Integer.parseInt(s);
    }

    private int[] intarr() throws IOException {
        return intarr(rd.readLine());
    }

    private int[] intarr(String s) {
        String[] q = s.split(" ");
        int n = q.length;
        int[] a = new int[n];
        for(int i=0;i<n;i++) {
            a[i] = Integer.parseInt(q[i]);
        }
        return a;
    }

    private static void out(Object x) {
        System.out.println(x);
    }

    public static void main(String[] args) throws IOException {
        new ProblemC2();
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class ProblemC2 {
    BufferedReader rd;
    char[] cd;
    int[] cur;

    private ProblemC2() throws IOException {
        rd = new BufferedReader(new InputStreamReader(System.in));
        compute();
    }

    private void compute() throws IOException {
        cd = rd.readLine().toCharArray();
        cur = new int[cd.length];
        for(int i=0;i<cd.length;i++) {
            cur[i] = i;
        }
        int n = pint();
        for(int i=0;i<n;i++) {
            int[] a = intarr();
            if(a[1] > 1 && a[1] < a[0]) {
                shuffle(a[0],a[1]);
            }
            StringBuilder buf = new StringBuilder();
            for(int j=0;j<cd.length;j++) {
                buf.append(cd[cur[j]]);
            }
            out(buf);
        }
    }

    private void shuffle(int k, int d) {
        int[] p = new int[k];
        int t = 0;
        int u = 0;
        int y = 1;
        while(t < k) {
            p[t] = u;
            u += d;
            if(u >= k) {
                u = y;
                y++;
            }
            t++;
        }

        int[] od = new int[k];
        for(int i=0;i<k;i++) {
            od[p[i]] = i;
        }

        boolean[] used = new boolean[k];
        int[] queue = new int[k];
        int s = 1;
        int len = 1;
        while(len < k && s < k) {
            s = p[s];
            used[s] = true;
            queue[len] = s;
            s++;
            len++;
        }

        List<List<Integer>> cycles = new ArrayList<>();
        for(int i=1;i<k;i++) {
            if(!used[i]) {
                List<Integer> cycle = new ArrayList<>();
                int f = i;
                cycle.add(f);
                used[f] = true;
                int g = od[f]-1;
                while(g != f) {
                    cycle.add(g);
                    used[g] = true;
                    g = od[g]-1;
                }
                cycles.add(cycle);
            }
        }
        int[] pcur = Arrays.copyOf(cur, k);

        int n = cur.length;
        boolean[] marked = new boolean[n];
        Queue<Integer> qi = new LinkedList<>();
        for(int i=0;i<len;i++) {
            qi.add(cur[queue[i]]);
        }
        for(int i=0;i<n-k;i++) {
            cur[i] = qi.poll();
            marked[i] = true;
            qi.add(cur[i+k]);
        }
        for(int i=0;i<len;i++) {
            int x = qi.poll();
            cur[n-k+od[queue[i]]] = x;
            marked[n-k+od[queue[i]]] = true;
        }

        for(List<Integer> cycle: cycles) {
            int cs = cycle.size();
            int[] orig = new int[cs];
            for(int i=0;i<cs;i++) {
                orig[i] = pcur[cycle.get(i)];
            }
            int offset = (n-k) % cs;
            for(int i=0;i<cs;i++) {
                int idx = (cs*100+i-offset)%cs;
                cur[n-k + od[cycle.get(i)]] = orig[idx];
            }
        }
    }

    private int pint() throws IOException {
        return pint(rd.readLine());
    }

    private int pint(String s) {
        return Integer.parseInt(s);
    }

    private int[] intarr() throws IOException {
        return intarr(rd.readLine());
    }

    private int[] intarr(String s) {
        String[] q = s.split(" ");
        int n = q.length;
        int[] a = new int[n];
        for(int i=0;i<n;i++) {
            a[i] = Integer.parseInt(q[i]);
        }
        return a;
    }

    private static void out(Object x) {
        System.out.println(x);
    }

    public static void main(String[] args) throws IOException {
        new ProblemC2();
    }
}


import java.util.*;
import java.io.*;
public class StrangeSorting
{
	public static void main(String[] args) throws Exception
	{
		FastScanner in = new FastScanner(System.in);
		PrintWriter out = new PrintWriter(System.out);

		String s = in.next();
		int n = s.length();

		int m = in.nextInt();

		for(int x = 0; x < m; x++)
		{
			int k = in.nextInt();
			int d = in.nextInt();

			int[] next = new int[k];
			int index = 0;
			for(int y = 0; y < k;)
			{
				int current = index;
				while(current < k)
				{
					next[current] = y - 1;
					y++;
					current += d;
				}

				index++;
			}

			ArrayList<ArrayList<Integer>> paths = new ArrayList<ArrayList<Integer>>();
			boolean[] visited = new boolean[k];
			int[] path = new int[k];
			int[] pos = new int[k];
			for(int z = k - 1; z >= 0; z--)
			{
				if(!visited[z])
				{
					ArrayList<Integer> temp = new ArrayList<Integer>();

					int current = z;
					while(current != -1 && !visited[current])
					{
						path[current] = paths.size();
						pos[current] = temp.size();
						temp.add(current);

						visited[current] = true;
						current = next[current];
					}

					paths.add(temp);
				}
			}

			char[] str = new char[n];
			for(int a = 0; a < s.length(); a++)
			{
				if(a < k)
				{
					if(path[a] == 0)
					{
						if(pos[a] + n - k + 1 >= paths.get(path[a]).size())
						{
							str[paths.get(path[a]).size() - 1 - pos[a]] = s.charAt(a);
						}
						else
						{
							str[n - k + 1 + paths.get(path[a]).get(pos[a] + n - k + 1)] = s.charAt(a);
						}
					}
					else
					{
						str[n - k + 1 + paths.get(path[a]).get((pos[a] + n - k + 1) % paths.get(path[a]).size())] = s.charAt(a);
					}
				}
				else
				{
					if(a + paths.get(0).size() - 1 < n)
					{
						str[a + paths.get(0).size() - k] = s.charAt(a);
					}
					else
					{
						str[n - k + 1 + paths.get(0).get(n - a)] = s.charAt(a);
					}
				}
			}

			s = new String(str);

			out.println(s);
		}

		out.close();
	}

	static class FastScanner
	{
		BufferedReader br;
		StringTokenizer st;

		public FastScanner(InputStream input)
		{
			br = new BufferedReader(new InputStreamReader(input));
			st = new StringTokenizer("");
		}

		public String next() throws IOException
		{
			if(st.hasMoreTokens())
			{
				return st.nextToken();
			}
			else
			{
				st = new StringTokenizer(br.readLine());
				return next();
			}
		}

		public int nextInt() throws IOException
		{
			return Integer.parseInt(next());
		}
	}
}


import java.io.PrintWriter;
import java.util.Scanner;

public class E {
    public static final Scanner cin = new Scanner(System.in);
    public static final PrintWriter cout = new PrintWriter(System.out);

    public static void P(int[] pos, int k, int d){
        int[] newPos = new int[pos.length];
        int begpos = 0;
        for(int i = 0; i < d; i++){
            for(int j = 0; j < k / d + ((k % d != 0) ? 1 : 0); j++){
                if(j * d + i < k) {
                    newPos[begpos] = pos[j * d + i];
                    begpos++;
                }
            }
        }
        System.arraycopy(newPos, 0, pos, 0, begpos);
    }

    public static void C(int[] pos){
        int tmp = pos[0];
        for(int i = 0; i < pos.length - 1; i++){
            pos[i] = pos[i + 1];
        }
        pos[pos.length - 1] = tmp;
    }

    public static int[] mul(int[] first, int second[]){
        int[] res = new int[first.length];
        for(int i = 0; i < first.length; i++){
            res[i] = second[first[i]];
        }
        return res;
    }

    public static int[] binpow(int k, int d, int pow, int len){
        if(pow == 1){
            int[] pos = new int[len];
            for(int i = 0; i < len; i++){
                pos[i] = i;
            }
            P(pos, k, d);
            C(pos);
            return pos;
        }
        int[] res;
        if(pow % 2 == 1){
            int[] first = binpow(k, d, 1, len);
            int[] second = binpow(k, d, pow - 1, len);
            res = mul(first, second);
        }else {
            int[] first = binpow(k, d, pow / 2, len);
            res = mul(first, first);
        }
        return res;
    }

    public static void main(String[] args) {
        String currStr = cin.next();
        char[] v = currStr.toCharArray();
        int n = cin.nextInt();
        for(int i = 0; i < n; i++){
            int k = cin.nextInt(), d = cin.nextInt();
            if(d == 1 || k == 1 || k == 2){
                System.out.println(currStr);
            }else {
                int[] posCurr = binpow(k, d, v.length - k + 1, v.length);
                int[] pos = new int[posCurr.length];
                for (int j = 0; j < v.length; j++) {
                    pos[j] = posCurr[(j + k - 1) % v.length];
                }
                char[] currv = new char[v.length];
                for (int j = 0; j < currv.length; j++) {
                    currv[j] = v[pos[j]];
                }
                v = currv;
                currStr = new String(v);
                System.out.println(currStr);
            }
        }
    }

}


import java.io.PrintWriter;
import java.util.Scanner;

public class E {
    public static final Scanner cin = new Scanner(System.in);
    public static final PrintWriter cout = new PrintWriter(System.out);

    public static void P(int[] pos, int k, int d){
        int[] newPos = new int[pos.length];
        int begpos = 0;
        for(int i = 0; i < d; i++){
            for(int j = 0; j < k / d + ((k % d != 0) ? 1 : 0); j++){
                if(j * d + i < k) {
                    newPos[begpos] = pos[j * d + i];
                    begpos++;
                }
            }
        }
        System.arraycopy(newPos, 0, pos, 0, begpos);
    }

    public static void C(int[] pos){
        int tmp = pos[0];
        for(int i = 0; i < pos.length - 1; i++){
            pos[i] = pos[i + 1];
        }
        pos[pos.length - 1] = tmp;
    }

    public static int[] mul(int[] first, int second[]){
        int[] res = new int[first.length];
        for(int i = 0; i < first.length; i++){
            res[i] = second[first[i]];
        }
        return res;
    }

    public static int[] binpow(int k, int d, int pow, int len){
        if(pow == 1){
            int[] pos = new int[len];
            for(int i = 0; i < len; i++){
                pos[i] = i;
            }
            P(pos, k, d);
            C(pos);
            return pos;
        }
        int[] res;
        if(pow % 2 == 1){
            int[] first = binpow(k, d, 1, len);
            int[] second = binpow(k, d, pow - 1, len);
            res = mul(first, second);
        }else {
            int[] first = binpow(k, d, pow / 2, len);
            res = mul(first, first);
        }
        return res;
    }

    public static void main(String[] args) {
        String currStr = cin.next();
        StringBuilder answer = new StringBuilder("");
        char[] v = currStr.toCharArray();
        int n = cin.nextInt();
        for(int i = 0; i < n; i++){
            int k = cin.nextInt(), d = cin.nextInt();
            if(d == 1 || k == 1 || k == 2){
                answer.append(currStr).append("\n");
            }else {
                int[] posCurr = binpow(k, d, v.length - k + 1, v.length);
                int[] pos = new int[posCurr.length];
                for (int j = 0; j < v.length; j++) {
                    pos[j] = posCurr[(j + k - 1) % v.length];
                }
                char[] currv = new char[v.length];
                for (int j = 0; j < currv.length; j++) {
                    currv[j] = v[pos[j]];
                }
                v = currv;
                currStr = new String(v);
                answer.append(currStr).append("\n");
            }
        }
        cout.print(answer.toString());
        cout.close();
    }

}


import java.io.PrintWriter;
import java.util.Scanner;

public class E {
    public static final Scanner cin = new Scanner(System.in);
    public static final PrintWriter cout = new PrintWriter(System.out);

    public static void P(int[] pos, int k, int d){
        int[] newPos = new int[pos.length];
        int begpos = 0;
        for(int i = 0; i < d; i++){
            for(int j = 0; j < k / d + ((k % d != 0) ? 1 : 0); j++){
                if(j * d + i < k) {
                    newPos[begpos] = pos[j * d + i];
                    begpos++;
                }
            }
        }
        System.arraycopy(newPos, 0, pos, 0, begpos);
    }

    public static void C(int[] pos){
        int tmp = pos[0];
        for(int i = 0; i < pos.length - 1; i++){
            pos[i] = pos[i + 1];
        }
        pos[pos.length - 1] = tmp;
    }

    public static int[] mul(int[] first, int second[]){
        int[] res = new int[first.length];
        for(int i = 0; i < first.length; i++){
            res[i] = second[first[i]];
        }
        return res;
    }

    public static int[] binpow(int k, int d, int pow, int len){
        if(pow == 1){
            int[] pos = new int[len];
            for(int i = 0; i < len; i++){
                pos[i] = i;
            }
            P(pos, k, d);
            C(pos);
            return pos;
        }
        int[] res;
        if(pow % 2 == 1){
            int[] first = binpow(k, d, 1, len);
            int[] second = binpow(k, d, pow - 1, len);
            res = mul(first, second);
        }else {
            int[] first = binpow(k, d, pow / 2, len);
            res = mul(first, first);
        }
        return res;
    }

    public static void main(String[] args) {
        char[] v = cin.next().toCharArray();
        int n = cin.nextInt();
        for(int i = 0; i < n; i++){
            int k = cin.nextInt(), d = cin.nextInt();
            int[] posCurr = binpow(k, d, v.length-k+1, v.length);
            int[] pos = new int[posCurr.length];
            for(int j = 0; j < v.length; j++){
                pos[j] = posCurr[(j + k - 1) % v.length];
            }
            char[] currv = new char[v.length];
            for(int j = 0; j < currv.length; j++){
                currv[j] = v[pos[j]];
                System.out.print(currv[j]);
            }
            System.out.println();
            v = currv;
        }
    }

}


import java.io.PrintWriter;
import java.util.Scanner;

public class E {
    public static final Scanner cin = new Scanner(System.in);
    public static final PrintWriter cout = new PrintWriter(System.out);

    public static void P(int[] pos, int k, int d){
        int[] newPos = new int[pos.length];
        int begpos = 0;
        for(int i = 0; i < d; i++){
            for(int j = 0; j < k / d + ((k % d != 0) ? 1 : 0); j++){
                if(j * d + i < k) {
                    newPos[begpos] = pos[j * d + i];
                    begpos++;
                }
            }
        }
        System.arraycopy(newPos, 0, pos, 0, begpos);
    }

    public static void C(int[] pos){
        int tmp = pos[0];
        for(int i = 0; i < pos.length - 1; i++){
            pos[i] = pos[i + 1];
        }
        pos[pos.length - 1] = tmp;
    }

    public static int[] mul(int[] first, int second[]){
        int[] res = new int[first.length];
        for(int i = 0; i < first.length; i++){
            res[i] = second[first[i]];
        }
        return res;
    }

    public static int[] binpow(int k, int d, int pow, int len){
        if(pow == 1){
            int[] pos = new int[len];
            for(int i = 0; i < len; i++){
                pos[i] = i;
            }
            P(pos, k, d);
            C(pos);
            return pos;
        }
        int[] res;
        if(pow % 2 == 1){
            int[] first = binpow(k, d, 1, len);
            int[] second = binpow(k, d, pow - 1, len);
            res = mul(first, second);
        }else {
            int[] first = binpow(k, d, pow / 2, len);
            res = mul(first, first);
        }
        return res;
    }

    public static void main(String[] args) {
        String currStr = cin.next();
        StringBuilder answer = new StringBuilder("");
        char[] v = currStr.toCharArray();
        int n = cin.nextInt();
        for(int i = 0; i < n; i++){
            int k = cin.nextInt(), d = cin.nextInt();
            if(d == 1 || k == 1 || k == 2){
                answer.append(v).append("\n");
            }else {
                int[] posCurr = binpow(k, d, v.length - k + 1, v.length);
                int[] pos = new int[posCurr.length];
                for (int j = 0; j < v.length; j++) {
                    pos[j] = posCurr[(j + k - 1) % v.length];
                }
                char[] currv = new char[v.length];
                for (int j = 0; j < currv.length; j++) {
                    currv[j] = v[pos[j]];
                }
                v = currv;
                answer.append(v).append("\n");
            }
        }
        cout.print(answer.toString());
        cout.close();
    }

}


import java.io.PrintWriter;
import java.util.Scanner;

public class E {
    public static final Scanner cin = new Scanner(System.in);
    public static final PrintWriter cout = new PrintWriter(System.out);

    public static void P(int[] pos, int k, int d){
        int[] newPos = new int[pos.length];
        int begpos = 0;
        for(int i = 0; i < d; i++){
            for(int j = 0; j < k / d + ((k % d != 0) ? 1 : 0); j++){
                if(j * d + i < k) {
                    newPos[begpos] = pos[j * d + i];
                    begpos++;
                }
            }
        }
        System.arraycopy(newPos, 0, pos, 0, begpos);
    }

    public static void C(int[] pos){
        int tmp = pos[0];
        for(int i = 0; i < pos.length - 1; i++){
            pos[i] = pos[i + 1];
        }
        pos[pos.length - 1] = tmp;
    }

    public static int[] mul(int[] first, int second[]){
        int[] res = new int[first.length];
        for(int i = 0; i < first.length; i++){
            res[i] = second[first[i]];
        }
        return res;
    }

    public static int[] binpow(int k, int d, int pow, int len){
        if(pow == 1){
            int[] pos = new int[len];
            for(int i = 0; i < len; i++){
                pos[i] = i;
            }
            P(pos, k, d);
            C(pos);
            return pos;
        }
        int[] res;
        if(pow % 2 == 1){
            int[] first = binpow(k, d, 1, len);
            int[] second = binpow(k, d, pow - 1, len);
            res = mul(first, second);
        }else {
            int[] first = binpow(k, d, pow / 2, len);
            res = mul(first, first);
        }
        return res;
    }

    public static void main(String[] args) {
        char[] v = cin.next().toCharArray();
        int n = cin.nextInt();
        for(int i = 0; i < n; i++){
            int k = cin.nextInt(), d = cin.nextInt();
            if(d == 1){
                for (int j = 0; j < v.length; j++) {
                    System.out.print(v[j]);
                }
                System.out.println();
            }else {
                int[] posCurr = binpow(k, d, v.length - k + 1, v.length);
                int[] pos = new int[posCurr.length];
                for (int j = 0; j < v.length; j++) {
                    pos[j] = posCurr[(j + k - 1) % v.length];
                }
                char[] currv = new char[v.length];
                for (int j = 0; j < currv.length; j++) {
                    currv[j] = v[pos[j]];
                    System.out.print(currv[j]);
                }
                System.out.println();
                v = currv;
            }
        }
    }

}


import java.util.Scanner;

public class E {
    public static final Scanner cin = new Scanner(System.in);

    public static void P(int[] pos, int k, int d){
        int[] newPos = new int[pos.length];
        int begpos = 0;
        for(int i = 0; i < d; i++){
            for(int j = 0; j < k / d + ((k % d != 0) ? 1 : 0); j++){
                if(j * d + i < k) {
                    newPos[begpos] = pos[j * d + i];
                    begpos++;
                }
            }
        }
        System.arraycopy(newPos, 0, pos, 0, begpos);
    }

    public static void C(int[] pos){
        int tmp = pos[0];
        for(int i = 0; i < pos.length - 1; i++){
            pos[i] = pos[i + 1];
        }
        pos[pos.length - 1] = tmp;
    }

    public static int[] mul(int[] first, int second[]){
        int[] res = new int[first.length];
        for(int i = 0; i < first.length; i++){
            res[i] = second[first[i]];
        }
        return res;
    }

    public static int[] binpow(int k, int d, int pow, int len){
        if(pow == 1){
            int[] pos = new int[len];
            for(int i = 0; i < len; i++){
                pos[i] = i;
            }
            P(pos, k, d);
            C(pos);
            return pos;
        }
        int[] res;
        if(pow % 2 == 1){
            int[] first = binpow(k, d, 1, len);
            int[] second = binpow(k, d, pow - 1, len);
            res = mul(first, second);
        }else {
            int[] first = binpow(k, d, pow / 2, len);
            res = mul(first, first);
        }
        return res;
    }

    public static void main(String[] args) {
        StringBuilder answer = new StringBuilder("");
        char[] v = cin.nextLine().toCharArray();
        int n = cin.nextInt();
        int[][] req = new int[n][2];
        for(int i = 0; i < n; i++){
            String r = cin.nextLine();
            if(r.isEmpty()){
                i--;
                continue;
            }
            String[] q = r.split(" ");
            req[i][0] = Integer.parseInt(q[0]);
            req[i][1] = Integer.parseInt(q[1]);
        }

        for(int i = 0; i < n; i++){
            int k = req[i][0], d = req[i][1];
            if(d == 1 || k == 1 || k == 2){
                answer.append(v).append("\n");
            }else {
                int[] posCurr = binpow(k, d, v.length - k + 1, v.length);
                int[] pos = new int[posCurr.length];
                for (int j = 0; j < v.length; j++) {
                    pos[j] = posCurr[(j + k - 1) % v.length];
                }
                char[] currv = new char[v.length];
                for (int j = 0; j < currv.length; j++) {
                    currv[j] = v[pos[j]];
                }
                v = currv;
                answer.append(v).append("\n");
            }
        }
        System.out.print(answer.toString());
    }

}


import java.util.Scanner;

public class E {
    public static final Scanner cin = new Scanner(System.in);

    public static void P(int[] pos, int k, int d){
        int[] newPos = new int[pos.length];
        int begpos = 0;
        for(int i = 0; i < d; i++){
            for(int j = 0; j < k / d + ((k % d != 0) ? 1 : 0); j++){
                if(j * d + i < k) {
                    newPos[begpos] = pos[j * d + i];
                    begpos++;
                }
            }
        }
        System.arraycopy(newPos, 0, pos, 0, begpos);
    }

    public static void C(int[] pos){
        int tmp = pos[0];
        for(int i = 0; i < pos.length - 1; i++){
            pos[i] = pos[i + 1];
        }
        pos[pos.length - 1] = tmp;
    }

    public static int[] mul(int[] first, int second[]){
        int[] res = new int[first.length];
        for(int i = 0; i < first.length; i++){
            res[i] = second[first[i]];
        }
        return res;
    }

    public static int[] binpow(int k, int d, int pow, int len){
        if(pow == 1){
            int[] pos = new int[len];
            for(int i = 0; i < len; i++){
                pos[i] = i;
            }
            P(pos, k, d);
            C(pos);
            return pos;
        }
        int[] res;
        if(pow % 2 == 1){
            int[] first = binpow(k, d, 1, len);
            int[] second = binpow(k, d, pow - 1, len);
            res = mul(first, second);
        }else {
            int[] first = binpow(k, d, pow / 2, len);
            res = mul(first, first);
        }
        return res;
    }

    public static void main(String[] args) {
        StringBuilder answer = new StringBuilder("");
        char[] v = cin.next().toCharArray();
        int n = cin.nextInt();
        for(int i = 0; i < n; i++){
            int k = cin.nextInt(), d = cin.nextInt();
            if(d == 1 || k == 1 || k == 2){
                answer.append(v).append("\n");
            }else {
                int[] posCurr = binpow(k, d, v.length - k + 1, v.length);
                int[] pos = new int[posCurr.length];
                for (int j = 0; j < v.length; j++) {
                    pos[j] = posCurr[(j + k - 1) % v.length];
                }
                char[] currv = new char[v.length];
                for (int j = 0; j < currv.length; j++) {
                    currv[j] = v[pos[j]];
                }
                v = currv;
                answer.append(v).append("\n");
            }
        }
        System.out.print(answer.toString());
    }

}


import java.io.*;
import java.util.StringTokenizer;

/**
 * Created by stanislav on 11/6/14.
 */
public class C484C implements Runnable {
    public static void main(String[] args) {
        new Thread(new C484C()).start();
    }
    public void run() {
        try {
            solve();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

    class FastIO {
        private final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        private final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
        private StringTokenizer tokenizer = null;

        String nextToken() throws IOException {
            if (tokenizer == null || !tokenizer.hasMoreTokens()) {
                tokenizer = new StringTokenizer(reader.readLine());
            }
            return tokenizer.nextToken();
        }

        int nextInt() throws IOException {
            return Integer.parseInt(nextToken());
        }

        void println(String s) throws IOException {
            writer.write(s + '\n');
        }
    }

    private void solve() throws IOException {
        FastIO io = new FastIO();
        String string = io.nextToken();
        int numQueries = io.nextInt();
        int[] sort = new int[string.length()];
        int[] shift = new int[string.length()];
        int[] buffer = new int[string.length()];
        int[] sortAll = new int[string.length()];
        int[] sorted = new int[string.length()];
        int[] inverse = new int[string.length()];

        for (int queryIndex = 0; queryIndex < numQueries; ++queryIndex) {
            int k = io.nextInt();
            int d = io.nextInt();
            int cnt = 0;
            for (int i = 0; i < d; ++i) {
                for (int j = i; j < k; j += d) {
                    sort[j] = cnt++;
                }
            }
            while (cnt < string.length()) {
                sort[cnt] = cnt;
                ++cnt;
            }
            for (int i = 0; i < string.length(); ++i) {
                shift[(i + 1) % string.length()] = i;
            }

            multiply(sort, shift, buffer);
            pow(buffer, string.length() - k + 1, sortAll);

            int[] reverse = new int[string.length()];
            for (int i = 0; i < string.length(); ++i) {
                reverse[i] = (i + string.length() - k + 1) % string.length();
            }
            multiply(sortAll, reverse, sorted);
            for (int i = 0; i < string.length(); ++i) {
                inverse[sorted[i]] = i;
            }
            StringBuffer answer = new StringBuffer();
            for (int i = 0; i < string.length(); ++i) {
                answer.append(string.charAt(inverse[i]));
            }
            string = answer.toString();
            io.println(string);
        }
    }

    void copy(final int[] src, int[] dst) {
        assert src.length == dst.length;
        for (int i = 0; i < dst.length; ++i) {
            dst[i] = src[i];
        }
    }

    void pow(final int[] a, int k, int[] result) {
        for (int i = 0; i < a.length; ++i) {
            result[i] = i;
        }
        int[] multiplier = new int[a.length];
        copy(a, multiplier);
        int[] buffer = new int[result.length];
        while (k > 0) {
            if ((k & 1) > 0) {
                multiply(result, multiplier, buffer);
                copy(buffer, result);
            }
            multiply(multiplier, multiplier, buffer);
            copy(buffer, multiplier);
            k >>= 1;
        }
    }

    void multiply(final int[] a, final int[] b, int[] out) {
        int n = a.length;
        assert n == b.length;
        for (int i = 0; i < n; ++i) {
            out[i] = b[a[i]];
        }
    }
}


import java.io.*;
import java.util.Arrays;
import java.util.Scanner;
import java.util.StringTokenizer;

/**
 * Created by stanislav on 11/6/14.
 */
public class C484C implements Runnable {
    public static void main(String[] args) {
        new Thread(new C484C()).start();
    }
    public void run() {
        try {
            solve();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

    class FastReader {
        private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        private StringTokenizer tokenizer = null;

        String nextToken() throws IOException {
            if (tokenizer == null || !tokenizer.hasMoreTokens()) {
                tokenizer = new StringTokenizer(reader.readLine());
            }
            return tokenizer.nextToken();
        }

        int nextInt() throws IOException {
            return Integer.parseInt(nextToken());
        }
    }

    private void solve() throws IOException {
        FastReader reader = new FastReader();
        String string = reader.nextToken();
        int numQueries = reader.nextInt();
        int[] sort = new int[string.length()];
        int[] shift = new int[string.length()];
        int[] buffer = new int[string.length()];
        int[] sortAll = new int[string.length()];
        int[] sorted = new int[string.length()];
        int[] inverse = new int[string.length()];

        for (int queryIndex = 0; queryIndex < numQueries; ++queryIndex) {
            int k = reader.nextInt();
            int d = reader.nextInt();
            int cnt = 0;
            for (int i = 0; i < d; ++i) {
                for (int j = i; j < k; j += d) {
                    sort[j] = cnt++;
                }
            }
            while (cnt < string.length()) {
                sort[cnt] = cnt;
                ++cnt;
            }
            for (int i = 0; i < string.length(); ++i) {
                shift[(i + 1) % string.length()] = i;
            }

            multiply(sort, shift, buffer);
            pow(buffer, string.length() - k + 1, sortAll);

            int[] reverse = new int[string.length()];
            for (int i = 0; i < string.length(); ++i) {
                reverse[i] = (i + string.length() - k + 1) % string.length();
            }
            multiply(sortAll, reverse, sorted);
            for (int i = 0; i < string.length(); ++i) {
                inverse[sorted[i]] = i;
            }
            StringBuffer answer = new StringBuffer();
            for (int i = 0; i < string.length(); ++i) {
                answer.append(string.charAt(inverse[i]));
            }
            System.out.println(answer);
            string = answer.toString();
        }
    }

    void copy(final int[] src, int[] dst) {
        assert src.length == dst.length;
        for (int i = 0; i < dst.length; ++i) {
            dst[i] = src[i];
        }
    }

    void pow(final int[] a, int k, int[] result) {
        for (int i = 0; i < a.length; ++i) {
            result[i] = i;
        }
        int[] multiplier = new int[a.length];
        copy(a, multiplier);
        int[] buffer = new int[result.length];
        while (k > 0) {
            if ((k & 1) > 0) {
                multiply(result, multiplier, buffer);
                copy(buffer, result);
            }
            multiply(multiplier, multiplier, buffer);
            copy(buffer, multiplier);
            k >>= 1;
        }
    }

    void multiply(final int[] a, final int[] b, int[] out) {
        int n = a.length;
        assert n == b.length;
        for (int i = 0; i < n; ++i) {
            out[i] = b[a[i]];
        }
    }
}


import java.io.*;
import java.util.StringTokenizer;

/**
 * Created by stanislav on 11/6/14.
 */
public class C484C implements Runnable {
    public static void main(String[] args) {
        new Thread(new C484C()).start();
    }
    public void run() {
        try {
            solve();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

    class FastIO {
        private final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        private final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
        private StringTokenizer tokenizer = null;

        String nextToken() throws IOException {
            if (tokenizer == null || !tokenizer.hasMoreTokens()) {
                tokenizer = new StringTokenizer(reader.readLine());
            }
            return tokenizer.nextToken();
        }

        int nextInt() throws IOException {
            return Integer.parseInt(nextToken());
        }

        void println(String s) throws IOException {
            writer.write(s + '\n');
        }

        void complete() throws IOException {
            writer.close();
        }
    }

    private void solve() throws IOException {
        FastIO io = new FastIO();
        String string = io.nextToken();
        int numQueries = io.nextInt();
        int[] sort = new int[string.length()];
        int[] shift = new int[string.length()];
        int[] buffer = new int[string.length()];
        int[] sortAll = new int[string.length()];
        int[] sorted = new int[string.length()];
        int[] inverse = new int[string.length()];

        for (int queryIndex = 0; queryIndex < numQueries; ++queryIndex) {
            int k = io.nextInt();
            int d = io.nextInt();
            int cnt = 0;
            for (int i = 0; i < d; ++i) {
                for (int j = i; j < k; j += d) {
                    sort[j] = cnt++;
                }
            }
            while (cnt < string.length()) {
                sort[cnt] = cnt;
                ++cnt;
            }
            for (int i = 0; i < string.length(); ++i) {
                shift[(i + 1) % string.length()] = i;
            }

            multiply(sort, shift, buffer);
            pow(buffer, string.length() - k + 1, sortAll);

            int[] reverse = new int[string.length()];
            for (int i = 0; i < string.length(); ++i) {
                reverse[i] = (i + string.length() - k + 1) % string.length();
            }
            multiply(sortAll, reverse, sorted);
            for (int i = 0; i < string.length(); ++i) {
                inverse[sorted[i]] = i;
            }
            StringBuffer answer = new StringBuffer();
            for (int i = 0; i < string.length(); ++i) {
                answer.append(string.charAt(inverse[i]));
            }
            string = answer.toString();
            io.println(string);
        }
        io.complete();
    }

    void copy(final int[] src, int[] dst) {
        assert src.length == dst.length;
        for (int i = 0; i < dst.length; ++i) {
            dst[i] = src[i];
        }
    }

    void pow(final int[] a, int k, int[] result) {
        for (int i = 0; i < a.length; ++i) {
            result[i] = i;
        }
        int[] multiplier = new int[a.length];
        copy(a, multiplier);
        int[] buffer = new int[result.length];
        while (k > 0) {
            if ((k & 1) > 0) {
                multiply(result, multiplier, buffer);
                copy(buffer, result);
            }
            multiply(multiplier, multiplier, buffer);
            copy(buffer, multiplier);
            k >>= 1;
        }
    }

    void multiply(final int[] a, final int[] b, int[] out) {
        int n = a.length;
        assert n == b.length;
        for (int i = 0; i < n; ++i) {
            out[i] = b[a[i]];
        }
    }
}


import java.util.Arrays;
import java.util.Scanner;

/**
 * Created by stanislav on 11/6/14.
 */
public class C484C implements Runnable {
    public static void main(String[] args) {
        new Thread(new C484C()).start();
    }
    public void run() {
        try {
            solve();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

    private Scanner in = new Scanner(System.in);

    private void solve() {
        String string = in.nextLine();
        int numQueries = Integer.parseInt(in.nextLine());
        for (int queryIndex = 0; queryIndex < numQueries; ++queryIndex) {
            int k = in.nextInt();
            int d = in.nextInt();
            int[] sort = new int[string.length()];
            int cnt = 0;
            for (int i = 0; i < d; ++i) {
                for (int j = i; j < k; j += d) {
                    sort[j] = cnt++;
                }
            }
            while (cnt < string.length()) {
                sort[cnt] = cnt;
                ++cnt;
            }
            //System.err.println("sort = " + Arrays.toString(sort));
            int[] shift = new int[string.length()];
            for (int i = 0; i < string.length(); ++i) {
                shift[(i + 1) % string.length()] = i;
            }
            int[] sortAll = pow(multiply(sort, shift), string.length() - k + 1);
            //System.err.println("sortAll = " + Arrays.toString(sortAll));
            int[] reverse = new int[string.length()];
            for (int i = 0; i < string.length(); ++i) {
                reverse[i] = (i + string.length() - k + 1) % string.length();
            }
            int[] sorted = multiply(sortAll, reverse);
            int[] inverse = new int[string.length()];
            for (int i = 0; i < string.length(); ++i) {
                inverse[sorted[i]] = i;
            }
            StringBuffer answer = new StringBuffer();
            for (int i = 0; i < string.length(); ++i) {
                answer.append(string.charAt(inverse[i]));
            }
            System.out.println(answer);
            string = answer.toString();
        }
    }

    int[] pow(int[] a, int k) {
        int[] result = new int[a.length];
        for (int i = 0; i < a.length; ++i) {
            result[i] = i;
        }
        while (k > 0) {
            if ((k & 1) > 0) {
                result = multiply(result, a);
            }
            a = multiply(a, a);
            k >>= 1;
        }
        return result;
    }
    int[] multiply(int[] a, int[] b) {
        int n = a.length;
        assert n == b.length;
        int[] result = new int[n];
        for (int i = 0; i < n; ++i) {
            result[i] = b[a[i]];
        }
        return result;
    }
}


import java.util.Arrays;
import java.util.Scanner;

/**
 * Created by stanislav on 11/6/14.
 */
public class C484C implements Runnable {
    public static void main(String[] args) {
        new Thread(new C484C()).start();
    }
    public void run() {
        try {
            solve();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

    private Scanner in = new Scanner(System.in);

    private void solve() {
        String string = in.nextLine();
        int numQueries = Integer.parseInt(in.nextLine());
        int[] sort = new int[string.length()];
        int[] shift = new int[string.length()];
        int[] buffer = new int[string.length()];
        int[] sortAll = new int[string.length()];
        int[] sorted = new int[string.length()];
        int[] inverse = new int[string.length()];

        for (int queryIndex = 0; queryIndex < numQueries; ++queryIndex) {
            int k = in.nextInt();
            int d = in.nextInt();
            int cnt = 0;
            for (int i = 0; i < d; ++i) {
                for (int j = i; j < k; j += d) {
                    sort[j] = cnt++;
                }
            }
            while (cnt < string.length()) {
                sort[cnt] = cnt;
                ++cnt;
            }
            for (int i = 0; i < string.length(); ++i) {
                shift[(i + 1) % string.length()] = i;
            }

            multiply(sort, shift, buffer);
            pow(buffer, string.length() - k + 1, sortAll);

            int[] reverse = new int[string.length()];
            for (int i = 0; i < string.length(); ++i) {
                reverse[i] = (i + string.length() - k + 1) % string.length();
            }
            multiply(sortAll, reverse, sorted);
            for (int i = 0; i < string.length(); ++i) {
                inverse[sorted[i]] = i;
            }
            StringBuffer answer = new StringBuffer();
            for (int i = 0; i < string.length(); ++i) {
                answer.append(string.charAt(inverse[i]));
            }
            System.out.println(answer);
            string = answer.toString();
        }
    }

    void copy(final int[] src, int[] dst) {
        assert src.length == dst.length;
        for (int i = 0; i < dst.length; ++i) {
            dst[i] = src[i];
        }
    }

    void pow(final int[] a, int k, int[] result) {
        for (int i = 0; i < a.length; ++i) {
            result[i] = i;
        }
        int[] multiplier = new int[a.length];
        copy(a, multiplier);
        int[] buffer = new int[result.length];
        while (k > 0) {
            if ((k & 1) > 0) {
                multiply(result, multiplier, buffer);
                copy(buffer, result);
            }
            multiply(multiplier, multiplier, buffer);
            copy(buffer, multiplier);
            k >>= 1;
        }
    }

    void multiply(final int[] a, final int[] b, int[] out) {
        int n = a.length;
        assert n == b.length;
        for (int i = 0; i < n; ++i) {
            out[i] = b[a[i]];
        }
    }
}


//package codeforces;

import java.io.BufferedReader;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;

public class C implements Closeable {
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    PrintWriter writer = new PrintWriter(System.out);
    StringTokenizer stringTokenizer;

    C() throws IOException {
//        reader = new BufferedReader(new FileReader("input.txt"));
//        writer = new PrintWriter(new FileWriter("bridges.out"));
    }

    String next() throws IOException {
        while (stringTokenizer == null || !stringTokenizer.hasMoreTokens()) {
            stringTokenizer = new StringTokenizer(reader.readLine());
        }
        return stringTokenizer.nextToken();
    }

    double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }

    int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    final int MOD = 1000 * 1000 + 3;

    int sum(int a, int b) {
        a += b;
        return a >= MOD ? a - MOD : a;
    }

    int product(int a, int b) {
        return (int) (1l * a * b % MOD);
    }

    int pow(int x, int k) {
        int result = 1;
        while (k > 0) {
            if (k % 2 == 1) {
                result = product(result, x);
            }
            x = product(x, x);
            k /= 2;
        }
        return result;
    }

    int inv(int x) {
        return pow(x, MOD - 2);
    }

    void solve() throws IOException {
        char[] str = next().toCharArray();
        final int n = str.length;
        int m = nextInt();
        class Util {
            int[] buffer = new int[n];

            int[][] getCycles(int[] r) {
                int k = r.length;
                boolean[] used = new boolean[k];
                List<int[]> cycles = new ArrayList<>(k);
                for(int i = 0; i < k; i++) {
                    if(!used[i]) {
                        int bi = 0;
                        for(int j = i; j != k && !used[j]; j = r[j]) {
                            used[j] = true;
                            buffer[bi++] = j;
                        }
                        cycles.add(Arrays.copyOf(buffer, bi));
                    }
                }
                return cycles.toArray(new int[cycles.size()][]);
            }
        }
        Util util = new Util();
        for(int qq = 0; qq < m; qq++) {
            int k = nextInt();
            int d = nextInt();
            int[] p = new int[k];
            int goesTo = -1;
            for(int i = 0; i < d; i++) {
                for(int j = i; j < k; j += d) {
                    p[j] = goesTo++;
                }
            }
            int[] r = new int[k];
            Arrays.fill(r, k);
            for(int i = 1; i < k; i++) {
                r[p[i]] = i;
            }
            int[][] cycles = util.getCycles(r);
            char[] s = new char[n];
            int[] chain = cycles[0];
            for(int i = 0; i < n - k + 1; i++) {
                s[i] = i < chain.length ? str[chain[i]] : str[k + i - chain.length];
            }
            int offset = n - k;
            int stayInChain = Math.max(0, chain.length - (n - k));
            int addToChain = chain.length - stayInChain;
            for(int i = n - k; i < chain.length; i++) {
                s[offset + chain[i - (n - k)]] = str[chain[i]];
            }
            for(int i = 0; i < addToChain; i++) {
                s[offset + chain[chain.length - 1 - i]] = str[n - 1 - i];
            }
            for(int ci = 1; ci < cycles.length; ci++) {
                int[] cycle = cycles[ci];
                int rotations = (n - k) % cycle.length;
                for(int i = 0; i < cycle.length; i++) {
                    int j = (i - rotations + cycle.length) % cycle.length;
                    s[offset + cycle[j]] = str[cycle[i]];
                }
            }
            goesTo = 0;
            for(int i = 0; i < d; i++) {
                for(int j = i; j < k; j += d) {
                    str[goesTo++] = s[offset + j];
                }
            }
            System.arraycopy(str, 0, s, offset, k);
            writer.println(s);
            str = s;
        }
    }

    public static void main(String[] args) throws IOException {
        try(C c = new C()) {
            c.solve();
        }
    }

    @Override
    public void close() throws IOException {
        reader.close();
        writer.close();
    }
}


//package codeforces;

import java.io.BufferedReader;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;

public class C implements Closeable {
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    PrintWriter writer = new PrintWriter(System.out);
    StringTokenizer stringTokenizer;

    C() throws IOException {
//        reader = new BufferedReader(new FileReader("input.txt"));
//        writer = new PrintWriter(new FileWriter("bridges.out"));
    }

    String next() throws IOException {
        while (stringTokenizer == null || !stringTokenizer.hasMoreTokens()) {
            stringTokenizer = new StringTokenizer(reader.readLine());
        }
        return stringTokenizer.nextToken();
    }

    double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }

    int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    final int MOD = 1000 * 1000 + 3;

    int sum(int a, int b) {
        a += b;
        return a >= MOD ? a - MOD : a;
    }

    int product(int a, int b) {
        return (int) (1l * a * b % MOD);
    }

    int pow(int x, int k) {
        int result = 1;
        while (k > 0) {
            if (k % 2 == 1) {
                result = product(result, x);
            }
            x = product(x, x);
            k /= 2;
        }
        return result;
    }

    int inv(int x) {
        return pow(x, MOD - 2);
    }

    void solve() throws IOException {
        char[] str = next().toCharArray();
        final int n = str.length;
        int m = nextInt();
        class Util {
            int[] buffer = new int[n];

            int[][] getCycles(int[] r) {
                int k = r.length;
                boolean[] used = new boolean[k];
                List<int[]> cycles = new ArrayList<>(k);
                for(int i = 0; i < k; i++) {
                    if(!used[i]) {
                        int bi = 0;
                        for(int j = i; j != k && !used[j]; j = r[j]) {
                            used[j] = true;
                            buffer[bi++] = j;
                        }
                        cycles.add(Arrays.copyOf(buffer, bi));
                    }
                }
                return cycles.toArray(new int[cycles.size()][]);
            }
        }
        Util util = new Util();
        for(int qq = 0; qq < m; qq++) {
            int k = nextInt();
            int d = nextInt();
            int[] p = new int[k];
            int goesTo = -1;
            for(int i = 0; i < d; i++) {
                for(int j = i; j < k; j += d) {
                    p[j] = goesTo++;
                }
            }
            int[] r = new int[k];
            Arrays.fill(r, k);
            for(int i = 1; i < k; i++) {
                r[p[i]] = i;
            }
            int[][] cycles = util.getCycles(r);
            char[] s = new char[n];
            int[] chain = cycles[0];
            for(int i = 0; i < n - k + 1; i++) {
                s[i] = i < chain.length ? str[chain[i]] : str[k + i];
            }
            int offset = n - k;
            int stayInChain = Math.max(0, chain.length - (n - k));
            int addToChain = chain.length - stayInChain;
            for(int i = n - k; i < chain.length; i++) {
                s[offset + chain[i - (n - k)]] = str[chain[i]];
            }
            for(int i = 0; i < addToChain; i++) {
                s[offset + chain[chain.length - 1 - i]] = str[n - 1 - i];
            }
            for(int ci = 1; ci < cycles.length; ci++) {
                int[] cycle = cycles[ci];
                int rotations = (n - k) % cycle.length;
                for(int i = 0; i < cycle.length; i++) {
                    int j = (i - rotations + cycle.length) % cycle.length;
                    s[offset + cycle[j]] = str[cycle[i]];
                }
            }
            goesTo = 0;
            for(int i = 0; i < d; i++) {
                for(int j = i; j < k; j += d) {
                    str[goesTo++] = s[offset + j];
                }
            }
            System.arraycopy(str, 0, s, offset, k);
            writer.println(s);
            str = s;
        }
    }

    public static void main(String[] args) throws IOException {
        try(C c = new C()) {
            c.solve();
        }
    }

    @Override
    public void close() throws IOException {
        reader.close();
        writer.close();
    }
}


import java.io.*;
import java.util.*;

public class strangesorting {
  private static BufferedReader in;
  private static PrintWriter out;
  public static int N;
  public static char[] let;
  public static int[] shiftright;
  public static StringBuffer ans;

  public static void shuffle (int k, int d) {
    int[] perm = new int[N];
    int idx = 0;
    for (int i = 0; i < d; i++) {
      for (int j = i; j < k; j += d) {
        perm[j] = idx++;
      }
    }
    for (int i = k; i < N; i++) perm[i] = i;
    perm = mult(shiftright, perm);

    int pow = N-k;
    int[] res = new int[N];
    System.arraycopy(perm, 0, res, 0, N);
    while (pow > 0) {
      if ((pow & 1) == 1) res = mult(res, perm);
      perm = mult(perm, perm);
      pow >>= 1;
    }

    char[] nlet = new char[N];
    for (int i = 0; i < N; i++) {
      nlet[(res[i] + N-k+1) % N] = let[i];
    }
    let = nlet;
    for (int i = 0; i < N; i++) {
      ans.append(let[i]);
    }
    ans.append("\n");
  }

  public static int[] mult(int[] a, int[] b) {
    int[] c = new int[N];
    for (int i = 0; i < N; i++) {
      c[i] = a[b[i]];
    }
    return c;
  }

  public static void main(String[] args) throws IOException {
    in = new BufferedReader(new InputStreamReader(System.in));
    out = new PrintWriter(System.out);

//    long start = System.currentTimeMillis();
    let = in.readLine().toCharArray();
    N = let.length;
    shiftright = new int[N];
    shiftright[0] = N-1;
    for (int i = 1; i < N; i++) shiftright[i] = i-1;
    int Q = Integer.parseInt(in.readLine());
    ans = new StringBuffer();
    StringTokenizer st;
    while (Q-- > 0) {
      st = new StringTokenizer(in.readLine());
      int K = Integer.parseInt(st.nextToken()), D = Integer.parseInt(st.nextToken());
      shuffle(K, D);
    }
    out.println(ans.toString());
//    System.out.println("Time: " + (System.currentTimeMillis() - start));
    out.close();
    System.exit(0);
  }
}


import java.io.*;
import java.util.*;

public class strangesorting {
  private static BufferedReader in;
  private static PrintWriter out;
  public static int N;
  public static char[] let;
  public static int[] shiftright;

  public static void shuffle (int k, int d) {
    int[] perm = new int[N];
    int idx = 0;
    for (int i = 0; i < d; i++) {
      for (int j = i; j < k; j += d) {
        perm[j] = idx++;
      }
    }
    for (int i = k; i < N; i++) perm[i] = i;
    perm = mult(shiftright, perm);

    int pow = N-k;
    int[] res = new int[N];
    System.arraycopy(perm, 0, res, 0, N);
    while (pow > 0) {
      if ((pow & 1) == 1) res = mult(res, perm);
      perm = mult(perm, perm);
      pow >>= 1;
    }

    char[] nlet = new char[N];
    for (int i = 0; i < N; i++) {
      nlet[(res[i] + N-k+1) % N] = let[i];
    }
    let = nlet;
    for (int i = 0; i < N; i++)
      out.print(let[i]);
    out.println();
  }

  public static int[] mult(int[] a, int[] b) {
    int[] c = new int[N];
    for (int i = 0; i < N; i++) {
      c[i] = a[b[i]];
    }
    return c;
  }

  public static void main(String[] args) throws IOException {
    in = new BufferedReader(new InputStreamReader(System.in));
    out = new PrintWriter(System.out, true);

    let = in.readLine().toCharArray();
    N = let.length;
    shiftright = new int[N];
    shiftright[0] = N-1;
    for (int i = 1; i < N; i++) shiftright[i] = i-1;
    int Q = Integer.parseInt(in.readLine());
    StringTokenizer st;
    while (Q-- > 0) {
      st = new StringTokenizer(in.readLine());
      int K = Integer.parseInt(st.nextToken()), D = Integer.parseInt(st.nextToken());
      shuffle(K, D);
    }
    out.close();
    System.exit(0);
  }
}


import java.io.*;
import java.util.*;

public class strangesorting {
  private static BufferedReader in;
  private static PrintWriter out;
  public static int N;
  public static char[] let;

  public static void shuffle (int k, int d) {
    int[] perm = new int[N];
    int idx = 0;
    for (int i = 0; i < d; i++) {
      for (int j = i; j < k; j += d) {
        perm[j] = idx++;
      }
    }
    for (int i = k; i < N; i++) perm[i] = i;
    int[] nperm = new int[N];
    nperm[0] = N-1;
    for (int i = 1; i < N; i++) nperm[i] = i-1;
    perm = mult(nperm, perm);

    int pow = N-k+1;
    int[] res = new int[N];
    for (int i = 0; i < N; i++) res[i] = i;
    while (pow > 0) {
      if ((pow & 1) == 1) res = mult(res, perm);
      perm = mult(perm, perm);
      pow >>= 1;
    }

    char[] nlet = new char[N];
    for (int i = 0; i < N; i++) {
      nlet[(res[i] + N-k+1) % N] = let[i];
    }
    String s = new String (nlet);
    let = s.toCharArray();
    out.println(s);
  }

  public static int[] mult(int[] a, int[] b) {
    int[] c = new int[N];
    for (int i = 0; i < N; i++) {
      c[i] = a[b[i]];
    }
    return c;
  }

  public static void main(String[] args) throws IOException {
    in = new BufferedReader(new InputStreamReader(System.in));
    out = new PrintWriter(System.out, true);
    let = in.readLine().toCharArray();
    N = let.length;
    int Q = Integer.parseInt(in.readLine());
    StringTokenizer st;
    while (Q-- > 0) {
      st = new StringTokenizer(in.readLine());
      int K = Integer.parseInt(st.nextToken()), D = Integer.parseInt(st.nextToken());
      shuffle(K, D);
    }
    out.close();
    System.exit(0);
  }
}


#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cctype>
using namespace std;

namespace zuiyt
{
	template<typename T>
	inline void read(T &x)
	{
		char c;
		bool f = false;
		x = 0;
		do
			c = getchar();
		while (c != '-' && !isdigit(c));
		if (c == '-')
			f = true, c = getchar();
		do
			x = x * 10 + c - '0', c = getchar();
		while (isdigit(c));
		if (f)
			x = -x;
	}
	inline void read(char *const s)
	{
		scanf("%s", s);
	}
	template<typename T>
	inline void write(T x)
	{
		static char buf[20];
		char *pos = buf;
		if (x < 0)
			putchar('-'), x = -x;
		do
			*pos++ = x % 10 + '0';
		while (x /= 10);
		while (pos > buf)
			putchar(*--pos);
	}
	inline void write(char *const s)
	{
		printf("%s", s);
	}
	const int N = 1e6 + 10;
	char s[N], t[N];
	int nowd, id[N], n, m;
	bool cmpd(const int a, const int b)
	{
		return a % nowd == b % nowd ? a < b : a % nowd < b % nowd;
	}
	void solvekd()
	{
		int k, d;
		static int to[N], tmp[N];
		read(k), read(d);
		nowd = d;
		for (int i = 0; i < n; i++)
			to[i] = i;
		for (int i = 0; i < k; i++)
			id[i] = i;
		sort(id, id + k, cmpd);
		for (int i = 0; i <= n - k; i++)
		{
			memcpy(tmp, to + i, sizeof(int[k]));
			for (int j = 0; j < k; j++)
				to[i + j] = tmp[id[j]];
		}
		memcpy(t, s, sizeof(char[n + 1]));
		for (int i = 0; i < n; i++)
			s[i] = t[to[i]];
		write(s), putchar('\n');
		while (--m)
		{
			memcpy(t, s, sizeof(char[n + 1]));
			read(k), read(d);
			for (int i = 0; i < n; i++)
				s[i] = t[to[i]];
			write(s), putchar('\n');
		}
	}
	int S[N], L[N], SL[N], to[N], tmp[N];//Sorting/Lefting
	char ctmp[N];
	int work()
	{
		read(s), read(m);
		n = strlen(s);
		/*if (n > 100)
			solvekd();*/
		while (m--)
		{
			int k, d;
			read(k), read(d);
			for (int i = 0; i < n; i++)
				to[i] = S[i] = i;
			for (int i = 0; i < n; i++)
				L[i] = (i < n - 1 ? i + 1 : 0);
			int pos = 0;
			for (int i = 0; i < d; i++)
				for (int j = i; j < k; j += d)
					S[pos++] = j;
			for (int i = 0; i < n; i++)
				SL[i] = S[L[i]];
			int b = n - k + 1;
			while (b)
			{
				if (b & 1)
				{
					memcpy(tmp, to, sizeof(int[n]));
					for (int i = 0; i < n; i++)
						to[i] = tmp[SL[i]];
				}
				memcpy(tmp, SL, sizeof(int[n]));
				for (int i = 0; i < n; i++)
					SL[i] = tmp[tmp[i]];
				b >>= 1;
			}
			memcpy(ctmp, s, sizeof(char[n]));
			for (int i = 0; i < n; i++)
				s[(i + n - k + 1) % n] = ctmp[to[i]];
			write(s), putchar('\n');
		}
		return 0;
	}
}
int main()
{
	return zuiyt::work();
}


#include <bits/stdc++.h>
#define pa pair<int,int>
#define mkp make_pair
using namespace std;
inline int read(){
	int k=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){k=k*10+ch-'0';ch=getchar();}
	return k*f;
}
inline void write(int x){
	if(x<0){putchar('-');x=-x;}if(x>9)write(x/10);
	putchar(x%10+'0');
}
inline void writeln(int x){
	write(x);puts("");
}
const int N=1e6+10;
char s[N],t[N];
int n,m,pt[21][N];
vector<int>e[N];
map<pa,int>mp;
inline int up(int x,int k){
	for(int i=20;i>=0;i--)if(k&(1<<i))x=pt[i][x];
	return x;
}
int main()
{
	scanf("%s",s);n=strlen(s);m=read();
	int cnt=0;
	for(int T=1;T<=m;T++){
		int k=read(),d=read();
		int ck;
		if(mp[mkp(k,d)])ck=mp[mkp(k,d)];
		else{
			ck=++cnt;mp[mkp(k,d)]=ck;
			int pnt=-1;
			for(int j=0;j<d;j++)
				for(int l=j;l<k;l+=d)pt[0][++pnt]=l+1;
			for(int i=k;i<n;i++)pt[0][i]=i+1;
			for(int i=1;i<21;i++)
				for(int j=0;j<n;j++)pt[i][j]=pt[i-1][pt[i-1][j]];
			for(int i=0;i<n-k;i++)e[ck].push_back(up(1,i)-1);
			for(int i=n-k;i<n;i++)e[ck].push_back(up(i-n+k,n-k+1)-1);
		}
		for(int i=0;i<n;i++)t[i]=s[e[ck][i]];
		for(int i=0;i<n;i++)s[i]=t[i];
		printf("%s\n",s);
	}
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define FOR(i, x, y) for(int i = (x); i < (y); ++i)
#define REP(i, x, y) for(int i = (x); i <= (y); ++i)
#define PB push_back
#define MP make_pair
#define PH push
#define fst first
#define snd second
typedef long long ll;
typedef unsigned long long ull;
typedef double lf;
typedef long double Lf;
typedef pair<int, int> pii;

const int maxn = 1e6 + 5;

int n, q;
char s[maxn], t[maxn];

class Permutation{
public:
	int p[maxn];
	inline Permutation(){ FOR(i, 0, n) p[i] = i; }
	inline int&operator [](const int &i){ return p[i]; }
	inline Permutation&operator *= (const Permutation &oth){
		Permutation ret = *this;
		FOR(i, 0, n)
			ret[i] = p[oth.p[i]];
		FOR(i, 0, n)
			p[i] = ret[i];
		return *this;
	}
}L, R;

inline Permutation qpow(Permutation P, int y){
	Permutation R;
	for(; y; y >>= 1){
		if(y & 1)
			R *= P;
		P *= P;
	}
	return R;
}

inline void solve(){
	int k, d;
	scanf("%d%d", &k, &d);
	Permutation P;
	for(int i = 0, r = 0, j = 0; i < k; ++i){
		P[i] = j;
		j += d;
		if(j >= k)
			j = ++r;
	}

	P *= R;
	P = qpow(P, n - k + 1);
	P *= qpow(L, n - k + 1);

	FOR(i, 0, n)
		t[i] = s[P[i]];
	FOR(i, 0, n)
		s[i] = t[i];
	puts(s);
	return;
}

int main(){
	scanf("%s", s);
	n = strlen(s);
	FOR(i, 0, n)
		R[i] = (i + 1) % n, L[i] = (i - 1 + n) % n;
	scanf("%d", &q);
	FOR(i, 0, q)
		solve();
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define FOR(i, x, y) for(int i = (x); i < (y); ++i)
#define REP(i, x, y) for(int i = (x); i <= (y); ++i)
#define PB push_back
#define MP make_pair
#define PH push
#define fst first
#define snd second
typedef long long ll;
typedef unsigned long long ull;
typedef double lf;
typedef long double Lf;
typedef pair<int, int> pii;

const int maxn = 1e6 + 5;

int n, q;
char s[maxn], t[maxn];

class Permutation{
public:
	int p[maxn];
	inline Permutation(){ FOR(i, 0, n) p[i] = i; }
	inline int&operator [](const int &i){ return p[i]; }
	inline Permutation&operator *= (const Permutation &oth){
		Permutation ret = *this;
		FOR(i, 0, n)
			ret[i] = p[oth.p[i]];
		(*this) = ret;
		return *this;
	}
}L, R;

inline Permutation qpow(Permutation P, int y){
	Permutation R;
	for(; y; y >>= 1){
		if(y & 1)
			R *= P;
		P *= P;
	}
	return R;
}

inline void solve(){
	int k, d;
	scanf("%d%d", &k, &d);
	Permutation P;
	for(int i = 0, r = 0, j = 0; i < k; ++i){
		P[i] = j;
		j += d;
		if(j >= k)
			j = ++r;
	}

	P *= R;
	P = qpow(P, n - k + 1);
	P *= qpow(L, n - k + 1);

	FOR(i, 0, n)
		t[i] = s[P[i]];
	FOR(i, 0, n)
		s[i] = t[i];
	puts(s);
	return;
}

int main(){
	scanf("%s", s);
	n = strlen(s);
	FOR(i, 0, n)
		R[i] = (i + 1) % n, L[i] = (i - 1 + n) % n;
	scanf("%d", &q);
	FOR(i, 0, q)
		solve();
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define FOR(i, x, y) for(int i = (x); i < (y); ++i)
#define REP(i, x, y) for(int i = (x); i <= (y); ++i)
#define PB push_back
#define MP make_pair
#define PH push
#define fst first
#define snd second
typedef long long ll;
typedef unsigned long long ull;
typedef double lf;
typedef long double Lf;
typedef pair<int, int> pii;

const int maxn = 1e6 + 5;

int n, q;
char s[maxn], t[maxn];

class Permutation{
public:
	vector<int> p;
	inline Permutation(){ p.resize(n); FOR(i, 0, n) p[i] = i; }
	inline int&operator [](const int &i){ return p[i]; }
	inline Permutation&operator *= (const Permutation &oth){
		Permutation ret = *this;
		FOR(i, 0, n)
			ret[i] = p[oth.p[i]];
		FOR(i, 0, n)
			p[i] = ret[i];
		return *this;
	}
}L, R;

inline Permutation qpow(Permutation P, int y){
	Permutation R;
	for(; y; y >>= 1){
		if(y & 1)
			R *= P;
		P *= P;
	}
	return R;
}

inline void solve(){
	int k, d;
	scanf("%d%d", &k, &d);
	Permutation P;
	for(int i = 0, r = 0, j = 0; i < k; ++i){
		P[i] = j;
		j += d;
		if(j >= k)
			j = ++r;
	}

	P *= R;
	P = qpow(P, n - k + 1);
	P *= qpow(L, n - k + 1);

	FOR(i, 0, n)
		t[i] = s[P[i]];
	FOR(i, 0, n)
		s[i] = t[i];
	puts(s);
	return;
}

int main(){
	scanf("%s", s);
	n = strlen(s);
	L = Permutation(), R = Permutation();
	FOR(i, 0, n)
		R[i] = (i + 1) % n, L[i] = (i - 1 + n) % n;
	scanf("%d", &q);
	FOR(i, 0, q)
		solve();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
inline char gc(){
	static char buf[100000],*p1=buf,*p2=buf;
	return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
#define gc getchar
#define ld long double
inline ll read(){
	ll x=0;char ch=gc();bool positive=1;
	for(;!isdigit(ch);ch=gc())if(ch=='-')positive=0;
	for(;isdigit(ch);ch=gc())x=x*10+ch-'0';
	return positive?x:-x;
}
inline void write(int x){
	if(x<0) putchar('-'),x=-x;
	if(x>=10) write(x/10);
	putchar(x%10+'0');
}
const int N=1000005,K=20;
int n,m,f[N][K];
char s[N],Alb[N];
int main(){
	scanf("%s",s);
	n=strlen(s);
	m=read();
	while(m--){
		int k=read(),d=read(),dq=-1;
		for(int i=0;i<d;i++)for(int j=i;j<k;j+=d)f[j][0]=dq++;
		for(int j=1;j<K;j++){
			for(register int i=0;i<k;i++){
				if(f[i][j-1]!=-1)f[i][j]=f[f[i][j-1]][j-1]; else f[i][j]=-1;
			}
		}
		for(int i=0;i<n;i++){
			if(i<k){
				int t=i,zt=0;
				for(register int j=K-1;j>=0;j--)if(f[t][j]!=-1&&zt+(1<<j)<=n-k+1){
					zt+=1<<j; t=f[t][j];
				}
				//cout<<(zt==n-k+1?n-k+t:zt)<<" "<<zt<<" "<<t<<endl;
				if(zt==n-k+1)Alb[n-k+t+1]=s[i]; else Alb[zt]=s[i];
			}else{
				int t=k-1,zt=0;
				for(register int j=K-1;j>=0;j--)if(f[t][j]!=-1&&zt+(1<<j)<=n-i){
					zt+=1<<j; t=f[t][j];
				}
				if(zt==n-i)Alb[n-k+t+1]=s[i]; else Alb[zt+i-k+1]=s[i];
			}
		}
		for(int j=0;j<n;j++)s[j]=Alb[j];
		printf("%s\n",s);
	}
}
/*
012345
021345
023145
023415
4->3->0
*/


/*
{
######################
#       Author       #
#        Gary        #
#        2020        #
######################
*/
//#pragma GCC target ("avx2")
//#pragma GCC optimization ("O3")
//#pragma GCC optimization ("unroll-loops")
#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define rb(a,b,c) for(int a=b;a<=c;++a)
#define rl(a,b,c) for(int a=b;a>=c;--a)
#define LL long long
#define IT iterator
#define PB push_back
#define II(a,b) make_pair(a,b)
#define FIR first
#define SEC second
#define FREO freopen("check.out","w",stdout)
#define rep(a,b) for(int a=0;a<b;++a)
#define SRAND mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define random(a) rng()%a
#define ALL(a) a.begin(),a.end()
#define POB pop_back
#define ff fflush(stdout)
#define fastio ios::sync_with_stdio(false)
#define check_min(a,b) a=min(a,b)
#define check_max(a,b) a=max(a,b)
using namespace std;
const int INF=0x3f3f3f3f;
typedef pair<int,int> mp;
/*}
*/
int n,m;
struct Move{
	vector<int>  moveto;//记录每一位到哪？
	Move operator * (Move B){
		Move ret;
		ret.moveto=vector<int>(moveto.size());
		rep(i,moveto.size())
			ret.moveto[i]=B.moveto[moveto[i]];
		return ret;
	}
};
Move quick(Move A,int times){
	Move ret;
	if(times==0){
		rep(i,n)
			ret.moveto.PB(i);
		return ret;
	}
	ret=quick(A,times>>1);
	ret=ret*ret;
	if(times&1)
		ret=ret*A;
	return ret;
}

int main(){
	string s;
	string rest;
	cin>>s;
	n=s.length();
	scanf("%d",&m);
	rb(Q,1,m){
		int k,d;
		scanf("%d%d",&k,&d);
		rest=s;
		Move To;
		To.moveto=vector<int> (n);
		vector<int> test(n);
		rep(i,n)
			test[i]=i;
		int is=0;
		rep(i,d){
			for(int j=i;j<k;j+=d){
				test[is++]=j;
			}
		}
		for(int j=k;j<n;++j){
			test[j]=j;
		}
		test.PB(test[0]);
		rep(i,test.size()){
			if(i){
				To.moveto[test[i]]=i-1;
			}
		}
		To=quick(To,n-k+1);
		string fake;
		fake.clear();
		rep(i,n){
			rest[To.moveto[i]]=s[i];
		}
		int id=k-1;
		rep(i,n){
			fake+=rest[id];
			id++;
			id%=n;
		}
		rest=fake;
		cout<<fake<<endl;
		s=rest;
	}
	return 0;
}
/** 程序框架：
  *
  *
  *
  *
  **/


/*
{
######################
#       Author       #
#        Gary        #
#        2020        #
######################
*/
//#pragma GCC target ("avx2")
//#pragma GCC optimization ("O3")
//#pragma GCC optimization ("unroll-loops")
#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define rb(a,b,c) for(int a=b;a<=c;++a)
#define rl(a,b,c) for(int a=b;a>=c;--a)
#define LL long long
#define IT iterator
#define PB push_back
#define II(a,b) make_pair(a,b)
#define FIR first
#define SEC second
#define FREO freopen("check.out","w",stdout)
#define rep(a,b) for(int a=0;a<b;++a)
#define SRAND mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define random(a) rng()%a
#define ALL(a) a.begin(),a.end()
#define POB pop_back
#define ff fflush(stdout)
#define fastio ios::sync_with_stdio(false)
#define check_min(a,b) a=min(a,b)
#define check_max(a,b) a=max(a,b)
using namespace std;
const int INF=0x3f3f3f3f;
typedef pair<int,int> mp;
/*}
*/
int n,m;
struct Move{
	vector<int>  moveto;//记录每一位到哪？
	Move operator * (Move B){
		Move ret;
		ret.moveto=vector<int>(moveto.size());
		rep(i,moveto.size())
			ret.moveto[i]=B.moveto[moveto[i]];
		return ret;
	}
};
Move Emt;
Move quick(Move A,int times){
	Move ret;
	if(times==0){
		return Emt;
	}
	ret=quick(A,times>>1);
	ret=ret*ret;
	if(times&1)
		ret=ret*A;
	return ret;
}

int main(){
	string s;
	string rest;
	cin>>s;
	n=s.length();
	scanf("%d",&m);
	rep(i,n)
		Emt.moveto.PB(i);
	rb(Q,1,m){
		int k,d;
		scanf("%d%d",&k,&d);
		rest=s;
		Move To;
		To.moveto=vector<int> (n);
		vector<int> test(n);
		rep(i,n)
			test[i]=i;
		int is=0;
		rep(i,d){
			for(int j=i;j<k;j+=d){
				test[is++]=j;
			}
		}
		for(int j=k;j<n;++j){
			test[j]=j;
		}
		test.PB(test[0]);
		rep(i,test.size()){
			if(i){
				To.moveto[test[i]]=i-1;
			}
		}
		To=quick(To,n-k+1);
		string fake;
		fake.clear();
		rep(i,n){
			rest[To.moveto[i]]=s[i];
		}
		int id=k-1;
		rep(i,n){
			fake+=rest[id];
			id++;
			id%=n;
		}
		rest=fake;
		printf("%s\n",fake.c_str());
//		cout<<fake<<endl;
		s=rest;
	}
	return 0;
}
/** 程序框架：
  *
  *
  *
  *
  **/


/*
{
######################
#       Author       #
#        Gary        #
#        2020        #
######################
*/
//#pragma GCC target ("avx2")
//#pragma GCC optimization ("O3")
//#pragma GCC optimization ("unroll-loops")
#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define rb(a,b,c) for(int a=b;a<=c;++a)
#define rl(a,b,c) for(int a=b;a>=c;--a)
#define LL long long
#define IT iterator
#define PB push_back
#define II(a,b) make_pair(a,b)
#define FIR first
#define SEC second
#define FREO freopen("check.out","w",stdout)
#define rep(a,b) for(int a=0;a<b;++a)
#define SRAND mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define random(a) rng()%a
#define ALL(a) a.begin(),a.end()
#define POB pop_back
#define ff fflush(stdout)
#define fastio ios::sync_with_stdio(false)
#define check_min(a,b) a=min(a,b)
#define check_max(a,b) a=max(a,b)
using namespace std;
const int INF=0x3f3f3f3f;
typedef pair<int,int> mp;
/*}
*/
int n,m;
struct Move{
	vector<int>  moveto;//记录每一位到哪？
	Move operator * (Move B){
		Move ret;
		ret.moveto=vector<int>(moveto.size());
		rep(i,moveto.size())
			ret.moveto[i]=B.moveto[moveto[i]];
		return ret;
	}
};
Move quick(Move A,int times){
	Move ret;
	if(times==0){
		rep(i,n)
			ret.moveto.PB(i);
	}
	ret=quick(A,times>>1);
	ret=ret*ret;
	if(times&1)
		ret=ret*A;
	return ret;
}

int main(){
	string s;
	string rest;
	cin>>s;
	n=s.length();
	scanf("%d",&m);
	rb(Q,1,m){
		int k,d;
		scanf("%d%d",&k,&d);
		rest=s;
		Move To;
		To.moveto=vector<int> (n);
		vector<int> test(n);
		rep(i,n)
			test[i]=i;
		int is=0;
		rep(i,d){
			for(int j=i;j<k;j+=d){
				test[is++]=j;
			}
		}
		for(int j=k;j<n;++j){
			test[j]=j;
		}
		test.PB(test[0]);
		rep(i,test.size()){
			if(i){
				To.moveto[test[i]]=i-1;
			}
		}
		To=quick(To,n-k-1);
		rep(i,n){
			rest[To.moveto[i]]=s[i];
		}
		rest+=rest[0];
		rest.erase(rest.begin());
		cout<<rest<<endl;
		s=rest;
	}
	return 0;
}
/** 程序框架：
  *
  *
  *
  *
  **/


#include <iostream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <algorithm>
#include <string>
#include <string.h>
#include <sstream>
#include <queue>
#include <map>
#include <set>
#include <cstring>
#include <cmath>
#include <unordered_map>
#include <cassert>
#include <bitset>
#include <string.h>

using namespace std;

const int MaxN = 100005;
char str[MaxN], nstr[MaxN];
int N, M;

void input() {
	scanf("%s %d", str, &M);
	N = strlen(str);
}

int perm[MaxN * 2];
int nperm[MaxN * 2];
bool used[MaxN * 2];

void power(int pwr) {
	fill(used, used + 2 * N, false);
	for (int i = 0; i < 2 * N; i++) {
		if (used[i]) {
			continue;
		}
		vector<int> cycle;
		int v = i;
		do {
			used[v] = true;
			cycle.push_back(v);
			v = perm[v];
		} while(v != i);
		int jump = pwr % (int)cycle.size();
		int ptr2 = i;
		while (jump--) {
			ptr2 = perm[ptr2];
		}
		for (int i = 0; i < (int)cycle.size(); i++) {
			nperm[cycle[i]] = ptr2;
			ptr2 = perm[ptr2];
		}
	}
	copy(nperm, nperm + 2 * N, perm);
}

void first_perm() {
	for (int i = 1; i < 2 * N; i++) {
		perm[i] = i - 1;
	}
	perm[0] = 2 * N - 1;
}

int main(){
	input();
	for (int ii = 0; ii < M; ii++) {
		int k, d;
		scanf("%d %d", &k, &d);
		first_perm();
		int ptr = N - 1;
		for (int i = 0; i < d; i++) {
			for (int j = i; j < k; j += d) {
				perm[N + j] = ptr++;
			}
		}
		power(N - k + 1);
		int incr = k - 1;
		for (int i = 0; i < N; i++) {
			nstr[perm[N + i] - incr] = str[i];
		}
		copy(nstr, nstr + N, str);
		printf("%s\n", str);
	}
	return 0;
}


#include <iostream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <algorithm>
#include <string>
#include <string.h>
#include <sstream>
#include <queue>
#include <map>
#include <set>
#include <cstring>
#include <cmath>
#include <unordered_map>
#include <cassert>
#include <bitset>
#include <string.h>

using namespace std;

const int MaxN = 1000005;
char str[MaxN], nstr[MaxN];
int N, M;

void input() {
	scanf("%s %d", str, &M);
	N = strlen(str);
}

int perm[MaxN * 2];
int nperm[MaxN * 2];
bool used[MaxN * 2];

void power(int pwr) {
	fill(used, used + 2 * N, false);
	for (int i = 0; i < 2 * N; i++) {
		if (used[i]) {
			continue;
		}
		vector<int> cycle;
		int v = i;
		do {
			used[v] = true;
			cycle.push_back(v);
			v = perm[v];
		} while(v != i);
		int jump = pwr % (int)cycle.size();
		int ptr2 = i;
		while (jump--) {
			ptr2 = perm[ptr2];
		}
		for (int i = 0; i < (int)cycle.size(); i++) {
			nperm[cycle[i]] = ptr2;
			ptr2 = perm[ptr2];
		}
	}
	copy(nperm, nperm + 2 * N, perm);
}

void first_perm() {
	for (int i = 1; i < 2 * N; i++) {
		perm[i] = i - 1;
	}
	perm[0] = 2 * N - 1;
}

int main(){
	input();
	for (int ii = 0; ii < M; ii++) {
		int k, d;
		scanf("%d %d", &k, &d);
		first_perm();
		int ptr = N - 1;
		for (int i = 0; i < d; i++) {
			for (int j = i; j < k; j += d) {
				perm[N + j] = ptr++;
			}
		}
		power(N - k + 1);
		int incr = k - 1;
		for (int i = 0; i < N; i++) {
			nstr[perm[N + i] - incr] = str[i];
		}
		copy(nstr, nstr + N, str);
		printf("%s\n", str);
	}
	return 0;
}


#include <iostream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <algorithm>
#include <string>
#include <string.h>
#include <sstream>
#include <queue>
#include <map>
#include <set>
#include <cstring>
#include <cmath>
#include <unordered_map>
#include <cassert>
#include <bitset>
#include <string.h>

using namespace std;

const int MaxN = 10005;
char str[MaxN], nstr[MaxN];
int N, M;

void input() {
	scanf("%s %d", str, &M);
	N = strlen(str);
}

int perm[MaxN * 2];
int nperm[MaxN * 2];
bool used[MaxN * 2];

void power(int pwr) {
	fill(used, used + 2 * N, false);
	for (int i = 0; i < 2 * N; i++) {
		if (used[i]) {
			continue;
		}
		vector<int> cycle;
		int v = i;
		do {
			used[v] = true;
			cycle.push_back(v);
			v = perm[v];
		} while(v != i);
		int jump = pwr % (int)cycle.size();
		int ptr2 = i;
		while (jump--) {
			ptr2 = perm[ptr2];
		}
		for (int i = 0; i < (int)cycle.size(); i++) {
			nperm[cycle[i]] = ptr2;
			ptr2 = perm[ptr2];
		}
	}
	copy(nperm, nperm + 2 * N, perm);
}

void first_perm() {
	for (int i = 1; i < 2 * N; i++) {
		perm[i] = i - 1;
	}
	perm[0] = 2 * N - 1;
}

int main(){
	input();
	for (int ii = 0; ii < M; ii++) {
		int k, d;
		scanf("%d %d", &k, &d);
		first_perm();
		int ptr = N - 1;
		for (int i = 0; i < d; i++) {
			for (int j = i; j < k; j += d) {
				perm[N + j] = ptr++;
			}
		}
		power(N - k + 1);
		int incr = k - 1;
		for (int i = 0; i < N; i++) {
			nstr[perm[N + i] - incr] = str[i];
		}
		copy(nstr, nstr + N, str);
		printf("%s\n", str);
	}
	return 0;
}


#include <bits/stdc++.h>
#define sz(x) int((x).size())
using namespace std;

const int kM = 1000010;

int N, M, K[kM], D[kM];
char S[kM];

void read();
void solve();

void mperm(int [], int []);

int main() {
  read();
  solve();

  return 0;
}

void read() {
  cin >> S >> M, N = strlen(S);
  for (int i = 0; i < M; ++i)
    cin >> K[i] >> D[i];
}

void solve() {
  static char s[2][kM];
  static int u[kM], v[kM], r[kM], p = 0, q = 1;

  copy(&S[0], &S[N], &s[q][0]);
  for (int mi = 0; mi < M; ++mi) {
    int k = K[mi], d = D[mi];

    for (int i = 0, c = 0; i < d; ++i)
      for (int j = i; j < k; j += d, ++c)
        u[c] = j;
    for (int i = k; i < N; ++i)
      u[i] = i;

    for (int i = 0; i < N; ++i)
      r[i] = i;

    v[N - 1] = 0;
    for (int i = 0; i < N - 1; ++i)
      v[i] = i + 1;

    for (int i = 20; i >= 0; --i) {
      mperm(r, r);
      if (((1 << i) & (N - k + 1)) != 0)
        mperm(r, u), mperm(r, v);
    }

    swap(p, q);
    for (int i = 0; i < N; ++i)
      s[q][i] = s[p][r[i]];
    rotate(&s[q][0], &s[q][k - 1], &s[q][N]);
    cout << s[q] << "\n";
  }
}

void mperm(int a[], int b[]) {
  static int c[kM];
  for (int i = 0; i < N; ++i)
    c[i] = a[b[i]];
  copy(&c[0], &c[N], &a[0]);
}


#include <bits/stdc++.h>
#define sz(x) int((x).size())
using namespace std;

const int kM = 1000010;

int N, M, K[kM], D[kM];
char S[kM];

void read();
void solve();

void mperm(int [], int []);

int main() {
  ios::sync_with_stdio(false);
  read();
  solve();

  return 0;
}

void read() {
  cin >> S >> M, N = strlen(S);
  for (int i = 0; i < M; ++i)
    cin >> K[i] >> D[i];
}

void solve() {
  static char s[2][kM];
  static int u[kM], v[kM], r[kM], p = 0, q = 1;

  copy(&S[0], &S[N], &s[q][0]);
  for (int mi = 0; mi < M; ++mi) {
    int k = K[mi], d = D[mi];

    for (int i = 0, c = 0; i < d; ++i)
      for (int j = i; j < k; j += d, ++c)
        u[c] = j;
    for (int i = k; i < N; ++i)
      u[i] = i;

    for (int i = 0; i < N; ++i)
      r[i] = i;

    v[N - 1] = 0;
    for (int i = 0; i < N - 1; ++i)
      v[i] = i + 1;

    for (int i = 20; i >= 0; --i) {
      mperm(r, r);
      if (((1 << i) & (N - k + 1)) != 0)
        mperm(r, u), mperm(r, v);
    }

    swap(p, q);
    for (int i = 0; i < N; ++i)
      s[q][i] = s[p][r[i]];
    rotate(&s[q][0], &s[q][k - 1], &s[q][N]);
    cout << s[q] << "\n";
  }
}

void mperm(int a[], int b[]) {
  static int c[kM];
  for (int i = 0; i < N; ++i)
    c[i] = a[b[i]];
  copy(&c[0], &c[N], &a[0]);
}


#include <bits/stdc++.h>
#define sz(x) int((x).size())
using namespace std;

const int kM = 1000010;

int N, M, K[kM], D[kM];
char S[kM];

void read();
void solve();

void mperm(int [], int []);

int main() {
  read();
  solve();

  return 0;
}

void read() {
  cin >> S >> M, N = strlen(S);
  for (int i = 0; i < M; ++i)
    cin >> K[i] >> D[i];
}

void solve() {
  static char s[2][kM];
  static int u[kM], v[kM], r[kM], p = 0, q = 1;

  copy(&S[0], &S[N], &s[q][0]);
  for (int mi = 0; mi < M; ++mi) {
    int k = K[mi], d = D[mi];

    for (int i = 0, c = 0; i < d; ++i)
      for (int j = i; j < k; j += d, ++c)
        u[c] = j;
    for (int i = k; i < N; ++i)
      u[i] = i;

    for (int i = 0; i < N; ++i)
      r[i] = i;

    v[N - 1] = 0;
    for (int i = 0; i < N - 1; ++i)
      v[i] = i + 1;

    for (int i = 20; i >= 0; --i) {
      mperm(r, r);
      if (((1 << i) & (N - k + 1)) != 0)
        mperm(r, u), mperm(r, v);
    }

    swap(p, q);
    for (int i = 0; i < N; ++i)
      s[q][i] = s[p][r[i]];
    rotate(&s[q][0], &s[q][k - 1], &s[q][N]);
    printf("%s\n", s[q]);
  }
}

void mperm(int a[], int b[]) {
  static int c[kM];
  for (int i = 0; i < N; ++i)
    c[i] = a[b[i]];
  copy(&c[0], &c[N], &a[0]);
}


#include <bits/stdc++.h>
#define sz(x) int((x).size())
using namespace std;

const int kM = 1000010;

int N, M, K[kM], D[kM];
string S;

void read();
void solve();

vector< int > mperm(const vector< int > &, const vector< int > &);

int main() {
  read();
  solve();

  return 0;
}

void read() {
  cin >> S >> M, N = sz(S);
  for (int i = 0; i < M; ++i)
    cin >> K[i] >> D[i];
}

void solve() {
  string s = S;

  for (int mi = 0; mi < M; ++mi) {
    int k = K[mi], d = D[mi];
    vector< int > u(N), v(N), r(N);

    for (int i = 0, c = 0; i < d; ++i)
      for (int j = i; j < k; j += d, ++c)
        u[c] = j;
    for (int i = k; i < N; ++i)
      u[i] = i;

    for (int i = 0; i < N; ++i)
      r[i] = i;

    v[N - 1] = 0;
    for (int i = 0; i < N - 1; ++i)
      v[i] = i + 1;

    for (int i = 20; i >= 0; --i) {
      r = mperm(r, r);
      if (((1 << i) & (N - k + 1)) != 0)
        r = mperm(r, u), r = mperm(r, v);
    }

    string res(N, ' ');
    for (int i = 0; i < N; ++i)
      res[i] = s[r[i]];
    rotate(res.begin(), res.begin() + (k - 1), res.end());
    cout << res << "\n", s = res;
  }
}

vector< int > mperm(const vector< int > &a, const vector< int > &b) {
  vector< int > ret(N);
  for (int i = 0; i < N; ++i)
    ret[i] = a[b[i]];
  return ret;
}


#include <bits/stdc++.h>
#define maxn 1000005

using namespace std;

char s[maxn], t[maxn];
int n, k, d;
int P[maxn], Ans[maxn], G[maxn];

void solve() {
    scanf ("%d %d", &k, &d);
    // Lap hoan vi
    for (int i = 0; i < n; ++i)
        P[i] = Ans[i] = i;
    int u = 0;
    for (int i = 0; i < d; ++i)
        for (int j = i; j < k; j += d)
            P[u++] = j;
    P[n] = P[0];
    for (int i = 0; i <= n - 1; ++i)
        P[i] = P[i + 1];

    // Luy thua Ans=Ans*(P^m)
    int m = n - k + 1;
    while (m > 0) {
        if (m % 2) {
            for (int i = 0; i < n; ++i)
                Ans[i] = P[Ans[i]];
        }
        for (int i = 0; i < n; ++i)
            G[i] = P[i];
        for (int i = 0; i < n; ++i)
            P[i] = G[P[i]];
        m /= 2;
    }

    // In ket qua
    for (int i = 0; i < n; ++i)
        t[ (i + n - k + 1) % n] = s[Ans[i]];
    for (int i = 0; i < n; ++i)
        s[i] = t[i];
    printf ("%s\n", s);
}

int main() {
#ifndef ONLINE_JUDGE
    freopen ("inp.txt", "r", stdin);
    freopen ("out.txt", "w", stdout);
#endif // ONLINE_JUDGE
    scanf ("%s", s);
    n = strlen (s);
    int Q;
    scanf ("%d", &Q);
    while (Q--)
        solve();
}


#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <map>
#include <stack>
#include <cmath>
#include <queue>
#include <map>
#include <string>
#include <cstdlib>
#include <bitset>
#include <set>
#include <list>
#include <string>
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
typedef unsigned int uint;
#define sign puts("SIGN");
#define F first
#define S second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, j, k) for (int i = int(j); i <= int(k); ++ i)
#define dwn(i, j, k) for (int i = int(j); i >= int(k); -- i)
#define lson rt << 1, l, m
#define rson rt << 1 | 1, m + 1, r
const LL MOD = 1e9 + 7;
const LL INF = 1.5e18;
const int N = 1e6 + 7;
const double PI = acos(-1.0);
char s[N];
int n, m;
int p[N], q[N], r[N];
int a[N], c[N], tmp[N], last[N];
int main() {
	scanf("%s", s);
	n = strlen(s);
	scanf("%d", &m);
	int k, d;
	rep(i, 0, n - 1) last[i] = i;
	while (m --) {
		scanf("%d%d", &k, &d);
		int b = n - k + 1;
		rep(i, 0, n - 1) p[i] = (i + b) % n;
		rep(i, 0, n - 1) q[i] = (i - 1 + n) % n;
		rep(i, 0, n - 1) r[i] = i;
		int tot = 0;
		rep(i, 0, d - 1)
		for (int j = 0; j * d + i <= k - 1; ++ j)
			r[j * d + i] = tot ++;
		rep(i, 0, n - 1) a[i] = q[r[i]];

		rep(i, 0, n - 1) c[i] = i;
		while (b) {
			if (b & 1) {
				rep(i, 0, n - 1) tmp[i] = a[c[i]];
				memcpy(c, tmp, sizeof(tmp));
			}
			rep(i, 0, n - 1) tmp[i] = a[a[i]];
			memcpy(a, tmp, sizeof(tmp));
			b >>= 1;
		}

		rep(i, 0, n - 1) a[i] = p[c[i]];
		rep(i, 0, n - 1) tmp[i] = a[last[i]];
		memcpy(last, tmp, sizeof(tmp));

		rep(i, 0, n - 1) c[tmp[i]] = i;
		rep(i, 0, n - 1) printf("%c", s[c[i]]);
		puts("");
	}
}


#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <map>
#include <stack>
#include <cmath>
#include <queue>
#include <map>
#include <string>
#include <cstdlib>
#include <bitset>
#include <set>
#include <list>
#include <string>
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
typedef unsigned int uint;
#define sign puts("SIGN");
#define F first
#define S second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, j, k) for (int i = int(j); i <= int(k); ++ i)
#define dwn(i, j, k) for (int i = int(j); i >= int(k); -- i)
#define lson rt << 1, l, m
#define rson rt << 1 | 1, m + 1, r
const LL MOD = 1e9 + 7;
const LL INF = 1.5e18;
const int N = 1e6 + 7;
const double PI = acos(-1.0);
char s[N];
int n, m;
int p[N], q[N], r[N];
int a[N], c[N], tmp[N], last[N];
int main() {
	scanf("%s", s);
	n = strlen(s);
	scanf("%d", &m);
	int k, d;
	rep(i, 0, n - 1) last[i] = i;
	while (m --) {
		scanf("%d%d", &k, &d);
		int b = n - k + 1;
		rep(i, 0, n - 1) p[i] = (i + b) % n;
		rep(i, 0, n - 1) q[i] = (i - 1 + n) % n;
		rep(i, 0, n - 1) r[i] = i;
		int tot = 0;
		rep(i, 0, d - 1)
		for (int j = 0; j * d + i <= k - 1; ++ j)
			r[j * d + i] = tot ++;
		rep(i, 0, n - 1) a[i] = q[r[i]];

		rep(i, 0, n - 1) c[i] = i;
		while (b) {
			if (b & 1) {
				rep(i, 0, n - 1) tmp[i] = a[c[i]];
				memcpy(c, tmp, n << 2);
			}
			rep(i, 0, n - 1) tmp[i] = a[a[i]];
			memcpy(a, tmp, n << 2);
			b >>= 1;
		}

		rep(i, 0, n - 1) a[i] = p[c[i]];
		rep(i, 0, n - 1) tmp[i] = a[last[i]];
		memcpy(last, tmp, n << 2);

		rep(i, 0, n - 1) c[tmp[i]] = i;
		rep(i, 0, n - 1) printf("%c", s[c[i]]);
		puts("");
	}
}