```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);
    }

    if (!graph.has(x)) throw new Error(`Item "${x}" does not appear in any pair`);
    if (!graph.has(y)) throw new Error(`Item "${y}" does not appear in any pair`);

    const dfs = (start: string, target: string, visited: Set<string> = new Set()): boolean => {
        if (visited.has(start)) return false;
        if (start === target) return true;
        visited.add(start);
        for (const neighbor of graph.get(start)!) {
            if (dfs(neighbor, target, visited)) return true;
        }
        return false;
    };

    const xBeforeY = dfs(x, y);
    const yBeforeX = dfs(y, x);

    if (xBeforeY && !yBeforeX) return "before";
    if (!xBeforeY && yBeforeX) return "after";
    if (xBeforeY && yBeforeX) return "both";
    return "unordered";
}
```