Problem:
Problem B: Polygonal Line Search

Multiple polygonal lines are given on the xy-plane.
Given a list of polygonal lines and a template,
you must find out polygonal lines
which have the same shape as the template.

A polygonal line consists of several line segments parallel to x-axis or
y-axis. It is defined by a list of xy-coordinates of vertices from the start-point to the end-point in order,
and always turns 90 degrees at each vertex.
A single polygonal line does not pass the same point twice.

Two polygonal lines have the same shape when they
fully overlap each other only with rotation and translation
within xy-plane (i.e. without magnification or a flip).
The vertices given in reverse order from the start-point to the end-point is the
same as that given in order.

Figure 1 shows examples of polygonal lines.
In this figure, polygonal lines A and B have the same shape.

Write a program that answers polygonal lines which have the same shape as
the template.

Figure 1: Polygonal lines

Input

The input consists of multiple datasets.
The end of the input is indicated by a line which contains a zero.

A dataset is given as follows.

n

Polygonal line0

Polygonal line1

Polygonal line2

...

Polygonal linen

n is the number of polygonal lines for the object of search on
xy-plane.
n is an integer, and 1 <= n <= 50.
Polygonal line0 indicates the template.

A polygonal line is given as follows.

m

x1 y1

x2 y2

...

xm ym

m is the number of the vertices of a polygonal line (3 <= m <= 10).

xi and yi, separated by a space,
are the x- and y-coordinates
of a vertex, respectively (-10000 < xi < 10000, -10000 <yi < 10000).

Output

For each dataset in the input, your program should report numbers assigned to the polygonal lines that have the same shape as the template, in ascending order.
Each number must be written in a separate line without any other characters such as leading or trailing spaces.

Five continuous "+"s must be placed in a line at the end of each dataset.

Sample Input

5
5
0 0
2 0
2 1
4 1
4 0
5
0 0
0 2
-1 2
-1 4
0 4
5
0 0
0 1
-2 1
-2 2
0 2
5
0 0
0 -1
2 -1
2 0
4 0
5
0 0
2 0
2 -1
4 -1
4 0
5
0 0
2 0
2 1
4 1
4 0
4
4
-60 -75
-60 -78
-42 -78
-42 -6
4
10 3
10 7
-4 7
-4 40
4
-74 66
-74 63
-92 63
-92 135
4
-12 22
-12 25
-30 25
-30 -47
4
12 -22
12 -25
30 -25
30 47
3
5
-8 5
-8 2
0 2
0 4
8 4
5
-3 -1
0 -1
0 7
-2 7
-2 16
5
-1 6
-1 3
7 3
7 5
16 5
5
0 1
0 -2
8 -2
8 0
17 0
0

Output for the Sample Input

1
3
5
+++++
3
4
+++++
+++++

Solution in JAVA (judge verdict: Accepted)

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

import static java.lang.Math.*;
import static java.util.Arrays.*;

public class Main {

	Scanner sc = new Scanner(System.in);

	int INF = 1 << 28;
	double EPS = 1e-9;

	int n;
	int[] m;
	int[][] xs,ys;
	
	void run() {
		for(;;){
			n=sc.nextInt();
			if(n==0){
				break;
			}
			m=new int[n+1];
			xs=new int[n+1][];
			ys=new int[n+1][];
			for(int j=0;j<=n;j++){
				m[j]=sc.nextInt();
				xs[j]=new int[m[j]];
				ys[j]=new int[m[j]];
				for(int i=0;i<m[j];i++){
					xs[j][i]=sc.nextInt();
					ys[j][i]=sc.nextInt();
				}
			}
			solve();
		}
	}

	void solve() {
		for(int i=1;i<=n;i++){
			//debug("i",i);
			if(m[0]==m[i]&&match(m[0],xs[0],ys[0],xs[i],ys[i])){
				println(i+"");
			}
		}
		println("+++++");
	}

	boolean match(int m,int[]xs1,int[]ys1,int[]xs2,int[]ys2){
		// vx,0
		// vx,0
		// -vx,0
		// 0,vx
		// 0,-vx
		
		int[][][]a={
				{
					{1,0},
					{0,1}
				},
				{
					{0,-1},
					{1,0}
				},
				{
					{-1,0},
					{0,-1}
				},
				{
					{0,1},
					{-1,0}
				}
		};
		
		int k1=-1,k2=-1;
		
		for(int j=0;j<4;j++){
			int vx1=xs1[1]-xs1[0];
			int vy1=ys1[1]-ys1[0];
			int vx2=xs2[1]-xs2[0];
			int vy2=ys2[1]-ys2[0];
			if(vx1*a[j][0][0]+vy1*a[j][0][1]==vx2&&
					vx1*a[j][1][0]+vy1*a[j][1][1]==vy2){
				k1=j;
			}
			int vx3=xs2[m-2]-xs2[m-1];
			int vy3=ys2[m-2]-ys2[m-1];
			if(vx1*a[j][0][0]+vy1*a[j][0][1]==vx3&&
					vx1*a[j][1][0]+vy1*a[j][1][1]==vy3){
				k2=j;
			}
		}
		
		//debug(k1,k2);
		
		if(k1==-1&&k2==-1){
			return false;
		}
		
		boolean f1=k1!=-1,f2=k2!=-1;
		
		for(int i=0;i<m-1;i++){
			int vx1=xs1[i+1]-xs1[i];
			int vy1=ys1[i+1]-ys1[i];
			int vx2=xs2[i+1]-xs2[i];
			int vy2=ys2[i+1]-ys2[i];
			int vx3=xs2[m-i-2]-xs2[m-i-1];
			int vy3=ys2[m-i-2]-ys2[m-i-1];
			if(k1!=-1){
				f1&=vx1*a[k1][0][0]+vy1*a[k1][0][1]==vx2&&
				vx1*a[k1][1][0]+vy1*a[k1][1][1]==vy2;
			}
			if(k2!=-1){
				f2&=vx1*a[k2][0][0]+vy1*a[k2][0][1]==vx3&&
				vx1*a[k2][1][0]+vy1*a[k2][1][1]==vy3;
			}
		}
		
		return f1||f2;
	}
	
	void debug(Object... os) {
		System.err.println(Arrays.deepToString(os));
	}

	void print(String s) {
		System.out.print(s);
	}

	void println(String s) {
		System.out.println(s);
	}

	public static void main(String[] args) {
		// System.setOut(new PrintStream(new BufferedOutputStream(System.out)));
		new Main().run();
	}
}