CF2004D.Colored Portals

传统题 时间 2000 ms 内存 256 MiB 8 尝试 3 已通过 1 标签

Colored Portals

There are nn cities located on a straight line. The cities are numbered from 11 to nn.

Portals are used to move between cities. There are 44 colors of portals: blue, green, red, and yellow. Each city has portals of two different colors. You can move from city ii to city jj if they have portals of the same color (for example, you can move between a "blue-red" city and a "blue-green" city). This movement costs ij|i-j| coins.

Your task is to answer qq independent queries: calculate the minimum cost to move from city xx to city yy.

Input

The first line contains a single integer tt (1t1041 \le t \le 10^4) — the number of test cases.

The first line of each test case contains two integers nn and qq (1n,q21051 \le n, q \le 2 \cdot 10^5) — the number of cities and the number of queries, respectively.

The second line contains nn strings of the following types: BG, BR, BY, GR, GY, or RY; the ii-th of them describes the portals located in the ii-th city; the symbol B indicates that there is a blue portal in the city, G — green, R — red, and Y — yellow.

The jj-th of the next qq lines contains two integers xjx_j and yjy_j (1xj,yjn1 \le x_j, y_j \le n) — the description of the jj-th query.

Additional constraints on the input:

  • the sum of nn over all test cases does not exceed 21052 \cdot 10^5;
  • the sum of qq over all test cases does not exceed 21052 \cdot 10^5.

Output

For each query, print a single integer — the minimum cost to move from city xx to city yy (or 1-1 if it is impossible).

Samples

2
4 5
BR BR GY GR
1 2
3 1
4 4
1 4
4 2
2 1
BG RY
1 2
1
4
0
3
2
-1

在线编程 IDE

建议全屏模式获得最佳体验