We know two arrays A, B of size n have same set of distinct integer values, but maybe ordered in different ways, so there is one-to-one match between the two arrays. We don't know the actual values stored in the arrays, and no way to access values of the array thus can't use function such as sorted. However we may ask question about any pair of A[i] and B[j], and get answer of whether A[i] = B[j], A[i] > B[j] or A[i] < B[j].
What's an efficient algorithm to match each element A to its corresponding element in B? First thing came to my mind is a simple brute force approach, first match A[1] with its counterpart in B by repeated asking relationship question between A[1] and B[j] for j = 1 to n until we find the match. Then do the same for A[i] for i = 2 to n to find match for rest of the A[i]s. The worst case running time would be n + (n - 1) + ... + 1 = O(n^2). But seems this would not be the most efficient way to do it, because we only used the information of whether A[i] = B[j], but not the more detailed information of whether A[i] > B[j] or A[i] < B[j]. Intuitively there should be a way to utilize the relative order between A[i] and B[j] to design an algorithm in less than O(n^2) time.
Any help would be greatly appreciated!