```ts
export function orderRelation(pairs: [string, string][], x: string, y: string): string {
  if (x === y) throw new Error("Query items cannot be equal");

  const graph = new Map<string, Set<string>>();
  for (const [a, b] of pairs) {
    if (!graph.has(a)) graph.set(a, new Set());
    if (!graph.has(b)) graph.set(b, new Set());
    graph.get(a)?.add(b);
  }

  function dfs(current: string, visited: Set<string>): boolean | null {
    if (visited.has(current)) return current === x ? true : false;
    visited.add(current);

    const neighbors = graph.get(current);
    if (neighbors) {
      for (const neighbor of neighbors) {
        const result = dfs(neighbor, visited);
        if (result !== null) return result;
      }
    }

    return null;
  }

  let xBeforeY = false;
  let yBeforeX = false;

  try {
    xBeforeY = dfs(x, new Set())!;
  } catch (_) {}

  try {
    yBeforeX = dfs(y, new Set())!;
  } catch (_) {}

  if (xBeforeY && yBeforeX) return "both";
  if (xBeforeY) return "before";
  if (yBeforeX) return "after";

  return "unordered";
}
```