#include <fstream>
#include <cmath>
#include <map>
#include <string>

using namespace std;

#define PI 3.14159265358979323846264

typedef pair <double, double> tPair;
typedef map <string, tPair> tMap;

typedef struct {
  double x, y, z;
} tP;

#include <iostream>
#define in cin
tMap m;

tP point(double x = 0, double y = 0, double z = 0) {
  tP a;
  a.x = x;
  a.y = y;
  a.z = z;
  return a;
}

tP operator- (tP a, tP b) {
  return point(a.x-b.x, a.y-b.y, a.z-b.z);
}

double operator* (tP a, tP b) {
  return a.x*b.x + a.y*b.y + a.z*b.z;
}

std::ostream & operator << (std::ostream &osm, const tP a) {
  osm <<"("<<a.x <<","<<a.y<<","<<a.z<<")";
  return osm;
}

int main() {
 
  while (1) {
    string s;
    in >> s;
    if (s == "#")
      break;
    double lat, lon;
    in >> lat >> lon;
    
    lat = lat/180.0*PI;
    lon = lon/180.0*PI;
    m[s] = tPair(lat, lon);
  }

  while (1) {
    string s1, s2, s3;
    in >> s1;
    if (s1 == "#")
      break;
    in >> s2 >> s3;
    
    cout << s3 << " is ";
    if (m.find(s1) == m.end() || m.find(s2) == m.end() || m.find(s3) == m.end())
      cout << "?";
    else {
      tP p1, p2, p3, n;

      // x/y-plane equatorial plane
      p1 = point(cos(m[s1].first)*sin(m[s1].second),
		 cos(m[s1].first)*cos(m[s1].second),
		 sin(m[s1].first));

      p2 = point(cos(m[s2].first)*sin(m[s2].second),
		 cos(m[s2].first)*cos(m[s2].second),
		 sin(m[s2].first));
      
      p3 = point(cos(m[s3].first)*sin(m[s3].second),
		 cos(m[s3].first)*cos(m[s3].second),
		 sin(m[s3].first));

      n = p1 - p2;
 
      if (sqrt(n*n)<1.0e-9)
	cout << "0";
      else {
	double d = asin( abs((n*p3))/ sqrt(n*n)/ sqrt(p3*p3));
	//	cout << n << " " << p3 << " " << d << endl;
	cout << floor(d*6378.0 + 0.5);
      }
    }
    cout << " km off "  << s1 << "/" << s2 << " equidistance." << endl;
  }

  
  return 0;
}
