class Array
	def accum
		ac = Array.new(size + 1)
		ac[0] = 0
		(1..size).each do |i|
			ac[i] = ac[i-1] + self[i-1]
		end
		ac
	end

	def accum!
		(1..size-1).each{|i|
			self[i] += self[i-1]
		}
		self.unshift(0)
	end

	def accum_2d
		ac = map{|r| r.accum}
		(1..size-1).each{|i|
			(1..ac[0].size-1).each{|j|
				ac[i][j] += ac[i-1][j]
			}
		}
		ac.unshift([0] * ac[0].size)
		ac
	end

	def accum_2d!
		each{|r| r.accum!}
		(1..size-1).each{|i|
			(1..self[0].size-1).each{|j|
				self[i][j] += self[i-1][j]
			}
		}
		unshift([0] * self[0].size)
	end
		
	def bsearch_first_index(l = 0, r = size - 1, &pred)
		return nil if l > r
		c = (l + r) / 2 
		if pred[self[c]]
			bsearch_first_index(l, c-1, &pred) || c
		else
			bsearch_first_index(c+1, r, &pred)
		end
	end

	def bsearch_first_item(l = 0, r = size - 1, &pred)
		i = bsearch_first_index(l, r, &pred)
		i ? self[i] : nil
	end

	def bsearch_last_index(l = 0, r = size - 1, &pred)
		return nil if l > r
		c = (l + r) / 2 
		if pred[self[c]]
			bsearch_last_index(c+1, r, &pred) || c
		else
			bsearch_last_index(l, c-1, &pred)
		end
	end

	def bsearch_last_item(l = 0, r = size - 1, &pred)
		i = bsearch_last_index(l, r, &pred)
		i ? self[i] : nil
	end
end

class String
	def suffix_array
		a = (0..size-1).to_a
		a.sa_sort!(self)
		a
	end	
end

class Array
	def sa_sort!(str, l = 0, r = str.size-1, k = 0)
		return if r <= l
		pivot = self[l]
		d = 2 ** k - 1
		compare = -> i, j do
			pos1 = i + d
			pos2 = j + d
			if pos1 >= str.size && pos2 >= str.size
				return 0
			elsif pos1 >= str.size
				return -1
			elsif pos2 >= str.size
				return 1
			else
				return str[pos1..pos1+d] <=> str[pos2..pos2+d]
			end
		end
		lesser = l
		greater = r
		i = l + 1
		loop do
			break if i > greater
			case compare[self[i], pivot]
			when -1
				self[i], self[lesser] = self[lesser], self[i]
				lesser += 1
				i += 1
			when 0
				i += 1
			when 1
				self[i], self[greater] = self[greater], self[i]
				greater -= 1
			end
		end
		sa_sort!(str, lesser, greater, k+1)
		sa_sort!(str, l, lesser-1, k)
		sa_sort!(str, greater+1, r, k)
	end
end

str = gets.chomp
sa = str.suffix_array
# puts sa.map {|i| str[i..-1]}

n = gets.to_i
n.times do
	x, y = gets.chomp.split
	if x.size > str.size || y.size > str.size
		p 0
		next
	end

	k1 = sa.bsearch_first_index {|i| x <= str[i..i+x.size-1]}
	if !k1 || str[sa[k1]..sa[k1]+x.size-1] != x
		p 0
		next
	end
	k2 = sa.bsearch_last_index {|i| str[i..i+x.size-1] <= x}
	i = sa[k1..k2].min

	k1 = sa.bsearch_first_index {|i| y <= str[i..i+y.size-1]}
	if !k1 || str[sa[k1]..sa[k1]+y.size-1] != y
		p 0
		next
	end
	k2 = sa.bsearch_last_index {|i| str[i..i+y.size-1] <= y}
	j = sa[k1..k2].max

	if j + y.size < i + x.size 
		p 0
		next
	end

	p j - i + y.size
end