Extracts reference IDs from the contents and cleans up the ref tags.
This function processes a list of strings, searching for reference ID tags
at the beginning and end of each string. When a ref tag is found, it is
removed from the string, and its ID is recorded. If no ref ID is found,
a generic ID of "-1" is assigned.
The search for ref tags is performed only at the beginning and
end of the string, with the assumption that there will
be at most one ref ID per string. Malformed ref tags are
handled gracefully.