DZY loves strings, and he enjoys collecting them.

In China, many people like to use strings containing their names' initials, for example: xyz, jcvb, dzy, dyh.

Once DZY found a lucky string s. A lot of pairs of good friends came to DZY when they heard about the news. The first member of the i-th pair has name ai, the second one has name bi. Each pair wondered if there is a substring of the lucky string containing both of their names. If so, they want to find the one with minimum length, which can give them good luck and make their friendship last forever.

Please help DZY for each pair find the minimum length of the substring of s that contains both ai and bi, or point out that such substring doesn't exist.

A substring of s is a string slsl + 1... sr for some integers l, r (1 ≤ l ≤ r ≤ |s|). The length of such the substring is (r - l + 1).

A string p contains some another string q if there is a substring of p equal to q.

Input

The first line contains a string s (1 ≤ |s| ≤ 50000).

The second line contains a non-negative integer q (0 ≤ q ≤ 100000) — the number of pairs. Each of the next q lines describes a pair, the line contains two space-separated strings ai and bi (1 ≤ |ai|, |bi| ≤ 4).

It is guaranteed that all the strings only consist of lowercase English letters.

Output

For each pair, print a line containing a single integer — the minimum length of the required substring. If there is no such substring, output -1.

Examples

Input

xudyhduxyz
3
xyz xyz
dyh xyz
dzy xyz


Output

3
8
-1


Input

abcabd
3
a c
ab abc
ab d


Output

2
3
3


Input

baabcabaaa
2
abca baa
aa aba


Output

6
4

Note

The shortest substrings in the first sample are: xyz, dyhduxyz.

The shortest substrings in the second sample are: ca, abc and abd.

The shortest substrings in the third sample are: baabca and abaa.
// practice with rainboy
import java.io.*;
import java.util.*;

public class CF444D extends PrintWriter {
	CF444D() { super(System.out); }
	Scanner sc = new Scanner(System.in);
	public static void main(String[] $) {
		CF444D o = new CF444D(); o.main(); o.flush();
	}

	static final int K = 400, L = 50000;
	static byte[] cc;
	static class V implements Comparable<V> {
		int i, l;
		V(int i, int l) {
			this.i = i; this.l = l;
		}
		int strcmp(V v) {
			int l_ = Math.min(l, v.l);
			for (int h = 0; h < l_; h++) {
				int a = cc[i + h], b = cc[v.i + h];
				if (a != b)
					return a - b;
			}
			return l - v.l;
		}
		@Override
		public int compareTo(V v) {
			int c = strcmp(v);
			return c != 0 ? c : i - v.i;
		}
	}
	static class W {
		String s; int[] ii;
		W(String s, int k) {
			this.s = s; ii = new int[k];
		}
	}
	int find(W[] ww, int m, String s) {
		int lower = -1, upper = m;
		while (upper - lower > 1) {
			int i = (lower + upper) / 2;
			int c = ww[i].s.compareTo(s);
			if (c == 0)
				return i;
			if (c < 0)
				lower = i;
			else
				upper = i;
		}
		return -1;
	}
	int find(int[] ii, int n, int j) {
		int lower = -1, upper = n;
		while (upper - lower > 1) {
			int g = (lower + upper) / 2;
			if (ii[g] <= j)
				lower = g;
			else
				upper = g;
		}
		return lower;
	}
	int solve1(W u, W v) {
		int lu = u.s.length(), lv = v.s.length();
		int[] ii = u.ii, jj = v.ii;
		int n = ii.length, m = jj.length;
		int ans = L;
		for (int h = 0; h < m; h++) {
			int j = jj[h];
			int g = find(ii, n, j);
			if (g >= 0) {
				int i = ii[g];
				ans = Math.min(ans, Math.max(i + lu, j + lv) - i);
			}
			g++;
			if (g < n) {
				int i = ii[g];
				ans = Math.min(ans, Math.max(i + lu, j + lv) - j);
			}
		}
		return ans;
	}
	int solve2(W u, W v) {
		int lu = u.s.length(), lv = v.s.length();
		int[] ii = u.ii, jj = v.ii;
		int n = ii.length, m = jj.length;
		int ans = L;
		for (int i = 0, j = 0; i < n && j < m; ) {
			int i_ = ii[i], j_ = jj[j];
			ans = Math.min(ans, Math.max(i_ + lu, j_ + lv) - Math.min(i_, j_));
			if (i_ <= j_)
				i++;
			else
				j++;
		}
		return ans;
	}
	void main() {
		cc = sc.next().getBytes();
		int n = cc.length;
		int m = n * 4;
		V[] vv = new V[m];
		m = 0;
		for (int i = 0; i < n; i++)
			for (int l = 1; l <= 4 && i + l <= n; l++)
				vv[m++] = new V(i, l);
		Arrays.sort(vv, 0, m);
		W[] ww = new W[m];
		int m_ = 0;
		for (int i = 0, j; i < m; i = j) {
			j = i + 1;
			while (j < m && vv[j].strcmp(vv[i]) == 0)
				j++;
			W w = new W(new String(cc, vv[i].i, vv[i].l), j - i);
			for (int h = i; h < j; h++)
				w.ii[h - i] = vv[h].i;
			ww[m_++] = w;
		}
		m = m_;
		int q = sc.nextInt();
		int[] ii = new int[q];
		int[] jj = new int[q];
		Integer[] hh = new Integer[q];
		int[] aa = new int[q];
		for (int h = 0; h < q; h++) {
			String a = sc.next();
			String b = sc.next();
			int i = find(ww, m, a), j = find(ww, m, b);
			if (i > j) {
				int tmp = i; i = j; j = tmp;
			}
			ii[h] = i; jj[h] = j;
			hh[h] = h;
		}
		Arrays.sort(hh, (i, j) -> ii[i] != ii[j] ? ii[i] - ii[j] : jj[i] - jj[j]);
		for (int i = 0, j; i < q; i = j) {
			int h_ = hh[i], i_ = ii[h_], j_ = jj[h_];
			j = i + 1;
			while (j < q && ii[hh[j]] == i_ && jj[hh[j]] == j_)
				j++;
			int a;
			if (i_ == -1)
				a = -1;
			else {
				W u = ww[i_], v = ww[j_];
				int nu = u.ii.length, nv = v.ii.length;
				if (nu < K || nv < K)
					a = nu < nv ? solve1(v, u) : solve1(u, v);
				else
					a = solve2(u, v);
			}
			for (int h = i; h < j; h++) {
				h_ = hh[h];
				aa[h_] = a;
			}
		}
		for (int h = 0; h < q; h++)
			println(aa[h]);
	}
}
