public static void main ( String args [ ] ) {
  FastScanner in = new FastScanner ( ) ;
  int t = in . nextInt ( ) ;
  while ( -- t >= 0 ) {
    int n = in . nextInt ( ) ;
    int x = in . nextInt ( ) ;
    int y = in . nextInt ( ) ;
    int d = in . nextInt ( ) ;
    int diff = Math . abs ( x - y ) ;
    if ( x != y && diff % d != 0 && ( y - 1 ) % d != 0 && ( n - y ) % d != 0 ) System . out . println ( - 1 ) ;
    else if ( diff % d == 0 ) System . out . println ( diff / d ) ;
    else {
      if ( ( y - 1 ) % d == 0 && ( n - y ) % d == 0 ) System . out . println ( Math . min ( ( y - 1 ) / d + ( x - 1 + d - 1 ) / d , ( n - y ) / d + ( n - x + d - 1 ) / d ) ) ;
      else if ( ( y - 1 ) % d == 0 ) System . out . println ( ( y - 1 ) / d + ( x - 1 + d - 1 ) / d ) ;
      else System . out . println ( ( n - y ) / d + ( n - x + d - 1 ) / d ) ;
    }
  }
}

public static void main ( String args [ ] ) { FastScanner in = new FastScanner ( ) ; int t = in . nextInt ( ) ; while ( -- t >= 0 ) { int n = in . nextInt ( ) ; int x = in . nextInt ( ) ; int y = in . nextInt ( ) ; int d = in . nextInt ( ) ; int diff = Math . abs ( x - y ) ; if ( x != y && diff % d != 0 && ( y - 1 ) % d != 0 && ( n - y ) % d != 0 ) System . out . println ( - 1 ) ; else if ( diff % d == 0 ) System . out . println ( diff / d ) ; else { if ( ( y - 1 ) % d == 0 && ( n - y ) % d == 0 ) System . out . println ( Math . min ( ( y - 1 ) / d + ( x - 1 + d - 1 ) / d , ( n - y ) / d + ( n - x + d - 1 ) / d ) ) ; else if ( ( y - 1 ) % d == 0 ) System . out . println ( ( y - 1 ) / d + ( x - 1 + d - 1 ) / d ) ; else System . out . println ( ( n - y ) / d + ( n - x + d - 1 ) / d ) ; } } }
(method_declaration (modifiers) type: (void_type) name: (identifier) parameters: (formal_parameters (formal_parameter type: (type_identifier) name: (identifier) dimensions: (dimensions))) body: (block (local_variable_declaration type: (type_identifier) declarator: (variable_declarator name: (identifier) value: (object_creation_expression type: (type_identifier) arguments: (argument_list)))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) (while_statement condition: (parenthesized_expression (binary_expression left: (update_expression (identifier)) right: (decimal_integer_literal))) body: (block (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (binary_expression left: (identifier) right: (identifier)))))) (if_statement condition: (parenthesized_expression (binary_expression left: (binary_expression left: (binary_expression left: (binary_expression left: (identifier) right: (identifier)) right: (binary_expression left: (binary_expression left: (identifier) right: (identifier)) right: (decimal_integer_literal))) right: (binary_expression left: (binary_expression left: (parenthesized_expression (binary_expression left: (identifier) right: (decimal_integer_literal))) right: (identifier)) right: (decimal_integer_literal))) right: (binary_expression left: (binary_expression left: (parenthesized_expression (binary_expression left: (identifier) right: (identifier))) right: (identifier)) right: (decimal_integer_literal)))) consequence: (expression_statement (method_invocation object: (field_access object: (identifier) field: (identifier)) name: (identifier) arguments: (argument_list (unary_expression operand: (decimal_integer_literal))))) alternative: (if_statement condition: (parenthesized_expression (binary_expression left: (binary_expression left: (identifier) right: (identifier)) right: (decimal_integer_literal))) consequence: (expression_statement (method_invocation object: (field_access object: (identifier) field: (identifier)) name: (identifier) arguments: (argument_list (binary_expression left: (identifier) right: (identifier))))) alternative: (block (if_statement condition: (parenthesized_expression (binary_expression left: (binary_expression left: (binary_expression left: (parenthesized_expression (binary_expression left: (identifier) right: (decimal_integer_literal))) right: (identifier)) right: (decimal_integer_literal)) right: (binary_expression left: (binary_expression left: (parenthesized_expression (binary_expression left: (identifier) right: (identifier))) right: (identifier)) right: (decimal_integer_literal)))) consequence: (expression_statement (method_invocation object: (field_access object: (identifier) field: (identifier)) name: (identifier) arguments: (argument_list (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (binary_expression left: (binary_expression left: (parenthesized_expression (binary_expression left: (identifier) right: (decimal_integer_literal))) right: (identifier)) right: (binary_expression left: (parenthesized_expression (binary_expression left: (binary_expression left: (binary_expression left: (identifier) right: (decimal_integer_literal)) right: (identifier)) right: (decimal_integer_literal))) right: (identifier))) (binary_expression left: (binary_expression left: (parenthesized_expression (binary_expression left: (identifier) right: (identifier))) right: (identifier)) right: (binary_expression left: (parenthesized_expression (binary_expression left: (binary_expression left: (binary_expression left: (identifier) right: (identifier)) right: (identifier)) right: (decimal_integer_literal))) right: (identifier)))))))) alternative: (if_statement condition: (parenthesized_expression (binary_expression left: (binary_expression left: (parenthesized_expression (binary_expression left: (identifier) right: (decimal_integer_literal))) right: (identifier)) right: (decimal_integer_literal))) consequence: (expression_statement (method_invocation object: (field_access object: (identifier) field: (identifier)) name: (identifier) arguments: (argument_list (binary_expression left: (binary_expression left: (parenthesized_expression (binary_expression left: (identifier) right: (decimal_integer_literal))) right: (identifier)) right: (binary_expression left: (parenthesized_expression (binary_expression left: (binary_expression left: (binary_expression left: (identifier) right: (decimal_integer_literal)) right: (identifier)) right: (decimal_integer_literal))) right: (identifier)))))) alternative: (expression_statement (method_invocation object: (field_access object: (identifier) field: (identifier)) name: (identifier) arguments: (argument_list (binary_expression left: (binary_expression left: (parenthesized_expression (binary_expression left: (identifier) right: (identifier))) right: (identifier)) right: (binary_expression left: (parenthesized_expression (binary_expression left: (binary_expression left: (binary_expression left: (identifier) right: (identifier)) right: (identifier)) right: (decimal_integer_literal))) right: (identifier)))))))))))))))