fork(1) download
  1. import java.io.*;
  2. import java.math.BigInteger;
  3. import java.util.*;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6.  
  7. import javax.net.ssl.SSLContext;
  8.  
  9. public class Main
  10. {
  11. static HashMap<Integer,Integer> index;static int g=0; static int l=0;
  12.  
  13. public static int lower_bound(int start,int end,int a[],int key)
  14. {
  15. if(start>end) return start;
  16. int mid=(start+end)>>1;
  17.  
  18. if(a[mid]==key) return mid;
  19.  
  20. if(key<a[mid]) return lower_bound(start,mid-1,a,key);
  21. else return lower_bound(mid+1,end,a,key);
  22. }
  23.  
  24. public static void main(String[] args) throws java.lang.Exception
  25. {
  26.  
  27. fast s = new fast();
  28. StringBuilder final_ans = new StringBuilder();
  29.  
  30. int t=s.nextInt();
  31.  
  32. while(t!=0)
  33. {
  34. int n=s.nextInt();
  35. int st=s.nextInt();
  36. index=new HashMap<Integer,Integer>();
  37.  
  38. int a[]=new int[n];
  39. int b[]=new int[st];
  40. int temp[]=new int[n];
  41.  
  42. for(int i=0;i<n;i++) {a[i]=s.nextInt();temp[i]=a[i];index.put(a[i], i);}
  43. for(int i=0;i<st;i++) b[i]=s.nextInt();
  44.  
  45. Arrays.sort(temp);
  46. for(int i=0;i<st;i++)
  47. {
  48. l=0; g=0;
  49. int pos=index.get(b[i]);
  50.  
  51. int low=0; int high=n-1;
  52.  
  53. while(low<=high)
  54. {
  55. int mid=(low+high)>>1;
  56.  
  57. if(mid==pos) break;
  58.  
  59. if(mid<pos)
  60. {
  61. if(a[mid]>b[i]) g++;
  62. low=mid+1;
  63. }
  64. else if(mid>pos)
  65. {
  66. if(a[mid]<b[i]) l++;
  67. high=mid-1;
  68. }
  69. }
  70.  
  71. int lb=lower_bound(0,n-1,temp,b[i]);
  72. int gb=n-1-lb;
  73.  
  74. if(l==g) System.out.println(l);
  75. else if(l>g)
  76. {
  77. if(gb<l) System.out.println("-1");
  78. else System.out.println(l);
  79.  
  80. }
  81. else
  82. {
  83. if(lb<g) System.out.println("-1");
  84. else System.out.println(g);
  85. }
  86. }
  87.  
  88. t--;
  89. }
  90.  
  91. }
  92.  
  93. static class fast {
  94. private InputStream i;
  95. private byte[] buf = new byte[1024];
  96. private int curChar;
  97. private int numChars;
  98.  
  99. public int gcd(int a,int b)
  100. {
  101. if(a==0) return b;
  102. return gcd(b%a,b);
  103. }
  104. public fast() {
  105. this(System.in);
  106. }
  107. public fast(InputStream is) {
  108. i = is;
  109. }
  110. public int read() {
  111. if (numChars == -1)
  112. throw new InputMismatchException();
  113. if (curChar >= numChars) {
  114. curChar = 0;
  115. try {
  116. numChars = i.read(buf);
  117. } catch (IOException e) {
  118. throw new InputMismatchException();
  119. }
  120. if (numChars <= 0)
  121. return -1;
  122. }
  123. return buf[curChar++];
  124. }
  125. public String nextLine() {
  126. int c = read();
  127. while (isSpaceChar(c))
  128. c = read();
  129. StringBuilder res = new StringBuilder();
  130. do {
  131. res.appendCodePoint(c);
  132. c = read();
  133. } while (!isEndOfLine(c));
  134. return res.toString();
  135. }
  136. public String nextString() {
  137. int c = read();
  138. while (isSpaceChar(c))
  139. c = read();
  140. StringBuilder res = new StringBuilder();
  141. do {
  142. res.appendCodePoint(c);
  143. c = read();
  144. } while (!isSpaceChar(c));
  145. return res.toString();
  146. }
  147. public long nextLong() {
  148. int c = read();
  149. while (isSpaceChar(c))
  150. c = read();
  151. int sgn = 1;
  152. if (c == '-') {
  153. sgn = -1;
  154. c = read();
  155. }
  156. long res = 0;
  157. do {
  158. if (c < '0' || c > '9')
  159. throw new InputMismatchException();
  160. res *= 10;
  161. res += c - '0';
  162. c = read();
  163. } while (!isSpaceChar(c));
  164. return res * sgn;
  165. }
  166. public int nextInt() {
  167. int c = read();
  168. while (isSpaceChar(c))
  169. c = read();
  170. int sgn = 1;
  171. if (c == '-') {
  172. sgn = -1;
  173. c = read();
  174. }
  175. int res = 0;
  176. do {
  177. if (c < '0' || c > '9')
  178. throw new InputMismatchException();
  179. res *= 10;
  180. res += c - '0';
  181. c = read();
  182. } while (!isSpaceChar(c));
  183. return res * sgn;
  184. }
  185. public boolean isSpaceChar(int c) {
  186. return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
  187. }
  188. public boolean isEndOfLine(int c) {
  189. return c == '\n' || c == '\r' || c == -1;
  190. }
  191.  
  192. }
  193. }
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
Success #stdin #stdout 0.05s 27456KB
stdin
1
7 7
3 1 6 7 2 5 4
1
2
3
4
5
6
7
stdout
0
1
1
2
1
0
0