Constellation Search

Problem:
You are searching for a constellation in a photo of the night sky. The photo contains exactly one shape that matches the shape, orientation, and size of the constellation you are looking for. However, there may be extra stars in the photo that are not part of the constellation.

For example, the constellation in Figure 1 is included in the photo in Figure 2 (highlighted by a circle). Shifting the coordinates of the stars in the constellation by +2 in the x-direction and -3 in the y-direction aligns them with the positions in the photo.

Given the shape of the desired constellation and the positions of stars in the photo, write a program to determine the amount by which the constellation's coordinates should be shifted to align with the coordinates in the photo.

Input:
The input consists of multiple datasets. Each dataset is provided in the following format:

The first line of each dataset contains the number of stars <i>m</i> in the desired constellation. The next <i>m</i> lines contain the x and y coordinates of the <i>m</i> stars in the constellation, separated by a space. The <i>m</i>+2nd line contains the number of stars <i>n</i> in the photo. The following <i>n</i> lines contain the x and y coordinates of the <i>n</i> stars in the photo, separated by a space.

The positions of the <i>m</i> stars in the constellation are all distinct. Similarly, the positions of the <i>n</i> stars in the photo are all distinct. 1 &le; <i>m</i> &le; 200, 1 &le; <i>n</i> &le; 1000. The x and y coordinates of the stars are all between 0 and 1000000.

When <i>m</i> is 0, it indicates the end of the input. The number of datasets does not exceed 5.

Output:
For each dataset, output a single line containing two integers separated by a space. These integers represent the amount by which the coordinates of the desired constellation should be shifted parallel to the x and y axes to align with the coordinates in the photo.

Input/Output Example:
(Provided in the original problem description)

# Buggy version:
def main()
  while True:
    m = int(input())
    if not m: break
    seiza = []
    for i in range(m):
      seiza.append(list(map(int,input().split())))
    kijun = seiza.pop(0)
    
    n = int(input())
    hosi = []
    point = {}
    
    for i in range(n):
      x,y = map(int,input().split())
      point[x,y] = 1
      hosi.append([x,y])
  
    for a in hosi:
      dx,dy = a[0] - kijun[0], a[1] - kijun[1]
      for b in seiza:
        if not (b[0] + dx, b[1] + dy) in point:
          break
      else:
        print(dx,dy)
        break
main() 


# Fixed version:
def main():
  while True:
    m = int(input())
    if not m: break
    seiza = []
    for i in range(m):
      seiza.append(list(map(int,input().split())))
    kijun = seiza.pop(0)
    
    n = int(input())
    hosi = []
    point = {}
    
    for i in range(n):
      x,y = map(int,input().split())
      point[x,y] = 1
      hosi.append([x,y])
  
    for a in hosi:
      dx,dy = a[0] - kijun[0], a[1] - kijun[1]
      for b in seiza:
        if not (b[0] + dx, b[1] + dy) in point:
          break
      else:
        print(dx,dy)
        break
main()

# Buggy version:
for e in iter(input,'0'):
 a=sorted([list(map(int,input().split()))for _ in[0]*int(e)],key=lambda x:x[0])
 b=sorted([tuple(map(int,input().split()))for _ in[0]*int(input())],key=lambda x:x[0])
 for s,t in b:
  u,v=a[0]
  x,y=s-u,t-v
  for u,v in a:
   if (u+x,v+y)not in b:break
  else:print(x,y)


# Fixed version:
for e in iter(input,'0'):
 a=sorted([list(map(int,input().split()))for _ in[0]*int(e)],key=lambda x:x[0])
 b=sorted([tuple(map(int,input().split()))for _ in[0]*int(input())],key=lambda x:x[0])
 for s,t in b:
  u,v=a[0]
  x,y=s-u,t-v
  if not set((u+x,v+y)for u,v in a)-set(b):print(x,y);break

# Buggy version:
for e in iter(input,'0'):
 a=[list(map(int,input().split()))for _ in[0]*int(e)]
 b=[list(map(int,input().split()))for _ in[0]*int(input())]
 for s,t in b:
  u,v=a[0]
  x,y=s-u,t-v
  for u,v in a:
   if[u+x,v+y]not in b:break
  else:print(x,y)


# Fixed version:
for e in iter(input,'0'):
 a=[list(map(int,input().split()))for _ in[0]*int(e)]
 b=[list(map(int,input().split()))for _ in[0]*int(input())]
 for s,t in b:
  u,v=a[0]
  x,y=s-u,t-v
  for u,v in a:
   if[u+x,v+y]not in b:break
  else:print(x,y)

