program ADVERT_CHECK;
uses
  testlib;

const
  maxn = 750;
  eps  = 1e-8;

type
 PLink = ^TLink;
 TLink = record
   next: PLink;
   town: integer;
 end;

 real = extended;

var
  wa: boolean;
  wm: string;
  x, y: array[1..maxn] of longint;
  g: array[1..maxn] of PLink;
  visit: array[1..maxn] of boolean;
  vcnt: integer;
  n, m, i, cnt: integer;
  a, b: longint;
  louf, lans: real;

procedure Traverse(i: integer);
var
  p: PLink;
begin
  if visit[i] then exit;
  visit[i]:= true;
  inc(vcnt);
  p:= g[i];
  while p <> nil do begin
    Traverse(p^.town);
    p:= p^.next;
  end;
end;

procedure AddUniEdge(a, b: integer);
var
  p: PLink;
begin
  if wa then exit;
  p:= g[a];
  while p <> nil do begin
    if p^.town = b then begin
      wa:= true;
      wm:= 'Highway is already build: ';
      exit;
    end;
    p:= p^.next;
  end;
  new(p);
  p^.next:= g[a];
  p^.town:= b;
  g[a]:= p;
end;

procedure AddEdge(a, b: integer);
begin
  if not wa and (a = b) then begin
    wa:= true;
    wm:= 'Invalid highway: ';
    exit;
  end;
  AddUniEdge(a, b);
  AddUniEdge(b, a);
end;

begin
  wa:= false;

  { Read input }
  n:= inf.readLongint;
  for i:= 1 to n do begin
    x[i]:= inf.readLongint;
    y[i]:= inf.readLongint;
  end;
  m:= inf.readLongint;
  fillchar(g, sizeof(g), 0);
  for i:= 1 to m do begin
    a:= inf.readLongint;
    b:= inf.readLongint;
    AddEdge(a, b);
  end;
  if wa then Quit(_Fail, wm);

  { Read output }
  cnt:= 0;
  louf:= 0;
  while not ouf.seekeof do begin
    a:= ouf.readLongint;
    b:= ouf.readLongint;
    if (a < 1) or (a > n) or (b < 1) or (b > n) and (a = b) then
      Quit(_PE, 'Invalid highway: ');
    AddEdge(a, b);
    if not wa then begin
      inc(cnt);
      if cnt > n then begin
        wa:= true;
        wm:= 'Surely wrong - too many highways';
      end;
      louf:= louf + sqrt(sqr(x[a]-x[b]) + sqr(y[a]-y[b]));
    end;
  end;

  { Reading done... }
  if wa then Quit(_WA, wm);

  { Check output }
  fillchar(visit, sizeof(visit), 0);
  vcnt:= 0;
  Traverse(1);
  if vcnt <> n then
    Quit(_WA, 'Graph is not connected');

  { Read answer }
  lans:= 0;
  while not ans.seekeof do begin
    a:= ans.readLongint;
    b:= ans.readLongint;
    lans:= lans + sqrt(sqr(x[a]-x[b]) + sqr(y[a]-y[b]));
  end;

  { Check for optimal }
  if louf > lans * (1 + eps) then
    Quit(_WA, 'Not optimal: ');

  if louf < lans * (1 - eps) then
    Quit(_Fail, 'Correct answer is not optimal: ');

  Quit(_OK, 'Ok');
end.
