Introductions Organization

아직 제출이 없습니다시간 제한40초메모리 제한1024 MB

문제

After Apricot Rules LLC went through a reorganization, a new large team was formed containing MM managers and NN non-managers. Since many people within the team do not know each other, a number of introduction sessions are to be scheduled. We know exactly which pairs of members already know each other.

The introduction sessions are organized into time slots that take 11 minute. The first time slot starts at 8:00 AM and ends at 8:01 AM. The ii-th time slot starts i1i-1 minutes after 8:00 AM and ends ii minutes after 8:00 AM. During each time slot, there can be one or more introduction sessions. A team member can be assigned to at most one introduction session per time slot. Each introduction session must have exactly three members: an assigned manager aa who must be a manager and two others bb and cc who can be managers or non-managers. The assigned manager aa must already know bb and cc for the session to be scheduled. After the introduction session, bb and cc are considered to know each other too. If bb and/or cc are managers, either of them can be the assigned manager of a future introduction session that includes both.

For some pairs of people in the team, we want to know the shortest time that is needed for them to finally know each other, or whether it is impossible for that to happen through the described process. If two people know each other before any introduction sessions happen, we define that shortest time to be 00 minutes.

Even though we are interested in multiple pairs of people, we are considering the situations independently. That is, the minimum time for each pair can depend on a specific organization of the introduction that is particular to that pair only.

입력

The first line of the input gives the number of test cases, TTTT test cases follow. Each test case begins with a line containing three integers MMNN, and PP: the number of managers on the new team, the number of non-managers on the new team, and the number of pairs of team members we are going to ask about, respectively. Managers are numbered from 11 through MM and non-managers are numbered from M+1M+1 through M+NM+N. Then, M+NM+N lines follow with M+NM+N characters each. The jj-th character on the ii-th of these lines C_i,jC\_{i,j} is Y if team members ii and jj know each other before the introduction process starts, and N otherwise. Then, there are PP more lines; the kk-th of which contains a pair of integers A_kA\_k and B_kB\_k each, representing the team member numbers of the kk-th pair we are interested in.

출력

For each test case, output one line containing Case #x: y1 y2 y3 ⋯ yP, where xx is the test case number (starting from 11) and y_iy\_i is 1-1 if team members A_kA\_k and B_kB\_k cannot get to know each other, or the shortest amount of time (in minutes) since the process starts until they do.

제한

  • 1T1001≤T≤100.
  • C_i,jC\_{i,j} is either uppercase Y or uppercase N, for all i,ji,j.
  • C_i,jC\_{i,j}C_j,iC\_{j,i}, for all i,ji,j.
  • C_i,iC\_{i,i} = Y, for all ii. (Team members know themselves.)
  • $1≤A_k
  • (A_k,B_k)(A_,B_)(A\_k,B\_k)≠(A\_ℓ,B\_ℓ), for all kk≠ℓ. (No pair of team members is asked about twice.)