Mutation fails because of error Some variables are defined twice

For the following schema

type Country {
        # **Don't delete** Comments in types should work
        id: ID! # **Don't delete** Comments in in lines should work
        name: String! @search(by: [trigram, hash])
        states: [State] @hasInverse(field: country)
}

type State {
        id: ID!
        xcode: String! @id @search(by: [regexp])
        name: String!
        capital: String
        region: Region
        country: Country
}

type Region {
        id: String! @id
        name: String!
        district: District
}

type District {
        id: String! @id
        name: String!
}

running this mutation on an empty instance

mutation {
  addCountry(input: [{
    name: "c1",
    states: [{
      xcode: "s1",
      name: "s1",
      region: {
        id: "r1",
        name: "r1",
        district: {
          id: "d1",
          name: "d1"
        }
      }
    }]
  }]) {
    country {
      id
      name
      states {
        xcode
        name
        region {
          id
          name 
          district {
            id
            name
          }
        }
      }
    }
  }
}

fails with the below error

{
  "errors": [
    {
      "message": "mutation addCountry failed because Dgraph execution failed because Some variables are declared multiple times.",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ]
    }
  ],
  "data": {
    "addCountry": null
  }
}

This happens because the rewritten query has a variable defined twice. On debugging I found that Region6 is defined twice. See the generated query below.

query {
  State3 as State3(func: eq(State.xcode, "s1")) @filter(type(State)) {
    uid
  }
  Region6 as Region6(func: eq(Region.id, "r1")) @filter(type(Region)) {
    uid
  }
  Region6 as Region6(func: eq(Region.id, "r1")) @filter(type(Region)) {
    uid
  }
  District8 as District8(func: eq(District.id, "d1")) @filter(type(District)) {
    uid
  }
}

This is fixed in master with the following PR: https://github.com/dgraph-io/dgraph/pull/7409

Cherry Pick to 20.11: https://github.com/dgraph-io/dgraph/pull/7413