# Buggy version:
for e in iter(input,'0'):
 a=sorted([list(map(int,input().split()))for _ in[0]*int(e)],key=lambda x:x[0])
 b=sorted([tuple(map(int,input().split()))for _ in[0]*int(input())],key=lambda x:x[0])
 for s,t in b:
  if not set((u+s-a[0][0],v+t-a[0][1])for u,v in a)-set(b):print(x,y);break


# Fixed version:
for e in iter(input,'0'):
 a=[list(map(int,input().split()))for _ in[0]*int(e)]
 b=[tuple(map(int,input().split()))for _ in[0]*int(input())]
 for s,t in b:
  u,v=a[0]
  x,y=s-u,t-v
  if not set((u+x,v+y)for u,v in a)-set(b):print(x,y);break

# Buggy version:
def f():
 for e in iter(input,'0'):
  a=sorted([list(map(int,input().split()))for _ in[0]*int(e)],key=lambda x:x[0])
  b=sorted([list(map(int,input().split()))for _ in[0]*int(input())],key=lambda x:x[0])
  for s,t in b:
   u,v=a[0]
   x,y=s-u,t-v
   for u,v in a:
    if[u+x,v+y]not in b:break
   else:return print(x,y)
f()


# Fixed version:
def f(e):
 a=sorted([list(map(int,input().split()))for _ in[0]*int(e)],key=lambda x:x[0])
 b=sorted([list(map(int,input().split()))for _ in[0]*int(input())],key=lambda x:x[0])
 for s,t in b:
  u,v=a[0]
  x,y=s-u,t-v
  for u,v in a:
   if[u+x,v+y]not in b:break
  else:return print(x,y)
for e in iter(input,'0'):f(e)

# Buggy version:
for e in iter(input,'0'):
 a={tuple(map(int,input().split())) for _ in range(int(e))}
 s,t=a[0]
 b={tuple(map(int,input().split())) for _ in range(int(input()))}
 for x,y in b:
  flag = True
  for u,v in a[1:]:
   if(x+u-s,y+v-t)not in b:
    flag = False
    break
  if flag == True:
   print(x - s, y - t)
   break


# Fixed version:
for e in iter(input,'0'):
 a=list(tuple(map(int,input().split())) for _ in range(int(e)))
 s,t=a[0]
 b=[tuple(map(int,input().split())) for _ in range(int(input()))]
 for x,y in b:
  flag = True
  for u,v in a[1:]:
   if(x+u-s,y+v-t)not in b:
    flag = False
    break
  if flag == True:
   print(x - s, y - t)
   break

# Buggy version:
def f(e):
 a=list([*map(int,input().split())]for _ in range(int(e)))
 s,t=a[0]
 b={(*map(int,input().split()))for _ in range(int(input()))}
 for x,y in b:
  for u,v in a[1:]:
   if(x+u-s,y+v-t)not in b:break
  else:return print(x-s,y-t)
for e in iter(input,'0'):f(e)


# Fixed version:
def f(e):
 a=list([*map(int,input().split())]for _ in range(int(e)))
 s,t=a[0]
 b={tuple(map(int,input().split()))for _ in range(int(input()))}
 for x,y in b:
  for u,v in a[1:]:
   if(x+u-s,y+v-t)not in b:break
  else:return print(x-s,y-t)
for e in iter(input,'0'):f(e)

# Buggy version:
import operator
for e in iter(input,'0'):
    target = [[*map(int,input().split())]for _ in[0]*int(e)]
    s,t = min(target)
    target = {(x - bx, y - by) for x, y in target}
    max_tx = max(map(lambda x:x[0], target))
    b = {tuple(map(int,input().split()))for _ in[0]*int(input())}
    max_sx = max(map(operator.itemgetter(0), b))
    lim_x = max_sx - max_tx
    for x,y in b:
        if x > lim_x:continue
        for u,v in target:
            if (x + u, y + v) not in b:break
        else:
            print(x - s, y - t)
            break


# Fixed version:
import operator
for e in iter(input,'0'):
    target = [[*map(int,input().split())]for _ in[0]*int(e)]
    s,t = min(target)
    target = {(x - s, y - t) for x, y in target}
    max_tx = max(target)[0]
    b = {tuple(map(int,input().split()))for _ in[0]*int(input())}
    max_sx = max(map(operator.itemgetter(0), b))
    lim_x = max_sx - max_tx
    for x,y in b:
        if x > lim_x:continue
        for u,v in target:
            if (x + u, y + v) not in b:break
        else:
            print(x - s, y - t)
            break